Skip to content

Commit

Permalink
Improved read_model tests
Browse files Browse the repository at this point in the history
it was randomly failing on local tests
to avoid destroy_all calls
schemadb changed as we are now on newer Rails
  • Loading branch information
andrzejkrzywda committed Aug 23, 2024
1 parent ff25c5f commit a784dde
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion rails_application/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_08_12_080357) do
ActiveRecord::Schema[7.2].define(version: 2024_08_12_080357) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down
22 changes: 10 additions & 12 deletions rails_application/test/read_model_handler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,43 @@ class ReadModelHandlerTest < InMemoryTestCase
cover "CreateRecord*"
cover "CopyEventAttribute*"

def setup
super
PublicOffer::Product.destroy_all
end

def test_create_record
event = product_registered
event_store.append(event)
previous_count = PublicOffer::Product.count
CreateRecord.new(event_store, PublicOffer::Product, :product_id).call(event)
assert_equal 1, PublicOffer::Product.count
assert_equal(previous_count + 1, PublicOffer::Product.count)
end

def test_dealing_with_at_least_once_delivery
event = product_registered
event_store.append(event)
previous_count = PublicOffer::Product.count
2.times { CreateRecord.new(event_store, PublicOffer::Product, :product_id).call(event) }
assert_equal 1, PublicOffer::Product.count
assert_equal(previous_count + 1, PublicOffer::Product.count)
end

def test_copy
event = product_named
event_store.append(event)
CopyEventAttribute.new(event_store, PublicOffer::Product, :product_id, :name, :name).call(event)
assert_equal product_name, PublicOffer::Product.first.name
assert_equal product_name, PublicOffer::Product.find_by(id: product_id).name
end

def test_copy_nested_attribute
event = vat_rate_set
event_store.append(event)
CopyEventAttribute.new(event_store, Products::Product, :product_id, [:vat_rate, :code], :vat_rate_code).call(event)
assert_equal available_vat_rate.code, Products::Product.first.vat_rate_code
assert_equal available_vat_rate.code, Products::Product.find_by(id: product_id).vat_rate_code
end

def test_no_specific_order_expected
event = product_registered
event_store.append(event)
previous_count = PublicOffer::Product.count
PublicOffer::Product.create(id: product_id)
CreateRecord.new(event_store, PublicOffer::Product, :product_id).call(event)
assert_equal 1, PublicOffer::Product.count
assert_equal(previous_count + 1, PublicOffer::Product.count)
end

def test_updating_with_newest_data
Expand All @@ -54,7 +52,7 @@ def test_updating_with_newest_data
handler.call(second_event)
handler.call(first_event)

assert_equal 'New name', PublicOffer::Product.first.name
assert_equal 'New name', PublicOffer::Product.find_by(id: product_id).name
end

def test_updating_with_newest_data_concurrently
Expand All @@ -74,7 +72,7 @@ def test_updating_with_newest_data_concurrently
end
wait_for_it = false
threads.each(&:join)
assert_equal 'New name', PublicOffer::Product.first.name
assert_equal 'New name', PublicOffer::Product.find_by(id: product_id).name
ensure
ActiveRecord::Base.connection_pool.disconnect!
end
Expand Down

0 comments on commit a784dde

Please sign in to comment.