Module: Cuprum::Collections::RSpec::Contracts::CollectionContracts::ShouldBeACollectionContract

Extended by:
RSpec::SleepingKingStudios::Contract
Defined in:
lib/cuprum/collections/rspec/contracts/collection_contracts.rb

Overview

Contract validating the behavior of a Collection.

Instance Method Summary collapse

Instance Method Details

#apply(example_group, **options) ⇒ Object

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    the example group to which the contract is applied.

  • options (Hash)

    additional options for the contract.

Options Hash (**options):

  • abstract (Boolean)

    if true, the collection is an abstract base class and does not define a query or commands.

  • default_entity_class (Class)

    the default entity class for the collection, if any.



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
# File 'lib/cuprum/collections/rspec/contracts/collection_contracts.rb', line 27

contract do |**options|
  shared_examples 'should define the command' \
  do |command_name, command_class_name = nil|
    next if options[:abstract]

    tools           = SleepingKingStudios::Tools::Toolbelt.instance
    class_name      = tools.str.camelize(command_name)
    command_options = %i[
      collection_name
      member_name
      primary_key_name
      primary_key_type
    ] + options.fetch(:command_options, []).map(&:intern)

    describe "::#{class_name}" do
      let(:constructor_options) { defined?(super()) ? super() : {} }
      let(:command_class) do
        command_class_name ||
          "#{options[:commands_namespace]}::#{class_name}"
            .then { |str| Object.const_get(str) }
      end
      let(:command) do
        collection.const_get(class_name).new(**constructor_options)
      end
      let(:expected_options) do
        Hash
          .new { |_, key| collection.send(key) }
          .merge(
            collection_name: collection.name,
            member_name:     collection.singular_name
          )
      end

      it { expect(collection).to define_constant(class_name) }

      it { expect(collection.const_get(class_name)).to be_a Class }

      it 'should be an instance of the command class' do
        expect(collection.const_get(class_name)).to be < command_class
      end

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

      command_options.each do |option_name|
        it "should set the ##{option_name}" do
          expect(command.send(option_name))
            .to be == expected_options[option_name]
        end
      end

      describe 'with options' do
        let(:constructor_options) do
          super().merge(
            custom_option: 'value',
            singular_name: 'tome'
          )
        end

        it { expect(command.options).to be >= { custom_option: 'value' } }

        command_options.each do |option_name|
          it "should set the ##{option_name}" do
            expect(command.send(option_name)).to(
              be == expected_options[option_name]
            )
          end
        end
      end
    end

    describe "##{command_name}" do
      let(:constructor_options) { defined?(super()) ? super() : {} }
      let(:command) do
        collection.send(command_name, **constructor_options)
      end
      let(:expected_options) do
        Hash
          .new { |_, key| collection.send(key) }
          .merge(
            collection_name: collection.name,
            member_name:     collection.singular_name
          )
      end

      it 'should define the command' do
        expect(collection)
          .to respond_to(command_name)
          .with(0).arguments
          .and_any_keywords
      end

      it { expect(command).to be_a collection.const_get(class_name) }

      command_options.each do |option_name|
        it "should set the ##{option_name}" do
          expect(command.send(option_name))
            .to be == expected_options[option_name]
        end
      end

      describe 'with options' do
        let(:constructor_options) do
          super().merge(
            custom_option: 'value',
            singular_name: 'tome'
          )
        end

        it { expect(command.options).to be >= { custom_option: 'value' } }

        command_options.each do |option_name|
          it "should set the ##{option_name}" do
            expect(command.send(option_name)).to(
              be == expected_options[option_name]
            )
          end
        end
      end
    end
  end

  include_contract 'should be a relation',
    constructor:          false,
    default_entity_class: options[:default_entity_class]

  include_contract 'should disambiguate parameter',
    :name,
    as: :collection_name

  include_contract 'should disambiguate parameter',
    :singular_name,
    as: :member_name

  include_contract 'should define primary keys'

  include_examples 'should define the command', :assign_one

  include_examples 'should define the command', :build_one

  include_examples 'should define the command', :destroy_one

  include_examples 'should define the command', :find_many

  include_examples 'should define the command', :find_matching

  include_examples 'should define the command', :find_one

  include_examples 'should define the command', :insert_one

  include_examples 'should define the command', :update_one

  include_examples 'should define the command', :validate_one

  describe '#==' do
    let(:other_options)    { { name: name } }
    let(:other_collection) { described_class.new(**other_options) }

    describe 'with nil' do
      it { expect(collection == nil).to be false } # rubocop:disable Style/NilComparison
    end

    describe 'with an object' do
      it { expect(collection == Object.new.freeze).to be false }
    end

    describe 'with a collection with non-matching properties' do
      let(:other_options) { super().merge(custom_option: 'value') }

      it { expect(collection == other_collection).to be false }
    end

    describe 'with a collection with matching properties' do
      it { expect(collection == other_collection).to be true }
    end

    describe 'with another type of collection' do
      let(:other_collection) do
        Spec::OtherCollection.new(**other_options)
      end

      example_class 'Spec::OtherCollection',
        Cuprum::Collections::Collection

      it { expect(collection == other_collection).to be false }
    end

    context 'when initialized with options' do
      let(:constructor_options) do
        super().merge(
          qualified_name: 'spec/scoped_books',
          singular_name:  'grimoire'
        )
      end

      describe 'with a collection with non-matching properties' do
        it { expect(collection == other_collection).to be false }
      end

      describe 'with a collection with matching properties' do
        let(:other_options) do
          super().merge(
            qualified_name: 'spec/scoped_books',
            singular_name:  'grimoire'
          )
        end

        it { expect(collection == other_collection).to be true }
      end
    end
  end

  describe '#count' do
    it { expect(collection).to respond_to(:count).with(0).arguments }

    it { expect(collection).to have_aliased_method(:count).as(:size) }

    next if options[:abstract]

    it { expect(collection.count).to be 0 }

    wrap_context 'when the collection has many items' do
      it { expect(collection.count).to be items.count }
    end
  end

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

    it 'should define the method' do
      expect(collection)
        .to respond_to(:matches?)
        .with(0).arguments
        .and_any_keywords
    end

    describe 'with no options' do
      it { expect(collection.matches?).to be true }
    end

    describe 'with non-matching entity class as a Class' do
      let(:other_options) { { entity_class: Grimoire } }

      it { expect(collection.matches?(**other_options)).to be false }
    end

    describe 'with non-matching entity class as a String' do
      let(:other_options) { { entity_class: 'Grimoire' } }

      it { expect(collection.matches?(**other_options)).to be false }
    end

    describe 'with non-matching name' do
      it { expect(collection.matches?(name: 'grimoires')).to be false }
    end

    describe 'with non-matching primary key name' do
      let(:other_options) { { primary_key_name: 'uuid' } }

      it { expect(collection.matches?(**other_options)).to be false }
    end

    describe 'with non-matching primary key type' do
      let(:other_options) { { primary_key_type: String } }

      it { expect(collection.matches?(**other_options)).to be false }
    end

    describe 'with non-matching qualified name' do
      let(:other_options) { { qualified_name: 'spec/scoped_books' } }

      it { expect(collection.matches?(**other_options)).to be false }
    end

    describe 'with non-matching singular name' do
      let(:other_options) { { singular_name: 'grimoire' } }

      it { expect(collection.matches?(**other_options)).to be false }
    end

    describe 'with non-matching custom options' do
      let(:other_options) { { custom_option: 'custom value' } }

      it { expect(collection.matches?(**other_options)).to be false }
    end

    describe 'with partially-matching options' do
      let(:other_options) do
        {
          name:          name,
          singular_name: 'grimoire'
        }
      end

      it { expect(collection.matches?(**other_options)).to be false }
    end

    describe 'with matching entity class as a Class' do
      let(:configured_entity_class) do
        options.fetch(:default_entity_class, Book)
      end
      let(:other_options) { { entity_class: configured_entity_class } }

      it { expect(collection.matches?(**other_options)).to be true }
    end

    describe 'with matching entity class as a String' do
      let(:configured_entity_class) do
        options.fetch(:default_entity_class, Book)
      end
      let(:other_options) do
        { entity_class: configured_entity_class.to_s }
      end

      it { expect(collection.matches?(**other_options)).to be true }
    end

    describe 'with matching name' do
      let(:other_options) { { collection_name: name } }

      it { expect(collection.matches?(**other_options)).to be true }
    end

    describe 'with matching primary key name' do
      let(:other_options) { { primary_key_name: 'id' } }

      it { expect(collection.matches?(**other_options)).to be true }
    end

    describe 'with matching primary key type' do
      let(:other_options) { { primary_key_type: Integer } }

      it { expect(collection.matches?(**other_options)).to be true }
    end

    describe 'with matching qualified name' do
      let(:other_options) { { qualified_name: name } }

      it { expect(collection.matches?(**other_options)).to be true }
    end

    describe 'with matching singular name' do
      let(:other_options) do
        { singular_name: tools.str.singularize(name) }
      end

      it { expect(collection.matches?(**other_options)).to be true }
    end

    describe 'with multiple matching options' do
      let(:other_options) do
        {
          collection_name:  name,
          primary_key_name: 'id',
          qualified_name:   name
        }
      end

      it { expect(collection.matches?(**other_options)).to be true }
    end
  end

  describe '#query' do
    let(:error_message) do
      "#{described_class.name} is an abstract class. Define a " \
        'repository subclass and implement the #query method.'
    end
    let(:default_order) { defined?(super()) ? super() : {} }
    let(:query)         { collection.query }

    it { expect(collection).to respond_to(:query).with(0).arguments }

    if options[:abstract]
      it 'should raise an exception' do
        expect { collection.query }
          .to raise_error(
            described_class::AbstractCollectionError,
            error_message
          )
      end
    else
      it { expect(collection.query).to be_a query_class }

      it 'should set the query options' do
        query_options.each do |option, value|
          expect(collection.query.send(option)).to be == value
        end
      end

      it { expect(query.criteria).to be == [] }

      it { expect(query.limit).to be nil }

      it { expect(query.offset).to be nil }

      it { expect(query.order).to be == default_order }
    end
  end
end