Empact-sexy_pg_constraints 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class GreaterLessThanTest < SexyPgConstraintsTest
|
4
|
+
def test_greater_than
|
5
|
+
ActiveRecord::Migration.constrain :books, :quantity, :greater_than => 5
|
6
|
+
|
7
|
+
assert_prohibits Book, :quantity, :greater_than do |book|
|
8
|
+
book.quantity = 5
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_allows Book do |book|
|
12
|
+
book.quantity = 6
|
13
|
+
end
|
14
|
+
|
15
|
+
ActiveRecord::Migration.deconstrain :books, :quantity, :greater_than
|
16
|
+
|
17
|
+
assert_allows Book do |book|
|
18
|
+
book.quantity = 5
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_less_than
|
23
|
+
ActiveRecord::Migration.constrain :books, :quantity, :less_than => 5
|
24
|
+
|
25
|
+
assert_prohibits Book, :quantity, :less_than do |book|
|
26
|
+
book.quantity = 5
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_allows Book do |book|
|
30
|
+
book.quantity = 4
|
31
|
+
end
|
32
|
+
|
33
|
+
ActiveRecord::Migration.deconstrain :books, :quantity, :less_than
|
34
|
+
|
35
|
+
assert_allows Book do |book|
|
36
|
+
book.quantity = 5
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_greater_than_or_equal_to
|
41
|
+
ActiveRecord::Migration.constrain :books, :quantity, :greater_than_or_equal_to => 5
|
42
|
+
|
43
|
+
assert_prohibits Book, :quantity, :greater_than_or_equal_to do |book|
|
44
|
+
book.quantity = 4
|
45
|
+
end
|
46
|
+
|
47
|
+
assert_allows Book do |book|
|
48
|
+
book.quantity = 5
|
49
|
+
end
|
50
|
+
|
51
|
+
assert_allows Book do |book|
|
52
|
+
book.quantity = 6
|
53
|
+
end
|
54
|
+
|
55
|
+
ActiveRecord::Migration.deconstrain :books, :quantity, :greater_than_or_equal_to
|
56
|
+
|
57
|
+
assert_allows Book do |book|
|
58
|
+
book.quantity = 4
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_less_than_or_equal_to
|
63
|
+
ActiveRecord::Migration.constrain :books, :quantity, :less_than_or_equal_to => 5
|
64
|
+
|
65
|
+
assert_prohibits Book, :quantity, :less_than_or_equal_to do |book|
|
66
|
+
book.quantity = 6
|
67
|
+
end
|
68
|
+
|
69
|
+
assert_allows Book do |book|
|
70
|
+
book.quantity = 5
|
71
|
+
end
|
72
|
+
|
73
|
+
assert_allows Book do |book|
|
74
|
+
book.quantity = 4
|
75
|
+
end
|
76
|
+
|
77
|
+
ActiveRecord::Migration.deconstrain :books, :quantity, :less_than_or_equal_to
|
78
|
+
|
79
|
+
assert_allows Book do |book|
|
80
|
+
book.quantity = 6
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class LengthWithinTest < SexyPgConstraintsTest
|
4
|
+
def test_length_within_inclusive
|
5
|
+
ActiveRecord::Migration.constrain :books, :title, :length_within => 5..11
|
6
|
+
|
7
|
+
assert_prohibits Book, :title, :length_within do |book|
|
8
|
+
book.title = 'abcdefghijkl'
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_prohibits Book, :title, :length_within do |book|
|
12
|
+
book.title = 'abcd'
|
13
|
+
end
|
14
|
+
|
15
|
+
assert_allows Book do |book|
|
16
|
+
book.title = 'abcdefg'
|
17
|
+
end
|
18
|
+
|
19
|
+
ActiveRecord::Migration.deconstrain :books, :title, :length_within
|
20
|
+
|
21
|
+
assert_allows Book do |book|
|
22
|
+
book.title = 'abcdefghijkl'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_length_within_inclusive_on_a_column_whose_name_is_a_sql_keyword
|
27
|
+
ActiveRecord::Migration.constrain :books, :as, :length_within => 5..11
|
28
|
+
|
29
|
+
assert_prohibits Book, :as, :length_within do |book|
|
30
|
+
book.as = 'abcdefghijkl'
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_prohibits Book, :as, :length_within do |book|
|
34
|
+
book.as = 'abcd'
|
35
|
+
end
|
36
|
+
|
37
|
+
assert_allows Book do |book|
|
38
|
+
book.as = 'abcdefg'
|
39
|
+
end
|
40
|
+
|
41
|
+
ActiveRecord::Migration.deconstrain :books, :as, :length_within
|
42
|
+
|
43
|
+
assert_allows Book do |book|
|
44
|
+
book.as = 'abcdefghijkl'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_length_within_non_inclusive
|
49
|
+
ActiveRecord::Migration.constrain :books, :title, :length_within => 5...11
|
50
|
+
|
51
|
+
assert_prohibits Book, :title, :length_within do |book|
|
52
|
+
book.title = 'abcdefghijk'
|
53
|
+
end
|
54
|
+
|
55
|
+
assert_prohibits Book, :title, :length_within do |book|
|
56
|
+
book.title = 'abcd'
|
57
|
+
end
|
58
|
+
|
59
|
+
assert_allows Book do |book|
|
60
|
+
book.title = 'abcdefg'
|
61
|
+
end
|
62
|
+
|
63
|
+
ActiveRecord::Migration.deconstrain :books, :title, :length_within
|
64
|
+
|
65
|
+
assert_allows Book do |book|
|
66
|
+
book.title = 'abcdefghijk'
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_length_within_non_inclusive_on_a_column_whose_name_is_a_sql_keyword
|
71
|
+
ActiveRecord::Migration.constrain :books, :as, :length_within => 5...11
|
72
|
+
|
73
|
+
assert_prohibits Book, :as, :length_within do |book|
|
74
|
+
book.as = 'abcdefghijk'
|
75
|
+
end
|
76
|
+
|
77
|
+
assert_prohibits Book, :as, :length_within do |book|
|
78
|
+
book.as = 'abcd'
|
79
|
+
end
|
80
|
+
|
81
|
+
assert_allows Book do |book|
|
82
|
+
book.as = 'abcdefg'
|
83
|
+
end
|
84
|
+
|
85
|
+
ActiveRecord::Migration.deconstrain :books, :as, :length_within
|
86
|
+
|
87
|
+
assert_allows Book do |book|
|
88
|
+
book.as = 'abcdefghijk'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class LowercaseTest < SexyPgConstraintsTest
|
4
|
+
def test_lowercase
|
5
|
+
ActiveRecord::Migration.constrain :books, :author, :lowercase => true
|
6
|
+
|
7
|
+
assert_prohibits Book, :author, :lowercase do |book|
|
8
|
+
book.author = 'UPPER'
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_allows Book do |book|
|
12
|
+
book.author = 'lower with 1337'
|
13
|
+
end
|
14
|
+
|
15
|
+
ActiveRecord::Migration.deconstrain :books, :author, :lowercase
|
16
|
+
|
17
|
+
assert_allows Book do |book|
|
18
|
+
book.author = 'UPPER'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class NotBlankTest < SexyPgConstraintsTest
|
4
|
+
def test_not_blank
|
5
|
+
ActiveRecord::Migration.constrain :books, :author, :not_blank => true
|
6
|
+
|
7
|
+
assert_prohibits Book, :author, :not_blank do |book|
|
8
|
+
book.author = ' '
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_allows Book do |book|
|
12
|
+
book.author = 'foo'
|
13
|
+
end
|
14
|
+
|
15
|
+
ActiveRecord::Migration.deconstrain :books, :author, :not_blank
|
16
|
+
|
17
|
+
assert_allows Book do |book|
|
18
|
+
book.author = ' '
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_not_blank_on_a_column_whose_name_is_a_sql_keyword
|
23
|
+
ActiveRecord::Migration.constrain :books, :as, :not_blank => true
|
24
|
+
|
25
|
+
assert_prohibits Book, :as, :not_blank do |book|
|
26
|
+
book.as = ' '
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_allows Book do |book|
|
30
|
+
book.as = 'foo'
|
31
|
+
end
|
32
|
+
|
33
|
+
ActiveRecord::Migration.deconstrain :books, :as, :not_blank
|
34
|
+
|
35
|
+
assert_allows Book do |book|
|
36
|
+
book.as = ' '
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class OddEventTest < SexyPgConstraintsTest
|
4
|
+
def test_odd
|
5
|
+
ActiveRecord::Migration.constrain :books, :quantity, :odd => true
|
6
|
+
|
7
|
+
assert_prohibits Book, :quantity, :odd do |book|
|
8
|
+
book.quantity = 2
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_allows Book do |book|
|
12
|
+
book.quantity = 1
|
13
|
+
end
|
14
|
+
|
15
|
+
ActiveRecord::Migration.deconstrain :books, :quantity, :odd
|
16
|
+
|
17
|
+
assert_allows Book do |book|
|
18
|
+
book.quantity = 2
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_odd_on_a_column_whose_name_is_a_sql_keyword
|
23
|
+
ActiveRecord::Migration.constrain :books, :from, :odd => true
|
24
|
+
|
25
|
+
assert_prohibits Book, :from, :odd do |book|
|
26
|
+
book.from = 2
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_allows Book do |book|
|
30
|
+
book.from = 1
|
31
|
+
end
|
32
|
+
|
33
|
+
ActiveRecord::Migration.deconstrain :books, :from, :odd
|
34
|
+
|
35
|
+
assert_allows Book do |book|
|
36
|
+
book.from = 2
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_even
|
41
|
+
ActiveRecord::Migration.constrain :books, :quantity, :even => true
|
42
|
+
|
43
|
+
assert_prohibits Book, :quantity, :even do |book|
|
44
|
+
book.quantity = 1
|
45
|
+
end
|
46
|
+
|
47
|
+
assert_allows Book do |book|
|
48
|
+
book.quantity = 2
|
49
|
+
end
|
50
|
+
|
51
|
+
ActiveRecord::Migration.deconstrain :books, :quantity, :even
|
52
|
+
|
53
|
+
assert_allows Book do |book|
|
54
|
+
book.quantity = 1
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_even_on_a_column_whose_name_is_a_sql_keyword
|
59
|
+
ActiveRecord::Migration.constrain :books, :from, :even => true
|
60
|
+
|
61
|
+
assert_prohibits Book, :from, :even do |book|
|
62
|
+
book.from = 1
|
63
|
+
end
|
64
|
+
|
65
|
+
assert_allows Book do |book|
|
66
|
+
book.from = 2
|
67
|
+
end
|
68
|
+
|
69
|
+
ActiveRecord::Migration.deconstrain :books, :from, :even
|
70
|
+
|
71
|
+
assert_allows Book do |book|
|
72
|
+
book.from = 1
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PositiveTest < SexyPgConstraintsTest
|
4
|
+
def test_positive
|
5
|
+
ActiveRecord::Migration.constrain :books, :quantity, :positive => true
|
6
|
+
|
7
|
+
assert_prohibits Book, :quantity, :positive do |book|
|
8
|
+
book.quantity = -1
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_allows Book do |book|
|
12
|
+
book.quantity = 0
|
13
|
+
end
|
14
|
+
|
15
|
+
assert_allows Book do |book|
|
16
|
+
book.quantity = 1
|
17
|
+
end
|
18
|
+
|
19
|
+
ActiveRecord::Migration.deconstrain :books, :quantity, :positive
|
20
|
+
|
21
|
+
assert_allows Book do |book|
|
22
|
+
book.quantity = -1
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_positive_on_a_column_whose_name_is_a_sql_keyword
|
27
|
+
ActiveRecord::Migration.constrain :books, :from, :positive => true
|
28
|
+
|
29
|
+
assert_prohibits Book, :from, :positive do |book|
|
30
|
+
book.from = -1
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_allows Book do |book|
|
34
|
+
book.from = 0
|
35
|
+
end
|
36
|
+
|
37
|
+
assert_allows Book do |book|
|
38
|
+
book.from = 1
|
39
|
+
end
|
40
|
+
|
41
|
+
ActiveRecord::Migration.deconstrain :books, :from, :positive
|
42
|
+
|
43
|
+
assert_allows Book do |book|
|
44
|
+
book.from = -1
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ReferenceTest < SexyPgConstraintsTest
|
4
|
+
def test_reference
|
5
|
+
ActiveRecord::Migration.constrain :books, :author_id, :reference => {:authors => :id}
|
6
|
+
|
7
|
+
assert_prohibits Book, :author_id, :reference, 'foreign key', ActiveRecord::InvalidForeignKey do |book|
|
8
|
+
book.author_id = 1
|
9
|
+
end
|
10
|
+
|
11
|
+
author = Author.new
|
12
|
+
author.name = "Mark Twain"
|
13
|
+
author.bio = "American writer"
|
14
|
+
assert author.save
|
15
|
+
|
16
|
+
assert_equal 1, author.id
|
17
|
+
|
18
|
+
assert_allows Book do |book|
|
19
|
+
book.author_id = 1
|
20
|
+
end
|
21
|
+
|
22
|
+
ActiveRecord::Migration.deconstrain :books, :author_id, :reference
|
23
|
+
|
24
|
+
assert_allows Book do |book|
|
25
|
+
book.author_id = 2
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_reference_on_a_column_whose_name_is_a_sql_keyword
|
30
|
+
ActiveRecord::Migration.constrain :books, :from, :reference => {:authors => :id}
|
31
|
+
|
32
|
+
assert_prohibits Book, :from, :reference, 'foreign key', ActiveRecord::InvalidForeignKey do |book|
|
33
|
+
book.from = 1
|
34
|
+
end
|
35
|
+
|
36
|
+
author = Author.new
|
37
|
+
author.name = "Mark Twain"
|
38
|
+
author.bio = "American writer"
|
39
|
+
assert author.save
|
40
|
+
|
41
|
+
assert_equal 1, author.id
|
42
|
+
|
43
|
+
assert_allows Book do |book|
|
44
|
+
book.from = 1
|
45
|
+
end
|
46
|
+
|
47
|
+
ActiveRecord::Migration.deconstrain :books, :from, :reference
|
48
|
+
|
49
|
+
assert_allows Book do |book|
|
50
|
+
book.from = 2
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_reference_with_on_delete
|
55
|
+
ActiveRecord::Migration.constrain :books, :author_id, :reference => {:authors => :id, :on_delete => :cascade}
|
56
|
+
|
57
|
+
author = Author.new
|
58
|
+
author.name = "Mark Twain"
|
59
|
+
author.bio = "American writer"
|
60
|
+
assert author.save
|
61
|
+
|
62
|
+
assert_equal 1, Author.count
|
63
|
+
|
64
|
+
assert_allows Book do |book|
|
65
|
+
book.title = "The Adventures of Tom Sawyer"
|
66
|
+
book.author_id = 1
|
67
|
+
end
|
68
|
+
|
69
|
+
assert_allows Book do |book|
|
70
|
+
book.title = "The Adventures of Huckleberry Finn"
|
71
|
+
book.author_id = 1
|
72
|
+
end
|
73
|
+
|
74
|
+
author.destroy
|
75
|
+
|
76
|
+
assert_equal 0, Author.count
|
77
|
+
assert_equal 0, Book.count
|
78
|
+
end
|
79
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -27,3 +27,15 @@ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:include, SexyPgConstra
|
|
27
27
|
|
28
28
|
class Test::Unit::TestCase
|
29
29
|
end
|
30
|
+
|
31
|
+
class SexyPgConstraintsTest < Test::Unit::TestCase
|
32
|
+
def setup
|
33
|
+
CreateBooks.up
|
34
|
+
CreateAuthors.up
|
35
|
+
end
|
36
|
+
|
37
|
+
def teardown
|
38
|
+
CreateBooks.down
|
39
|
+
CreateAuthors.down
|
40
|
+
end
|
41
|
+
end
|