From c98abda699cc0cce84b13905996930ffb01df68e Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 18 Sep 2023 11:24:42 +1000 Subject: [PATCH] fix: ensure pact associations are eager loaded when finding a single pact PACT-1319 --- lib/pact_broker/pacts/repository.rb | 11 ++++++----- spec/lib/pact_broker/pacts/repository_spec.rb | 5 +++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/pact_broker/pacts/repository.rb b/lib/pact_broker/pacts/repository.rb index 9ebb32fc9..c5f816b8f 100644 --- a/lib/pact_broker/pacts/repository.rb +++ b/lib/pact_broker/pacts/repository.rb @@ -252,23 +252,24 @@ def find_pact consumer_name, consumer_version_number, provider_name, pact_versio .reverse_order(:consumer_version_order, :revision_number) .limit(1) + # Must use .all to trigger the eager loading if consumer_version_number && !pact_version_sha pact_publication_by_consumer_version - .first&.to_domain_with_content + .all.first&.to_domain_with_content elsif pact_version_sha && !consumer_version_number latest_pact_publication_by_sha - .first&.to_domain_with_content + .all.first&.to_domain_with_content elsif consumer_version_number && pact_version_sha - pact_publication = pact_publication_by_consumer_version.first + pact_publication = pact_publication_by_consumer_version.all.first if pact_publication && pact_publication.pact_version.sha == pact_version_sha pact_publication.to_domain_with_content else - latest_pact_publication_by_sha.first&.to_domain_with_content + latest_pact_publication_by_sha.all.first&.to_domain_with_content end else pact_publication_by_consumer_version .reverse_order(:consumer_version_order, :revision_number) - .first&.to_domain_with_content + .all.first&.to_domain_with_content end end # rubocop: enable Metrics/CyclomaticComplexity, Metrics/MethodLength diff --git a/spec/lib/pact_broker/pacts/repository_spec.rb b/spec/lib/pact_broker/pacts/repository_spec.rb index 8c4221e9e..8a9fe65a9 100644 --- a/spec/lib/pact_broker/pacts/repository_spec.rb +++ b/spec/lib/pact_broker/pacts/repository_spec.rb @@ -608,7 +608,7 @@ module Pacts .create_consumer_version("1.2.2") .create_provider("Provider") .create_pact - .create_consumer_version("1.2.4") + .create_consumer_version("1.2.4", branch: "dev") .create_consumer_version_tag("prod") .create_pact .revise_pact @@ -629,7 +629,8 @@ module Pacts expect(subject.provider.name).to eq "Provider" expect(subject.consumer_version_number).to eq "1.2.4" expect(subject.consumer_version.number).to eq "1.2.4" - expect(subject.consumer_version.tags.first.name).to eq "prod" + expect(subject.consumer_version_branch_names.first).to eq "dev" + expect(subject.consumer_version_tag_names.first).to eq "prod" expect(subject.json_content).to_not be_nil end