nakajima / fixjour

Word to your object mother.

This URL has Read+Write access

fixjour / spec / fixjour_spec.rb
100644 529 lines (439 sloc) 14.646 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
require 'spec/spec_helper'
 
describe Fixjour do
  before(:each) do
    define_all_builders
  end
 
  describe "when Fixjour is not included" do
    it "does not have access to creation methods" do
      self.should_not respond_to(:new_foo)
    end
  end
 
  describe "counter" do
    include Fixjour
 
    before(:each) do
      Fixjour::Counter.reset
    end
 
    it "provides default counter" do
      counter.should == 1
      counter.should == 2
      counter.should == 3
    end
 
    it "should increase" do
      counter(:foo).should == 1
      counter(:foo).should == 2
      counter(:foo).should == 3
    end
 
    it "should allow multiple counters" do
      counter(:foo).should == 1
      counter(:foo).should == 2
      counter(:bar).should == 1
      counter(:foo).should == 3
      counter(:bar).should == 2
    end
 
    it "should allow resetting a single counter" do
      counter(:foo).should == 1
      counter(:bar).should == 1
      Fixjour::Counter.reset :foo
      counter(:foo).should == 1
      counter(:bar).should == 2
    end
  end
 
  describe "when Fixjour is included" do
    include Fixjour
 
    describe "new_* methods" do
      it "generates new_[model] method" do
        proc {
          new_foo
        }.should_not raise_error
      end
 
      it "should raise a helpful error when the class can't be found" do
        lambda {
          Fixjour { define_builder(WhereOWhereAmI) }
        }.should raise_error(NameError)
      end
 
      context "passing a builder block with one arg" do
        context "when it returns a model object" do
          before(:each) do
            Fixjour.remove(Foo)
            Fixjour do
              define_builder(Foo) do |klass|
                klass.new(:name => 'Foo Namery', :bar => new_bar)
              end
            end
          end
 
          it "returns a new model object" do
            new_foo.should be_kind_of(Foo)
          end
 
          it "is a new record" do
            new_foo.should be_new_record
          end
 
          it "returns defaults specified in block" do
            new_foo.name.should == 'Foo Namery'
          end
 
          it "merges overrides" do
            new_foo(:name => nil).name.should be_nil
          end
 
          it "is indifferent" do
            new_foo('name' => nil).name.should be_nil
          end
 
          it "can be made invalid associated objects" do
            new_foo(:bar => nil).should_not be_valid
          end
 
          it "allows access to other builders" do
            bar = new_bar
            mock(self).new_bar { bar }
            new_foo.bar.should == bar
          end
        end
 
        context "when passed a hash" do
          before(:each) do
            Fixjour.remove(Foo)
            Fixjour do
              define_builder(Foo, :name => 'Foo Namery')
            end
          end
 
          it "returns a new model object" do
            new_foo.should be_kind_of(Foo)
          end
 
          it "is a new record" do
            new_foo.should be_new_record
          end
 
          it "returns defaults specified in block" do
            new_foo.name.should == 'Foo Namery'
          end
 
          it "merges overrides" do
            new_foo(:name => nil).name.should be_nil
          end
        end
 
        context "when it returns a hash" do
          before(:each) do
            Fixjour.remove(Foo)
            Fixjour do
              define_builder(Foo) do |overrides|
                { :name => 'Foo Namery', :bar => new_bar }
              end
            end
          end
 
          it "returns a new model object" do
            new_foo.should be_kind_of(Foo)
          end
 
          it "is a new record" do
            new_foo.should be_new_record
          end
 
          it "returns defaults specified in block" do
            new_foo.name.should == 'Foo Namery'
          end
 
          it "merges overrides" do
            new_foo(:name => nil).name.should be_nil
          end
 
          it "can be made invalid associated objects" do
            new_foo(:bar => nil).should_not be_valid
          end
 
          it "allows access to other builders" do
            bar = new_bar
            mock(self).new_bar { bar }
            new_foo.bar.should == bar
          end
        end
      end
 
      context "passing a builder block with two args" do
        before(:each) do
          Fixjour.remove(Foo)
          Fixjour do
            define_builder(Foo) do |klass, overrides|
              klass.new({ :name => 'Foo Namery', :bar => new_bar })
            end
          end
        end
 
        it "returns a new model object" do
          new_foo.should be_kind_of(Foo)
        end
 
        it "is a new record" do
          new_foo.should be_new_record
        end
 
        it "returns defaults specified in block" do
          new_foo.name.should == 'Foo Namery'
        end
 
        it "merges overrides" do
          new_foo(:name => nil).name.should be_nil
        end
 
        it "is indifferent" do
          new_foo('name' => nil).name.should be_nil
        end
 
        it "can be made invalid associated objects" do
          new_foo(:bar => nil).should_not be_valid
        end
 
        it "allows access to other builders" do
          bar = new_bar
          mock(self).new_bar { bar }
          new_foo.bar.should == bar
        end
      end
    end
 
    describe "create_* methods" do
      it "calls new_* method then saves the result" do
        # mocking here to make sure it's still using the new_person helper
        # as opposed to calling Foo.new again. We don't want to duplicate
        # that sort of behavior
        mock(foo = Object.new).save!
        mock(self).new_foo { foo }
 
        create_foo
      end
 
      context "declared with a block" do
        it "saves the record" do
          foo = create_foo
          foo.should_not be_new_record
        end
 
        it "retains defaults" do
          create_foo.name.should == 'Foo Namery'
        end
 
        it "still allows options override" do
          create_foo(:name => "created").name.should == "created"
        end
      end
 
      context "declared with a hash" do
        it "saves the record" do
          bazz = create_bazz
          bazz.should_not be_new_record
        end
 
        it "retains defaults" do
          create_bazz.name.should == 'Bazz Namery'
        end
 
        it "still allows options override" do
          create_bazz(:name => "created").name.should == "created"
        end
      end
    end
 
    describe "valid_*_attributes" do
      it "returns a hash containing the valid attributes specified in the builder" do
        valid_foo_attributes[:name].should == new_foo.name
      end
 
      it "does not include attributes that aren't defined in the builder block" do
        valid_foo_attributes.should_not have_key(:age)
      end
 
      it "allows overrides" do
        valid_foo_attributes(:name => "as attr")[:name].should == "as attr"
      end
 
      it "is indifferent" do
        valid_foo_attributes[:name].should == valid_foo_attributes['name']
      end
 
      it "overrides indifferently" do
        valid_foo_attributes("name" => "as attr")[:name].should == "as attr"
        valid_foo_attributes(:name => "as attr")["name"].should == "as attr"
      end
 
      describe "singular association ids" do
        it "sets appropriate foreign keys" do
          valid_foo_attributes[:bar].should be_nil
          valid_foo_attributes[:bar_id].should_not be_nil
        end
 
        it "respects custom foreign keys" do
          valid_foo_attributes[:owner].should be_nil
          valid_foo_attributes[:person_id].should_not be_nil
        end
      end
 
      describe "plural association ids" do
        it "sets appropriate foreign keys" do
          valid_bar_attributes[:people].should be_nil
          valid_bar_attributes[:person_ids].should_not be_empty
        end
 
        it "leaves out blank keys" do
          valid_bar_attributes.should_not have_key('foo_ids')
        end
      end
 
      describe "returning new values every time" do
        before(:each) do
          FooBar.validates_uniqueness_of :name
          create_foo_bar(valid_foo_bar_attributes)
        end
 
        it "returns new values every time" do
          new_foo_bar(valid_foo_bar_attributes).should be_valid
        end
      end
 
      context "declared with a hash" do
        it "works the same way as builder block style" do
          valid_bazz_attributes[:name].should == new_bazz.name
        end
      end
    end
 
    describe "Fixjour.builders" do
      it "contains the classes for which there are builders" do
        Fixjour.should have(5).builders
        Fixjour.builders.map(&:klass).should include(Foo, Bar, Bazz)
      end
 
      context "when defining multiple builders for same class" do
        it "raises RedundantBuilder error" do
          proc {
            Fixjour do
              define_builder(Foo) { |overrides| Foo.new(:name => 'bad!') }
            end
          }.should raise_error(Fixjour::RedundantBuilder)
        end
      end
 
      describe "redundancy checker" do
        context "when :allow_redundancy is true" do
          before(:each) do
            Fixjour.builders.clear
          end
 
          it "doesn't blow up" do
            proc {
              Fixjour :allow_redundancy => true do
                define_builder(Bar) { Bar.new }
                define_builder(Bar) { Bar.new }
              end
            }.should_not raise_error
          end
 
          it "resets the settings when done" do
            Fixjour :allow_redundancy => true do
              define_builder(Bar) { Bar.new }
              define_builder(Bar) { Bar.new }
            end
 
            proc {
              Fixjour do
                define_builder(Bar) { Bar.new }
              end
            }.should raise_error(Fixjour::RedundantBuilder)
          end
        end
 
        describe "method_added hook" do
          context "when it's already defined for the class" do
            before(:each) do
              @klass = Class.new do
                def self.added_methods
                  @added_methods ||= []
                end
 
                def self.method_added(name)
                  added_methods << name
                end
 
                include Fixjour
              end
            end
 
            it "does not lose old behavior" do
              @klass.class_eval { def foo; :foo end }
              @klass.added_methods.should include(:foo)
            end
 
            it "gets Fixjour behavior" do
              foo = @klass.new.new_foo
              foo.should be_kind_of(Foo)
            end
          end
        end
 
        context "when the method is redundant" do
          it "raises RedundantBuilder for new_*" do
            proc {
              self.class.class_eval do
                def new_foo(overrides={}); Foo.new end
              end
            }.should raise_error(Fixjour::RedundantBuilder)
          end
 
          it "raises RedundantBuilder for new_ when there's an underscore" do
            proc {
              self.class.class_eval do
                def new_foo_bar(overrides={}); end
              end
            }.should raise_error(Fixjour::RedundantBuilder)
          end
 
          it "raises RedundantBuilder for create_*" do
            proc {
              self.class.class_eval do
                def create_foo(overrides={}); Foo.new end
              end
            }.should raise_error(Fixjour::RedundantBuilder)
          end
 
          it "raises RedundantBuilder for valid_*_attributes" do
            proc {
              self.class.class_eval do
                def valid_foo_attributes(overrides={}); end
              end
            }.should raise_error(Fixjour::RedundantBuilder)
          end
        end
 
        context "when the method is not redundant" do
          it "handles *similar* names" do
            proc {
              self.class.class_eval do
                def new_foo_source(overrides={}); end
                def choice_new_foo(overrides={}); end
              end
            }.should_not raise_error(Fixjour::RedundantBuilder)
          end
 
          it "does not raise error" do
            proc {
              self.class.class_eval do
                def valid_nothing_attributes(overrides={}); Foo.new end
              end
            }.should_not raise_error
          end
        end
      end
    end
 
    describe "a virtual attribute" do
      before(:each) do
        Fixjour.remove(Foo)
        Foo.class_eval { attr_accessor :bizzle }
        Fixjour do
          define_builder(Foo) do |klass, overrides|
            klass.new(:bizzle => 'fizzle')
          end
        end
      end
 
      it "gets preserved" do
        new_foo.bizzle.should == 'fizzle'
      end
 
      it "is overrideable" do
        new_foo(:bizzle => 'bliggety').bizzle.should == 'bliggety'
      end
    end
 
    describe "protected attributes" do
      before(:each) do
        Fixjour.remove(Bar)
        Bar.attr_protected :name
        Fixjour do
          define_builder(Bar) do |klass, overrides|
            klass.protected :name
            klass.new :name => "Protect me!"
          end
        end
      end
 
      it "returns default value" do
        new_bar.name.should == "Protect me!"
      end
 
      it "can be overridden" do
        new_bar(:name => 'pwnd').name.should == 'pwnd'
      end
    end
 
    describe "processing overrides" do
      before(:each) do
        Fixjour.remove(Foo)
      end
 
      context "when the builder block has one arg" do
        it "is raises DeprecatedMergeAttempt when #process is called" do
          proc {
            Fixjour.define_builder(Foo) do |overrides|
              overrides.process(:alias) { |v| overrides[:name] = v }
            end
 
            new_foo
          }.should raise_error(Fixjour::DeprecatedMergeAttempt)
        end
      end
 
      context "when the builder block has two args" do
        before(:each) do
          Fixjour do
            define_builder(Foo) do |klass, overrides|
              overrides.process(:alias) do |value|
                overrides[:name] = value
              end
 
              klass.new(:name => "El Nameo!")
            end
          end
        end
 
        it "returns a new Fixjour::OverridesHash" do
          mock.proxy(Fixjour::OverridesHash).new(:alias => "Bart Simpson")
          new_foo(:alias => "Bart Simpson")
        end
 
        it "merges overrides" do
          new_foo(:alias => "Bart Simpson").name.should == "Bart Simpson"
        end
      end
    end
  end
end