Module: Cuprum::Collections::RSpec::Contracts::CommandContracts::ShouldBeADestroyOneCommandContract

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.



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

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

    it 'should validate the :primary_key keyword' do
      expect(command)
        .to validate_parameter(:call, :primary_key)
        .using_constraint(primary_key_type)
    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: 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

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

    context 'when the collection has many items' do
      let(:data) { fixtures_data }
      let(:matching_data) do
        mapped_data.find do |item|
          item[primary_key_name.to_s] == primary_key
        end
      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: 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

        it 'should not remove an entity from the collection' do
          expect { command.call(primary_key: primary_key) }
            .not_to(change { query.reset.count })
        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

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

        it 'should remove the entity from the collection' do
          command.call(primary_key: primary_key)

          expect(query.map { |item| item[primary_key_name.to_s] })
            .not_to include primary_key
        end
      end
    end
  end
end