Module: Cuprum::Rails::RSpec::Contracts::CommandContracts::ShouldBeARailsCommandContract

Extended by:
RSpec::SleepingKingStudios::Contract
Defined in:
lib/cuprum/rails/rspec/contracts/command_contracts.rb

Overview

Contract validating the behavior of a Rails command implementation.

Instance Method Summary collapse

Instance Method Details

#apply(example_group) ⇒ Object

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    the example group to which the contract is applied.



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
# File 'lib/cuprum/rails/rspec/contracts/command_contracts.rb', line 22

contract do
  describe '.subclass' do
    let(:subclass) { described_class.subclass }
    let(:constructor_options) do
      {
        record_class: Book,
        optional_key: 'optional value'
      }
    end

    it 'should define the class method' do
      expect(described_class)
        .to respond_to(:subclass)
        .with(0).arguments
        .and_any_keywords
    end

    it { expect(subclass).to be_a Class }

    it { expect(subclass).to be < described_class }

    it 'should define the constructor' do
      expect(subclass)
        .to respond_to(:new)
        .with(0).arguments
        .and_any_keywords
    end

    it 'should return the record class' do
      expect(subclass.new(**constructor_options).record_class)
        .to be record_class
    end

    it 'should return the options' do
      expect(subclass.new(**constructor_options).options)
        .to be == { optional_key: 'optional value' }
    end

    describe 'with options' do
      let(:default_options) do
        {
          record_class: Book,
          custom_key:   'custom value'
        }
      end
      let(:constructor_options) do
        {
          optional_key: 'optional value'
        }
      end
      let(:subclass) { described_class.subclass(**default_options) }

      it { expect(subclass).to be_a Class }

      it { expect(subclass).to be < described_class }

      it 'should define the constructor' do
        expect(subclass)
          .to respond_to(:new)
          .with(0).arguments
          .and_any_keywords
      end

      it 'should return the record class' do
        expect(subclass.new(**constructor_options).record_class)
          .to be record_class
      end

      it 'should return the options' do
        expect(subclass.new(**constructor_options).options)
          .to be == {
            custom_key:   'custom value',
            optional_key: 'optional value'
          }
      end
    end
  end

  describe '#collection_name' do
    let(:expected) { record_class.name.underscore.pluralize }

    include_examples 'should define reader',
      :collection_name,
      -> { be == expected }

    context 'when initialized with collection_name: string' do
      let(:collection_name) { 'books' }
      let(:constructor_options) do
        super().merge(collection_name: collection_name)
      end

      it { expect(command.collection_name).to be == collection_name }
    end

    context 'when initialized with collection_name: symbol' do
      let(:collection_name) { :books }
      let(:constructor_options) do
        super().merge(collection_name: collection_name)
      end

      it { expect(command.collection_name).to be == collection_name.to_s }
    end
  end

  describe '#member_name' do
    def tools
      SleepingKingStudios::Tools::Toolbelt.instance
    end

    include_examples 'should have reader',
      :member_name,
      -> { record_class.name.underscore }

    context 'when initialized with collection_name: value' do
      let(:collection_name) { :books }

      it 'should return the singular collection name' do
        expect(command.member_name)
          .to be == tools.str.singularize(collection_name.to_s)
      end
    end

    context 'when initialized with member_name: string' do
      let(:member_name) { 'tome' }
      let(:constructor_options) do
        super().merge(member_name: member_name)
      end

      it 'should return the singular collection name' do
        expect(command.member_name).to be member_name
      end
    end

    context 'when initialized with member_name: symbol' do
      let(:member_name) { :tome }
      let(:constructor_options) do
        super().merge(member_name: member_name)
      end

      it 'should return the singular collection name' do
        expect(command.member_name).to be == member_name.to_s
      end
    end
  end

  describe '#options' do
    let(:expected_options) do
      defined?(super()) ? super() : constructor_options
    end

    include_examples 'should define reader',
      :options,
      -> { be == expected_options }

    context 'when initialized with options' do
      let(:constructor_options) { super().merge({ key: 'value' }) }
      let(:expected_options)    { super().merge({ key: 'value' }) }

      it { expect(command.options).to be == expected_options }
    end
  end

  describe '#primary_key_name' do
    include_examples 'should define reader', :primary_key_name, 'id'

    context 'with a record class with custom primary key' do
      let(:record_class) { Tome }

      include_examples 'should define reader', :primary_key_name, 'uuid'
    end
  end

  describe '#primary_key_type' do
    include_examples 'should define reader', :primary_key_type, Integer

    context 'with a record class with custom primary key' do
      let(:record_class) { Tome }

      include_examples 'should define reader', :primary_key_type, String
    end
  end

  describe '#record_class' do
    include_examples 'should define reader',
      :record_class,
      -> { record_class }
  end

  describe '#validate_entity' do
    let(:expected_error) do
      type     = record_class
      contract = Stannum::Contracts::ParametersContract.new do
        keyword :entity, type
      end
      errors = contract.errors_for(
        {
          arguments: [],
          block:     nil,
          keywords:  { entity: nil }
        }
      )

      Cuprum::Collections::Errors::InvalidParameters.new(
        command: command,
        errors:  errors
      )
    end

    it 'should define the private method' do
      expect(command)
        .to respond_to(:validate_entity, true)
        .with(1).argument
    end

    describe 'with nil' do
      it 'should return a failing result' do
        expect(command.send(:validate_entity, nil))
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with an Object' do
      it 'should return a failing result' do
        expect(command.send(:validate_entity, Object.new.freeze))
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with an invalid record instance' do
      it 'should return a failing result' do
        expect(command.send(:validate_entity, Tome.new))
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with a valid record instance' do
      it 'should not return a result' do
        expect(command.send(:validate_entity, Book.new))
          .not_to be_a_result
      end
    end
  end

  describe '#validate_primary_key' do
    let(:primary_key_type) { Integer }
    let(:expected_error) do
      type     = primary_key_type
      contract = Stannum::Contracts::ParametersContract.new do
        keyword :primary_key, type
      end
      errors = contract.errors_for(
        {
          arguments: [],
          block:     nil,
          keywords:  { primary_key: nil }
        }
      )

      Cuprum::Collections::Errors::InvalidParameters.new(
        command: command,
        errors:  errors
      )
    end

    it 'should define the private method' do
      expect(command)
        .to respond_to(:validate_primary_key, true)
        .with(1).argument
    end

    describe 'with nil' do
      it 'should return a failing result' do
        expect(command.send(:validate_primary_key, nil))
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with an Object' do
      it 'should return a failing result' do
        expect(command.send(:validate_primary_key, Object.new.freeze))
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with a String' do
      it 'should return a failing result' do
        expect(command.send(:validate_primary_key, '12345'))
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with an Integer' do
      it 'should not return a result' do
        expect(command.send(:validate_primary_key, 12_345))
          .not_to be_a_result
      end
    end

    context 'with a record class with custom primary key' do
      let(:record_class)     { Tome }
      let(:primary_key_type) { String }

      describe 'with an Integer' do
        it 'should return a failing result' do
          expect(command.send(:validate_primary_key, 12_345))
            .to be_a_failing_result
            .with_error(expected_error)
        end
      end

      describe 'with a String' do
        it 'should not return a result' do
          expect(command.send(:validate_primary_key, '12345'))
            .not_to be_a_result
        end
      end
    end
  end

  describe '#validate_primary_keys' do
    let(:primary_keys)     { nil }
    let(:primary_key_type) { Integer }
    let(:expected_error) do
      type     = primary_key_type
      contract = Stannum::Contracts::ParametersContract.new do
        keyword :primary_keys,
          Stannum::Constraints::Types::ArrayType.new(item_type: type)
      end
      errors = contract.errors_for(
        {
          arguments: [],
          block:     nil,
          keywords:  { primary_keys: primary_keys }
        }
      )

      Cuprum::Collections::Errors::InvalidParameters.new(
        command: command,
        errors:  errors
      )
    end

    it 'should define the private method' do
      expect(command)
        .to respond_to(:validate_primary_keys, true)
        .with(1).argument
    end

    describe 'with nil' do
      it 'should return a failing result' do
        expect(command.send(:validate_primary_keys, nil))
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with an Object' do
      it 'should return a failing result' do
        expect(command.send(:validate_primary_keys, Object.new.freeze))
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with a String' do
      it 'should return a failing result' do
        expect(command.send(:validate_primary_keys, '12345'))
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with an Integer' do
      it 'should return a failing result' do
        expect(command.send(:validate_primary_keys, 12_345))
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with an empty Array' do
      it 'should not return a result' do
        expect(command.send(:validate_primary_keys, []))
          .not_to be_a_result
      end
    end

    describe 'with an Array with nil values' do
      let(:primary_keys) { Array.new(3, nil) }

      it 'should return a failing result' do
        expect(command.send(:validate_primary_keys, primary_keys))
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with an Array with Object values' do
      let(:primary_keys) { Array.new(3) { Object.new.freeze } }

      it 'should return a failing result' do
        expect(command.send(:validate_primary_keys, primary_keys))
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with an Array with String values' do
      let(:primary_keys) { %w[ichi ni san] }

      it 'should return a failing result' do
        expect(command.send(:validate_primary_keys, primary_keys))
          .to be_a_failing_result
          .with_error(expected_error)
      end
    end

    describe 'with an Array with Integer values' do
      it 'should not return a result' do
        expect(command.send(:validate_primary_keys, [0, 1, 2]))
          .not_to be_a_result
      end
    end

    context 'with a record class with custom primary key' do
      let(:record_class)     { Tome }
      let(:primary_key_type) { String }

      describe 'with an Array with String values' do
        let(:primary_keys) { %w[ichi ni san] }

        it 'should not return a result' do
          expect(command.send(:validate_primary_keys, primary_keys))
            .not_to be_a_result
        end
      end

      describe 'with an Array with Integer values' do
        let(:primary_keys) { [0, 1, 2] }

        it 'should return a failing result' do
          expect(command.send(:validate_primary_keys, primary_keys))
            .to be_a_failing_result
            .with_error(expected_error)
        end
      end
    end
  end
end