Skip to content

Commit

Permalink
fix: ensure pact associations are eager loaded when finding a single …
Browse files Browse the repository at this point in the history
…pact

PACT-1319
  • Loading branch information
bethesque committed Sep 18, 2023
1 parent 6e3d4af commit c98abda
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
11 changes: 6 additions & 5 deletions lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions spec/lib/pact_broker/pacts/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit c98abda

Please sign in to comment.