Module: Cuprum::Collections::RSpec::Contracts::CommandContracts::ShouldBeAFindManyCommandContract

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

Overview

Contract validating the behavior of a FindMany 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.



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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# File 'lib/cuprum/collections/rspec/contracts/command_contracts.rb', line 401

contract do
  describe '#call' do
    let(:mapped_data) do
      defined?(super()) ? super() : data
    end
    let(:primary_key_name) { defined?(super()) ? super() : 'id' }
    let(:primary_key_type) { defined?(super()) ? super() : Integer }
    let(:primary_keys_contract) do
      Stannum::Constraints::Types::ArrayType
        .new(item_type: primary_key_type)
    end
    let(:invalid_primary_key_values) do
      defined?(super()) ? super() : [100, 101, 102]
    end
    let(:valid_primary_key_values) do
      defined?(super()) ? super() : [0, 1, 2]
    end

    it 'should validate the :allow_partial keyword' do
      expect(command)
        .to validate_parameter(:call, :allow_partial)
        .using_constraint(Stannum::Constraints::Boolean.new)
    end

    it 'should validate the :envelope keyword' do
      expect(command)
        .to validate_parameter(:call, :envelope)
        .using_constraint(Stannum::Constraints::Boolean.new)
    end

    it 'should validate the :primary_keys keyword' do
      expect(command)
        .to validate_parameter(:call, :primary_keys)
        .using_constraint(Array)
    end

    it 'should validate the :primary_keys keyword items' do
      expect(command)
        .to validate_parameter(:call, :primary_keys)
        .with_value([nil])
        .using_constraint(primary_keys_contract)
    end

    it 'should validate the :scope keyword' do
      expect(command)
        .to validate_parameter(:call, :scope)
        .using_constraint(
          Stannum::Constraints::Type.new(query.class, optional: true)
        )
        .with_value(Object.new.freeze)
    end

    describe 'with an array of invalid primary keys' do
      let(:primary_keys) { invalid_primary_key_values }
      let(:expected_error) do
        Cuprum::Errors::MultipleErrors.new(
          errors: primary_keys.map do |primary_key|
            Cuprum::Collections::Errors::NotFound.new(
              attribute_name:  primary_key_name,
              attribute_value: primary_key,
              collection_name: command.collection_name,
              primary_key:     true
            )
          end
        )
      end

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

    context 'when the collection has many items' do
      let(:data) { fixtures_data }
      let(:matching_data) do
        primary_keys
          .map do |key|
            mapped_data.find { |item| item[primary_key_name.to_s] == key }
          end
      end
      let(:expected_data) do
        defined?(super()) ? super() : matching_data
      end

      describe 'with an array of invalid primary keys' do
        let(:primary_keys) { invalid_primary_key_values }
        let(:expected_error) do
          Cuprum::Errors::MultipleErrors.new(
            errors: primary_keys.map do |primary_key|
              Cuprum::Collections::Errors::NotFound.new(
                attribute_name:  primary_key_name,
                attribute_value: primary_key,
                collection_name: command.collection_name,
                primary_key:     true
              )
            end
          )
        end

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

      describe 'with a partially valid array of primary keys' do
        let(:primary_keys) do
          invalid_primary_key_values + valid_primary_key_values
        end
        let(:expected_error) do
          Cuprum::Errors::MultipleErrors.new(
            errors: primary_keys.map do |primary_key|
              unless invalid_primary_key_values.include?(primary_key)
                next nil
              end

              Cuprum::Collections::Errors::NotFound.new(
                attribute_name:  primary_key_name,
                attribute_value: primary_key,
                collection_name: command.collection_name,
                primary_key:     true
              )
            end
          )
        end

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

      describe 'with a valid array of primary keys' do
        let(:primary_keys) { valid_primary_key_values }

        it 'should return a passing result' do
          expect(command.call(primary_keys: primary_keys))
            .to be_a_passing_result
            .with_value(expected_data)
        end

        describe 'with an ordered array of primary keys' do
          let(:primary_keys) { valid_primary_key_values.reverse }

          it 'should return a passing result' do
            expect(command.call(primary_keys: primary_keys))
              .to be_a_passing_result
              .with_value(expected_data)
          end
        end
      end

      describe 'with allow_partial: true' do
        describe 'with an array of invalid primary keys' do
          let(:primary_keys) { invalid_primary_key_values }
          let(:expected_error) do
            Cuprum::Errors::MultipleErrors.new(
              errors: invalid_primary_key_values.map do |primary_key|
                Cuprum::Collections::Errors::NotFound.new(
                  attribute_name:  primary_key_name,
                  attribute_value: primary_key,
                  collection_name: command.collection_name,
                  primary_key:     true
                )
              end
            )
          end

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

        describe 'with a partially valid array of primary keys' do
          let(:primary_keys) do
            invalid_primary_key_values + valid_primary_key_values
          end
          let(:expected_error) do
            Cuprum::Errors::MultipleErrors.new(
              errors: primary_keys.map do |primary_key|
                unless invalid_primary_key_values.include?(primary_key)
                  next nil
                end

                Cuprum::Collections::Errors::NotFound.new(
                  attribute_name:  primary_key_name,
                  attribute_value: primary_key,
                  collection_name: command.collection_name,
                  primary_key:     true
                )
              end
            )
          end

          it 'should return a passing result' do
            expect(
              command.call(
                primary_keys:  primary_keys,
                allow_partial: true
              )
            )
              .to be_a_passing_result
              .with_value(expected_data)
              .and_error(expected_error)
          end
        end

        describe 'with a valid array of primary keys' do
          let(:primary_keys) { valid_primary_key_values }

          it 'should return a passing result' do
            expect(
              command.call(
                primary_keys:  primary_keys,
                allow_partial: true
              )
            )
              .to be_a_passing_result
              .with_value(expected_data)
          end

          describe 'with an ordered array of primary keys' do
            let(:primary_keys) { valid_primary_key_values.reverse }

            it 'should return a passing result' do
              expect(
                command.call(
                  primary_keys:  primary_keys,
                  allow_partial: true
                )
              )
                .to be_a_passing_result
                .with_value(expected_data)
            end
          end
        end
      end

      describe 'with envelope: true' do
        describe 'with a valid array of primary keys' do
          let(:primary_keys) { valid_primary_key_values }

          it 'should return a passing result' do
            expect(
              command.call(primary_keys: primary_keys, envelope: true)
            )
              .to be_a_passing_result
              .with_value({ collection_name => expected_data })
          end

          describe 'with an ordered array of primary keys' do
            let(:primary_keys) { valid_primary_key_values.reverse }

            it 'should return a passing result' do
              expect(
                command.call(primary_keys: primary_keys, envelope: true)
              )
                .to be_a_passing_result
                .with_value({ collection_name => expected_data })
            end
          end
        end
      end

      describe 'with scope: query' do
        let(:scope_filter) { -> { {} } }

        describe 'with an array of invalid primary keys' do
          let(:primary_keys) { invalid_primary_key_values }
          let(:expected_error) do
            Cuprum::Errors::MultipleErrors.new(
              errors: primary_keys.map do |primary_key|
                Cuprum::Collections::Errors::NotFound.new(
                  attribute_name:  primary_key_name,
                  attribute_value: primary_key,
                  collection_name: command.collection_name,
                  primary_key:     true
                )
              end
            )
          end

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

        describe 'with a scope that does not match any keys' do
          let(:scope_filter) { -> { { author: 'Ursula K. LeGuin' } } }

          describe 'with a valid array of primary keys' do
            let(:primary_keys) { valid_primary_key_values }
            let(:expected_error) do
              Cuprum::Errors::MultipleErrors.new(
                errors: primary_keys.map do |primary_key|
                  Cuprum::Collections::Errors::NotFound.new(
                    attribute_name:  primary_key_name,
                    attribute_value: primary_key,
                    collection_name: command.collection_name,
                    primary_key:     true
                  )
                end
              )
            end

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

        describe 'with a scope that matches some keys' do
          let(:scope_filter) { -> { { series: nil } } }
          let(:matching_data) do
            super().map do |item|
              next nil unless item['series'].nil?

              item
            end
          end

          describe 'with a valid array of primary keys' do
            let(:primary_keys) { valid_primary_key_values }
            let(:expected_error) do
              found_keys =
                matching_data
                  .compact
                  .map { |item| item[primary_key_name.to_s] }

              Cuprum::Errors::MultipleErrors.new(
                errors: primary_keys.map do |primary_key|
                  next if found_keys.include?(primary_key)

                  Cuprum::Collections::Errors::NotFound.new(
                    attribute_name:  primary_key_name,
                    attribute_value: primary_key,
                    collection_name: command.collection_name,
                    primary_key:     true
                  )
                end
              )
            end

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

          describe 'with allow_partial: true' do
            describe 'with a valid array of primary keys' do
              let(:primary_keys) { valid_primary_key_values }
              let(:expected_error) do
                found_keys =
                  matching_data
                    .compact
                    .map { |item| item[primary_key_name.to_s] }

                Cuprum::Errors::MultipleErrors.new(
                  errors: primary_keys.map do |primary_key|
                    next if found_keys.include?(primary_key)

                    Cuprum::Collections::Errors::NotFound.new(
                      attribute_name:  primary_key_name,
                      attribute_value: primary_key,
                      collection_name: command.collection_name,
                      primary_key:     true
                    )
                  end
                )
              end

              it 'should return a passing result' do
                expect(
                  command.call(
                    allow_partial: true,
                    primary_keys:  primary_keys,
                    scope:         scope
                  )
                )
                  .to be_a_passing_result
                  .with_value(expected_data)
                  .and_error(expected_error)
              end
            end
          end
        end

        describe 'with a scope that matches all keys' do
          let(:scope_filter) { -> { { author: 'J.R.R. Tolkien' } } }

          describe 'with a valid array of primary keys' do
            let(:primary_keys) { valid_primary_key_values }

            it 'should return a passing result' do
              expect(command.call(primary_keys: primary_keys))
                .to be_a_passing_result
                .with_value(expected_data)
            end
          end
        end
      end
    end
  end
end