Empact-sexy_pg_constraints 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Empact-sexy_pg_constraints.gemspec +39 -8
- data/VERSION +1 -1
- data/lib/sexy_pg_constraints/constraints.rb +41 -1
- data/test/alphanumeric_test.rb +39 -0
- data/test/blacklist_test.rb +39 -0
- data/test/email_test.rb +39 -0
- data/test/exact_length_test.rb +47 -0
- data/test/format_test.rb +91 -0
- data/test/general_test.rb +132 -0
- data/test/greater_less_than_test.rb +83 -0
- data/test/length_within_test.rb +91 -0
- data/test/lowercase_test.rb +21 -0
- data/test/not_blank_test.rb +39 -0
- data/test/odd_event_test.rb +75 -0
- data/test/positive_test.rb +47 -0
- data/test/reference_test.rb +79 -0
- data/test/test_helper.rb +12 -0
- data/test/whitelist_test.rb +39 -0
- data/test/within_test.rb +135 -0
- data/test/xor_test.rb +27 -0
- metadata +34 -4
- data/test/sexy_pg_constraints_test.rb +0 -943
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{Empact-sexy_pg_constraints}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Maxim Chernyak", "Ben Woosley"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-05-26}
|
13
13
|
s.description = %q{Use migrations and simple syntax to manage constraints in PostgreSQL DB.}
|
14
14
|
s.email = %q{ben.woosley@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
"CHANGELOG.rdoc",
|
21
|
+
"Empact-sexy_pg_constraints.gemspec",
|
21
22
|
"Gemfile",
|
22
23
|
"Gemfile.lock",
|
23
24
|
"LICENSE.txt",
|
@@ -30,23 +31,53 @@ Gem::Specification.new do |s|
|
|
30
31
|
"lib/sexy_pg_constraints/constraints.rb",
|
31
32
|
"lib/sexy_pg_constraints/deconstrainer.rb",
|
32
33
|
"lib/sexy_pg_constraints/helpers.rb",
|
33
|
-
"lib/sexy_pg_constraints/
|
34
|
-
"sexy_pg_constraints.
|
35
|
-
"test/
|
34
|
+
"lib/sexy_pg_constraints/railtie.rb",
|
35
|
+
"lib/sexy_pg_constraints/schema_definitions.rb",
|
36
|
+
"test/alphanumeric_test.rb",
|
37
|
+
"test/blacklist_test.rb",
|
38
|
+
"test/email_test.rb",
|
39
|
+
"test/exact_length_test.rb",
|
40
|
+
"test/format_test.rb",
|
41
|
+
"test/general_test.rb",
|
42
|
+
"test/greater_less_than_test.rb",
|
43
|
+
"test/length_within_test.rb",
|
44
|
+
"test/lowercase_test.rb",
|
45
|
+
"test/not_blank_test.rb",
|
46
|
+
"test/odd_event_test.rb",
|
47
|
+
"test/positive_test.rb",
|
48
|
+
"test/reference_test.rb",
|
36
49
|
"test/support/assert_prohibits_allows.rb",
|
37
50
|
"test/support/database.yml.example",
|
38
51
|
"test/support/models.rb",
|
39
|
-
"test/test_helper.rb"
|
52
|
+
"test/test_helper.rb",
|
53
|
+
"test/whitelist_test.rb",
|
54
|
+
"test/within_test.rb",
|
55
|
+
"test/xor_test.rb"
|
40
56
|
]
|
41
57
|
s.homepage = %q{http://github.com/maxim/sexy_pg_constraints}
|
42
58
|
s.require_paths = ["lib"]
|
43
59
|
s.rubygems_version = %q{1.7.2}
|
44
60
|
s.summary = nil
|
45
61
|
s.test_files = [
|
46
|
-
"test/
|
62
|
+
"test/alphanumeric_test.rb",
|
63
|
+
"test/blacklist_test.rb",
|
64
|
+
"test/email_test.rb",
|
65
|
+
"test/exact_length_test.rb",
|
66
|
+
"test/format_test.rb",
|
67
|
+
"test/general_test.rb",
|
68
|
+
"test/greater_less_than_test.rb",
|
69
|
+
"test/length_within_test.rb",
|
70
|
+
"test/lowercase_test.rb",
|
71
|
+
"test/not_blank_test.rb",
|
72
|
+
"test/odd_event_test.rb",
|
73
|
+
"test/positive_test.rb",
|
74
|
+
"test/reference_test.rb",
|
47
75
|
"test/support/assert_prohibits_allows.rb",
|
48
76
|
"test/support/models.rb",
|
49
|
-
"test/test_helper.rb"
|
77
|
+
"test/test_helper.rb",
|
78
|
+
"test/whitelist_test.rb",
|
79
|
+
"test/within_test.rb",
|
80
|
+
"test/xor_test.rb"
|
50
81
|
]
|
51
82
|
|
52
83
|
if s.respond_to? :specification_version then
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
@@ -105,7 +105,47 @@ module SexyPgConstraints
|
|
105
105
|
# constrain :books, :quantity, :positive => true
|
106
106
|
#
|
107
107
|
def positive(table, column, options)
|
108
|
-
|
108
|
+
greater_than_or_equal_to(table, column, 0)
|
109
|
+
end
|
110
|
+
|
111
|
+
##
|
112
|
+
# Allow only values less than the provided limit.
|
113
|
+
#
|
114
|
+
# Example:
|
115
|
+
# constrain :books, :quantity, :greater_than => 12
|
116
|
+
#
|
117
|
+
def less_than(table, column, options)
|
118
|
+
"check (#{table}.#{column} < #{options})"
|
119
|
+
end
|
120
|
+
|
121
|
+
##
|
122
|
+
# Allow only values less than or equal to the provided limit.
|
123
|
+
#
|
124
|
+
# Example:
|
125
|
+
# constrain :books, :quantity, :greater_than => 12
|
126
|
+
#
|
127
|
+
def less_than_or_equal_to(table, column, options)
|
128
|
+
"check (#{table}.#{column} <= #{options})"
|
129
|
+
end
|
130
|
+
|
131
|
+
##
|
132
|
+
# Allow only values greater than the provided limit.
|
133
|
+
#
|
134
|
+
# Example:
|
135
|
+
# constrain :books, :quantity, :greater_than => 12
|
136
|
+
#
|
137
|
+
def greater_than(table, column, options)
|
138
|
+
"check (#{table}.#{column} > #{options})"
|
139
|
+
end
|
140
|
+
|
141
|
+
##
|
142
|
+
# Allow only values greater than or equal to the provided limit.
|
143
|
+
#
|
144
|
+
# Example:
|
145
|
+
# constrain :books, :quantity, :greater_than_or_equal_to => 12
|
146
|
+
#
|
147
|
+
def greater_than_or_equal_to(table, column, options)
|
148
|
+
"check (#{table}.#{column} >= #{options})"
|
109
149
|
end
|
110
150
|
|
111
151
|
##
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AlphanumericTest < SexyPgConstraintsTest
|
4
|
+
def test_alphanumeric
|
5
|
+
ActiveRecord::Migration.constrain :books, :title, :alphanumeric => true
|
6
|
+
|
7
|
+
assert_prohibits Book, :title, :alphanumeric do |book|
|
8
|
+
book.title = 'asdf@asdf'
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_allows Book do |book|
|
12
|
+
book.title = 'asdf'
|
13
|
+
end
|
14
|
+
|
15
|
+
ActiveRecord::Migration.deconstrain :books, :title, :alphanumeric
|
16
|
+
|
17
|
+
assert_allows Book do |book|
|
18
|
+
book.title = 'asdf@asdf'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_alphanumeric_on_a_column_whose_name_is_a_sql_keyword
|
23
|
+
ActiveRecord::Migration.constrain :books, :as, :alphanumeric => true
|
24
|
+
|
25
|
+
assert_prohibits Book, :as, :alphanumeric do |book|
|
26
|
+
book.as = 'asdf@asdf'
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_allows Book do |book|
|
30
|
+
book.as = 'asdf'
|
31
|
+
end
|
32
|
+
|
33
|
+
ActiveRecord::Migration.deconstrain :books, :as, :alphanumeric
|
34
|
+
|
35
|
+
assert_allows Book do |book|
|
36
|
+
book.as = 'asdf@asdf'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class BlacklistTest < SexyPgConstraintsTest
|
4
|
+
def test_blacklist
|
5
|
+
ActiveRecord::Migration.constrain :books, :author, :blacklist => %w(blacklisted1 blacklisted2 blacklisted3)
|
6
|
+
|
7
|
+
assert_prohibits Book, :author, :blacklist do |book|
|
8
|
+
book.author = 'blacklisted2'
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_allows Book do |book|
|
12
|
+
book.author = 'not_blacklisted'
|
13
|
+
end
|
14
|
+
|
15
|
+
ActiveRecord::Migration.deconstrain :books, :author, :blacklist
|
16
|
+
|
17
|
+
assert_allows Book do |book|
|
18
|
+
book.author = 'blacklisted2'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_blacklist_on_a_column_whose_name_is_a_sql_keyword
|
23
|
+
ActiveRecord::Migration.constrain :books, :as, :blacklist => %w(blacklisted1 blacklisted2 blacklisted3)
|
24
|
+
|
25
|
+
assert_prohibits Book, :as, :blacklist do |book|
|
26
|
+
book.as = 'blacklisted2'
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_allows Book do |book|
|
30
|
+
book.as = 'not_blacklisted'
|
31
|
+
end
|
32
|
+
|
33
|
+
ActiveRecord::Migration.deconstrain :books, :as, :blacklist
|
34
|
+
|
35
|
+
assert_allows Book do |book|
|
36
|
+
book.as = 'blacklisted2'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/test/email_test.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class EmailTest < SexyPgConstraintsTest
|
4
|
+
def test_email
|
5
|
+
ActiveRecord::Migration.constrain :books, :author, :email => true
|
6
|
+
|
7
|
+
assert_prohibits Book, :author, :email do |book|
|
8
|
+
book.author = 'blah@example'
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_allows Book do |book|
|
12
|
+
book.author = 'blah@example.com'
|
13
|
+
end
|
14
|
+
|
15
|
+
ActiveRecord::Migration.deconstrain :books, :author, :email
|
16
|
+
|
17
|
+
assert_allows Book do |book|
|
18
|
+
book.author = 'blah@example'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_email_on_a_column_whose_name_is_a_sql_keyword
|
23
|
+
ActiveRecord::Migration.constrain :books, :as, :email => true
|
24
|
+
|
25
|
+
assert_prohibits Book, :as, :email do |book|
|
26
|
+
book.as = 'blah@example'
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_allows Book do |book|
|
30
|
+
book.as = 'blah@example.com'
|
31
|
+
end
|
32
|
+
|
33
|
+
ActiveRecord::Migration.deconstrain :books, :as, :email
|
34
|
+
|
35
|
+
assert_allows Book do |book|
|
36
|
+
book.as = 'blah@example'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ExactLengthTest < SexyPgConstraintsTest
|
4
|
+
def test_exact_length
|
5
|
+
ActiveRecord::Migration.constrain :books, :isbn, :exact_length => 5
|
6
|
+
|
7
|
+
assert_prohibits Book, :isbn, :exact_length do |book|
|
8
|
+
book.isbn = '123456'
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_prohibits Book, :isbn, :exact_length do |book|
|
12
|
+
book.isbn = '1234'
|
13
|
+
end
|
14
|
+
|
15
|
+
assert_allows Book do |book|
|
16
|
+
book.isbn = '12345'
|
17
|
+
end
|
18
|
+
|
19
|
+
ActiveRecord::Migration.deconstrain :books, :isbn, :exact_length
|
20
|
+
|
21
|
+
assert_allows Book do |book|
|
22
|
+
book.isbn = '123456'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_exact_length_on_a_column_whose_name_is_a_sql_keyword
|
27
|
+
ActiveRecord::Migration.constrain :books, :as, :exact_length => 5
|
28
|
+
|
29
|
+
assert_prohibits Book, :as, :exact_length do |book|
|
30
|
+
book.as = '123456'
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_prohibits Book, :as, :exact_length do |book|
|
34
|
+
book.as = '1234'
|
35
|
+
end
|
36
|
+
|
37
|
+
assert_allows Book do |book|
|
38
|
+
book.as = '12345'
|
39
|
+
end
|
40
|
+
|
41
|
+
ActiveRecord::Migration.deconstrain :books, :as, :exact_length
|
42
|
+
|
43
|
+
assert_allows Book do |book|
|
44
|
+
book.as = '123456'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/test/format_test.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FormatTest < SexyPgConstraintsTest
|
4
|
+
def test_format_case_insensitive
|
5
|
+
ActiveRecord::Migration.constrain :books, :title, :format => /^[a-z]+$/i
|
6
|
+
|
7
|
+
assert_prohibits Book, :title, :format do |book|
|
8
|
+
book.title = 'abc3'
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_prohibits Book, :title, :format do |book|
|
12
|
+
book.title = ''
|
13
|
+
end
|
14
|
+
|
15
|
+
assert_allows Book do |book|
|
16
|
+
book.title = 'abc'
|
17
|
+
end
|
18
|
+
|
19
|
+
assert_allows Book do |book|
|
20
|
+
book.title = 'ABc'
|
21
|
+
end
|
22
|
+
|
23
|
+
ActiveRecord::Migration.deconstrain :books, :title, :format
|
24
|
+
|
25
|
+
assert_allows Book do |book|
|
26
|
+
book.title = 'abc3'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_format_case_insensitive_on_a_column_whose_name_is_a_sql_keyword
|
31
|
+
ActiveRecord::Migration.constrain :books, :as, :format => /^[a-z]+$/i
|
32
|
+
|
33
|
+
assert_prohibits Book, :as, :format do |book|
|
34
|
+
book.as = 'abc3'
|
35
|
+
end
|
36
|
+
|
37
|
+
assert_prohibits Book, :as, :format do |book|
|
38
|
+
book.as = ''
|
39
|
+
end
|
40
|
+
|
41
|
+
assert_allows Book do |book|
|
42
|
+
book.as = 'abc'
|
43
|
+
end
|
44
|
+
|
45
|
+
assert_allows Book do |book|
|
46
|
+
book.as = 'ABc'
|
47
|
+
end
|
48
|
+
|
49
|
+
ActiveRecord::Migration.deconstrain :books, :as, :format
|
50
|
+
|
51
|
+
assert_allows Book do |book|
|
52
|
+
book.as = 'abc3'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_format_case_sensitive
|
57
|
+
ActiveRecord::Migration.constrain :books, :title, :format => /^[a-z]+$/
|
58
|
+
|
59
|
+
assert_prohibits Book, :title, :format do |book|
|
60
|
+
book.title = 'aBc'
|
61
|
+
end
|
62
|
+
|
63
|
+
assert_allows Book do |book|
|
64
|
+
book.title = 'abc'
|
65
|
+
end
|
66
|
+
|
67
|
+
ActiveRecord::Migration.deconstrain :books, :title, :format
|
68
|
+
|
69
|
+
assert_allows Book do |book|
|
70
|
+
book.title = 'aBc'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_format_case_sensitive_on_a_column_whose_name_is_a_sql_keyword
|
75
|
+
ActiveRecord::Migration.constrain :books, :as, :format => /^[a-z]+$/
|
76
|
+
|
77
|
+
assert_prohibits Book, :as, :format do |book|
|
78
|
+
book.as = 'aBc'
|
79
|
+
end
|
80
|
+
|
81
|
+
assert_allows Book do |book|
|
82
|
+
book.as = 'abc'
|
83
|
+
end
|
84
|
+
|
85
|
+
ActiveRecord::Migration.deconstrain :books, :as, :format
|
86
|
+
|
87
|
+
assert_allows Book do |book|
|
88
|
+
book.as = 'aBc'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class GeneralTest < SexyPgConstraintsTest
|
6
|
+
def test_should_create_book
|
7
|
+
Book.create
|
8
|
+
assert_equal 1, Book.count
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_block_syntax
|
12
|
+
ActiveRecord::Migration.constrain :books do |t|
|
13
|
+
t.title :not_blank => true
|
14
|
+
t.isbn :exact_length => 15
|
15
|
+
t.author :alphanumeric => true
|
16
|
+
end
|
17
|
+
|
18
|
+
assert_prohibits Book, :title, :not_blank do |book|
|
19
|
+
book.title = ' '
|
20
|
+
end
|
21
|
+
|
22
|
+
assert_prohibits Book, :isbn, :exact_length do |book|
|
23
|
+
book.isbn = 'asdf'
|
24
|
+
end
|
25
|
+
|
26
|
+
assert_prohibits Book, :author, :alphanumeric do |book|
|
27
|
+
book.author = 'foo#bar'
|
28
|
+
end
|
29
|
+
|
30
|
+
ActiveRecord::Migration.deconstrain :books do |t|
|
31
|
+
t.title :not_blank
|
32
|
+
t.isbn :exact_length
|
33
|
+
t.author :alphanumeric
|
34
|
+
end
|
35
|
+
|
36
|
+
assert_allows Book do |book|
|
37
|
+
book.title = ' '
|
38
|
+
book.isbn = 'asdf'
|
39
|
+
book.author = 'foo#bar'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_multiple_constraints_per_line
|
44
|
+
ActiveRecord::Migration.constrain :books do |t|
|
45
|
+
t.title :not_blank => true, :alphanumeric => true, :blacklist => %w(foo bar)
|
46
|
+
end
|
47
|
+
|
48
|
+
assert_prohibits Book, :title, [:not_blank, :alphanumeric] do |book|
|
49
|
+
book.title = ' '
|
50
|
+
end
|
51
|
+
|
52
|
+
assert_prohibits Book, :title, :alphanumeric do |book|
|
53
|
+
book.title = 'asdf@asdf'
|
54
|
+
end
|
55
|
+
|
56
|
+
assert_prohibits Book, :title, :blacklist do |book|
|
57
|
+
book.title = 'foo'
|
58
|
+
end
|
59
|
+
|
60
|
+
ActiveRecord::Migration.deconstrain :books do |t|
|
61
|
+
t.title :not_blank, :alphanumeric, :blacklist
|
62
|
+
end
|
63
|
+
|
64
|
+
assert_allows Book do |book|
|
65
|
+
book.title = ' '
|
66
|
+
end
|
67
|
+
|
68
|
+
assert_allows Book do |book|
|
69
|
+
book.title = 'asdf@asdf'
|
70
|
+
end
|
71
|
+
|
72
|
+
assert_allows Book do |book|
|
73
|
+
book.title = 'foo'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_multicolumn_constraint
|
78
|
+
ActiveRecord::Migration.constrain :books, [:title, :isbn], :unique => true
|
79
|
+
|
80
|
+
assert_allows Book do |book|
|
81
|
+
book.title = 'foo'
|
82
|
+
book.isbn = 'bar'
|
83
|
+
end
|
84
|
+
|
85
|
+
assert_allows Book do |book|
|
86
|
+
book.title = 'foo'
|
87
|
+
book.isbn = 'foo'
|
88
|
+
end
|
89
|
+
|
90
|
+
assert_prohibits Book, [:title, :isbn], :unique, 'unique', ActiveRecord::RecordNotUnique do |book|
|
91
|
+
book.title = 'foo'
|
92
|
+
book.isbn = 'bar'
|
93
|
+
end
|
94
|
+
|
95
|
+
ActiveRecord::Migration.deconstrain :books, [:title, :isbn], :unique
|
96
|
+
|
97
|
+
assert_allows Book do |book|
|
98
|
+
book.title = 'foo'
|
99
|
+
book.isbn = 'bar'
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_multicolumn_constraint_block_syntax
|
104
|
+
ActiveRecord::Migration.constrain :books do |t|
|
105
|
+
t[:title, :isbn].all :unique => true
|
106
|
+
end
|
107
|
+
|
108
|
+
assert_allows Book do |book|
|
109
|
+
book.title = 'foo'
|
110
|
+
book.isbn = 'bar'
|
111
|
+
end
|
112
|
+
|
113
|
+
assert_allows Book do |book|
|
114
|
+
book.title = 'foo'
|
115
|
+
book.isbn = 'foo'
|
116
|
+
end
|
117
|
+
|
118
|
+
assert_prohibits Book, [:title, :isbn], :unique, 'unique', ActiveRecord::RecordNotUnique do |book|
|
119
|
+
book.title = 'foo'
|
120
|
+
book.isbn = 'bar'
|
121
|
+
end
|
122
|
+
|
123
|
+
ActiveRecord::Migration.deconstrain :books do |t|
|
124
|
+
t[:title, :isbn].all :unique
|
125
|
+
end
|
126
|
+
|
127
|
+
assert_allows Book do |book|
|
128
|
+
book.title = 'foo'
|
129
|
+
book.isbn = 'bar'
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|