Module: Cuprum::Collections::RSpec::Contracts::RelationContracts::ShouldDefinePrimaryKeysContract

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

Overview

Contract validating a Relation’s primary key properties.

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.



1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
# File 'lib/cuprum/collections/rspec/contracts/relation_contracts.rb', line 1325

contract do
  describe '#primary_key_name' do
    let(:expected_primary_key_name) do
      return super() if defined?(super())

      constructor_options.fetch(:primary_key_name, 'id')
    end

    include_examples 'should define reader',
      :primary_key_name,
      -> { expected_primary_key_name }

    context 'when initialized with primary_key_name: a String' do
      let(:primary_key_name) { 'uuid' }
      let(:constructor_options) do
        super().merge(primary_key_name: primary_key_name)
      end

      it { expect(subject.primary_key_name).to be == primary_key_name }
    end

    context 'when initialized with primary_key_name: a Symbol' do
      let(:primary_key_name) { :uuid }
      let(:constructor_options) do
        super().merge(primary_key_name: primary_key_name)
      end

      it 'should set the primary key name' do
        expect(subject.primary_key_name).to be == primary_key_name.to_s
      end
    end
  end

  describe '#primary_key_type' do
    let(:expected_primary_key_type) do
      return super() if defined?(super())

      constructor_options.fetch(:primary_key_type, Integer)
    end

    include_examples 'should define reader',
      :primary_key_type,
      -> { expected_primary_key_type }

    context 'when initialized with primary_key_type: value' do
      let(:primary_key_type) { String }
      let(:constructor_options) do
        super().merge(primary_key_type: primary_key_type)
      end

      it { expect(subject.primary_key_type).to be == primary_key_type }
    end
  end
end