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.
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ class WhitelistTest < SexyPgConstraintsTest
4
+ def test_whitelist
5
+ ActiveRecord::Migration.constrain :books, :author, :whitelist => %w(whitelisted1 whitelisted2 whitelisted3)
6
+
7
+ assert_prohibits Book, :author, :whitelist do |book|
8
+ book.author = 'not_whitelisted'
9
+ end
10
+
11
+ assert_allows Book do |book|
12
+ book.author = 'whitelisted2'
13
+ end
14
+
15
+ ActiveRecord::Migration.deconstrain :books, :author, :whitelist
16
+
17
+ assert_allows Book do |book|
18
+ book.author = 'not_whitelisted'
19
+ end
20
+ end
21
+
22
+ def test_whitelist_on_a_column_whose_name_is_a_sql_keyword
23
+ ActiveRecord::Migration.constrain :books, :as, :whitelist => %w(whitelisted1 whitelisted2 whitelisted3)
24
+
25
+ assert_prohibits Book, :as, :whitelist do |book|
26
+ book.as = 'not_whitelisted'
27
+ end
28
+
29
+ assert_allows Book do |book|
30
+ book.as = 'whitelisted2'
31
+ end
32
+
33
+ ActiveRecord::Migration.deconstrain :books, :as, :whitelist
34
+
35
+ assert_allows Book do |book|
36
+ book.as = 'not_whitelisted'
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,135 @@
1
+ require 'test_helper'
2
+
3
+ class WithinTest < SexyPgConstraintsTest
4
+ def test_within_inclusive
5
+ ActiveRecord::Migration.constrain :books, :quantity, :within => 5..11
6
+
7
+ assert_prohibits Book, :quantity, :within do |book|
8
+ book.quantity = 12
9
+ end
10
+
11
+ assert_prohibits Book, :quantity, :within do |book|
12
+ book.quantity = 4
13
+ end
14
+
15
+ assert_allows Book do |book|
16
+ book.quantity = 7
17
+ end
18
+
19
+ ActiveRecord::Migration.deconstrain :books, :quantity, :within
20
+
21
+ assert_allows Book do |book|
22
+ book.quantity = 12
23
+ end
24
+ end
25
+
26
+ def test_within_inclusive_on_a_column_whose_name_is_a_sql_keyword
27
+ ActiveRecord::Migration.constrain :books, :from, :within => 5..11
28
+
29
+ assert_prohibits Book, :from, :within do |book|
30
+ book.from = 12
31
+ end
32
+
33
+ assert_prohibits Book, :from, :within do |book|
34
+ book.from = 4
35
+ end
36
+
37
+ assert_allows Book do |book|
38
+ book.from = 7
39
+ end
40
+
41
+ ActiveRecord::Migration.deconstrain :books, :from, :within
42
+
43
+ assert_allows Book do |book|
44
+ book.from = 12
45
+ end
46
+ end
47
+
48
+ def test_within_non_inclusive
49
+ ActiveRecord::Migration.constrain :books, :quantity, :within => 5...11
50
+
51
+ assert_prohibits Book, :quantity, :within do |book|
52
+ book.quantity = 11
53
+ end
54
+
55
+ assert_prohibits Book, :quantity, :within do |book|
56
+ book.quantity = 4
57
+ end
58
+
59
+ assert_allows Book do |book|
60
+ book.quantity = 10
61
+ end
62
+
63
+ ActiveRecord::Migration.deconstrain :books, :quantity, :within
64
+
65
+ assert_allows Book do |book|
66
+ book.quantity = 11
67
+ end
68
+ end
69
+
70
+ def test_within_non_inclusive_on_a_column_whose_name_is_a_sql_keyword
71
+ ActiveRecord::Migration.constrain :books, :from, :within => 5...11
72
+
73
+ assert_prohibits Book, :from, :within do |book|
74
+ book.from = 11
75
+ end
76
+
77
+ assert_prohibits Book, :from, :within do |book|
78
+ book.from = 4
79
+ end
80
+
81
+ assert_allows Book do |book|
82
+ book.from = 10
83
+ end
84
+
85
+ ActiveRecord::Migration.deconstrain :books, :from, :within
86
+
87
+ assert_allows Book do |book|
88
+ book.from = 11
89
+ end
90
+ end
91
+
92
+ def test_within_exclude_beginning
93
+ ActiveRecord::Migration.constrain :books, :from, :within => {:range => 5...11, :exclude_beginning => true}
94
+
95
+ assert_prohibits Book, :from, :within do |book|
96
+ book.from = 11
97
+ end
98
+
99
+ assert_prohibits Book, :from, :within do |book|
100
+ book.from = 5
101
+ end
102
+
103
+ assert_allows Book do |book|
104
+ book.from = 10
105
+ end
106
+
107
+ ActiveRecord::Migration.deconstrain :books, :from, :within
108
+
109
+ assert_allows Book do |book|
110
+ book.from = 5
111
+ end
112
+ end
113
+
114
+ def test_within_exclude_end_overrides_range
115
+ ActiveRecord::Migration.constrain :books, :from, :within => {:range => 5...11, :exclude_end => false}
116
+
117
+ assert_prohibits Book, :from, :within do |book|
118
+ book.from = 12
119
+ end
120
+
121
+ assert_prohibits Book, :from, :within do |book|
122
+ book.from = 4
123
+ end
124
+
125
+ assert_allows Book do |book|
126
+ book.from = 11
127
+ end
128
+
129
+ ActiveRecord::Migration.deconstrain :books, :from, :within
130
+
131
+ assert_allows Book do |book|
132
+ book.from = 12
133
+ end
134
+ end
135
+ end
data/test/xor_test.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class XorTest < SexyPgConstraintsTest
4
+ def test_xor
5
+ ActiveRecord::Migration.constrain :books, [:xor_col_1, :xor_col_2], :xor => true
6
+
7
+ assert_prohibits Book, [:xor_col_1, :xor_col_2], :xor do |book|
8
+ book.xor_col_1 = 123
9
+ book.xor_col_2 = 321
10
+ end
11
+
12
+ assert_allows Book do |book|
13
+ book.xor_col_1 = 123
14
+ end
15
+
16
+ assert_allows Book do |book|
17
+ book.xor_col_2 = 123
18
+ end
19
+
20
+ ActiveRecord::Migration.deconstrain :books, [:xor_col_1, :xor_col_2], :xor
21
+
22
+ assert_allows Book do |book|
23
+ book.xor_col_1 = 123
24
+ book.xor_col_2 = 123
25
+ end
26
+ end
27
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: Empact-sexy_pg_constraints
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.2
5
+ version: 0.2.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Maxim Chernyak
@@ -105,11 +105,26 @@ files:
105
105
  - lib/sexy_pg_constraints/helpers.rb
106
106
  - lib/sexy_pg_constraints/railtie.rb
107
107
  - lib/sexy_pg_constraints/schema_definitions.rb
108
- - test/sexy_pg_constraints_test.rb
108
+ - test/alphanumeric_test.rb
109
+ - test/blacklist_test.rb
110
+ - test/email_test.rb
111
+ - test/exact_length_test.rb
112
+ - test/format_test.rb
113
+ - test/general_test.rb
114
+ - test/greater_less_than_test.rb
115
+ - test/length_within_test.rb
116
+ - test/lowercase_test.rb
117
+ - test/not_blank_test.rb
118
+ - test/odd_event_test.rb
119
+ - test/positive_test.rb
120
+ - test/reference_test.rb
109
121
  - test/support/assert_prohibits_allows.rb
110
122
  - test/support/database.yml.example
111
123
  - test/support/models.rb
112
124
  - test/test_helper.rb
125
+ - test/whitelist_test.rb
126
+ - test/within_test.rb
127
+ - test/xor_test.rb
113
128
  homepage: http://github.com/maxim/sexy_pg_constraints
114
129
  licenses: []
115
130
 
@@ -123,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
138
  requirements:
124
139
  - - ">="
125
140
  - !ruby/object:Gem::Version
126
- hash: -864157024733078018
141
+ hash: 1636863430316657218
127
142
  segments:
128
143
  - 0
129
144
  version: "0"
@@ -141,7 +156,22 @@ signing_key:
141
156
  specification_version: 3
142
157
  summary: ""
143
158
  test_files:
144
- - test/sexy_pg_constraints_test.rb
159
+ - test/alphanumeric_test.rb
160
+ - test/blacklist_test.rb
161
+ - test/email_test.rb
162
+ - test/exact_length_test.rb
163
+ - test/format_test.rb
164
+ - test/general_test.rb
165
+ - test/greater_less_than_test.rb
166
+ - test/length_within_test.rb
167
+ - test/lowercase_test.rb
168
+ - test/not_blank_test.rb
169
+ - test/odd_event_test.rb
170
+ - test/positive_test.rb
171
+ - test/reference_test.rb
145
172
  - test/support/assert_prohibits_allows.rb
146
173
  - test/support/models.rb
147
174
  - test/test_helper.rb
175
+ - test/whitelist_test.rb
176
+ - test/within_test.rb
177
+ - test/xor_test.rb
@@ -1,943 +0,0 @@
1
- require 'test_helper'
2
-
3
- class SexyPgConstraintsTest < Test::Unit::TestCase
4
- def setup
5
- CreateBooks.up
6
- CreateAuthors.up
7
- end
8
-
9
- def teardown
10
- CreateBooks.down
11
- CreateAuthors.down
12
- end
13
-
14
- def test_should_create_book
15
- Book.create
16
- assert_equal 1, Book.count
17
- end
18
-
19
- def test_whitelist
20
- ActiveRecord::Migration.constrain :books, :author, :whitelist => %w(whitelisted1 whitelisted2 whitelisted3)
21
-
22
- assert_prohibits Book, :author, :whitelist do |book|
23
- book.author = 'not_whitelisted'
24
- end
25
-
26
- assert_allows Book do |book|
27
- book.author = 'whitelisted2'
28
- end
29
-
30
- ActiveRecord::Migration.deconstrain :books, :author, :whitelist
31
-
32
- assert_allows Book do |book|
33
- book.author = 'not_whitelisted'
34
- end
35
- end
36
-
37
- def test_whitelist_on_a_column_whose_name_is_a_sql_keyword
38
- ActiveRecord::Migration.constrain :books, :as, :whitelist => %w(whitelisted1 whitelisted2 whitelisted3)
39
-
40
- assert_prohibits Book, :as, :whitelist do |book|
41
- book.as = 'not_whitelisted'
42
- end
43
-
44
- assert_allows Book do |book|
45
- book.as = 'whitelisted2'
46
- end
47
-
48
- ActiveRecord::Migration.deconstrain :books, :as, :whitelist
49
-
50
- assert_allows Book do |book|
51
- book.as = 'not_whitelisted'
52
- end
53
- end
54
-
55
- def test_blacklist
56
- ActiveRecord::Migration.constrain :books, :author, :blacklist => %w(blacklisted1 blacklisted2 blacklisted3)
57
-
58
- assert_prohibits Book, :author, :blacklist do |book|
59
- book.author = 'blacklisted2'
60
- end
61
-
62
- assert_allows Book do |book|
63
- book.author = 'not_blacklisted'
64
- end
65
-
66
- ActiveRecord::Migration.deconstrain :books, :author, :blacklist
67
-
68
- assert_allows Book do |book|
69
- book.author = 'blacklisted2'
70
- end
71
- end
72
-
73
- def test_blacklist_on_a_column_whose_name_is_a_sql_keyword
74
- ActiveRecord::Migration.constrain :books, :as, :blacklist => %w(blacklisted1 blacklisted2 blacklisted3)
75
-
76
- assert_prohibits Book, :as, :blacklist do |book|
77
- book.as = 'blacklisted2'
78
- end
79
-
80
- assert_allows Book do |book|
81
- book.as = 'not_blacklisted'
82
- end
83
-
84
- ActiveRecord::Migration.deconstrain :books, :as, :blacklist
85
-
86
- assert_allows Book do |book|
87
- book.as = 'blacklisted2'
88
- end
89
- end
90
-
91
- def test_not_blank
92
- ActiveRecord::Migration.constrain :books, :author, :not_blank => true
93
-
94
- assert_prohibits Book, :author, :not_blank do |book|
95
- book.author = ' '
96
- end
97
-
98
- assert_allows Book do |book|
99
- book.author = 'foo'
100
- end
101
-
102
- ActiveRecord::Migration.deconstrain :books, :author, :not_blank
103
-
104
- assert_allows Book do |book|
105
- book.author = ' '
106
- end
107
- end
108
-
109
- def test_not_blank_on_a_column_whose_name_is_a_sql_keyword
110
- ActiveRecord::Migration.constrain :books, :as, :not_blank => true
111
-
112
- assert_prohibits Book, :as, :not_blank do |book|
113
- book.as = ' '
114
- end
115
-
116
- assert_allows Book do |book|
117
- book.as = 'foo'
118
- end
119
-
120
- ActiveRecord::Migration.deconstrain :books, :as, :not_blank
121
-
122
- assert_allows Book do |book|
123
- book.as = ' '
124
- end
125
- end
126
-
127
- def test_within_inclusive
128
- ActiveRecord::Migration.constrain :books, :quantity, :within => 5..11
129
-
130
- assert_prohibits Book, :quantity, :within do |book|
131
- book.quantity = 12
132
- end
133
-
134
- assert_prohibits Book, :quantity, :within do |book|
135
- book.quantity = 4
136
- end
137
-
138
- assert_allows Book do |book|
139
- book.quantity = 7
140
- end
141
-
142
- ActiveRecord::Migration.deconstrain :books, :quantity, :within
143
-
144
- assert_allows Book do |book|
145
- book.quantity = 12
146
- end
147
- end
148
-
149
- def test_within_inclusive_on_a_column_whose_name_is_a_sql_keyword
150
- ActiveRecord::Migration.constrain :books, :from, :within => 5..11
151
-
152
- assert_prohibits Book, :from, :within do |book|
153
- book.from = 12
154
- end
155
-
156
- assert_prohibits Book, :from, :within do |book|
157
- book.from = 4
158
- end
159
-
160
- assert_allows Book do |book|
161
- book.from = 7
162
- end
163
-
164
- ActiveRecord::Migration.deconstrain :books, :from, :within
165
-
166
- assert_allows Book do |book|
167
- book.from = 12
168
- end
169
- end
170
-
171
- def test_within_non_inclusive
172
- ActiveRecord::Migration.constrain :books, :quantity, :within => 5...11
173
-
174
- assert_prohibits Book, :quantity, :within do |book|
175
- book.quantity = 11
176
- end
177
-
178
- assert_prohibits Book, :quantity, :within do |book|
179
- book.quantity = 4
180
- end
181
-
182
- assert_allows Book do |book|
183
- book.quantity = 10
184
- end
185
-
186
- ActiveRecord::Migration.deconstrain :books, :quantity, :within
187
-
188
- assert_allows Book do |book|
189
- book.quantity = 11
190
- end
191
- end
192
-
193
- def test_within_non_inclusive_on_a_column_whose_name_is_a_sql_keyword
194
- ActiveRecord::Migration.constrain :books, :from, :within => 5...11
195
-
196
- assert_prohibits Book, :from, :within do |book|
197
- book.from = 11
198
- end
199
-
200
- assert_prohibits Book, :from, :within do |book|
201
- book.from = 4
202
- end
203
-
204
- assert_allows Book do |book|
205
- book.from = 10
206
- end
207
-
208
- ActiveRecord::Migration.deconstrain :books, :from, :within
209
-
210
- assert_allows Book do |book|
211
- book.from = 11
212
- end
213
- end
214
-
215
- def test_within_exclude_beginning
216
- ActiveRecord::Migration.constrain :books, :from, :within => {:range => 5...11, :exclude_beginning => true}
217
-
218
- assert_prohibits Book, :from, :within do |book|
219
- book.from = 11
220
- end
221
-
222
- assert_prohibits Book, :from, :within do |book|
223
- book.from = 5
224
- end
225
-
226
- assert_allows Book do |book|
227
- book.from = 10
228
- end
229
-
230
- ActiveRecord::Migration.deconstrain :books, :from, :within
231
-
232
- assert_allows Book do |book|
233
- book.from = 5
234
- end
235
- end
236
-
237
- def test_within_exclude_end_overrides_range
238
- ActiveRecord::Migration.constrain :books, :from, :within => {:range => 5...11, :exclude_end => false}
239
-
240
- assert_prohibits Book, :from, :within do |book|
241
- book.from = 12
242
- end
243
-
244
- assert_prohibits Book, :from, :within do |book|
245
- book.from = 4
246
- end
247
-
248
- assert_allows Book do |book|
249
- book.from = 11
250
- end
251
-
252
- ActiveRecord::Migration.deconstrain :books, :from, :within
253
-
254
- assert_allows Book do |book|
255
- book.from = 12
256
- end
257
- end
258
-
259
- def test_length_within_inclusive
260
- ActiveRecord::Migration.constrain :books, :title, :length_within => 5..11
261
-
262
- assert_prohibits Book, :title, :length_within do |book|
263
- book.title = 'abcdefghijkl'
264
- end
265
-
266
- assert_prohibits Book, :title, :length_within do |book|
267
- book.title = 'abcd'
268
- end
269
-
270
- assert_allows Book do |book|
271
- book.title = 'abcdefg'
272
- end
273
-
274
- ActiveRecord::Migration.deconstrain :books, :title, :length_within
275
-
276
- assert_allows Book do |book|
277
- book.title = 'abcdefghijkl'
278
- end
279
- end
280
-
281
- def test_length_within_inclusive_on_a_column_whose_name_is_a_sql_keyword
282
- ActiveRecord::Migration.constrain :books, :as, :length_within => 5..11
283
-
284
- assert_prohibits Book, :as, :length_within do |book|
285
- book.as = 'abcdefghijkl'
286
- end
287
-
288
- assert_prohibits Book, :as, :length_within do |book|
289
- book.as = 'abcd'
290
- end
291
-
292
- assert_allows Book do |book|
293
- book.as = 'abcdefg'
294
- end
295
-
296
- ActiveRecord::Migration.deconstrain :books, :as, :length_within
297
-
298
- assert_allows Book do |book|
299
- book.as = 'abcdefghijkl'
300
- end
301
- end
302
-
303
- def test_length_within_non_inclusive
304
- ActiveRecord::Migration.constrain :books, :title, :length_within => 5...11
305
-
306
- assert_prohibits Book, :title, :length_within do |book|
307
- book.title = 'abcdefghijk'
308
- end
309
-
310
- assert_prohibits Book, :title, :length_within do |book|
311
- book.title = 'abcd'
312
- end
313
-
314
- assert_allows Book do |book|
315
- book.title = 'abcdefg'
316
- end
317
-
318
- ActiveRecord::Migration.deconstrain :books, :title, :length_within
319
-
320
- assert_allows Book do |book|
321
- book.title = 'abcdefghijk'
322
- end
323
- end
324
-
325
- def test_length_within_non_inclusive_on_a_column_whose_name_is_a_sql_keyword
326
- ActiveRecord::Migration.constrain :books, :as, :length_within => 5...11
327
-
328
- assert_prohibits Book, :as, :length_within do |book|
329
- book.as = 'abcdefghijk'
330
- end
331
-
332
- assert_prohibits Book, :as, :length_within do |book|
333
- book.as = 'abcd'
334
- end
335
-
336
- assert_allows Book do |book|
337
- book.as = 'abcdefg'
338
- end
339
-
340
- ActiveRecord::Migration.deconstrain :books, :as, :length_within
341
-
342
- assert_allows Book do |book|
343
- book.as = 'abcdefghijk'
344
- end
345
- end
346
-
347
- def test_email
348
- ActiveRecord::Migration.constrain :books, :author, :email => true
349
-
350
- assert_prohibits Book, :author, :email do |book|
351
- book.author = 'blah@example'
352
- end
353
-
354
- assert_allows Book do |book|
355
- book.author = 'blah@example.com'
356
- end
357
-
358
- ActiveRecord::Migration.deconstrain :books, :author, :email
359
-
360
- assert_allows Book do |book|
361
- book.author = 'blah@example'
362
- end
363
- end
364
-
365
- def test_email_on_a_column_whose_name_is_a_sql_keyword
366
- ActiveRecord::Migration.constrain :books, :as, :email => true
367
-
368
- assert_prohibits Book, :as, :email do |book|
369
- book.as = 'blah@example'
370
- end
371
-
372
- assert_allows Book do |book|
373
- book.as = 'blah@example.com'
374
- end
375
-
376
- ActiveRecord::Migration.deconstrain :books, :as, :email
377
-
378
- assert_allows Book do |book|
379
- book.as = 'blah@example'
380
- end
381
- end
382
-
383
- def test_alphanumeric
384
- ActiveRecord::Migration.constrain :books, :title, :alphanumeric => true
385
-
386
- assert_prohibits Book, :title, :alphanumeric do |book|
387
- book.title = 'asdf@asdf'
388
- end
389
-
390
- assert_allows Book do |book|
391
- book.title = 'asdf'
392
- end
393
-
394
- ActiveRecord::Migration.deconstrain :books, :title, :alphanumeric
395
-
396
- assert_allows Book do |book|
397
- book.title = 'asdf@asdf'
398
- end
399
- end
400
-
401
- def test_alphanumeric_on_a_column_whose_name_is_a_sql_keyword
402
- ActiveRecord::Migration.constrain :books, :as, :alphanumeric => true
403
-
404
- assert_prohibits Book, :as, :alphanumeric do |book|
405
- book.as = 'asdf@asdf'
406
- end
407
-
408
- assert_allows Book do |book|
409
- book.as = 'asdf'
410
- end
411
-
412
- ActiveRecord::Migration.deconstrain :books, :as, :alphanumeric
413
-
414
- assert_allows Book do |book|
415
- book.as = 'asdf@asdf'
416
- end
417
- end
418
-
419
- def test_positive
420
- ActiveRecord::Migration.constrain :books, :quantity, :positive => true
421
-
422
- assert_prohibits Book, :quantity, :positive do |book|
423
- book.quantity = -1
424
- end
425
-
426
- assert_allows Book do |book|
427
- book.quantity = 0
428
- end
429
-
430
- assert_allows Book do |book|
431
- book.quantity = 1
432
- end
433
-
434
- ActiveRecord::Migration.deconstrain :books, :quantity, :positive
435
-
436
- assert_allows Book do |book|
437
- book.quantity = -1
438
- end
439
- end
440
-
441
- def test_positive_on_a_column_whose_name_is_a_sql_keyword
442
- ActiveRecord::Migration.constrain :books, :from, :positive => true
443
-
444
- assert_prohibits Book, :from, :positive do |book|
445
- book.from = -1
446
- end
447
-
448
- assert_allows Book do |book|
449
- book.from = 0
450
- end
451
-
452
- assert_allows Book do |book|
453
- book.from = 1
454
- end
455
-
456
- ActiveRecord::Migration.deconstrain :books, :from, :positive
457
-
458
- assert_allows Book do |book|
459
- book.from = -1
460
- end
461
- end
462
-
463
- def test_odd
464
- ActiveRecord::Migration.constrain :books, :quantity, :odd => true
465
-
466
- assert_prohibits Book, :quantity, :odd do |book|
467
- book.quantity = 2
468
- end
469
-
470
- assert_allows Book do |book|
471
- book.quantity = 1
472
- end
473
-
474
- ActiveRecord::Migration.deconstrain :books, :quantity, :odd
475
-
476
- assert_allows Book do |book|
477
- book.quantity = 2
478
- end
479
- end
480
-
481
- def test_odd_on_a_column_whose_name_is_a_sql_keyword
482
- ActiveRecord::Migration.constrain :books, :from, :odd => true
483
-
484
- assert_prohibits Book, :from, :odd do |book|
485
- book.from = 2
486
- end
487
-
488
- assert_allows Book do |book|
489
- book.from = 1
490
- end
491
-
492
- ActiveRecord::Migration.deconstrain :books, :from, :odd
493
-
494
- assert_allows Book do |book|
495
- book.from = 2
496
- end
497
- end
498
-
499
- def test_even
500
- ActiveRecord::Migration.constrain :books, :quantity, :even => true
501
-
502
- assert_prohibits Book, :quantity, :even do |book|
503
- book.quantity = 1
504
- end
505
-
506
- assert_allows Book do |book|
507
- book.quantity = 2
508
- end
509
-
510
- ActiveRecord::Migration.deconstrain :books, :quantity, :even
511
-
512
- assert_allows Book do |book|
513
- book.quantity = 1
514
- end
515
- end
516
-
517
- def test_even_on_a_column_whose_name_is_a_sql_keyword
518
- ActiveRecord::Migration.constrain :books, :from, :even => true
519
-
520
- assert_prohibits Book, :from, :even do |book|
521
- book.from = 1
522
- end
523
-
524
- assert_allows Book do |book|
525
- book.from = 2
526
- end
527
-
528
- ActiveRecord::Migration.deconstrain :books, :from, :even
529
-
530
- assert_allows Book do |book|
531
- book.from = 1
532
- end
533
- end
534
-
535
- def test_unique
536
- ActiveRecord::Migration.constrain :books, :isbn, :unique => true
537
-
538
- assert_allows Book do |book|
539
- book.isbn = 'foo'
540
- end
541
-
542
- assert_prohibits Book, :isbn, :unique, 'unique', ActiveRecord::RecordNotUnique do |book|
543
- book.isbn = 'foo'
544
- end
545
-
546
- ActiveRecord::Migration.deconstrain :books, :isbn, :unique
547
-
548
- assert_allows Book do |book|
549
- book.isbn = 'foo'
550
- end
551
- end
552
-
553
- def test_unique_on_a_column_whose_name_is_a_sql_keyword
554
- ActiveRecord::Migration.constrain :books, :as, :unique => true
555
-
556
- assert_allows Book do |book|
557
- book.as = 'foo'
558
- end
559
-
560
- assert_prohibits Book, :as, :unique, 'unique', ActiveRecord::RecordNotUnique do |book|
561
- book.as = 'foo'
562
- end
563
-
564
- ActiveRecord::Migration.deconstrain :books, :as, :unique
565
-
566
- assert_allows Book do |book|
567
- book.as = 'foo'
568
- end
569
- end
570
-
571
- def test_exact_length
572
- ActiveRecord::Migration.constrain :books, :isbn, :exact_length => 5
573
-
574
- assert_prohibits Book, :isbn, :exact_length do |book|
575
- book.isbn = '123456'
576
- end
577
-
578
- assert_prohibits Book, :isbn, :exact_length do |book|
579
- book.isbn = '1234'
580
- end
581
-
582
- assert_allows Book do |book|
583
- book.isbn = '12345'
584
- end
585
-
586
- ActiveRecord::Migration.deconstrain :books, :isbn, :exact_length
587
-
588
- assert_allows Book do |book|
589
- book.isbn = '123456'
590
- end
591
- end
592
-
593
- def test_exact_length_on_a_column_whose_name_is_a_sql_keyword
594
- ActiveRecord::Migration.constrain :books, :as, :exact_length => 5
595
-
596
- assert_prohibits Book, :as, :exact_length do |book|
597
- book.as = '123456'
598
- end
599
-
600
- assert_prohibits Book, :as, :exact_length do |book|
601
- book.as = '1234'
602
- end
603
-
604
- assert_allows Book do |book|
605
- book.as = '12345'
606
- end
607
-
608
- ActiveRecord::Migration.deconstrain :books, :as, :exact_length
609
-
610
- assert_allows Book do |book|
611
- book.as = '123456'
612
- end
613
- end
614
-
615
- def test_format_case_insensitive
616
- ActiveRecord::Migration.constrain :books, :title, :format => /^[a-z]+$/i
617
-
618
- assert_prohibits Book, :title, :format do |book|
619
- book.title = 'abc3'
620
- end
621
-
622
- assert_prohibits Book, :title, :format do |book|
623
- book.title = ''
624
- end
625
-
626
- assert_allows Book do |book|
627
- book.title = 'abc'
628
- end
629
-
630
- assert_allows Book do |book|
631
- book.title = 'ABc'
632
- end
633
-
634
- ActiveRecord::Migration.deconstrain :books, :title, :format
635
-
636
- assert_allows Book do |book|
637
- book.title = 'abc3'
638
- end
639
- end
640
-
641
- def test_format_case_insensitive_on_a_column_whose_name_is_a_sql_keyword
642
- ActiveRecord::Migration.constrain :books, :as, :format => /^[a-z]+$/i
643
-
644
- assert_prohibits Book, :as, :format do |book|
645
- book.as = 'abc3'
646
- end
647
-
648
- assert_prohibits Book, :as, :format do |book|
649
- book.as = ''
650
- end
651
-
652
- assert_allows Book do |book|
653
- book.as = 'abc'
654
- end
655
-
656
- assert_allows Book do |book|
657
- book.as = 'ABc'
658
- end
659
-
660
- ActiveRecord::Migration.deconstrain :books, :as, :format
661
-
662
- assert_allows Book do |book|
663
- book.as = 'abc3'
664
- end
665
- end
666
-
667
- def test_format_case_sensitive
668
- ActiveRecord::Migration.constrain :books, :title, :format => /^[a-z]+$/
669
-
670
- assert_prohibits Book, :title, :format do |book|
671
- book.title = 'aBc'
672
- end
673
-
674
- assert_allows Book do |book|
675
- book.title = 'abc'
676
- end
677
-
678
- ActiveRecord::Migration.deconstrain :books, :title, :format
679
-
680
- assert_allows Book do |book|
681
- book.title = 'aBc'
682
- end
683
- end
684
-
685
- def test_format_case_sensitive_on_a_column_whose_name_is_a_sql_keyword
686
- ActiveRecord::Migration.constrain :books, :as, :format => /^[a-z]+$/
687
-
688
- assert_prohibits Book, :as, :format do |book|
689
- book.as = 'aBc'
690
- end
691
-
692
- assert_allows Book do |book|
693
- book.as = 'abc'
694
- end
695
-
696
- ActiveRecord::Migration.deconstrain :books, :as, :format
697
-
698
- assert_allows Book do |book|
699
- book.as = 'aBc'
700
- end
701
- end
702
-
703
- def test_reference
704
- ActiveRecord::Migration.constrain :books, :author_id, :reference => {:authors => :id}
705
-
706
- assert_prohibits Book, :author_id, :reference, 'foreign key', ActiveRecord::InvalidForeignKey do |book|
707
- book.author_id = 1
708
- end
709
-
710
- author = Author.new
711
- author.name = "Mark Twain"
712
- author.bio = "American writer"
713
- assert author.save
714
-
715
- assert_equal 1, author.id
716
-
717
- assert_allows Book do |book|
718
- book.author_id = 1
719
- end
720
-
721
- ActiveRecord::Migration.deconstrain :books, :author_id, :reference
722
-
723
- assert_allows Book do |book|
724
- book.author_id = 2
725
- end
726
- end
727
-
728
- def test_reference_on_a_column_whose_name_is_a_sql_keyword
729
- ActiveRecord::Migration.constrain :books, :from, :reference => {:authors => :id}
730
-
731
- assert_prohibits Book, :from, :reference, 'foreign key', ActiveRecord::InvalidForeignKey do |book|
732
- book.from = 1
733
- end
734
-
735
- author = Author.new
736
- author.name = "Mark Twain"
737
- author.bio = "American writer"
738
- assert author.save
739
-
740
- assert_equal 1, author.id
741
-
742
- assert_allows Book do |book|
743
- book.from = 1
744
- end
745
-
746
- ActiveRecord::Migration.deconstrain :books, :from, :reference
747
-
748
- assert_allows Book do |book|
749
- book.from = 2
750
- end
751
- end
752
-
753
- def test_reference_with_on_delete
754
- ActiveRecord::Migration.constrain :books, :author_id, :reference => {:authors => :id, :on_delete => :cascade}
755
-
756
- author = Author.new
757
- author.name = "Mark Twain"
758
- author.bio = "American writer"
759
- assert author.save
760
-
761
- assert_equal 1, Author.count
762
-
763
- assert_allows Book do |book|
764
- book.title = "The Adventures of Tom Sawyer"
765
- book.author_id = 1
766
- end
767
-
768
- assert_allows Book do |book|
769
- book.title = "The Adventures of Huckleberry Finn"
770
- book.author_id = 1
771
- end
772
-
773
- author.destroy
774
-
775
- assert_equal 0, Author.count
776
- assert_equal 0, Book.count
777
- end
778
-
779
- def test_block_syntax
780
- ActiveRecord::Migration.constrain :books do |t|
781
- t.title :not_blank => true
782
- t.isbn :exact_length => 15
783
- t.author :alphanumeric => true
784
- end
785
-
786
- assert_prohibits Book, :title, :not_blank do |book|
787
- book.title = ' '
788
- end
789
-
790
- assert_prohibits Book, :isbn, :exact_length do |book|
791
- book.isbn = 'asdf'
792
- end
793
-
794
- assert_prohibits Book, :author, :alphanumeric do |book|
795
- book.author = 'foo#bar'
796
- end
797
-
798
- ActiveRecord::Migration.deconstrain :books do |t|
799
- t.title :not_blank
800
- t.isbn :exact_length
801
- t.author :alphanumeric
802
- end
803
-
804
- assert_allows Book do |book|
805
- book.title = ' '
806
- book.isbn = 'asdf'
807
- book.author = 'foo#bar'
808
- end
809
- end
810
-
811
- def test_multiple_constraints_per_line
812
- ActiveRecord::Migration.constrain :books do |t|
813
- t.title :not_blank => true, :alphanumeric => true, :blacklist => %w(foo bar)
814
- end
815
-
816
- assert_prohibits Book, :title, [:not_blank, :alphanumeric] do |book|
817
- book.title = ' '
818
- end
819
-
820
- assert_prohibits Book, :title, :alphanumeric do |book|
821
- book.title = 'asdf@asdf'
822
- end
823
-
824
- assert_prohibits Book, :title, :blacklist do |book|
825
- book.title = 'foo'
826
- end
827
-
828
- ActiveRecord::Migration.deconstrain :books do |t|
829
- t.title :not_blank, :alphanumeric, :blacklist
830
- end
831
-
832
- assert_allows Book do |book|
833
- book.title = ' '
834
- end
835
-
836
- assert_allows Book do |book|
837
- book.title = 'asdf@asdf'
838
- end
839
-
840
- assert_allows Book do |book|
841
- book.title = 'foo'
842
- end
843
- end
844
-
845
- def test_multicolumn_constraint
846
- ActiveRecord::Migration.constrain :books, [:title, :isbn], :unique => true
847
-
848
- assert_allows Book do |book|
849
- book.title = 'foo'
850
- book.isbn = 'bar'
851
- end
852
-
853
- assert_allows Book do |book|
854
- book.title = 'foo'
855
- book.isbn = 'foo'
856
- end
857
-
858
- assert_prohibits Book, [:title, :isbn], :unique, 'unique', ActiveRecord::RecordNotUnique do |book|
859
- book.title = 'foo'
860
- book.isbn = 'bar'
861
- end
862
-
863
- ActiveRecord::Migration.deconstrain :books, [:title, :isbn], :unique
864
-
865
- assert_allows Book do |book|
866
- book.title = 'foo'
867
- book.isbn = 'bar'
868
- end
869
- end
870
-
871
- def test_multicolumn_constraint_block_syntax
872
- ActiveRecord::Migration.constrain :books do |t|
873
- t[:title, :isbn].all :unique => true
874
- end
875
-
876
- assert_allows Book do |book|
877
- book.title = 'foo'
878
- book.isbn = 'bar'
879
- end
880
-
881
- assert_allows Book do |book|
882
- book.title = 'foo'
883
- book.isbn = 'foo'
884
- end
885
-
886
- assert_prohibits Book, [:title, :isbn], :unique, 'unique', ActiveRecord::RecordNotUnique do |book|
887
- book.title = 'foo'
888
- book.isbn = 'bar'
889
- end
890
-
891
- ActiveRecord::Migration.deconstrain :books do |t|
892
- t[:title, :isbn].all :unique
893
- end
894
-
895
- assert_allows Book do |book|
896
- book.title = 'foo'
897
- book.isbn = 'bar'
898
- end
899
- end
900
-
901
- def test_lowercase
902
- ActiveRecord::Migration.constrain :books, :author, :lowercase => true
903
-
904
- assert_prohibits Book, :author, :lowercase do |book|
905
- book.author = 'UPPER'
906
- end
907
-
908
- assert_allows Book do |book|
909
- book.author = 'lower with 1337'
910
- end
911
-
912
- ActiveRecord::Migration.deconstrain :books, :author, :lowercase
913
-
914
- assert_allows Book do |book|
915
- book.author = 'UPPER'
916
- end
917
-
918
- end
919
-
920
- def test_xor
921
- ActiveRecord::Migration.constrain :books, [:xor_col_1, :xor_col_2], :xor => true
922
-
923
- assert_prohibits Book, [:xor_col_1, :xor_col_2], :xor do |book|
924
- book.xor_col_1 = 123
925
- book.xor_col_2 = 321
926
- end
927
-
928
- assert_allows Book do |book|
929
- book.xor_col_1 = 123
930
- end
931
-
932
- assert_allows Book do |book|
933
- book.xor_col_2 = 123
934
- end
935
-
936
- ActiveRecord::Migration.deconstrain :books, [:xor_col_1, :xor_col_2], :xor
937
-
938
- assert_allows Book do |book|
939
- book.xor_col_1 = 123
940
- book.xor_col_2 = 123
941
- end
942
- end
943
- end