Skip to content

Commit

Permalink
Stub the PG::Connection class instead of the migration class
Browse files Browse the repository at this point in the history
Rails 7 changed the way migrations are run.  The migration class
constant is now removed and the file is reloaded between migrations.

This means tests should not use expect/allow on migration stub classes
as the constant stubbed is not the same constant the migration is run against.

Additionally, comparisons of objects created before the migration with ones
after will also not work.  In that case, you can assert by id or other
attributes instead of comparing objects.

Rails also checks association classes are correct and will also not match
expectations.  For example, instead of using host to create or query
the host association, use host_id instead.  This also works with has_manys
such as miq_product_feature_ids.

See https://github.com/rails/rails/pull 42198
  • Loading branch information
jrafanie committed May 29, 2024
1 parent 688323b commit 23b18a0
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
:type => "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::VaultCredential"
)
expect(auth.manager_ref).to be_nil
expect(described_class).to_not receive(:pg_connection)
allow(PG::Connection).to receive(:new).and_call_original
expect(PG::Connection).to_not receive(:new).with(a_hash_including(:dbname => "awx"))

migrate
end
Expand All @@ -108,7 +109,9 @@
:type => "ManageIQ::Providers::AnsibleTower::AutomationManager::GoogleCredential",
:manager_ref => "1"
)
expect(described_class).to_not receive(:pg_connection)

allow(PG::Connection).to receive(:new).and_call_original
expect(PG::Connection).to_not receive(:new).with(a_hash_including(:dbname => "awx"))

migrate
end
Expand All @@ -121,14 +124,17 @@
:options => {:become_method => ""},
:manager_ref => "1"
)
expect(described_class).to receive(:pg_connection).and_raise(PG::ConnectionBad)

allow(PG::Connection).to receive(:new).and_call_original
expect(PG::Connection).to receive(:new).with(a_hash_including(:dbname => "awx")).and_raise(PG::ConnectionBad)

migrate
end

context "with an awx database connection" do
before do
allow(described_class).to receive(:pg_connection).with(a_hash_including(:dbname => "awx")).and_return(awx_conn)
allow(PG::Connection).to receive(:new).and_call_original
allow(PG::Connection).to receive(:new).with(a_hash_including(:dbname => "awx")).and_return(awx_conn)
end

it "removes unwanted data from options when the credential doesn't exist in the awx database" do
Expand Down

0 comments on commit 23b18a0

Please sign in to comment.