Skip to content

Commit

Permalink
More tests for basic features common to all fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmhaig committed Nov 17, 2023
1 parent 9f7df72 commit 040b667
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ coverage
.idea
gemfiles/*.lock
.ruby-version
tmp/*
78 changes: 76 additions & 2 deletions spec/lib/amoeba/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,38 @@
end
end

RSpec.shared_examples 'can have a nullified field' do
let(:config) { 'amoeba { nullify :test_field }' }

context 'with a field that can be null' do
let(:db_config) { { null: true } }

it { is_expected.to be_nil }
end

context 'with a field that cannot be null and has a default' do
let(:db_config) { { null: false, default: new_value } }

before { pending 'Bug. See https://github.com/amoeba-rb/amoeba/issues/106' }

it { is_expected.to eq new_value }
end
end

RSpec.shared_examples 'can have a field set' do
let(:config) { "amoeba { set test_field: '#{new_value}' }" }

context 'with the field set to a value' do
it { is_expected.to eq new_value }
end

context 'with the field set to nil' do
let(:test_model) { TestModel.create(test_field: nil) }

it { is_expected.to eq new_value }
end
end

RSpec.describe Amoeba::Config do
subject { original.amoeba_dup.test_field }

Expand All @@ -33,84 +65,126 @@
context 'with a string field' do
let(:field) { :string }
let(:value) { 'Test string' }
let(:new_value) { 'abc ' }

it_behaves_like 'can copy field without modification'
it_behaves_like 'can have a nullified field'
it_behaves_like 'can have a field set'
end

context 'with a text field' do
let(:field) { :text }
let(:value) { 'Test text' }
let(:new_value) { 'abc' }

it_behaves_like 'can copy field without modification'
it_behaves_like 'can have a nullified field'
it_behaves_like 'can have a field set'
end

context 'with an integer field' do
let(:field) { :integer }
let(:value) { 3 }
let(:new_value) { 9 }

it_behaves_like 'can copy field without modification'
it_behaves_like 'can have a nullified field'
it_behaves_like 'can have a field set'
end

context 'with a big integer field' do
let(:field) { :bigint }
let(:value) { 3_333_333_333 }
let(:new_value) { 9_999_999_999 }

it_behaves_like 'can copy field without modification'
it_behaves_like 'can have a nullified field'
it_behaves_like 'can have a field set'
end

context 'with a float field' do
let(:field) { :float }
let(:value) { 3.14159 }
let(:new_value) { 1.14121 }

it_behaves_like 'can copy field without modification'
it_behaves_like 'can have a nullified field'
it_behaves_like 'can have a field set'
end

context 'with a decimal field' do
let(:field) { :decimal }
let(:value) { 3.14 }
let(:new_value) { 1.14 }

it_behaves_like 'can copy field without modification'
it_behaves_like 'can have a nullified field'
it_behaves_like 'can have a field set'
end

context 'with a numeric field' do
let(:field) { :numeric }
let(:value) { 3.14 }
let(:new_value) { 1.14 }

it_behaves_like 'can copy field without modification'
it_behaves_like 'can have a nullified field'
it_behaves_like 'can have a field set'
end

context 'with a datetime field' do
let(:field) { :datetime }
let(:value) { DateTime.parse('2 October 2023 15:57') }
let(:new_value) { DateTime.parse('3 November 2024 17:01') }

it_behaves_like 'can copy field without modification'
it_behaves_like 'can have a nullified field'
it_behaves_like 'can have a field set'
end

context 'with a time field' do
let(:field) { :time }
let(:value) { DateTime.parse('2 October 2023 15:57') }
let(:value) { Time.parse('15:57') }
let(:new_value) { Time.parse('17:01') }

it_behaves_like 'can copy field without modification'
it_behaves_like 'can have a nullified field'
it_behaves_like 'can have a field set' do
# I'm not sure if this is a bug or not. If the field is has a
# `set time_field: Time.new(...)` then the duplicate has the field
# set with the correct time on 1 January 1970 while setting it on a new
# instance has it on the current day.
before { skip 'Check issue with setting time field' }
end
end

context 'with a date field' do
let(:field) { :date }
let(:value) { DateTime.parse('2 October 2023') }
let(:value) { Date.parse('2 October 2023') }
let(:new_value) { Date.parse('3 November 2024') }

it_behaves_like 'can copy field without modification'
it_behaves_like 'can have a nullified field'
it_behaves_like 'can have a field set'
end

context 'with a binary field' do
let(:field) { :binary }
let(:value) { 'abcdefghijklmnopqrstuvwxyz' }
let(:new_value) { 'zyxwvutsrqponmlkjihgfedcba' }

it_behaves_like 'can copy field without modification'
it_behaves_like 'can have a nullified field'
it_behaves_like 'can have a field set'
end

context 'with a boolean field' do
let(:field) { :boolean }
let(:value) { false }
let(:new_value) { true }

it_behaves_like 'can copy field without modification'
it_behaves_like 'can have a nullified field'
it_behaves_like 'can have a field set'
end
end

0 comments on commit 040b667

Please sign in to comment.