Skip to content

Commit 3d78f79

Browse files
committed
feat(matrix): implement querying by latest without a tag
1 parent 3dff7d0 commit 3d78f79

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

lib/pact_broker/matrix/repository.rb

+7
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,20 @@ def find_all selectors
6060
def look_up_versions_for_tags(selectors)
6161
selectors.collect do | selector |
6262
# resource validation currently stops tag being specified without latest=true
63+
6364
if selector[:tag] && selector[:latest]
6465
version = version_repository.find_by_pacticpant_name_and_latest_tag(selector[:pacticipant_name], selector[:tag])
6566
# validation in resource should ensure we always have a version
6667
{
6768
pacticipant_name: selector[:pacticipant_name],
6869
pacticipant_version_number: version.number
6970
}
71+
elsif selector[:latest]
72+
version = version_repository.find_latest_by_pacticpant_name(selector[:pacticipant_name])
73+
{
74+
pacticipant_name: selector[:pacticipant_name],
75+
pacticipant_version_number: version.number
76+
}
7077
else
7178
selector
7279
end

lib/pact_broker/versions/repository.rb

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ def find_by_pacticpant_name_and_latest_tag pacticipant_name, tag
2323
.first
2424
end
2525

26+
def find_latest_by_pacticpant_name pacticipant_name
27+
PactBroker::Domain::Version
28+
.select_all_qualified
29+
.join(:pacticipants, {id: :pacticipant_id}, {implicit_qualifier: :versions})
30+
.where(name_like(Sequel[:pacticipants][:name], pacticipant_name))
31+
.reverse_order(:order)
32+
.first
33+
end
34+
2635
def find_by_pacticipant_name_and_number pacticipant_name, number
2736
PactBroker::Domain::Version
2837
.select(Sequel[:versions][:id], Sequel[:versions][:number], Sequel[:versions][:pacticipant_id], Sequel[:versions][:order], Sequel[:versions][:created_at], Sequel[:versions][:updated_at])

spec/lib/pact_broker/matrix/repository_spec.rb

+46
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,52 @@ def build_selectors(hash)
246246
expect(subject.size).to eq 1
247247
end
248248
end
249+
250+
context "when the latest version is specified for a provider without a tag" do
251+
before do
252+
td.create_pact_with_hierarchy("A", "1.2.3", "B")
253+
.create_verification(provider_version: "1.0.0")
254+
.use_provider_version("1.0.0")
255+
.create_verification(provider_version: "2.0.0", number: 2)
256+
.use_provider_version("2.0.0")
257+
.create_verification(provider_version: "3.0.0", number: 3)
258+
end
259+
260+
let(:selectors) do
261+
[
262+
{ pacticipant_name: "A", pacticipant_version_number: "1.2.3" },
263+
{ pacticipant_name: "B", latest: true }
264+
]
265+
end
266+
267+
subject { Repository.new.find(selectors) }
268+
269+
it "returns the row for the version " do
270+
expect(subject.first).to include provider_version_number: "3.0.0"
271+
expect(subject.size).to eq 1
272+
end
273+
end
274+
275+
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
276+
before do
277+
td.create_pact_with_hierarchy("A", "1.2.3", "B")
278+
.create_verification(provider_version: "1.0.0")
279+
.create_provider_version("5.0.0")
280+
end
281+
282+
let(:selectors) do
283+
[
284+
{ pacticipant_name: "A", pacticipant_version_number: "1.2.3" },
285+
{ pacticipant_name: "B", latest: true }
286+
]
287+
end
288+
289+
subject { Repository.new.find(selectors) }
290+
291+
it "returns no data - this may be confusing. Might need to re-think this logic." do
292+
expect(subject.size).to eq 0
293+
end
294+
end
249295
end
250296

251297
describe "#find_for_consumer_and_provider" do

0 commit comments

Comments
 (0)