File: varia_model_spec.rb

package info (click to toggle)
ruby-varia-model 0.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 204 kB
  • sloc: ruby: 783; makefile: 4
file content (676 lines) | stat: -rw-r--r-- 18,813 bytes parent folder | download
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
require 'spec_helper'

describe VariaModel do
  describe "ClassMethods" do
    subject do
      Class.new do
        include VariaModel
      end
    end

    describe "::attributes" do
      it "returns a VariaModel::Attributes" do
        expect(subject.attributes).to be_a(described_class::Attributes)
      end

      it "is empty by default" do
        expect(subject.attributes).to be_empty
      end
    end

    describe "::attribute" do
      it "adds an attribute to the attributes hash for each attribute function call" do
        subject.attribute 'jamie.winsor'
        subject.attribute 'brooke.winsor'

        expect(subject.attributes.size).to eq(2)
      end

      it "adds a validation if :required option is true" do
        subject.attribute 'brooke.winsor', required: true

        expect(subject.validations.size).to eq(1)
      end

      it "adds a validation if the :type option is provided" do
        subject.attribute 'brooke.winsor', type: :string

        expect(subject.validations.size).to eq(1)
      end

      it "sets a default value if :default option is provided" do
        subject.attribute 'brooke.winsor', default: 'rhode island'

        expect(subject.attributes.berks_dig('brooke.winsor')).to eql('rhode island')
      end

      it "allows an attribute called 'attributes'" do
        subject.attribute 'attributes', default: 'bag of junk'

        expect(subject.attributes.berks_dig('attributes')).to eql('bag of junk')
      end

      it "allows an attribute called 'attribute'" do
        subject.attribute 'attribute', default: 'some value'

        expect(subject.attributes.berks_dig('attribute')).to eql('some value')
      end

      it "allows an attribute that has a lambda default value" do
        subject.attribute 'brooke', default: ->{ "winsor".upcase }

        expect(subject.attributes.berks_dig('brooke')).to be_a(Proc)
      end
    end

    describe "::validations" do
      it "returns a Hashie::Mash" do
        expect(subject.validations).to be_a(Hashie::Mash)
      end

      it "is empty by default" do
        expect(subject.validations).to be_empty
      end
    end

    describe "::validations_for" do
      context "when an attribute is registered and has validations" do
        before(:each) do
          subject.attribute("nested.attribute", required: true, type: String)
        end

        it "returns an array of procs" do
          validations = subject.validations_for("nested.attribute")

          expect(validations).to be_a(Array)
          expect(validations).to each be_a(Proc)
        end
      end

      context "when an attribute is registered but has no validations" do
        before(:each) do
          subject.attribute("nested.attribute")
        end

        it "returns an empty array" do
          validations = subject.validations_for("nested.attribute")

          expect(validations).to be_a(Array)
          expect(validations).to be_empty
        end
      end

      context "when an attribute is not registered" do
        it "returns an empty array" do
          validations = subject.validations_for("not_existing.attribute")

          expect(validations).to be_a(Array)
          expect(validations).to be_empty
        end
      end

      describe "#assignment_mode" do
        it "returns the default assignment mode :whitelist" do
          expect(subject.assignment_mode).to eql(:whitelist)
        end
      end

      describe "#set_assignment_mode" do
        it "sets the assignment_mode to whitelist" do
          subject.set_assignment_mode(:whitelist)

          expect(subject.assignment_mode).to eql(:whitelist)
        end

        it "sets the assignment_mode to carefree" do
          subject.set_assignment_mode(:carefree)

          expect(subject.assignment_mode).to eql(:carefree)
        end

        it "raises if given an invalid assignment mode" do
          expect { subject.set_assignment_mode(:not_a_real_mode) }.to raise_error(ArgumentError)
        end
      end
    end

    describe "::validate_kind_of" do
      let(:types) { [ String, Buff::Boolean ] }
      let(:key) { 'nested.one' }

      subject do
        Class.new do
          include VariaModel

          attribute 'nested.one', types: [String, Buff::Boolean]
        end
      end

      let(:model) { subject.new }

      it "returns an array" do
        expect(subject.validate_kind_of(types, model, key)).to be_a(Array)
      end

      context "failure" do
        before(:each) { model.nested.one = nil }

        it "returns an array where the first element is ':error'" do
          expect(subject.validate_kind_of(types, model, key).first).to eql(:error)
        end

        it "returns an array where the second element is an error message containing the attribute and types" do
          types.each do |type|
            expect(subject.validate_kind_of(types, model, key)[1]).to match(/#{type}/)
          end
          expect(subject.validate_kind_of(types, model, key)[1]).to match(/#{key}/)
        end
      end

      context "success" do
        before(:each) { model.nested.one = true }

        it "returns an array where the first element is ':ok'" do
          expect(subject.validate_kind_of(types, model, key).first).to eql(:ok)
        end

        it "returns an array where the second element is a blank string" do
          expect(subject.validate_kind_of(types, model, key)[1]).to be_blank
        end
      end

      context "when given two types of the same kind" do
        let(:types) { [ String, String ] }
        let(:key) { 'nested.one' }

        subject do
          Class.new do
            include VariaModel

            attribute 'nested.one', types: [String, Buff::Boolean]
          end
        end

        let(:model) { subject.new }
        before(:each) { model.nested.one = nil }

        it "returns a error message that contains the type error only once" do
          error_message = "Expected attribute: 'nested.one' to be a type of: 'String'"
          expect(subject.validate_kind_of(types, model, key)[1]).to eql(error_message)
        end
      end
    end

    describe "::validate_required" do
      subject do
        Class.new do
          include VariaModel

          attribute 'nested.one', required: true
        end
      end

      let(:key) { 'nested.one' }
      let(:model) { subject.new }

      it "returns an array" do
        expect(subject.validate_required(model, key)).to be_a(Array)
      end

      it "fails validation if the value of the attribute is nil" do
        model.set_attribute(key, nil)

        expect(subject.validate_required(model, key).first).to eql(:error)
      end

      it "passes validation if the value of the attribute is false" do
        model.set_attribute(key, false)

        expect(subject.validate_required(model, key).first).to eql(:ok)
      end

      it "passes validation if the value of the attribute is not nil" do
        model.set_attribute(key, 'some_value')

        expect(subject.validate_required(model, key).first).to eql(:ok)
      end

      context "failure" do
        before(:each) { model.nested.one = nil }

        it "returns an array where the first element is ':error'" do
          expect(subject.validate_required(model, key).first).to eql(:error)
        end

        it "returns an array where the second element is an error message containing the attribute name" do
          expect(subject.validate_required(model, key)[1]).to match(/#{key}/)
        end
      end

      context "success" do
        before(:each) { model.nested.one = "hello" }

        it "returns an array where the first element is ':ok'" do
          expect(subject.validate_required(model, key).first).to eql(:ok)
        end

        it "returns an array where the second element is a blank string" do
          expect(subject.validate_required(model, key)[1]).to be_blank
        end
      end
    end
  end

  subject do
    Class.new do
      include VariaModel

      attribute 'nested.not_coerced', default: 'hello'
      attribute 'nested.no_default'
      attribute 'nested.coerced', coerce: lambda { |m| m.to_s }
      attribute 'toplevel', default: 'hello'
      attribute 'no_default'
      attribute 'coerced', coerce: lambda { |m| m.to_s }
    end.new
  end

  describe "GeneratedAccessors" do
    describe "nested getter" do
      it "returns the default value" do
        expect(subject.nested.not_coerced).to eql('hello')
      end

      it "returns nil if there is no default value" do
        expect(subject.nested.no_default).to be_nil
      end
    end

    describe "toplevel getter" do
      it "returns the default value" do
        expect(subject.toplevel).to eql('hello')
      end

      it "returns nil if there is no default value" do
        expect(subject.no_default).to be_nil
      end
    end

    describe "nested setter" do
      it "sets the value of the nested attribute" do
        subject.nested.not_coerced = 'world'

        expect(subject.nested.not_coerced).to eql('world')
      end
    end

    describe "toplevel setter" do
      it "sets the value of the top level attribute" do
        subject.toplevel = 'world'

        expect(subject.toplevel).to eql('world')
      end
    end

    describe "nested coerced setter" do
      it "sets the value of the nested coerced attribute" do
        subject.nested.coerced = 1

        expect(subject.nested.coerced).to eql("1")
      end
    end

    describe "toplevel coerced setter" do
      it "sets the value of the top level coerced attribute" do
        subject.coerced = 1

        expect(subject.coerced).to eql('1')
      end
    end

    context "given two nested attributes with a common parent and default values" do
      subject do
        Class.new do
          include VariaModel

          attribute 'nested.one', default: 'val_one'
          attribute 'nested.two', default: 'val_two'
        end.new
      end

      it "sets a default value for each nested attribute" do
        expect(subject.nested.one).to eql('val_one')
        expect(subject.nested.two).to eql('val_two')
      end
    end

    context "given two nested attributes with a common parent and coercions" do
      subject do
        Class.new do
          include VariaModel

          attribute 'nested.one', coerce: lambda { |m| m.to_s }
          attribute 'nested.two', coerce: lambda { |m| m.to_s }
        end.new
      end

      it "coerces each value if both have a coercion" do
        subject.nested.one = 1
        subject.nested.two = 2

        expect(subject.nested.one).to eql("1")
        expect(subject.nested.two).to eql("2")
      end
    end

    context "given an attribute called 'attributes'" do
      subject do
        Class.new do
          include VariaModel

          attribute 'attributes', default: Hash.new
        end.new
      end

      it "allows the setting and getting of the 'attributes' mimic methods" do
        expect(subject.attributes).to be_a(Hash)
        expect(subject.attributes).to be_empty

        new_hash = { something: "here" }
        subject.attributes = new_hash
        expect(subject.attributes[:something]).to eql("here")
      end
    end
  end

  describe "Validations" do
    describe "validate required" do
      subject do
        Class.new do
          include VariaModel

          attribute 'brooke.winsor', required: true
        end.new
      end

      it "is not valid if it fails validation" do
        expect(subject).not_to be_valid
      end

      it "adds an error for each attribute that fails validations" do
        subject.validate

        expect(subject.errors.size).to eq(1)
      end

      it "adds a message for each failed validation" do
        subject.validate

        expect(subject.errors['brooke.winsor'].size).to eq(1)
        expect(subject.errors['brooke.winsor'][0]).to eql("A value is required for attribute: 'brooke.winsor'")
      end
    end

    describe "validate type" do
      subject do
        Class.new do
          include VariaModel

          attribute 'brooke.winsor', type: String
        end.new
      end

      before(:each) { subject.brooke.winsor = false }

      it "returns false if it fails validation" do
        expect(subject).not_to be_valid
      end

      it "adds an error if it fails validation" do
        subject.validate

        expect(subject.errors.size).to eq(1)
        expect(subject.errors['brooke.winsor'].size).to eq(1)
        expect(subject.errors['brooke.winsor'][0]).to eql("Expected attribute: 'brooke.winsor' to be a type of: 'String', 'NilClass'")
      end
    end
  end

  describe "#set_attribute" do
    subject do
      Class.new do
        include VariaModel

        attribute 'brooke.winsor', type: String, default: 'sister'
        attribute 'brooke.costantini', type: String, default: 'sister'
      end.new
    end

    it "sets the value of the given attribute" do
      subject.set_attribute('brooke.winsor', 'rhode island')

      expect(subject.brooke.winsor).to eql('rhode island')
    end

    it "does not disturb the other attributes" do
      subject.set_attribute('brooke.winsor', 'rhode island')

      expect(subject.brooke.costantini).to eql('sister')
    end
  end

  describe "#get_attribute" do
    subject do
      Class.new do
        include VariaModel

        attribute 'brooke.winsor', type: String, default: 'sister'
      end.new
    end

    it "returns the value of the given dotted path" do
      expect(subject.get_attribute('brooke.winsor')).to eql('sister')
    end

    it "returns nil if the dotted path matches no attributes" do
      expect(subject.get_attribute('brooke.costantini')).to be_nil
    end

    it "returns the current value of the Proc" do
      subject.brooke.winsor = ->{ "bacon".upcase }
      expect(subject.get_attribute("brooke.winsor")).to eql("BACON")
    end

    it "returns the current value of the Proc each time" do
      @magic = "ponies"
      subject.brooke.winsor = -> { @magic }
      expect(subject.get_attribute("brooke.winsor")).to eql("ponies")
      @magic = "unicorns"
      expect(subject.get_attribute("brooke.winsor")).to eql("unicorns")
    end
  end

  describe "#[]" do
    subject do
      Class.new do
        include VariaModel
        attribute 'foo', default: ->{ String.new("Bacon").upcase }
      end.new
    end

    it "returns the current value of the Proc" do
      expect(subject['foo']).to eql("BACON")
    end

    it "returns the updated value of the Proc" do
      subject['foo']
      subject['foo'] = 'hello'
      expect(subject['foo']).to eql('hello')
    end
  end

  describe "#mass_assign" do
    subject do
      Class.new do
        include VariaModel

        attribute 'brooke.winsor', type: String, default: 'sister'
        attribute 'jamie.winsor', type: String, default: 'brother'
        attribute 'gizmo', type: String, default: 'dog'
      end.new
    end

    it "sets the values of all matching defined attributes" do
      new_attrs = {
        brooke: {
          winsor: "other"
        },
        jamie: {
          winsor: "other_two"
        }
      }

      subject.mass_assign(new_attrs)
      expect(subject.brooke.winsor).to eql("other")
      expect(subject.jamie.winsor).to eql("other_two")
    end

    it "leaves the values of untouched attributes" do
      new_attrs = {
        brooke: {
          winsor: "other"
        },
        jamie: {
          winsor: "other_two"
        }
      }

      subject.mass_assign(new_attrs)
      expect(subject.gizmo).to eql("dog")
    end

    it "ignores values which are not defined attributes" do
      new_attrs = {
        undefined_attribute: "value"
      }

      subject.mass_assign(new_attrs)
      expect(subject.get_attribute(:undefined_attribute)).to be_nil
      expect(subject).not_to respond_to(:undefined_attribute)
    end

    context "when in carefree assignment mode" do
      subject do
        Class.new do
          include VariaModel

          set_assignment_mode :carefree
        end.new
      end

      it "does not ignore values which are not defined" do
        new_attrs = {
          undefined_attribute: "value"
        }

        subject.mass_assign(new_attrs)
        expect(subject.get_attribute(:undefined_attribute)).to eql("value")
      end
    end
  end

  describe "#from_json" do
    subject do
      Class.new do
        include VariaModel

        attribute 'first_name', type: String
        attribute 'nick', type: String
      end.new
    end

    it "returns self" do
      expect(subject.from_json(JSON.dump(first_name: "jamie", nick: "reset"))).to be_a(described_class)
    end

    it "updates self from JSON data" do
      subject.from_json(JSON.dump(first_name: "jamie", nick: "reset"))

      expect(subject.first_name).to eql("jamie")
      expect(subject.nick).to eql("reset")
    end
  end

  describe "#from_hash" do
    subject do
      Class.new do
        include VariaModel

        attribute 'first_name', type: String
        attribute 'nick', type: String
      end.new
    end

    it "returns self" do
      expect(subject.from_hash(first_name: "jamie", nick: "reset")).to be_a(described_class)
    end

    it "updates and returns self from a Hash" do
      subject.from_hash(first_name: "jamie", nick: "reset")

      expect(subject.first_name).to eql("jamie")
      expect(subject.nick).to eql("reset")
    end
  end

  describe "#to_json" do
    class Playa
      include VariaModel

      attribute 'first_name', type: String
      attribute 'nick', type: String
    end

    subject do
      Playa.new
    end

    it "returns a JSON string containin the serialized attributes" do
      subject.first_name = "brooke"
      subject.nick = "leblanc"

      expect(subject.to_json).to eql(JSON.dump(first_name: "brooke", nick: "leblanc", json_class: "Playa"))
    end

    it "includes the most recent value for any Procs" do
      subject.first_name = ->{ "seth".capitalize }
      subject.nick = ->{ "name".upcase }

      expect(subject.to_json).to eql(JSON.dump(first_name: "Seth", nick: "NAME", json_class: "Playa"))
    end

    describe "when JSON.create_id is nil" do
      before do
        @_old_create_id = JSON.create_id
        JSON.create_id = nil
      end

      after do
        JSON.create_id = @_old_create_id
      end

      it "does not include a nil key" do
        skip ("This won't work on Ruby 3.0") unless RUBY_VERSION < '3'
        subject.first_name = "brooke"
        subject.nick = "leblanc"

        expect(subject.to_json).to eql(JSON.dump(first_name: "brooke", nick: "leblanc"))
      end
    end
  end

  describe "#to_hash" do
    it "returns all of the varia dattributes" do
      expect(subject.to_hash).to eql(subject.send(:_attributes_))
    end
  end
end