diff --git a/.github/workflows/rails_application-coverage.yml b/.github/workflows/rails_application-coverage.yml index 8f2ea509..a5222f01 100644 --- a/.github/workflows/rails_application-coverage.yml +++ b/.github/workflows/rails_application-coverage.yml @@ -14,6 +14,7 @@ on: paths: - "apps/rails_application/Gemfile.lock" - ".github/workflows/rails_application-coverage.yml" + - "apps/rails_application/test/test_helper.rb" schedule: - cron: '0 17 * * *' jobs: @@ -59,8 +60,15 @@ jobs: ruby-version: ruby-3.4.6 bundler-cache: true working-directory: apps/rails_application + - name: Prepare test database + run: bin/rails db:test:prepare + working-directory: apps/rails_application + env: + DATABASE_URL: "postgres://postgres:secret@localhost:5432/cqrs-es-sample-with-res_test" + RAILS_ENV: test - name: Run full mutation testing - run: make -C apps/rails_application mutate + run: RAILS_ENV=test bundle exec mutant run --jobs 1 + working-directory: apps/rails_application env: DATABASE_URL: "postgres://postgres:secret@localhost:5432/cqrs-es-sample-with-res_test" RAILS_ENV: test diff --git a/apps/rails_application/Makefile b/apps/rails_application/Makefile index 3898e74f..f5212855 100644 --- a/apps/rails_application/Makefile +++ b/apps/rails_application/Makefile @@ -7,6 +7,8 @@ dev: @$(MAKE) -j 2 web css mutate: ## Run full mutation tests + @echo "Preparing test database..." + @RAILS_ENV=test bin/rails db:test:prepare @echo "Running full mutation tests..." @RAILS_ENV=test bundle exec mutant run diff --git a/apps/rails_application/test/invoices/assign_store_to_invoice_test.rb b/apps/rails_application/test/invoices/assign_store_to_invoice_test.rb index 439deb91..4768a44b 100644 --- a/apps/rails_application/test/invoices/assign_store_to_invoice_test.rb +++ b/apps/rails_application/test/invoices/assign_store_to_invoice_test.rb @@ -91,5 +91,37 @@ def test_assigns_store_to_correct_invoice_when_multiple_invoices_exist assert_equal(store_id_1, invoice_1.store_id) assert_equal(store_id_2, invoice_2.store_id) end + + def test_returns_nil_when_invoice_exists_in_different_store + event_store = Rails.configuration.event_store + store_id_1 = SecureRandom.uuid + store_id_2 = SecureRandom.uuid + invoice_id = SecureRandom.uuid + + event_store.publish( + Invoicing::InvoiceItemAdded.new( + data: { + invoice_id: invoice_id, + product_id: SecureRandom.uuid, + title: "Test Product", + quantity: 1, + unit_price: 100, + vat_rate: { rate: 20, code: "VAT" } + } + ) + ) + + event_store.publish( + Stores::InvoiceRegistered.new( + data: { + store_id: store_id_1, + invoice_id: invoice_id + } + ) + ) + + assert_nil Invoices.find_invoice_in_store(invoice_id, store_id_2) + assert_not_nil Invoices.find_invoice_in_store(invoice_id, store_id_1) + end end end diff --git a/apps/rails_application/test/test_helper.rb b/apps/rails_application/test/test_helper.rb index 5f5bfb79..1bbc3e48 100644 --- a/apps/rails_application/test/test_helper.rb +++ b/apps/rails_application/test/test_helper.rb @@ -142,6 +142,7 @@ def before_setup Configuration.new.call(Rails.configuration.event_store, Rails.configuration.command_bus) @default_store_id = ensure_default_store + cookies[:current_store_id] = @default_store_id result end