Skip to content

Commit

Permalink
feat(matrix): implement querying by latest without a tag
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 31, 2017
1 parent 3dff7d0 commit 3d78f79
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,20 @@ def find_all selectors
def look_up_versions_for_tags(selectors)
selectors.collect do | selector |
# resource validation currently stops tag being specified without latest=true

if selector[:tag] && selector[:latest]
version = version_repository.find_by_pacticpant_name_and_latest_tag(selector[:pacticipant_name], selector[:tag])
# validation in resource should ensure we always have a version
{
pacticipant_name: selector[:pacticipant_name],
pacticipant_version_number: version.number
}
elsif selector[:latest]
version = version_repository.find_latest_by_pacticpant_name(selector[:pacticipant_name])
{
pacticipant_name: selector[:pacticipant_name],
pacticipant_version_number: version.number
}
else
selector
end
Expand Down
9 changes: 9 additions & 0 deletions lib/pact_broker/versions/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def find_by_pacticpant_name_and_latest_tag pacticipant_name, tag
.first
end

def find_latest_by_pacticpant_name pacticipant_name
PactBroker::Domain::Version
.select_all_qualified
.join(:pacticipants, {id: :pacticipant_id}, {implicit_qualifier: :versions})
.where(name_like(Sequel[:pacticipants][:name], pacticipant_name))
.reverse_order(:order)
.first
end

def find_by_pacticipant_name_and_number pacticipant_name, number
PactBroker::Domain::Version
.select(Sequel[:versions][:id], Sequel[:versions][:number], Sequel[:versions][:pacticipant_id], Sequel[:versions][:order], Sequel[:versions][:created_at], Sequel[:versions][:updated_at])
Expand Down
46 changes: 46 additions & 0 deletions spec/lib/pact_broker/matrix/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,52 @@ def build_selectors(hash)
expect(subject.size).to eq 1
end
end

context "when the latest version is specified for a provider without a tag" do
before do
td.create_pact_with_hierarchy("A", "1.2.3", "B")
.create_verification(provider_version: "1.0.0")
.use_provider_version("1.0.0")
.create_verification(provider_version: "2.0.0", number: 2)
.use_provider_version("2.0.0")
.create_verification(provider_version: "3.0.0", number: 3)
end

let(:selectors) do
[
{ pacticipant_name: "A", pacticipant_version_number: "1.2.3" },
{ pacticipant_name: "B", latest: true }
]
end

subject { Repository.new.find(selectors) }

it "returns the row for the version " do
expect(subject.first).to include provider_version_number: "3.0.0"
expect(subject.size).to eq 1
end
end

context "when the latest version is specified for a provider without a tag but the latest known version for a provider does not have a verification" do
before do
td.create_pact_with_hierarchy("A", "1.2.3", "B")
.create_verification(provider_version: "1.0.0")
.create_provider_version("5.0.0")
end

let(:selectors) do
[
{ pacticipant_name: "A", pacticipant_version_number: "1.2.3" },
{ pacticipant_name: "B", latest: true }
]
end

subject { Repository.new.find(selectors) }

it "returns no data - this may be confusing. Might need to re-think this logic." do
expect(subject.size).to eq 0
end
end
end

describe "#find_for_consumer_and_provider" do
Expand Down

0 comments on commit 3d78f79

Please sign in to comment.