Skip to content

Commit

Permalink
Workaround Rails 7 raising type mismatch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jrafanie committed May 20, 2024
1 parent 4e6d91e commit 9a40d34
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 60 deletions.
8 changes: 4 additions & 4 deletions spec/migrations/20190729170013_split_storages_per_ems_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Host < ActiveRecord::Base

it "host is still linked to the storage" do
migrate
expect(host.reload.storages.first).to eq(storage_stub.first)
expect(host.reload.storages.first.id).to eq(storage_stub.first.id)
end
end

Expand All @@ -62,8 +62,8 @@ class Host < ActiveRecord::Base
migrate

storage = storage_stub.first
expect(host_1.reload.storages.first).to eq(storage)
expect(host_2.reload.storages.first).to eq(storage)
expect(host_1.reload.storages.first.id).to eq(storage.id)
expect(host_2.reload.storages.first.id).to eq(storage.id)
end
end

Expand Down Expand Up @@ -150,7 +150,7 @@ class Host < ActiveRecord::Base

it "links hosts to the old storage" do
migrate
expect(host.reload.host_storages.first.storage).to eq(storage)
expect(host.reload.host_storages.first.storage.id).to eq(storage.id)
end

it "sets the ems_ref" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
migrate
user_role.reload

expect(user_role.miq_product_features).not_to include(automation_manager)
expect(user_role.miq_product_features).to include(ems_automation)
expect(user_role.miq_product_features).to include(automation_manager_configured_system)
expect(user_role.miq_product_features).to include(configuration_script)
expect(user_role.miq_product_feature_ids).not_to include(automation_manager.id)
expect(user_role.miq_product_feature_ids).to include(ems_automation.id)
expect(user_role.miq_product_feature_ids).to include(automation_manager_configured_system.id)
expect(user_role.miq_product_feature_ids).to include(configuration_script.id)
end
end

Expand Down Expand Up @@ -85,10 +85,10 @@
migrate
user_role.reload

expect(user_role.miq_product_features).to include(automation_manager)
expect(user_role.miq_product_features).not_to include(ems_automation)
expect(user_role.miq_product_features).not_to include(automation_manager_configured_system)
expect(user_role.miq_product_features).not_to include(configuration_script)
expect(user_role.miq_product_feature_ids).to include(automation_manager.id)
expect(user_role.miq_product_feature_ids).not_to include(ems_automation.id)
expect(user_role.miq_product_feature_ids).not_to include(automation_manager_configured_system.id)
expect(user_role.miq_product_feature_ids).not_to include(configuration_script.id)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

migrate

expect(container_template.reload.type).to eq("ManageIQ::Providers::Amazon::ContainerManager::ContainerTemplate")
expect(service_instance.reload.type).to eq("ManageIQ::Providers::Amazon::ContainerManager::ServiceInstance")
expect(service_offering.reload.type).to eq("ManageIQ::Providers::Amazon::ContainerManager::ServiceOffering")
expect(service_parameters_set.reload.type).to eq("ManageIQ::Providers::Amazon::ContainerManager::ServiceParametersSet")
expect(container_template_stub.first.type).to eq("ManageIQ::Providers::Amazon::ContainerManager::ContainerTemplate")
expect(service_instance_stub.first.type).to eq("ManageIQ::Providers::Amazon::ContainerManager::ServiceInstance")
expect(service_offering_stub.first.type).to eq("ManageIQ::Providers::Amazon::ContainerManager::ServiceOffering")
expect(service_parameters_set_stub.first.type).to eq("ManageIQ::Providers::Amazon::ContainerManager::ServiceParametersSet")
end

it "fixes STI class for AKS providers" do
Expand All @@ -36,10 +36,10 @@

migrate

expect(container_template.reload.type).to eq("ManageIQ::Providers::Azure::ContainerManager::ContainerTemplate")
expect(service_instance.reload.type).to eq("ManageIQ::Providers::Azure::ContainerManager::ServiceInstance")
expect(service_offering.reload.type).to eq("ManageIQ::Providers::Azure::ContainerManager::ServiceOffering")
expect(service_parameters_set.reload.type).to eq("ManageIQ::Providers::Azure::ContainerManager::ServiceParametersSet")
expect(container_template_stub.first.type).to eq("ManageIQ::Providers::Azure::ContainerManager::ContainerTemplate")
expect(service_instance_stub.first.type).to eq("ManageIQ::Providers::Azure::ContainerManager::ServiceInstance")
expect(service_offering_stub.first.type).to eq("ManageIQ::Providers::Azure::ContainerManager::ServiceOffering")
expect(service_parameters_set_stub.first.type).to eq("ManageIQ::Providers::Azure::ContainerManager::ServiceParametersSet")
end

it "fixes STI class for GKE providers" do
Expand All @@ -52,10 +52,10 @@

migrate

expect(container_template.reload.type).to eq("ManageIQ::Providers::Google::ContainerManager::ContainerTemplate")
expect(service_instance.reload.type).to eq("ManageIQ::Providers::Google::ContainerManager::ServiceInstance")
expect(service_offering.reload.type).to eq("ManageIQ::Providers::Google::ContainerManager::ServiceOffering")
expect(service_parameters_set.reload.type).to eq("ManageIQ::Providers::Google::ContainerManager::ServiceParametersSet")
expect(container_template_stub.first.type).to eq("ManageIQ::Providers::Google::ContainerManager::ContainerTemplate")
expect(service_instance_stub.first.type).to eq("ManageIQ::Providers::Google::ContainerManager::ServiceInstance")
expect(service_offering_stub.first.type).to eq("ManageIQ::Providers::Google::ContainerManager::ServiceOffering")
expect(service_parameters_set_stub.first.type).to eq("ManageIQ::Providers::Google::ContainerManager::ServiceParametersSet")
end

it "fixes STI class for IKS providers" do
Expand All @@ -68,10 +68,10 @@

migrate

expect(container_template.reload.type).to eq("ManageIQ::Providers::IbmCloud::ContainerManager::ContainerTemplate")
expect(service_instance.reload.type).to eq("ManageIQ::Providers::IbmCloud::ContainerManager::ServiceInstance")
expect(service_offering.reload.type).to eq("ManageIQ::Providers::IbmCloud::ContainerManager::ServiceOffering")
expect(service_parameters_set.reload.type).to eq("ManageIQ::Providers::IbmCloud::ContainerManager::ServiceParametersSet")
expect(container_template_stub.first.type).to eq("ManageIQ::Providers::IbmCloud::ContainerManager::ContainerTemplate")
expect(service_instance_stub.first.type).to eq("ManageIQ::Providers::IbmCloud::ContainerManager::ServiceInstance")
expect(service_offering_stub.first.type).to eq("ManageIQ::Providers::IbmCloud::ContainerManager::ServiceOffering")
expect(service_parameters_set_stub.first.type).to eq("ManageIQ::Providers::IbmCloud::ContainerManager::ServiceParametersSet")
end

it "fixes STI class for OKE providers" do
Expand All @@ -84,10 +84,10 @@

migrate

expect(container_template.reload.type).to eq("ManageIQ::Providers::OracleCloud::ContainerManager::ContainerTemplate")
expect(service_instance.reload.type).to eq("ManageIQ::Providers::OracleCloud::ContainerManager::ServiceInstance")
expect(service_offering.reload.type).to eq("ManageIQ::Providers::OracleCloud::ContainerManager::ServiceOffering")
expect(service_parameters_set.reload.type).to eq("ManageIQ::Providers::OracleCloud::ContainerManager::ServiceParametersSet")
expect(container_template_stub.first.type).to eq("ManageIQ::Providers::OracleCloud::ContainerManager::ContainerTemplate")
expect(service_instance_stub.first.type).to eq("ManageIQ::Providers::OracleCloud::ContainerManager::ServiceInstance")
expect(service_offering_stub.first.type).to eq("ManageIQ::Providers::OracleCloud::ContainerManager::ServiceOffering")
expect(service_parameters_set_stub.first.type).to eq("ManageIQ::Providers::OracleCloud::ContainerManager::ServiceParametersSet")
end

it "fixes STI class for Tanzu providers" do
Expand All @@ -100,10 +100,10 @@

migrate

expect(container_template.reload.type).to eq("ManageIQ::Providers::Vmware::ContainerManager::ContainerTemplate")
expect(service_instance.reload.type).to eq("ManageIQ::Providers::Vmware::ContainerManager::ServiceInstance")
expect(service_offering.reload.type).to eq("ManageIQ::Providers::Vmware::ContainerManager::ServiceOffering")
expect(service_parameters_set.reload.type).to eq("ManageIQ::Providers::Vmware::ContainerManager::ServiceParametersSet")
expect(container_template_stub.first.type).to eq("ManageIQ::Providers::Vmware::ContainerManager::ContainerTemplate")
expect(service_instance_stub.first.type).to eq("ManageIQ::Providers::Vmware::ContainerManager::ServiceInstance")
expect(service_offering_stub.first.type).to eq("ManageIQ::Providers::Vmware::ContainerManager::ServiceOffering")
expect(service_parameters_set_stub.first.type).to eq("ManageIQ::Providers::Vmware::ContainerManager::ServiceParametersSet")
end
end

Expand All @@ -118,10 +118,10 @@

migrate

expect(container_template.reload.type).to be_nil
expect(service_instance.reload.type).to be_nil
expect(service_offering.reload.type).to be_nil
expect(service_parameters_set.reload.type).to be_nil
expect(container_template_stub.first.type).to be_nil
expect(service_instance_stub.first.type).to be_nil
expect(service_offering_stub.first.type).to be_nil
expect(service_parameters_set_stub.first.type).to be_nil
end

it "resets STI class for AKS providers" do
Expand All @@ -134,10 +134,10 @@

migrate

expect(container_template.reload.type).to be_nil
expect(service_instance.reload.type).to be_nil
expect(service_offering.reload.type).to be_nil
expect(service_parameters_set.reload.type).to be_nil
expect(container_template_stub.first.type).to be_nil
expect(service_instance_stub.first.type).to be_nil
expect(service_offering_stub.first.type).to be_nil
expect(service_parameters_set_stub.first.type).to be_nil
end

it "resets STI class for GKE providers" do
Expand All @@ -150,10 +150,10 @@

migrate

expect(container_template.reload.type).to be_nil
expect(service_instance.reload.type).to be_nil
expect(service_offering.reload.type).to be_nil
expect(service_parameters_set.reload.type).to be_nil
expect(container_template_stub.first.type).to be_nil
expect(service_instance_stub.first.type).to be_nil
expect(service_offering_stub.first.type).to be_nil
expect(service_parameters_set_stub.first.type).to be_nil
end

it "resets STI class for IKS providers" do
Expand All @@ -166,10 +166,10 @@

migrate

expect(container_template.reload.type).to be_nil
expect(service_instance.reload.type).to be_nil
expect(service_offering.reload.type).to be_nil
expect(service_parameters_set.reload.type).to be_nil
expect(container_template_stub.first.type).to be_nil
expect(service_instance_stub.first.type).to be_nil
expect(service_offering_stub.first.type).to be_nil
expect(service_parameters_set_stub.first.type).to be_nil
end

it "resets STI class for OKE providers" do
Expand All @@ -182,10 +182,10 @@

migrate

expect(container_template.reload.type).to be_nil
expect(service_instance.reload.type).to be_nil
expect(service_offering.reload.type).to be_nil
expect(service_parameters_set.reload.type).to be_nil
expect(container_template_stub.first.type).to be_nil
expect(service_instance_stub.first.type).to be_nil
expect(service_offering_stub.first.type).to be_nil
expect(service_parameters_set_stub.first.type).to be_nil
end

it "resets STI class for Tanzu providers" do
Expand All @@ -198,10 +198,10 @@

migrate

expect(container_template.reload.type).to be_nil
expect(service_instance.reload.type).to be_nil
expect(service_offering.reload.type).to be_nil
expect(service_parameters_set.reload.type).to be_nil
expect(container_template_stub.first.type).to be_nil
expect(service_instance_stub.first.type).to be_nil
expect(service_offering_stub.first.type).to be_nil
expect(service_parameters_set_stub.first.type).to be_nil
end
end
end
9 changes: 9 additions & 0 deletions spec/support/type_mismatch_expected_in_migration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module TypeMismatchExpectedInMigrationSpec
# Rails 7 now calls this from the klass method, forcing association classes to be a type of the freshly loaded class
# For migration specs, we purposely cache the stub models so these models don't match the associations where we specify
# an explicit class_name after constantize.
def raise_on_type_mismatch!(record)
return
end
end
ActiveRecord::Associations::Association.prepend(TypeMismatchExpectedInMigrationSpec) if ActiveRecord::VERSION::STRING >= "7.0"

0 comments on commit 9a40d34

Please sign in to comment.