Module: Cuprum::Collections::RSpec::Contracts::CommandContracts::ShouldBeAnInsertOneCommandContract

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

Overview

Contract validating the behavior of an InsertOne 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.



1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
# File 'lib/cuprum/collections/rspec/contracts/command_contracts.rb', line 1191

contract do
  describe '#call' do
    let(:matching_data) { attributes }
    let(:expected_data) do
      defined?(super()) ? super() : matching_data
    end
    let(:primary_key_name) do
      defined?(super()) ? super() : 'id'
    end
    let(:primary_key_type) do
      defined?(super()) ? super() : Integer
    end
    let(:scoped) do
      key    = primary_key_name
      value  = entity[primary_key_name.to_s]

      query.where { { key => value } }
    end

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

    context 'when the item does not exist in the collection' do
      it 'should return a passing result' do
        expect(command.call(entity: entity))
          .to be_a_passing_result
          .with_value(be == expected_data)
      end

      it 'should append an item to the collection' do
        expect { command.call(entity: entity) }
          .to(
            change { query.reset.count }
            .by(1)
          )
      end

      it 'should add the entity to the collection' do
        expect { command.call(entity: entity) }
          .to change(scoped, :exists?)
          .to be true
      end

      it 'should set the attributes' do
        command.call(entity: entity)

        expect(scoped.to_a.first).to be == expected_data
      end
    end

    context 'when the item exists in the collection' do
      let(:data) { fixtures_data }
      let(:expected_error) do
        Cuprum::Collections::Errors::AlreadyExists.new(
          attribute_name:  primary_key_name,
          attribute_value: attributes.fetch(
            primary_key_name.to_s,
            attributes[primary_key_name.intern]
          ),
          collection_name: collection_name,
          primary_key:     true
        )
      end

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

      it 'should not append an item to the collection' do
        expect { command.call(entity: entity) }
          .not_to(change { query.reset.count })
      end
    end
  end
end