123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- RSpec.shared_examples 'ApplicationModel::CanCreatesAndUpdates' do
- describe '.create_if_not_exists' do
- let!(:record) { create(described_class.name.underscore) }
- context 'when given a valid #id' do
- let(:id) { record.id }
- it 'returns that record' do
- expect(described_class.create_if_not_exists(id: id)).to eq(record)
- end
- it 'does not create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_if_not_exists(id: id)
- expect(described_class).not_to have_received(:create).with(id: id)
- end
- end
- context 'when given an invalid #id' do
- let(:id) { described_class.pluck(:id).max + 1 }
- it 'attempts to create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_if_not_exists(id: id)
- expect(described_class).to have_received(:create).with(id: id)
- end
- end
- shared_examples 'for #name attribute' do
- context 'when given a valid #name' do
- let(:name) { record.name }
- it 'returns that record' do
- expect(described_class.create_if_not_exists(name: name)).to eq(record)
- end
- it 'does not create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_if_not_exists(name: name)
- expect(described_class).not_to have_received(:create).with(name: name)
- end
- end
- context 'when given an invalid #name' do
- let(:name) { "#{described_class.pluck(:name).max}foo" }
- it 'attempts to create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_if_not_exists(name: name)
- expect(described_class).to have_received(:create).with(name: name)
- end
- end
- end
- shared_examples 'for #login attribute' do
- context 'when given a valid #login' do
- let(:login) { record.login }
- it 'returns that record' do
- expect(described_class.create_if_not_exists(login: login)).to eq(record)
- end
- it 'does not create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_if_not_exists(login: login)
- expect(described_class).not_to have_received(:create).with(login: login)
- end
- end
- context 'when given an invalid #login' do
- let(:login) { "#{described_class.pluck(:login).max}foo" }
- it 'attempts to create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_if_not_exists(login: login)
- expect(described_class).to have_received(:create).with(login: login)
- end
- end
- end
- shared_examples 'for #email attribute' do
- context 'when given a valid #email' do
- let(:email) { record.email }
- it 'returns that record' do
- expect(described_class.create_if_not_exists(email: email)).to eq(record)
- end
- it 'does not create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_if_not_exists(email: email)
- expect(described_class).not_to have_received(:create).with(email: email)
- end
- end
- context 'when given an invalid #email' do
- let(:email) { "#{described_class.pluck(:email).max}foo" }
- it 'attempts to create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_if_not_exists(email: email)
- expect(described_class).to have_received(:create).with(email: email)
- end
- end
- end
- shared_examples 'for #source and #locale attributes' do
- context 'when given a valid #source and #locale' do
- let(:source) { record.source }
- let(:locale) { record.locale }
- it 'returns that record' do
- expect(described_class.create_if_not_exists(source: source, locale: locale)).to eq(record)
- end
- it 'does not create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_if_not_exists(source: source, locale: locale)
- expect(described_class).not_to have_received(:create).with(source: source, locale: locale)
- end
- end
- context 'when given an invalid #source or #locale' do
- let(:source) { "#{described_class.pluck(:source).max}foo" }
- let(:locale) { record.locale }
- it 'attempts to create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_if_not_exists(source: source, locale: locale)
- expect(described_class).to have_received(:create).with(source: source, locale: locale)
- end
- end
- end
- include_examples 'for #name attribute' if described_class.attribute_names.include?('name')
- include_examples 'for #login attribute' if described_class.attribute_names.include?('login')
- include_examples 'for #email attribute' if described_class.attribute_names.include?('email')
- include_examples 'for #source and #locale attributes' if (described_class.attribute_names & %w[source locale]).many?
- end
- describe '.create_or_update' do
- let!(:record) { create(described_class.name.underscore) }
- let(:yesterday) { 1.day.ago }
- context 'when given a valid #id' do
- let(:id) { record.id }
- it 'updates other attributes on (and returns) that record' do
- expect { described_class.create_or_update(id: id, updated_at: yesterday) }
- .to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
- end
- end
- context 'when given an invalid #id' do
- let(:id) { described_class.pluck(:id).max + 1 }
- it 'attempts to create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_or_update(id: id)
- expect(described_class).to have_received(:create).with(id: id)
- end
- end
- shared_examples 'for #name attribute' do
- context 'when given a valid #name' do
- let(:name) { record.name }
- it 'updates other attributes on (and returns) that record' do
- expect { described_class.create_or_update(name: name, updated_at: yesterday) }
- .to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
- end
- end
- context 'when given an invalid #name' do
- let(:name) { "#{described_class.pluck(:name).max}foo" }
- it 'attempts to create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_or_update(name: name)
- expect(described_class).to have_received(:create).with(name: name)
- end
- end
- end
- shared_examples 'for #login attribute' do
- context 'when given a valid #login' do
- let(:login) { record.login }
- it 'updates other attributes on (and returns) that record' do
- expect { described_class.create_or_update(login: login, updated_at: yesterday) }
- .to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
- end
- end
- context 'when given an invalid #login' do
- let(:login) { "#{described_class.pluck(:login).max}foo" }
- it 'attempts to create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_or_update(login: login)
- expect(described_class).to have_received(:create).with(login: login)
- end
- end
- end
- shared_examples 'for #email attribute' do
- context 'when given a valid #email' do
- let(:email) { record.email }
- it 'updates other attributes on (and returns) that record' do
- expect { described_class.create_or_update(email: email, updated_at: yesterday) }
- .to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
- end
- end
- context 'when given an invalid #email' do
- let(:email) { "#{described_class.pluck(:email).max}foo" }
- it 'attempts to create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_or_update(email: email)
- expect(described_class).to have_received(:create).with(email: email)
- end
- end
- end
- shared_examples 'for #locale attribute' do
- context 'when given a valid #locale' do
- let(:locale) { record.locale }
- it 'updates other attributes on (and returns) that record' do
- expect { described_class.create_or_update(locale: locale, updated_at: yesterday) }
- .to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
- end
- end
- context 'when given an invalid #locale' do
- let(:locale) { record.locale }
- it 'attempts to create a new record' do
- allow(described_class).to receive(:create)
- described_class.create_or_update(locale: locale)
- expect(described_class).to have_received(:create).with(locale: locale)
- end
- end
- end
- include_examples 'for #name attribute' if described_class.attribute_names.include?('name')
- include_examples 'for #login attribute' if described_class.attribute_names.include?('login')
- include_examples 'for #email attribute' if described_class.attribute_names.include?('email')
- include_examples 'for #locale attribute' if described_class.attribute_names.include?('locale')
- end
- end
|