Module: Cuprum::Collections::RSpec::Contracts::QueryContracts::ShouldBeAQuery

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

Overview

Contract validating the behavior of a Query implementation.

Instance Method Summary collapse

Instance Method Details

#apply(example_group, operators: ) ⇒ Object

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    the example group to which the contract is applied.

  • operators (Array<Symbol>) (defaults to: )

    the expected operators.



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

contract do |operators: OPERATORS.values|
  include Cuprum::Collections::RSpec::Contracts::QueryContracts

  operators = Set.new(operators.map(&:to_sym))

  include_contract 'with query contexts'

  shared_context 'when the query has composed filters' do
    let(:scoped_query) do
      super()
        .where { { author: 'Ursula K. LeGuin' } }
        .where { { series: not_equal('Earthsea') } }
    end
    let(:matching_data) do
      super()
        .select { |item| item['author'] == 'Ursula K. LeGuin' }
        .reject { |item| item['series'] == 'Earthsea' }
    end
  end

  let(:scoped_query) do
    # :nocov:
    scoped =
      if filter.is_a?(Proc)
        query.where(&filter)
      elsif !filter.nil?
        query.where(filter)
      else
        query
      end
    # :nocov:
    scoped = scoped.limit(limit)   if limit
    scoped = scoped.offset(offset) if offset
    scoped = scoped.order(order)   if order

    scoped
  end

  it 'should be enumerable' do
    expect(described_class).to be < Enumerable
  end

  describe '#count' do
    let(:data)          { [] }
    let(:matching_data) { data }
    let(:expected_data) do
      defined?(super()) ? super() : matching_data
    end

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

    it { expect(query.count).to be == expected_data.count }

    wrap_context 'when the query has composed filters' do
      it { expect(scoped_query.count).to be == expected_data.count }
    end

    context 'when the collection data changes' do
      let(:item) { BOOKS_FIXTURES.first }

      before(:example) do
        query.count # Cache query results.

        add_item_to_collection(item)
      end

      it { expect(query.count).to be == expected_data.count }
    end

    context 'when the collection has many items' do
      let(:data) { BOOKS_FIXTURES }

      it { expect(query.count).to be == expected_data.count }

      wrap_context 'when the query has composed filters' do
        it { expect(scoped_query.count).to be == expected_data.count }
      end

      context 'when the collection data changes' do
        let(:data) { BOOKS_FIXTURES[0...-1] }
        let(:item) { BOOKS_FIXTURES.last }

        before(:example) do
          query.count # Cache query results.

          add_item_to_collection(item)
        end

        it { expect(query.count).to be == expected_data.count }
      end
    end
  end

  describe '#criteria' do
    include_examples 'should have reader', :criteria, []

    wrap_context 'when the query has where: a simple block filter' do
      let(:expected) { [['author', :equal, 'Ursula K. LeGuin']] }

      it { expect(scoped_query.criteria).to be == expected }
    end

    wrap_context 'when the query has where: a complex block filter' do
      let(:expected) do
        [
          ['author', :equal,     'Ursula K. LeGuin'],
          ['series', :not_equal, 'Earthsea']
        ]
      end

      if operators.include?(OPERATORS::EQUAL) &&
         operators.include?(OPERATORS::NOT_EQUAL)
        it { expect(scoped_query.criteria).to be == expected }
      else
        # :nocov:
        pending
        # :nocov:
      end
    end

    wrap_context 'when the query has composed filters' do
      let(:expected) do
        [
          ['author', :equal,     'Ursula K. LeGuin'],
          ['series', :not_equal, 'Earthsea']
        ]
      end

      it { expect(scoped_query.criteria).to be == expected }
    end

    wrap_context 'when the query has where: an equal block filter' do
      let(:expected) { [['author', :equal, 'Ursula K. LeGuin']] }

      if operators.include?(OPERATORS::EQUAL)
        it { expect(scoped_query.criteria).to be == expected }
      else
        # :nocov:
        pending
        # :nocov:
      end
    end

    wrap_context 'when the query has where: a not_equal block filter' do
      let(:expected) { [['author', :not_equal, 'Ursula K. LeGuin']] }

      if operators.include?(OPERATORS::NOT_EQUAL)
        it { expect(scoped_query.criteria).to be == expected }
      else
        # :nocov:
        pending
        # :nocov:
      end
    end
  end

  describe '#each' do
    shared_examples 'should enumerate the matching data' do
      describe 'with no arguments' do
        it { expect(scoped_query.each).to be_a Enumerator }

        it { expect(scoped_query.each.count).to be == matching_data.size }

        it { expect(scoped_query.each.to_a).to deep_match expected_data }
      end

      describe 'with a block' do
        it 'should yield each matching item' do
          expect { |block| scoped_query.each(&block) }
            .to yield_successive_args(*expected_data)
        end
      end
    end

    let(:data)          { [] }
    let(:matching_data) { data }
    let(:expected_data) do
      defined?(super()) ? super() : matching_data
    end

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

    include_examples 'should enumerate the matching data'

    include_contract 'should perform queries',
      block:     lambda {
        include_examples 'should enumerate the matching data'
      },
      operators: operators

    wrap_context 'when the query has composed filters' do
      include_examples 'should enumerate the matching data'
    end

    context 'when the collection data changes' do
      let(:item) { BOOKS_FIXTURES.first }

      before(:example) do
        query.each {} # Cache query results.

        add_item_to_collection(item)
      end

      include_examples 'should enumerate the matching data'
    end

    context 'when the collection has many items' do
      let(:data) { BOOKS_FIXTURES }

      include_examples 'should enumerate the matching data'

      include_contract 'should perform queries',
        block:     lambda {
          include_examples 'should enumerate the matching data'
        },
        operators: operators

      wrap_context 'when the query has composed filters' do
        include_examples 'should enumerate the matching data'
      end

      context 'when the collection data changes' do
        let(:data) { BOOKS_FIXTURES[0...-1] }
        let(:item) { BOOKS_FIXTURES.last }

        before(:example) do
          query.each {} # Cache query results.

          add_item_to_collection(item)
        end

        include_examples 'should enumerate the matching data'
      end
    end
  end

  describe '#exists?' do
    shared_examples 'should check the existence of matching data' do
      it { expect(query.exists?).to be == !matching_data.empty? }
    end

    let(:data)          { [] }
    let(:matching_data) { data }

    include_examples 'should define predicate', :exists?

    include_examples 'should check the existence of matching data'

    include_contract 'should perform queries',
      block:     lambda {
        include_examples 'should check the existence of matching data'
      },
      operators: operators

    wrap_context 'when the query has composed filters' do
      include_examples 'should check the existence of matching data'
    end

    context 'when the collection has many items' do
      let(:data) { BOOKS_FIXTURES }

      include_examples 'should check the existence of matching data'

      include_contract 'should perform queries',
        block:     lambda {
          include_examples 'should check the existence of matching data'
        },
        operators: operators

      wrap_context 'when the query has composed filters' do
        include_examples 'should check the existence of matching data'
      end
    end
  end

  describe '#limit' do
    it { expect(query).to respond_to(:limit).with(0..1).arguments }

    describe 'with no arguments' do
      it { expect(query.limit).to be nil }
    end

    describe 'with nil' do
      let(:error_message) { 'limit must be a non-negative integer' }

      it 'should raise an exception' do
        expect { query.limit nil }
          .to raise_error ArgumentError, error_message
      end
    end

    describe 'with an object' do
      let(:error_message) { 'limit must be a non-negative integer' }

      it 'should raise an exception' do
        expect { query.limit Object.new.freeze }
          .to raise_error ArgumentError, error_message
      end
    end

    describe 'with a negative integer' do
      let(:error_message) { 'limit must be a non-negative integer' }

      it 'should raise an exception' do
        expect { query.limit(-1) }
          .to raise_error ArgumentError, error_message
      end
    end

    describe 'with zero' do
      it { expect(query.limit(0)).to be_a described_class }

      it { expect(query.limit(0)).not_to be query }

      it { expect(query.limit(0).limit).to be 0 }
    end

    describe 'with a positive integer' do
      it { expect(query.limit(3)).to be_a described_class }

      it { expect(query.limit(3)).not_to be query }

      it { expect(query.limit(3).limit).to be 3 }
    end
  end

  describe '#offset' do
    it { expect(query).to respond_to(:offset).with(0..1).argument }

    describe 'with no arguments' do
      it { expect(query.offset).to be nil }
    end

    describe 'with nil' do
      let(:error_message) { 'offset must be a non-negative integer' }

      it 'should raise an exception' do
        expect { query.offset nil }
          .to raise_error ArgumentError, error_message
      end
    end

    describe 'with an object' do
      let(:error_message) { 'offset must be a non-negative integer' }

      it 'should raise an exception' do
        expect { query.offset Object.new.freeze }
          .to raise_error ArgumentError, error_message
      end
    end

    describe 'with a negative integer' do
      let(:error_message) { 'offset must be a non-negative integer' }

      it 'should raise an exception' do
        expect { query.offset(-1) }
          .to raise_error ArgumentError, error_message
      end
    end

    describe 'with zero' do
      it { expect(query.offset(0)).to be_a described_class }

      it { expect(query.offset(0)).not_to be query }

      it { expect(query.offset(0).offset).to be 0 }
    end

    describe 'with a positive integer' do
      it { expect(query.offset(3)).to be_a described_class }

      it { expect(query.offset(3)).not_to be query }

      it { expect(query.offset(3).offset).to be 3 }
    end
  end

  describe '#order' do
    let(:default_order) { defined?(super()) ? super() : {} }
    let(:error_message) do
      'order must be a list of attribute names and/or a hash of ' \
        'attribute names with values :asc or :desc'
    end

    it 'should define the method' do
      expect(query)
        .to respond_to(:order)
        .with(0).arguments
        .and_unlimited_arguments
    end

    it { expect(query).to have_aliased_method(:order).as(:order_by) }

    describe 'with no arguments' do
      it { expect(query.order).to be == default_order }
    end

    describe 'with a hash with invalid keys' do
      it 'should raise an exception' do
        expect { query.order({ nil => :asc }) }
          .to raise_error ArgumentError, error_message
      end
    end

    describe 'with a hash with empty string keys' do
      it 'should raise an exception' do
        expect { query.order({ '' => :asc }) }
          .to raise_error ArgumentError, error_message
      end
    end

    describe 'with a hash with empty symbol keys' do
      it 'should raise an exception' do
        expect { query.order({ '': :asc }) }
          .to raise_error ArgumentError, error_message
      end
    end

    describe 'with a hash with nil value' do
      it 'should raise an exception' do
        expect { query.order({ title: nil }) }
          .to raise_error ArgumentError, error_message
      end
    end

    describe 'with a hash with object value' do
      it 'should raise an exception' do
        expect { query.order({ title: Object.new.freeze }) }
          .to raise_error ArgumentError, error_message
      end
    end

    describe 'with a hash with empty value' do
      it 'should raise an exception' do
        expect { query.order({ title: '' }) }
          .to raise_error ArgumentError, error_message
      end
    end

    describe 'with a hash with invalid value' do
      it 'should raise an exception' do
        expect { query.order({ title: 'wibbly' }) }
          .to raise_error ArgumentError, error_message
      end
    end

    describe 'with a valid ordering' do
      let(:expected) do
        { title: :asc }
      end

      it { expect(query.order(:title)).to be_a described_class }

      it { expect(query.order(:title)).not_to be query }

      it { expect(query.order(:title).order).to be == expected }
    end
  end

  describe '#reset' do
    let(:data)          { [] }
    let(:matching_data) { data }
    let(:expected_data) do
      defined?(super()) ? super() : matching_data
    end

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

    it { expect(query.reset).to be_a query.class }

    it { expect(query.reset).not_to be query }

    it { expect(query.reset.to_a).to be == query.to_a }

    context 'when the collection data changes' do
      let(:item)          { BOOKS_FIXTURES.first }
      let(:matching_data) { [item] }

      before(:example) do
        query.to_a # Cache query results.

        add_item_to_collection(item)
      end

      it { expect(query.reset.count).to be expected_data.size }

      it { expect(query.reset.to_a).to deep_match expected_data }
    end

    context 'when the collection has many items' do
      let(:data) { BOOKS_FIXTURES }

      it { expect(query.reset).to be_a query.class }

      it { expect(query.reset).not_to be query }

      it { expect(query.reset.to_a).to be == query.to_a }

      context 'when the collection data changes' do
        let(:data)          { BOOKS_FIXTURES[0...-1] }
        let(:item)          { BOOKS_FIXTURES.last }
        let(:matching_data) { [*data, item] }

        before(:example) do
          query.to_a # Cache query results.

          add_item_to_collection(item)
        end

        it { expect(query.reset.count).to be expected_data.size }

        it { expect(query.reset.to_a).to deep_match expected_data }
      end
    end
  end

  describe '#to_a' do
    let(:data)          { [] }
    let(:matching_data) { data }
    let(:expected_data) do
      defined?(super()) ? super() : matching_data
    end

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

    it { expect(query.to_a).to deep_match expected_data }

    include_contract 'should perform queries',
      block:     lambda {
        it { expect(scoped_query.to_a).to deep_match expected_data }
      },
      operators: operators

    wrap_context 'when the query has composed filters' do
      it { expect(scoped_query.to_a).to deep_match expected_data }
    end

    context 'when the collection data changes' do
      let(:item) { BOOKS_FIXTURES.first }

      before(:example) do
        query.to_a # Cache query results.

        add_item_to_collection(item)
      end

      it { expect(query.to_a).to deep_match expected_data }
    end

    context 'when the collection has many items' do
      let(:data) { BOOKS_FIXTURES }

      it { expect(query.to_a).to deep_match expected_data }

      include_contract 'should perform queries',
        block:     lambda {
          it { expect(scoped_query.to_a).to deep_match expected_data }
        },
        operators: operators

      wrap_context 'when the query has composed filters' do
        it { expect(scoped_query.to_a).to deep_match expected_data }
      end

      context 'when the collection data changes' do
        let(:data) { BOOKS_FIXTURES[0...-1] }
        let(:item) { BOOKS_FIXTURES.last }

        before(:example) do
          query.to_a # Cache query results.

          add_item_to_collection(item)
        end

        it { expect(query.to_a).to deep_match expected_data }
      end
    end
  end

  describe '#where' do
    let(:block) { -> { { title: 'The Caves of Steel' } } }

    it 'should define the method' do
      expect(query)
        .to respond_to(:where)
        .with(0..1).arguments
        .and_keywords(:strategy)
        .and_a_block
    end

    describe 'with no arguments' do
      it { expect(query.where).to be_a described_class }

      it { expect(query.where).not_to be query }
    end

    describe 'with a block' do
      it { expect(query.where(&block)).to be_a described_class }

      it { expect(query.where(&block)).not_to be query }
    end

    describe 'with a valid strategy' do
      it 'should return a query instance' do
        expect(query.where(strategy: :block, &block))
          .to be_a described_class
      end

      it { expect(query.where(strategy: :block, &block)).not_to be query }
    end

    describe 'with parameters that do not match a strategy' do
      let(:error_class) do
        Cuprum::Collections::QueryBuilder::ParseError
      end
      let(:error_message) { 'unable to parse query with strategy nil' }

      it 'should raise an exception' do
        expect { query.where(%w[ichi ni san]) }
          .to raise_error error_class, error_message
      end
    end

    describe 'with an invalid strategy' do
      let(:error_class) do
        Cuprum::Collections::QueryBuilder::ParseError
      end
      let(:error_message) do
        'unable to parse query with strategy :random'
      end

      it 'should raise an exception' do
        expect { query.where(strategy: :random) }
          .to raise_error error_class, error_message
      end
    end

    describe 'with invalid parameters for a strategy' do
      let(:error_class) do
        Cuprum::Collections::QueryBuilder::ParseError
      end
      let(:error_message) { 'unable to parse query with strategy :block' }

      it 'should raise an exception' do
        expect { query.where(strategy: :block) }
          .to raise_error error_class, error_message
      end
    end
  end
end