Module: Cuprum::Collections::RSpec::Contracts::CommandContracts::ShouldBeAFindOneCommandContract

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

Overview

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



1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
# File 'lib/cuprum/collections/rspec/contracts/command_contracts.rb', line 1029

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(:invalid_primary_key_value) do
      defined?(super()) ? super() : 100
    end
    let(:valid_primary_key_value) do
      defined?(super()) ? super() : 0
    end

    def tools
      SleepingKingStudios::Tools::Toolbelt.instance
    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_key keyword' do
      expect(command)
        .to validate_parameter(:call, :primary_key)
        .using_constraint(primary_key_type)
    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 invalid primary key' do
      let(:primary_key) { invalid_primary_key_value }
      let(:expected_error) do
        Cuprum::Collections::Errors::NotFound.new(
          attribute_name:  primary_key_name,
          attribute_value: primary_key,
          collection_name: command.collection_name,
          primary_key:     true
        )
      end

      it 'should return a failing result' do
        expect(command.call(primary_key: primary_key))
          .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
        mapped_data
          .find { |item| item[primary_key_name.to_s] == primary_key }
      end
      let(:expected_data) do
        defined?(super()) ? super() : matching_data
      end

      describe 'with an invalid primary key' do
        let(:primary_key) { invalid_primary_key_value }
        let(:expected_error) do
          Cuprum::Collections::Errors::NotFound.new(
            attribute_name:  primary_key_name,
            attribute_value: primary_key,
            collection_name: command.collection_name,
            primary_key:     true
          )
        end

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

      describe 'with a valid primary key' do
        let(:primary_key) { valid_primary_key_value }

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

      describe 'with envelope: true' do
        let(:member_name) { tools.str.singularize(collection_name) }

        describe 'with a valid primary key' do
          let(:primary_key) { valid_primary_key_value }

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

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

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

          describe 'with an valid primary key' do
            let(:primary_key) { valid_primary_key_value }
            let(:expected_error) do
              Cuprum::Collections::Errors::NotFound.new(
                attribute_name:  primary_key_name,
                attribute_value: primary_key,
                collection_name: command.collection_name,
                primary_key:     true
              )
            end

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

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

          describe 'with a valid primary key' do
            let(:primary_key) { valid_primary_key_value }

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