Skip to content

Commit

Permalink
transaction write tests only creates tables when they are needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ckhsponge committed Jan 17, 2024
1 parent d9ee674 commit 994a4db
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 195 deletions.
2 changes: 2 additions & 0 deletions spec/dynamoid/transaction_write/build_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

context "incrementally builds" do
it "executes" do
klass.create_table
klass_with_composite_key.create_table
transaction = Dynamoid::TransactionWrite.new
obj1 = transaction.create!(klass, { name: "one" })
obj2_id = SecureRandom.uuid
Expand Down
6 changes: 0 additions & 6 deletions spec/dynamoid/transaction_write/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,4 @@
end
end

before do
klass.create_table
klass_with_composite_key.create_table
klass_with_callback.create_table
klass_with_validation.create_table
end
end
105 changes: 59 additions & 46 deletions spec/dynamoid/transaction_write/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

context 'creates' do
context 'simple primary key' do
before do
klass.create_table
end
it 'with attribute in constructor' do
obj1 = klass.new(name: "one")
expect(obj1.persisted?).to eql(false)
Expand Down Expand Up @@ -44,6 +47,9 @@
end

context 'composite key' do
before do
klass_with_composite_key.create_table
end
it 'with attribute in constructor' do
obj1 = klass_with_composite_key.new(name: "one", age: 1)
Dynamoid::TransactionWrite.execute do |txn|
Expand Down Expand Up @@ -74,6 +80,7 @@
end

it 'creates timestamps' do
klass.create_table
obj1 = klass.new(name: "one")
expect(obj1.created_at).to be_nil
expect(obj1.updated_at).to be_nil
Expand All @@ -85,67 +92,73 @@
expect(obj1_found.updated_at.to_f).to be_within(1.seconds).of Time.now.to_f
end

it 'does not create when invalid' do
obj1 = klass_with_validation.new(name: "one")
obj2 = klass_with_validation.new(name: "twotwo")
Dynamoid::TransactionWrite.execute do |txn|
expect(txn.create(obj1)).to eql(false)
expect(txn.create(obj2)).to be_present
context 'validates' do
before do
klass_with_validation.create_table
end
it 'does not create when invalid' do
obj1 = klass_with_validation.new(name: "one")
obj2 = klass_with_validation.new(name: "twotwo")
Dynamoid::TransactionWrite.execute do |txn|
expect(txn.create(obj1)).to eql(false)
expect(txn.create(obj2)).to be_present
end
expect(obj1.id).to be_present
expect(klass_with_validation.exists?(obj1.id)).to be_falsey
expect(klass_with_validation.exists?(obj2.id)).to be_truthy
end
expect(obj1.id).to be_present
expect(klass_with_validation.exists?(obj1.id)).to be_falsey
expect(klass_with_validation.exists?(obj2.id)).to be_truthy
end

it 'succeeds when valid' do
obj1 = klass_with_validation.new(name: "oneone")
Dynamoid::TransactionWrite.execute do |txn|
expect(txn.create(obj1)).to be_present
it 'succeeds when valid' do
obj1 = klass_with_validation.new(name: "oneone")
Dynamoid::TransactionWrite.execute do |txn|
expect(txn.create(obj1)).to be_present
end

obj1_found = klass_with_validation.find(obj1.id)
expect(obj1_found).to eql(obj1)
expect(obj1_found.name).to eql("oneone")
end

obj1_found = klass_with_validation.find(obj1.id)
expect(obj1_found).to eql(obj1)
expect(obj1_found.name).to eql("oneone")
end
it 'raises DocumentNotValid when not valid' do
obj1 = klass_with_validation.new(name: "one")
obj2 = klass_with_validation.new(name: "twotwo")
expect {
Dynamoid::TransactionWrite.execute do |txn|
txn.create! obj2
txn.create! obj1
end
}.to raise_error(Dynamoid::Errors::DocumentNotValid)
expect(obj1.id).to be_present
expect(klass_with_validation.exists?(obj1.id)).to be_falsey
expect(obj2.id).to be_present
expect(klass_with_validation.exists?(obj2.id)).to be_falsey
end

it 'raises DocumentNotValid when not valid' do
obj1 = klass_with_validation.new(name: "one")
obj2 = klass_with_validation.new(name: "twotwo")
expect {
it 'does not raise exception when valid' do
obj1 = klass_with_validation.new(name: "oneone")
Dynamoid::TransactionWrite.execute do |txn|
txn.create! obj2
txn.create! obj1
txn.create!(obj1)
end
}.to raise_error(Dynamoid::Errors::DocumentNotValid)
expect(obj1.id).to be_present
expect(klass_with_validation.exists?(obj1.id)).to be_falsey
expect(obj2.id).to be_present
expect(klass_with_validation.exists?(obj2.id)).to be_falsey
end

it 'does not raise exception when valid' do
obj1 = klass_with_validation.new(name: "oneone")
Dynamoid::TransactionWrite.execute do |txn|
txn.create!(obj1)
obj1_found = klass_with_validation.find(obj1.id)
expect(obj1_found).to eql(obj1)
expect(obj1_found.name).to eql("oneone")
end

obj1_found = klass_with_validation.find(obj1.id)
expect(obj1_found).to eql(obj1)
expect(obj1_found.name).to eql("oneone")
end
it 'does not raise exception when skipping validation' do
obj1 = klass_with_validation.new(name: "one")
Dynamoid::TransactionWrite.execute do |txn|
txn.create!(obj1, options: {skip_validation: true})
end

it 'does not raise exception when skipping validation' do
obj1 = klass_with_validation.new(name: "one")
Dynamoid::TransactionWrite.execute do |txn|
txn.create!(obj1, options: {skip_validation: true})
obj1_found = klass_with_validation.find(obj1.id)
expect(obj1_found).to eql(obj1)
expect(obj1_found.name).to eql("one")
end

obj1_found = klass_with_validation.find(obj1.id)
expect(obj1_found).to eql(obj1)
expect(obj1_found.name).to eql("one")
end

it 'uses callbacks' do
klass_with_callback.create_table
obj2 = klass_with_callback.new(name: "two")
Dynamoid::TransactionWrite.execute do |txn|
txn.create! obj2, {says_more: "bark2"}
Expand Down
6 changes: 6 additions & 0 deletions spec/dynamoid/transaction_write/delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

context 'deletes' do
context 'simple primary key' do
before do
klass.create_table
end
it 'with instance' do
obj1 = klass.create!(name: "one")
obj1_found = klass.find(obj1.id)
Expand All @@ -33,6 +36,9 @@
end

context 'composite key' do
before do
klass_with_composite_key.create_table
end
it 'with instance' do
obj1 = klass_with_composite_key.create!(name: "one", age: 1)
obj1_found = klass_with_composite_key.find(obj1.id, range_key: 1)
Expand Down
7 changes: 7 additions & 0 deletions spec/dynamoid/transaction_write/destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

context 'destroys' do
context 'simple primary key' do
before do
klass.create_table
end
it 'with instance' do
obj1 = klass.create!(name: "one")
obj1_found = klass.find(obj1.id)
Expand All @@ -37,6 +40,9 @@
end

context 'composite key' do
before do
klass_with_composite_key.create_table
end
it 'with instance' do
obj1 = klass_with_composite_key.create!(name: "one", age: 1)
obj2 = klass_with_composite_key.create!(name: "two", age: 2)
Expand Down Expand Up @@ -73,6 +79,7 @@
end

it 'uses callbacks' do
klass_with_callback.create_table
obj1 = klass_with_callback.create!(name: "one", says_more: "bark1")
Dynamoid::TransactionWrite.execute do |txn|
txn.destroy! obj1
Expand Down
6 changes: 6 additions & 0 deletions spec/dynamoid/transaction_write/put_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
# a 'put' is a create that overwrites existing records if present
context 'puts' do
context 'simple primary key' do
before do
klass.create_table
end
it 'fails without skip_existence_check' do
obj1 = klass.create!(name: "one")
expect {
Expand Down Expand Up @@ -44,6 +47,9 @@
end

context 'composite key' do
before do
klass_with_composite_key.create_table
end
it 'fails without skip_existence_check' do
obj1 = klass_with_composite_key.create!(name: "one", age: 1)
expect {
Expand Down
73 changes: 42 additions & 31 deletions spec/dynamoid/transaction_write/save_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
# a 'save' does an update or create depending on if the record is new or not
context 'saves' do
context 'simple primary key' do
before do
klass.create_table
end
it 'with an update' do
obj1 = klass.create!(name: "one")
Dynamoid::TransactionWrite.execute do |txn|
Expand All @@ -36,6 +39,9 @@


context 'composite key' do
before do
klass_with_composite_key.create_table
end
it 'with an update' do
obj1 = klass_with_composite_key.create!(name: "one", age: 1)
Dynamoid::TransactionWrite.execute do |txn|
Expand All @@ -57,46 +63,51 @@
end
end

it 'does not save when invalid' do
obj1 = klass_with_validation.new(name: "one")
Dynamoid::TransactionWrite.execute do |txn|
expect(txn.save(obj1)).to eql(false)
context 'validates' do
before do
klass_with_validation.create_table
end
expect(obj1.id).to be_present
expect(klass_with_validation.exists?(obj1.id)).to be_falsey
end
it 'does not save when invalid' do
obj1 = klass_with_validation.new(name: "one")
Dynamoid::TransactionWrite.execute do |txn|
expect(txn.save(obj1)).to eql(false)
end
expect(obj1.id).to be_present
expect(klass_with_validation.exists?(obj1.id)).to be_falsey
end

it 'saves when valid' do
obj1 = klass_with_validation.new(name: "oneone")
Dynamoid::TransactionWrite.execute do |txn|
expect(txn.save(obj1)).to be_present
end

it 'saves when valid' do
obj1 = klass_with_validation.new(name: "oneone")
Dynamoid::TransactionWrite.execute do |txn|
expect(txn.save(obj1)).to be_present
obj1_found = klass_with_validation.find(obj1.id)
expect(obj1_found).to eql(obj1)
expect(obj1_found.name).to eql("oneone")
end

obj1_found = klass_with_validation.find(obj1.id)
expect(obj1_found).to eql(obj1)
expect(obj1_found.name).to eql("oneone")
end
it 'raises DocumentNotValid when not valid' do
obj1 = klass_with_validation.new(name: "one")
expect {
Dynamoid::TransactionWrite.execute do |txn|
txn.save! obj1
end
}.to raise_error(Dynamoid::Errors::DocumentNotValid)
expect(obj1.id).to be_present
expect(klass_with_validation.exists?(obj1.id)).to be_falsey
end

it 'raises DocumentNotValid when not valid' do
obj1 = klass_with_validation.new(name: "one")
expect {
it 'does not raise exception when valid' do
obj1 = klass_with_validation.new(name: "oneone")
Dynamoid::TransactionWrite.execute do |txn|
txn.save! obj1
txn.save!(obj1)
end
}.to raise_error(Dynamoid::Errors::DocumentNotValid)
expect(obj1.id).to be_present
expect(klass_with_validation.exists?(obj1.id)).to be_falsey
end

it 'does not raise exception when valid' do
obj1 = klass_with_validation.new(name: "oneone")
Dynamoid::TransactionWrite.execute do |txn|
txn.save!(obj1)
obj1_found = klass_with_validation.find(obj1.id)
expect(obj1_found).to eql(obj1)
expect(obj1_found.name).to eql("oneone")
end

obj1_found = klass_with_validation.find(obj1.id)
expect(obj1_found).to eql(obj1)
expect(obj1_found.name).to eql("oneone")
end
end
end
Expand Down
Loading

0 comments on commit 994a4db

Please sign in to comment.