Module: Cuprum::Collections::RSpec::Contracts::CommandContracts::ShouldBeAFindMatchingCommandContract

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

Overview

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



832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
# File 'lib/cuprum/collections/rspec/contracts/command_contracts.rb', line 832

contract do
  include Stannum::RSpec::Matchers
  include Cuprum::Collections::RSpec::Contracts::QueryContracts

  describe '#call' do
    include_contract 'with query contexts'

    shared_examples 'should return the matching items' do
      it { expect(result).to be_a_passing_result }

      it { expect(result.value).to be_a Enumerator }

      it { expect(result.value.to_a).to be == expected_data }
    end

    shared_examples 'should return the wrapped items' do
      it { expect(result).to be_a_passing_result }

      it { expect(result.value).to be_a Hash }

      it { expect(result.value.keys).to be == [collection_name] }

      it { expect(result.value[collection_name]).to be == expected_data }
    end

    let(:options) do
      opts = {}

      opts[:limit]  = limit  if limit
      opts[:offset] = offset if offset
      opts[:order]  = order  if order
      opts[:where]  = filter unless filter.nil? || filter.is_a?(Proc)

      opts
    end
    let(:block)         { filter.is_a?(Proc) ? filter : nil }
    let(:result)        { command.call(**options, &block) }
    let(:data)          { [] }
    let(:matching_data) { data }
    let(:expected_data) do
      defined?(super()) ? super() : matching_data
    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 :limit keyword' do
      expect(command)
        .to validate_parameter(:call, :limit)
        .with_value(Object.new)
        .using_constraint(Integer, required: false)
    end

    it 'should validate the :offset keyword' do
      expect(command)
        .to validate_parameter(:call, :offset)
        .with_value(Object.new)
        .using_constraint(Integer, required: false)
    end

    it 'should validate the :order keyword' do
      constraint = Cuprum::Collections::Constraints::Ordering.new

      expect(command)
        .to validate_parameter(:call, :order)
        .with_value(Object.new)
        .using_constraint(constraint, required: false)
    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

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

    include_examples 'should return the matching items'

    include_contract 'should perform queries',
      block: lambda {
        include_examples 'should return the matching items'
      }

    describe 'with an invalid filter block' do
      let(:block) { -> {} }
      let(:expected_error) do
        an_instance_of(Cuprum::Collections::Errors::InvalidQuery)
      end

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

    describe 'with envelope: true' do
      let(:options) { super().merge(envelope: true) }

      include_examples 'should return the wrapped items'

      include_contract 'should perform queries',
        block: lambda {
          include_examples 'should return the wrapped items'
        }
    end

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

      include_examples 'should return the matching items'

      include_contract 'should perform queries',
        block: lambda {
          include_examples 'should return the matching items'
        }

      describe 'with envelope: true' do
        let(:options) { super().merge(envelope: true) }

        include_examples 'should return the wrapped items'

        include_contract 'should perform queries',
          block: lambda {
            include_examples 'should return the wrapped items'
          }
      end

      describe 'with scope: query' do
        let(:scope_filter) { -> { {} } }
        let(:options)      { super().merge(scope: scope) }

        describe 'with a scope that does not match any values' do
          let(:scope_filter)  { -> { { series: 'Mistborn' } } }
          let(:matching_data) { [] }

          include_examples 'should return the matching items'
        end

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

          include_examples 'should return the matching items'

          describe 'with a where filter' do
            let(:filter)  { -> { { author: 'Ursula K. LeGuin' } } }
            let(:options) { super().merge(where: filter) }
            let(:matching_data) do
              super()
                .select { |item| item['author'] == 'Ursula K. LeGuin' }
            end

            include_examples 'should return the matching items'
          end
        end

        describe 'with a scope that matches all values' do
          let(:scope_filter) { -> { { id: not_equal(nil) } } }

          include_examples 'should return the matching items'

          describe 'with a where filter' do
            let(:filter)  { -> { { author: 'Ursula K. LeGuin' } } }
            let(:options) { super().merge(where: filter) }
            let(:matching_data) do
              super()
                .select { |item| item['author'] == 'Ursula K. LeGuin' }
            end

            include_examples 'should return the matching items'
          end
        end
      end
    end
  end
end