test_dummy 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,59 @@
1
+ require_relative '../helper'
2
+
3
+ class TestDefinition < MiniTest::Unit::TestCase
4
+ def test_defaults
5
+ definition = TestDummy::Definition.new
6
+
7
+ assert_equal [ ], definition.fields
8
+ assert_equal true, definition.fields?(nil)
9
+ assert_equal false, definition.fields?([ :field ])
10
+ end
11
+
12
+ def test_define_operation_on_reflection
13
+ definition = TestDummy::Definition.new
14
+
15
+ definition.define_operation(Bill, [ :account ], { })
16
+
17
+ assert_equal [ :account ], definition.fields
18
+ assert_equal true, definition.fields?(:account)
19
+ assert_equal false, definition.fields?(:invalid)
20
+ assert_equal false, definition.fields?([ :invalid, :account ])
21
+ end
22
+
23
+ def test_define_operation_with_options
24
+ definition = TestDummy::Definition.new
25
+
26
+ triggered = 0
27
+
28
+ options = {
29
+ :only => [ :only_tag ].freeze,
30
+ :except => [ :except_tag ].freeze,
31
+ :block => lambda { triggered += 1 }
32
+ }.freeze
33
+
34
+ definition.define_operation(Bill, [ :account ], options)
35
+
36
+ assert_equal [ :account ], definition.fields([ :only_tag ])
37
+ end
38
+
39
+ def test_apply_with_after_option
40
+ definition = TestDummy::Definition.new
41
+
42
+ triggered = 0
43
+
44
+ options = {
45
+ :after => :save,
46
+ :block => lambda { triggered += 1 }
47
+ }.freeze
48
+
49
+ definition.define_operation(Bill, [ :account ], options)
50
+
51
+ definition.apply!(nil, { }, [ ])
52
+
53
+ assert_equal 0, triggered
54
+
55
+ definition.apply_after_save!(nil, { }, [ ])
56
+
57
+ assert_equal 1, triggered
58
+ end
59
+ end
@@ -1,8 +1,12 @@
1
- require File.expand_path('../helper', File.dirname(__FILE__))
1
+ require_relative '../helper'
2
2
 
3
- class TestItem < ActiveSupport::TestCase
3
+ class TestItem < MiniTest::Unit::TestCase
4
4
  def test_extension_loaded
5
5
  assert Item.respond_to?(:create_dummy)
6
+
7
+ assert TestDummy::Loader.load!(Item)
8
+
9
+ assert_equal [ :account, :bill, :description ], Item.dummy_definition.fields
6
10
  end
7
11
 
8
12
  def test_reflection_properties
@@ -38,6 +42,9 @@ class TestItem < ActiveSupport::TestCase
38
42
  def test_create_dummy_via_association
39
43
  bill = a Bill
40
44
 
45
+ assert bill.account
46
+ assert bill.account.id
47
+
41
48
  assert_equal false, bill.new_record?
42
49
 
43
50
  item = one_of bill.items
@@ -52,7 +59,11 @@ class TestItem < ActiveSupport::TestCase
52
59
 
53
60
  account = bill.account
54
61
 
62
+ assert account
63
+ assert account.bills
55
64
  assert_equal [ bill.id ], account.bills.collect(&:id)
65
+
66
+ assert account.items
56
67
  assert_equal [ item.id ], account.items.collect(&:id)
57
68
  end
58
69
  end
@@ -0,0 +1,23 @@
1
+ require_relative '../helper'
2
+
3
+ class TestOperation < MiniTest::Unit::TestCase
4
+ def test_defaults
5
+ loader = TestDummy::Loader.new
6
+
7
+ assert_equal false, loader['test']
8
+ assert_equal true, loader[Account]
9
+ end
10
+
11
+ def test_broken_load
12
+ exception = nil
13
+ loader = TestDummy::Loader.new
14
+
15
+ begin
16
+ loader[Broken]
17
+ rescue NameError => e
18
+ exception = e
19
+ end
20
+
21
+ assert_equal 'NameError', exception.class.to_s
22
+ end
23
+ end
@@ -0,0 +1,570 @@
1
+ require_relative '../helper'
2
+
3
+ class TestOperation < MiniTest::Unit::TestCase
4
+ class MockModelExample
5
+ COLUMNS = [
6
+ :field,
7
+ :field_id,
8
+ :root_id,
9
+ :integer
10
+ ].freeze
11
+
12
+ attr_accessor *COLUMNS
13
+ attr_reader :changed
14
+ attr_accessor :temporary
15
+
16
+ def self.column_names
17
+ COLUMNS.collect(&:to_s)
18
+ end
19
+
20
+ def self.create_dummy(*args)
21
+ created_dummy = self.new(*args)
22
+
23
+ yield(created_dummy) if (block_given?)
24
+
25
+ created_dummy
26
+ end
27
+
28
+ def initialize(options = nil)
29
+ @field = options && options[:field]
30
+ @field_id = options && options[:field_id]
31
+ @integer = options && options[:integer].to_i || 0
32
+
33
+ # root_id is not "accessible"
34
+
35
+ @changed = options && options.keys.collect(&:to_s) || [ ]
36
+ end
37
+ end
38
+
39
+ def test_defaults
40
+ operation = TestDummy::Operation.new({ })
41
+
42
+ assert_equal nil, operation.fields
43
+ assert_equal [ ], operation.source_keys
44
+ assert_equal [ ], operation.source_methods
45
+ assert_equal nil, operation.after
46
+ assert_equal nil, operation.only
47
+ assert_equal nil, operation.except
48
+ assert_equal nil, operation.model_class
49
+ assert_equal nil, operation.foreign_key
50
+ end
51
+
52
+ def test_with_option
53
+ operation = TestDummy::Operation.new(
54
+ :fields => [ :field ],
55
+ :with => :random_string
56
+ )
57
+
58
+ assert_equal true, TestDummy::Helper.respond_to?(:random_string)
59
+
60
+ model = MockModelExample.new
61
+
62
+ operation.apply!(model, { }, [ ])
63
+
64
+ assert model.field
65
+ assert_equal 12, model.field.length
66
+ end
67
+
68
+ def test_with_after
69
+ triggered = 0
70
+ operation = TestDummy::Operation.new(
71
+ :block => lambda { triggered += 1 },
72
+ :after => :save
73
+ )
74
+
75
+ assert_equal :save, operation.after
76
+
77
+ assert_equal 0, triggered
78
+
79
+ operation.apply!(nil, { }, [ ])
80
+
81
+ assert_equal 1, triggered
82
+ end
83
+
84
+ def test_with_fields
85
+ operation = TestDummy::Operation.new(
86
+ :fields => [ :field, :field_id ]
87
+ )
88
+
89
+ assert_equal [ :field, :field_id ], operation.fields
90
+ end
91
+
92
+ def test_with_after
93
+ operation = TestDummy::Operation.new(
94
+ :after => :save
95
+ )
96
+
97
+ assert_equal :save, operation.after
98
+ end
99
+
100
+ def test_block_with_only_tag
101
+ triggered = 0
102
+
103
+ operation = TestDummy::Operation.new(
104
+ :only => :test_tag,
105
+ :block => lambda { triggered += 1 }
106
+ )
107
+
108
+ assert_equal [ ], operation.fields(nil)
109
+ assert_equal [ ], operation.fields([ ])
110
+ assert_equal nil, operation.fields([ :test_tag ])
111
+
112
+ assert_equal [ :test_tag ], operation.only
113
+ assert_equal nil, operation.except
114
+
115
+ assert_equal false, operation.assignments(nil, { }, [ ])
116
+
117
+ operation.apply!(nil, { }, [ ])
118
+
119
+ assert_equal 0, triggered
120
+
121
+ assert_equal nil, operation.assignments(nil, { }, [ :test_tag ])
122
+
123
+ operation.apply!(nil, { }, [ :test_tag ])
124
+
125
+ assert_equal 1, triggered
126
+ end
127
+
128
+ def test_block_with_multiple_only_tags
129
+ triggered = 0
130
+
131
+ operation = TestDummy::Operation.new(
132
+ :only => [ :first_tag, :second_tag ],
133
+ :block => lambda { triggered += 1 }
134
+ )
135
+
136
+ assert_equal [ ], operation.fields(nil)
137
+ assert_equal [ ], operation.fields([ ])
138
+ assert_equal nil, operation.fields([ :first_tag ])
139
+ assert_equal nil, operation.fields([ :second_tag ])
140
+ assert_equal nil, operation.fields([ :first_tag, :second_tag ])
141
+
142
+ assert_equal [ :first_tag, :second_tag ], operation.only
143
+ assert_equal nil, operation.except
144
+
145
+ assert_equal false, operation.assignments(nil, { }, [ ])
146
+
147
+ operation.apply!(nil, { }, [ ])
148
+
149
+ assert_equal 0, triggered
150
+
151
+ assert_equal nil, operation.assignments(nil, { }, [ :first_tag ])
152
+ assert_equal nil, operation.assignments(nil, { }, [ :second_tag ])
153
+ assert_equal nil, operation.assignments(nil, { }, [ :first_tag, :second_tag ])
154
+ end
155
+
156
+ def test_block_with_only_tags_with_fields
157
+ triggered = 0
158
+
159
+ operation = TestDummy::Operation.new(
160
+ :fields => [ :test_field ],
161
+ :only => [ :test_tag ],
162
+ :block => lambda { triggered += 1 }
163
+ )
164
+
165
+ assert_equal [ :test_tag ], operation.only
166
+ assert_equal nil, operation.except
167
+
168
+ assert_equal false, operation.assignments(nil, { }, [ ])
169
+
170
+ operation.apply!(nil, { }, [ ])
171
+
172
+ assert_equal 0, triggered
173
+
174
+ assert_equal [ :test_field ], operation.assignments(nil, { }, [ :test_tag ])
175
+
176
+ operation.apply!(nil, { }, [ :test_tag ])
177
+
178
+ assert_equal 1, triggered
179
+ end
180
+
181
+ def test_block_with_except_tags
182
+ triggered = 0
183
+
184
+ operation = TestDummy::Operation.new(
185
+ :except => [ :test_tag ],
186
+ :block => lambda { triggered += 1 }
187
+ )
188
+
189
+ assert_equal nil, operation.only
190
+ assert_equal [ :test_tag ], operation.except
191
+
192
+ assert_equal nil, operation.assignments(nil, { }, [ ])
193
+
194
+ operation.apply!(nil, { }, [ ])
195
+
196
+ assert_equal 1, triggered
197
+
198
+ assert_equal false, operation.assignments(nil, { }, [ :test_tag ])
199
+
200
+ operation.apply!(nil, { }, [ :test_tag ])
201
+
202
+ assert_equal 1, triggered
203
+ end
204
+
205
+ def test_block_with_except_tags_with_fields
206
+ triggered = 0
207
+
208
+ operation = TestDummy::Operation.new(
209
+ :fields => [ :test_field ],
210
+ :except => [ :test_tag ],
211
+ :block => lambda { triggered += 1 }
212
+ )
213
+
214
+ assert_equal nil, operation.only
215
+ assert_equal [ :test_tag ], operation.except
216
+
217
+ assert_equal [ :test_field ], operation.assignments(nil, { }, [ ])
218
+
219
+ operation.apply!(nil, { }, [ ])
220
+
221
+ assert_equal 1, triggered
222
+
223
+ assert_equal false, operation.assignments(nil, { }, [ :test_tag ])
224
+
225
+ operation.apply!(nil, { }, [ :test_tag ])
226
+
227
+ assert_equal 1, triggered
228
+ end
229
+
230
+ def test_block_with_only_and_except_tags
231
+ triggered = 0
232
+
233
+ operation = TestDummy::Operation.new(
234
+ :only => [ :only_tag ],
235
+ :except => [ :except_tag ],
236
+ :block => lambda { triggered += 1 }
237
+ )
238
+
239
+ assert_equal [ :only_tag ], operation.only
240
+ assert_equal [ :except_tag ], operation.except
241
+
242
+ assert_equal false, operation.assignments(nil, { }, [ ])
243
+ assert_equal nil, operation.assignments(nil, { }, [ :only_tag ])
244
+ assert_equal false, operation.assignments(nil, { }, [ :except_tag ])
245
+ assert_equal false, operation.assignments(nil, { }, [ :only_tag, :except_tag ])
246
+ assert_equal false, operation.assignments(nil, { }, [ :except_tag, :only_tag ])
247
+ assert_equal false, operation.assignments(nil, { }, [ :except_tag, :only_tag, :irrelevant_tag ])
248
+ assert_equal nil, operation.assignments(nil, { }, [ :only_tag, :irrelevant_tag ])
249
+
250
+ operation.apply!(nil, { }, [ ])
251
+
252
+ assert_equal 0, triggered
253
+
254
+ operation.apply!(nil, { }, [ :only_tag ])
255
+
256
+ assert_equal 1, triggered
257
+
258
+ operation.apply!(nil, { }, [ :except_tag ])
259
+
260
+ assert_equal 1, triggered
261
+
262
+ operation.apply!(nil, { }, [ :only_tag, :except_tag ])
263
+
264
+ assert_equal 1, triggered
265
+ end
266
+
267
+ def test_block_with_only_and_except_tags_with_fields
268
+ triggered = 0
269
+
270
+ operation = TestDummy::Operation.new(
271
+ :fields => [ :test_field ],
272
+ :only => [ :only_tag ],
273
+ :except => [ :except_tag ],
274
+ :block => lambda { triggered += 1 }
275
+ )
276
+
277
+ assert_equal [ :only_tag ], operation.only
278
+ assert_equal [ :except_tag ], operation.except
279
+
280
+ assert_equal false, operation.assignments(nil, { }, [ ])
281
+
282
+ operation.apply!(nil, { }, [ ])
283
+
284
+ assert_equal 0, triggered
285
+
286
+ assert_equal false, operation.assignments(nil, { }, [ ])
287
+ assert_equal [ :test_field ], operation.assignments(nil, { }, [ :only_tag ])
288
+ assert_equal false, operation.assignments(nil, { }, [ :except_tag ])
289
+ assert_equal false, operation.assignments(nil, { }, [ :only_tag, :except_tag ])
290
+ assert_equal false, operation.assignments(nil, { }, [ :except_tag, :only_tag ])
291
+ assert_equal false, operation.assignments(nil, { }, [ :except_tag, :only_tag, :irrelevant_tag ])
292
+ assert_equal [ :test_field ], operation.assignments(nil, { }, [ :only_tag, :irrelevant_tag ])
293
+
294
+ operation.apply!(nil, { }, [ :except_tag ])
295
+
296
+ assert_equal 0, triggered
297
+
298
+ operation.apply!(nil, { }, [ :except_tag, :only_tag ])
299
+
300
+ assert_equal 0, triggered
301
+
302
+ operation.apply!(nil, { }, [ :only_tag ])
303
+
304
+ assert_equal 1, triggered
305
+ end
306
+
307
+ def test_block_with_model_on_multiple_fields
308
+ index = 0
309
+
310
+ operation = TestDummy::Operation.new(
311
+ :fields => [ :field_id, :root_id ],
312
+ :block => lambda { index += 1 }
313
+ )
314
+
315
+ model = MockModelExample.new
316
+
317
+ operation.apply!(model, { }, [ ])
318
+
319
+ assert_equal 1, index
320
+
321
+ assert_equal 1, model.field_id
322
+ assert_equal 1, model.root_id
323
+ end
324
+
325
+ def test_block_without_model
326
+ triggered = 0
327
+ block = lambda { triggered += 1 }
328
+
329
+ operation = TestDummy::Operation.new(
330
+ :block => block
331
+ )
332
+
333
+ operation.apply!(nil, { }, [ ])
334
+
335
+ assert_equal 1, triggered
336
+ end
337
+
338
+ def test_block_without_option_specified
339
+ triggered = false
340
+ block = lambda { triggered = :set }
341
+ model = MockModelExample.new
342
+
343
+ operation = TestDummy::Operation.new(
344
+ :fields => [ :field ],
345
+ :block => block
346
+ )
347
+
348
+ operation.apply!(model, { }, [ ])
349
+
350
+ assert_equal :set, triggered
351
+
352
+ assert_equal :set, model.field
353
+ end
354
+
355
+ def test_block_with_option_specified
356
+ triggered = 0
357
+ block = lambda { triggered += 1 }
358
+ model = MockModelExample.new(:field => nil)
359
+
360
+ operation = TestDummy::Operation.new(
361
+ :fields => [ :field ],
362
+ :block => block
363
+ )
364
+
365
+ operation.apply!(model, { :field => true }, [ ])
366
+
367
+ assert_equal 0, triggered
368
+
369
+ assert_equal nil, model.field
370
+ end
371
+
372
+ def test_block_with_model_set
373
+ triggered = false
374
+ block = lambda { triggered = :set }
375
+ model = MockModelExample.new(:field => :default)
376
+
377
+ operation = TestDummy::Operation.new(
378
+ :fields => [ :field ],
379
+ :block => block
380
+ )
381
+
382
+ operation.apply!(model, { :field => true }, [ ])
383
+
384
+ assert_equal false, triggered
385
+
386
+ assert_equal :default, model.field
387
+ end
388
+
389
+ def test_block_without_model_set
390
+ triggered = false
391
+ block = lambda { triggered = :set }
392
+ model = MockModelExample.new
393
+
394
+ operation = TestDummy::Operation.new(
395
+ :fields => [ :field ],
396
+ :block => block
397
+ )
398
+
399
+ operation.apply!(model, { }, [ ])
400
+
401
+ assert_equal :set, triggered
402
+
403
+ assert_equal :set, model.field
404
+ end
405
+
406
+ def test_block_with_model_reflection_attribute_set
407
+ triggered = 0
408
+ block = lambda { triggered += 1 }
409
+ model = MockModelExample.new(:field => :reference)
410
+
411
+ assert_equal %w[ field ], model.changed
412
+
413
+ operation = TestDummy::Operation.new(
414
+ :fields => [ :field ],
415
+ :foreign_key => :field_id,
416
+ :block => block
417
+ )
418
+
419
+ assert_equal [ :field, 'field', :field_id, 'field_id' ], operation.source_keys
420
+ assert_equal [ :field, :field_id ], operation.source_methods
421
+
422
+ assert_equal false, operation.assignments(model, { :field => :reference }, [ ])
423
+
424
+ operation.apply!(model, { }, [ ])
425
+
426
+ assert_equal 0, triggered
427
+
428
+ assert_equal :reference, model.field
429
+ end
430
+
431
+ def test_block_with_model_foreign_key_set
432
+ triggered = false
433
+ block = lambda { triggered = 0 }
434
+ model = MockModelExample.new(:field_id => 1)
435
+
436
+ operation = TestDummy::Operation.new(
437
+ :fields => [ :field ],
438
+ :foreign_key => :field_id,
439
+ :block => block
440
+ )
441
+
442
+ assert_equal [ :field, 'field', :field_id, 'field_id' ], operation.source_keys
443
+ assert_equal [ :field, :field_id ], operation.source_methods
444
+
445
+ assert_equal %w[ field_id ], model.changed
446
+
447
+ assert_equal false, operation.assignments(model, { }, [ ])
448
+
449
+ operation.apply!(model, { }, [ ])
450
+
451
+ assert_equal false, triggered
452
+
453
+ assert_equal 1, model.field_id
454
+ end
455
+
456
+ def test_block_with_model_inheritance_as_symbol
457
+ model = MockModelExample.new
458
+ model.root_id = 1
459
+
460
+ operation = TestDummy::Operation.new(
461
+ :fields => [ :field ],
462
+ :inherit => :root_id,
463
+ :model_class => MockModelExample,
464
+ :foreign_key => :field_id
465
+ )
466
+
467
+ assert_equal [ :field, 'field', :field_id, 'field_id' ], operation.source_keys
468
+ assert_equal [ :field, :field_id ], operation.source_methods
469
+
470
+ assert_equal [ :field ], operation.assignments(model, { }, [ ])
471
+
472
+ operation.apply!(model, { }, [ ])
473
+
474
+ assert model.field
475
+
476
+ assert_equal 1, model.field.root_id
477
+ end
478
+
479
+ def test_block_with_model_inheritance_as_hash
480
+ model = MockModelExample.new
481
+ model.root_id = 1
482
+
483
+ operation = TestDummy::Operation.new(
484
+ :fields => [ :field ],
485
+ :inherit => { :root_id => [ :root_id ] },
486
+ :model_class => MockModelExample,
487
+ :foreign_key => :field_id
488
+ )
489
+
490
+ assert_equal [ :field, 'field', :field_id, 'field_id' ], operation.source_keys
491
+ assert_equal [ :field, :field_id ], operation.source_methods
492
+
493
+ assert_equal [ :field ], operation.assignments(model, { }, [ ])
494
+
495
+ operation.apply!(model, { }, [ ])
496
+
497
+ assert model.field
498
+
499
+ assert_equal 1, model.field.root_id
500
+ end
501
+
502
+ def test_with_reflection_already_assigned
503
+ model = MockModelExample.new(:field_id => 1)
504
+
505
+ assert_equal %w[ field_id ], model.changed
506
+
507
+ operation = TestDummy::Operation.new(
508
+ :fields => [ :field ],
509
+ :model_class => MockModelExample,
510
+ :foreign_key => :field_id
511
+ )
512
+
513
+ assert_equal false, operation.assignments(model, { }, [ ])
514
+ end
515
+
516
+ def test_with_model_field_default
517
+ model = MockModelExample.new
518
+
519
+ assert_equal 0, model.integer
520
+ assert_equal [ ], model.changed
521
+
522
+ triggered = 0
523
+
524
+ operation = TestDummy::Operation.new(
525
+ :fields => [ :integer ],
526
+ :block => lambda { triggered += 1 }
527
+ )
528
+
529
+ assert_equal 0, triggered
530
+
531
+ operation.apply!(model, { }, [ ])
532
+
533
+ assert_equal 1, model.integer
534
+
535
+ assert_equal 1, triggered
536
+ end
537
+
538
+ def test_with_model_accessor_when_not_populated
539
+ model = MockModelExample.new
540
+
541
+ triggered = 0
542
+
543
+ operation = TestDummy::Operation.new(
544
+ :fields => [ :temporary ],
545
+ :block => lambda { triggered += 1 }
546
+ )
547
+
548
+ operation.apply!(model, { }, [ ])
549
+
550
+ assert_equal 1, triggered
551
+ assert_equal 1, model.temporary
552
+ end
553
+
554
+ def test_with_model_accessor_when_populated
555
+ model = MockModelExample.new
556
+ model.temporary = :temp
557
+
558
+ triggered = 0
559
+
560
+ operation = TestDummy::Operation.new(
561
+ :fields => [ :temporary ],
562
+ :block => lambda { triggered += 1 }
563
+ )
564
+
565
+ operation.apply!(model, { }, [ ])
566
+
567
+ assert_equal 0, triggered
568
+ assert_equal :temp, model.temporary
569
+ end
570
+ end