minitest-sequel 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ba7809791b48d3d37abf55385778ff37b31a0743
4
- data.tar.gz: eb4eca066d3a6a7e39a975a85f5587ad5f6faecc
3
+ metadata.gz: 2997e3b0fa5879eef663dc938c536c4ce8fd1fd3
4
+ data.tar.gz: 533196e4a81a67e333c4251d71b2aa48999497e8
5
5
  SHA512:
6
- metadata.gz: 73425aa76579e6118327dc672089e9725451551a00bf80d39b388c666eb31d6f1f26ba905935cf38ed0aad0446813783db0f6cc34d126befdd9b405ad1035007
7
- data.tar.gz: 101c5b1c3c9b2d8e0be36353e63f8e9c84e7e371952fb7c7a8b17770e7bdc6f38aacd61e450e8df9b324e0ac9ba143b47035c1c86190daae4c65ad11f993a931
6
+ metadata.gz: 10b33ee43a344c43568e2c7c6207a3bf8fda4fb048e75abe916029a49ab8646d1a3539b84fb3293290a9600f9e05d5a03a8a9650312c7c799428fb0c22a906a9
7
+ data.tar.gz: 37de04749d64329f6ba80d754643c1633e3880e037a037c54a570570a4165b0758221e1d603ee26dc8dd2f7a56396e3cca782ad1a9795761e744f440b8701293
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /rubocop/
data/README.md CHANGED
@@ -13,44 +13,53 @@ Please help out with missing features / functionality.
13
13
  ## Model Definitions
14
14
 
15
15
 
16
- ### `assert_have_column ()`
16
+ ### `assert_have_column ()` or `.must_have_column()`
17
17
 
18
18
  Conveniently test your Model definitions as follows:
19
19
 
20
- let(:m) { Post.first }
21
-
22
- it { assert_have_column(m, :title, type: :string, db_type: 'varchar(250)', allow_null: :false ) }
23
-
24
- # definition of args
25
- assert_have_column(<instance>, <column_name>, <options>, <custom_error_message>)
20
+ ```ruby
21
+ let(:m) { Post.first }
26
22
 
23
+ it { assert_have_column(m, :title, type: :string, db_type: 'varchar(250)', allow_null: :false) }
24
+
25
+ it { m.must_have_column(:title, type: :string, allow_null: :false) }
26
+
27
+ # definition of args
28
+ assert_have_column(
29
+ <instance>,
30
+ <column_name>,
31
+ <options>,
32
+ <custom_error_message>
33
+ )
34
+ ```
27
35
 
28
36
  The `assert_have_column()` method first tests if the column name is defined in the Model and then checks all passed options.
29
37
 
30
38
 
31
39
  The following options are valid and checked:
32
40
 
33
- * :type
34
- * :db_type
35
- * :allow_null
36
- * :max_length
37
- * :default
38
- * :primary_key
39
- * :auto_increment
41
+ * `:type`
42
+ * `:db_type`
43
+ * `:allow_null`
44
+ * `:max_length`
45
+ * `:default`
46
+ * `:primary_key`
47
+ * `:auto_increment`
40
48
 
41
49
 
42
50
  In the event the specs differ from the actual database implementation an extensive error message with the
43
51
  differing option(s) is provided to help speed up debugging the issue:
44
52
 
45
- Expected Post model to have column: :title with: \
46
- { type: 'string', db_type: 'varchar(250)', allow_null: 'false' } \
47
- but found: { db_type: 'varchar(255)' }
48
-
53
+ ```
54
+ Expected Post model to have column: :title with: \
55
+ { type: 'string', db_type: 'varchar(250)', allow_null: 'false' } \
56
+ but found: { db_type: 'varchar(255)' }
57
+ ```
49
58
 
50
59
  **Please NOTE!**
51
60
 
52
61
  To test options with a value that is either `nil`, `true` or `false`, please use `:nil`, `:false` or `:true` and provide
53
- numbers as 'strings' instead, ie: '1' instead of 1.
62
+ numbers as 'strings' instead, ie: `'1'` instead of `1`.
54
63
 
55
64
 
56
65
  <br>
@@ -66,9 +75,13 @@ numbers as 'strings' instead, ie: '1' instead of 1.
66
75
  Conveniently test model associations quickly and easily with these Minitest assertions:
67
76
 
68
77
  * `assert_association_one_to_one`
78
+
69
79
  * `assert_association_one_to_many`
80
+
70
81
  * `assert_association_many_to_one`
82
+
71
83
  * `assert_association_many_to_many`
84
+
72
85
  * `assert_association`
73
86
 
74
87
  <br>
@@ -80,35 +93,43 @@ Conveniently test model associations quickly and easily with these Minitest asse
80
93
 
81
94
  A model defined with an association like this:
82
95
 
83
- class Post < Sequel::Model
84
- one_to_one :first_comment, :class=>:Comment, :order=>:id
85
- end
96
+
97
+ ```ruby
98
+ class Post < Sequel::Model
99
+ one_to_one :first_comment, class: :Comment, order: :id
100
+ end
101
+ ```
86
102
 
87
103
  Can be easily and quickly tested with `assert_association_one_to_one()` like this:
88
104
 
89
- let(:m) { Post.first }
105
+ ```ruby
106
+ let(:m) { Post.first }
90
107
 
91
- it { assert_association_one_to_one(m, :first_comment) }
108
+ it { assert_association_one_to_one(m, :first_comment)
109
+ # or
110
+ it { m.must_have_one_to_one_association(:first_comment) }
92
111
 
93
112
 
94
- # definition of args
95
- assert_association_one_to_one(
96
- <model_instance>,
97
- <association_name>, # ie: :first_comment
98
- <options>,
99
- <custom_error_message>
100
- )
101
-
113
+ # definition of args
114
+ assert_association_one_to_one(
115
+ <model_instance>,
116
+ <association_name>, # ie: :first_comment
117
+ <options>,
118
+ <custom_error_message>
119
+ )
120
+ ```
102
121
 
103
122
  In the event of errors an extensive error message is provided:
104
123
 
105
- # example error message
124
+ ```
125
+ # example error message
106
126
 
107
- Expected Author to have a :one_to_one association :key_posts but no association ':key_posts' was found -
108
- available associations are: [ \
109
- {:attribute=>:posts, :type=>:one_to_many, :class=>:Post, :keys=>[:author_id]}, \
110
- {:attribute=>:key_post, :type=>:one_to_one, :class=>:Post, :keys=>[:author_id]} \
111
- ]
127
+ Expected Author to have a :one_to_one association :key_posts but no association \
128
+ ':key_posts' was found - available associations are: [ \
129
+ {:attribute=>:posts, :type=>:one_to_many, :class=>:Post, :keys=>[:author_id]}, \
130
+ {:attribute=>:key_post, :type=>:one_to_one, :class=>:Post, :keys=>[:author_id]} \
131
+ ]
132
+ ```
112
133
 
113
134
  <br>
114
135
  <br>
@@ -117,16 +138,21 @@ In the event of errors an extensive error message is provided:
117
138
 
118
139
  A model defined with an association like this:
119
140
 
120
- class Post < Sequel::Model
121
- one_to_many :comments
122
- end
141
+ ```ruby
142
+ class Post < Sequel::Model
143
+ one_to_many :comments
144
+ end
145
+ ```
123
146
 
124
147
  Can be easily and quickly tested with `assert_association_one_to_many()` like this:
125
148
 
126
- let(:p) { Post.first }
127
-
128
- it { assert_association_one_to_many(p, :comments) }
129
-
149
+ ```ruby
150
+ let(:p) { Post.first }
151
+
152
+ it { assert_association_one_to_many(p, :comments) }
153
+ # or
154
+ it { m.must_have_one_to_many_association(:comments) }
155
+ ```
130
156
 
131
157
  As above the assertion provides an extensive error message if something is wrong.
132
158
 
@@ -137,16 +163,21 @@ As above the assertion provides an extensive error message if something is wrong
137
163
 
138
164
  A model defined with an association like this:
139
165
 
140
- class Post < Sequel::Model
141
- many_to_one :author
142
- end
166
+ ```ruby
167
+ class Post < Sequel::Model
168
+ many_to_one :author
169
+ end
170
+ ```
143
171
 
144
172
  Can be easily and quickly tested with `assert_association_many_to_one()` like this:
145
173
 
146
- let(:p) { Post.first }
147
-
148
- it { assert_association_many_to_one(p, :author) }
149
-
174
+ ```ruby
175
+ let(:p) { Post.first }
176
+
177
+ it { assert_association_many_to_one(p, :author) }
178
+ # or
179
+ it { m.must_have_many_to_one_association(:author) }
180
+ ```
150
181
 
151
182
 
152
183
  As above the assertion provides an extensive error message if something is wrong.
@@ -158,49 +189,64 @@ As above the assertion provides an extensive error message if something is wrong
158
189
 
159
190
  A model defined with an association like this:
160
191
 
161
- class Post < Sequel::Model
162
- many_to_many :categories
163
- end
192
+ ```ruby
193
+ class Post < Sequel::Model
194
+ many_to_many :categories
195
+ end
196
+ ```
164
197
 
165
198
  Can be easily and quickly tested with `assert_association_many_to_many()` like this:
166
199
 
167
- let(:p) { Post.first }
168
-
169
- it { assert_association_many_to_many(p, :categories) }
170
-
200
+ ```ruby
201
+ let(:p) { Post.first }
202
+
203
+ it { assert_association_many_to_many(p, :categories) }
204
+ # or
205
+ it { m.must_have_many_to_many_association(:categories) }
206
+ ```
171
207
 
172
208
  If something is wrong an extensive error message is provided:
173
209
 
174
- Expected Category to have a :many_to_many association :posts with given options: \
175
- {:class_name=>'Posts'} but should be {:class_name=>'Post' }
210
+ ```
211
+ Expected Category to have a :many_to_many association :posts with given options: \
212
+ {:class_name=>'Posts'} but should be {:class_name=>'Post' }
213
+ ```
176
214
 
177
215
  or
178
216
 
179
- Expected Category to have a :many_to_many association :post but no association ':post' was found - \
180
- available associations are: [ \
181
- { :attribute=>:posts, :type=>:many_to_many, :class=>:Post, :join_table=>:categories_posts, \
182
- :left_keys=>[:category_id], :right_keys=>[:post_id]
183
- }
184
- ]
185
-
217
+ ```
218
+ Expected Category to have a :many_to_many association :post but no association \
219
+ ':post' was found - available associations are: [ \
220
+ { :attribute=>:posts, :type=>:many_to_many, :class=>:Post, :join_table=>:categories_posts, \
221
+ :left_keys=>[:category_id], :right_keys=>[:post_id]
222
+ }
223
+ ]
224
+ ```
186
225
 
187
226
  <br>
188
227
  <br>
189
228
 
190
229
  ### `assert_association() assertion`
230
+ spec: `.must_have_association()`
191
231
 
192
232
  if the above assertion methods are insufficient, you can use the base `assert_association` method instead.
193
233
 
194
- it "should have a :one_through_one association" do
195
- assert_association(Post, :one_through_one, :author)
196
- end
197
-
198
-
199
- #
200
- assert_association(
201
- <model_class>, <association_type>, <association_name>, <options>, <custom_error_message>
202
- )
203
-
234
+ ```ruby
235
+ it "should have a :one_through_one association" do
236
+ assert_association(Post, :one_through_one, :author)
237
+ # or
238
+ Post.must_have_association(:one_through_one, :author)
239
+ end
240
+
241
+ # definition of args
242
+ assert_association(
243
+ <model_class>,
244
+ <association_type>,
245
+ <association_name>,
246
+ <options>,
247
+ <custom_error_message>
248
+ )
249
+ ```
204
250
 
205
251
  <br>
206
252
  <br>
@@ -211,110 +257,310 @@ if the above assertion methods are insufficient, you can use the base `assert_as
211
257
 
212
258
  ## Validations
213
259
 
214
- **NOTE! NOT YET IMPLEMENTED! WORK IN PROGRESS**
215
-
260
+ If you are using the recommended `:validation_class_methods` plugin in your app, the following instance validation methods are supported:
216
261
 
217
- If you are using the recommended `:validation_helpers` plugin in your app, the following instance validation methods
218
- are supported:
219
-
220
- * `validates_presence`
262
+ * `assert_validates_presence()`
221
263
 
222
- * `validates_exact_length`
264
+ * `assert_validates_exact_length()`
223
265
 
224
- * `validates_length_range`
266
+ * `assert_validates_length_range()`
225
267
 
226
- * `validates_max_length`
268
+ * `assert_validates_max_length()`
227
269
 
228
- * `validates_min_length`
270
+ * `assert_validates_min_length()`
229
271
 
230
- * `validates_format`
272
+ * `assert_validates_format()`
231
273
 
232
- * `validates_includes`
274
+ * `assert_validates_inclusion()`
233
275
 
234
- * `validates_integer`
276
+ * `assert_validates_integer()`
235
277
 
236
- * `validates_not_string`
278
+ * `assert_validates_numericality()`
237
279
 
238
- * `validates_numeric`
280
+ * `assert_validates_uniqueness()`
281
+
282
+ * `assert_validates_acceptance()`
239
283
 
240
- * `validates_unique`
284
+ * `assert_validates_confirmation()`
285
+
286
+ With all valid options checked
287
+
288
+ ---
289
+
290
+ ### `assert_validates_presence(obj, attribute, opts = {}, msg = nil)`
291
+ alias: `:assert_validates_presence_of`
292
+
293
+ Test for validating presence of a model attribute
294
+
295
+ ```ruby
296
+ it { assert_validates_presence(model, :title) }
297
+ it { model.must_validate_presence_of(:title, { message: '...' }) }
298
+ ```
299
+ <br>
300
+
301
+
302
+ ### `assert_validates_length(obj, attribute, opts = {}, msg = nil)`
303
+ alias `:assert_validates_length_of`
304
+
305
+ Test for validating the length of a model's attribute.
306
+
307
+ Available options:
308
+
309
+ * :message - The message to use (no default, overrides :nil_message, :too_long, :too_short, and :wrong_length options if present)
310
+
311
+ * :nil_message - The message to use use if :maximum option is used and the value is nil (default: 'is not present')
312
+
313
+ * :too_long - The message to use use if it the value is too long (default: 'is too long')
314
+
315
+ * :too_short - The message to use use if it the value is too short (default: 'is too short')
316
+
317
+ * :wrong_length - The message to use use if it the value is not valid (default: 'is the wrong length')
318
+
319
+ Size related options:
320
+
321
+ * :is - The exact size required for the value to be valid (no default)
322
+
323
+ * :minimum - The minimum size allowed for the value (no default)
324
+
325
+ * :maximum - The maximum size allowed for the value (no default)
326
+
327
+ * :within - The array/range that must include the size of the value for it to be valid (no default)
328
+
329
+ ```ruby
330
+ it { assert_validates_length(model, :title, { maximum: 12 }) }
331
+ it { model.must_validate_length_of(:title, { within: 4..12 }) }
332
+ ```
333
+ <br>
334
+
335
+
336
+ ### `assert_validates_exact_length(obj, attribute, exact_length, opts = {}, msg = nil)`
337
+ alias: `:assert_validates_exact_length_of`
338
+
339
+ Test for validating the exact length of a model's attribute.
340
+
341
+ ```ruby
342
+ it { assert_validates_exact_length(model, :title, 12, { message: '...' }) }
343
+ it { model.must_validate_exact_length_of(:title, 12, { message: '...' }) }
344
+ ```
345
+ <br>
346
+
347
+
348
+ ### `assert_validates_length_range(obj, attribute, range, opts = {}, msg = nil)`
349
+ alias: `:assert_validates_length_range_of`
350
+
351
+ Test for validating the exact length of a model's attribute.
352
+
353
+ ```ruby
354
+ it { assert_validates_length_range(model, :title, 4..12, { message: '...' }) }
355
+ it { model.must_validate_length_range_of(:title, 4..12, { message: '...' }) }
356
+ ```
357
+
358
+ <br>
241
359
 
242
360
 
243
- The following valid options will be checked:
361
+ ### `assert_validates_max_length(obj, attribute, max_length, opts = {}, msg = nil)`
362
+ alias: `:assert_validates_max_length_of`
363
+
364
+ Test for validating the maximum length of a model's attribute.
365
+
366
+ ```ruby
367
+ it { assert_validates_max_length(model, :title, 12, { message: '...' }) }
368
+ it { model.must_validate_max_length_of(:title, 12, { message: '...' }) }
369
+ ```
370
+
371
+ <br>
372
+
373
+
374
+ ### `assert_validates_min_length(obj, attribute, min_length, opts = {}, msg = nil)`
375
+ alias: `:assert_validates_min_length_of`
376
+
377
+ Test for validating the minimum length of a model's attribute.
378
+
379
+ ```ruby
380
+ it { assert_validates_min_length(model, :title, 12, { message: '...' }) }
381
+ it { model.must_validate_min_length_of(:title, 12, { message: '...' }) }
382
+ ```
383
+
384
+ <br>
385
+
386
+
387
+ ## `assert_validates_format(obj, attribute, opts = {}, msg = nil)`
388
+ alias: `:assert_validates_format_of`
389
+
390
+ Test for validating the format of a model's attribute with a regexp.
391
+
392
+ ```ruby
393
+ it { assert_validates_format(model, :title, 12, { with: /[a-z+]/ }) }
394
+ it { model.must_validate_format_of(:title, 12, { with: /[a-z]+/ }) }
395
+ ```
396
+
397
+ <br>
398
+
399
+
400
+ ### `assert_validates_inclusion(obj, attribute, opts = {}, msg = nil)`
401
+ alias: `:assert_validates_inclusion_of`
402
+
403
+ Test for validating that a model's attribute is within a specified range or set of values.
404
+
405
+ ```ruby
406
+ it { assert_validates_inclusion(model, :status, { in: [:a, :b, :c] }) }
407
+ it { model.must_validate_inclusion_of(:status, { in: [:a, :b, :c] }) }
408
+ ```
409
+
410
+ <br>
411
+
412
+
413
+ ### `assert_validates_integer(obj, attribute, opts = {}, msg = nil)`
414
+ alias: none
415
+
416
+ Test for validating that a a model's attribute is an integer.
417
+
418
+ ```ruby
419
+ it { assert_validates_integer(model, :author_id, { message: '...' }) }
420
+ it { model.must_validate_integer_of(:author_id, { message: '...' }) }
421
+ ```
422
+
423
+ <br>
424
+
425
+
426
+ ### `assert_validates_numericality(obj, attribute, opts = {}, msg = nil)`
427
+ alias: `:assert_validates_numericality_of`
428
+
429
+ Test for validating that a model's attribute is numeric (number).
430
+
431
+ ```ruby
432
+ it { assert_validates_numericality(model, :author_id, { message: '...' }) }
433
+ it { model.must_validate_numericality_of(:author_id, { message: '...' }) }
434
+ ```
435
+
436
+ <br>
437
+
438
+
439
+ ### `assert_validates_uniqueness(obj, attribute, opts = {}, msg = nil)`
440
+ alias: `:assert_validates_uniqueness_of`
441
+
442
+ Test for validating that a model's attribute is unique.
443
+
444
+ ```ruby
445
+ it { assert_validates_uniqueness(model, :urlslug, { message: '...' }) }
446
+ it { model.must_validate_uniqueness_of(:urlslug, { message: '...' }) }
447
+ ```
448
+
449
+ <br>
450
+
451
+
452
+ ### `assert_validates_acceptance(obj, attribute, opts = {}, msg = nil)`
453
+ alias: `assert_validates_acceptance_of`
454
+
455
+ Test for validating the acceptance of a model's attribute.
456
+
457
+ ```ruby
458
+ it { assert_validates_acceptance(Order.new, :toc, { message: '...' }) }
459
+ it { model.must_validate_acceptance_of(:toc, { message: '...' }) }
460
+ ```
461
+
462
+ <br>
463
+
464
+
465
+ ### `assert_validates_confirmation(obj, attribute, opts = {}, msg = nil)`
466
+ alias: `:assert_validates_confirmation_of`
467
+
468
+ Test for validating the confirmation of a model's attribute.
469
+
470
+ ```ruby
471
+ it { assert_validates_confirmation(User.new, :password, { message: '...' }) }
472
+ it { User.new.must_validate_confirmation_of(:password, { message: '...' }) }
473
+ ```
474
+
475
+ <br>
476
+
477
+
478
+ Each validation assertion have a responding negative test, ie: `refute_validate_presence()`
479
+
244
480
 
245
- * `:allow_blank`
246
- * `:allow_missing`
247
- * `:allow_nil`
248
- * `:message`
249
481
 
250
482
 
251
483
  ### Usage
252
484
 
253
485
  A model defined with validations like this:
254
486
 
255
- class Post < Sequel::Model
256
- plugin :validation_helpers
257
-
258
- def validate
259
- super
260
- validates_presence(:title)
261
- validates_format(/\w+/, :title)
262
- end
263
- end
487
+ ```ruby
488
+ class Post < Sequel::Model
489
+ plugin :validation_helpers
490
+
491
+ def validate
492
+ super
493
+ validates_presence(:title)
494
+ validates_format(/\w+/, :title)
495
+ end
496
+ end
497
+ ```
264
498
 
265
499
  Can be quickly tested like this:
266
500
 
267
- <snip...>
501
+ ```ruby
502
+ # <snip...>
268
503
 
269
- let(:m) { Post.first }
504
+ let(:m) { Post.first }
270
505
 
271
- it "shoud validate presence of :title column" do
272
- assert_validates_presence(m, :title)
273
- end
506
+ it "shoud validate presence of :title column" do
507
+ assert_validates_presence(m, :title)
508
+ end
274
509
 
275
- it "shoud validate format of :title column with regexp" do
276
- assert_validates_format(m, :title, /\w+/)
277
- end
278
-
510
+ it "shoud validate format of :title column with regexp" do
511
+ assert_validates_format(m, :title, /\w+/)
512
+ end
513
+ ```
279
514
 
280
515
 
281
516
  ## Installation
282
517
 
283
518
  Add this line to your application's Gemfile:
284
519
 
285
- gem 'minitest-sequel'
520
+ ```ruby
521
+ gem 'minitest-sequel'
522
+ ```
286
523
 
287
524
  And then execute:
288
525
 
289
- $ bundle
526
+ ```bash
527
+ $ bundle
528
+ ```
290
529
 
291
530
  Or install it yourself as:
292
531
 
293
- $ gem install minitest-sequel
294
-
532
+ ```bash
533
+ $ gem install minitest-sequel
534
+ ```
295
535
 
296
536
  ## Usage
297
537
 
298
538
  In your project's `spec/spec_helper.rb` or `test/test_helper.rb` file ensure the following code is present:
299
539
 
540
+
541
+ ```ruby
542
+ gem 'minitest'
300
543
 
301
- require 'minitest/autorun'
302
- require 'minitest/sequel'
303
- require 'sqlite3' # using sqlite for tests
544
+ require 'minitest/autorun'
545
+ require 'minitest/sequel' # NB!! can be loaded after minitest/autorun
546
+
547
+ require 'sqlite3' # using sqlite for tests
304
548
 
305
- # The preferred default validations plugin, which uses instance-level methods.
306
- Sequel::Model.plugin(:validation_helpers)
549
+ # The preferred default validations plugin, which uses class-level methods.
550
+ Sequel::Model.plugin(:validation_class_methods)
307
551
 
308
- # connect to database
309
- DB = Sequel.sqlite # :memory
552
+ # connect to database
553
+ DB = Sequel.sqlite # :memory
310
554
 
311
- ## add migrations and seeds below
555
+ ## add migrations and seeds below
312
556
 
313
- DB.create_table(:posts) do
314
- primary_key :id
315
- <snip...>
316
- end
557
+ DB.create_table(:posts) do
558
+ primary_key :id
559
+ # <snip...>
560
+ end
317
561
 
562
+ # <snip...>
563
+ ```
318
564
 
319
565
  Then in your tests you should be good to go when using the sequel assertions.
320
566