Skip to content

Commit

Permalink
feat(matrix): update validation to allow latest tag to be specified
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 31, 2017
1 parent fe498a7 commit 6da6e02
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def find_all selectors
def look_up_versions_for_tags(selectors)
selectors.collect do | selector |
if selector[:latest_tag]
version = version_repository.find_by_latest_tag(selector[:pacticipant_name], selector[:latest_tag])
version = version_repository.find_by_pacticpant_name_and_latest_tag(selector[:pacticipant_name], selector[:latest_tag])
# validation in resource should ensure we always have a version
{
pacticipant_name: selector[:pacticipant_name],
Expand Down
23 changes: 15 additions & 8 deletions lib/pact_broker/matrix/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,32 @@ def find_compatible_pacticipant_versions criteria
def validate_selectors selectors
error_messages = []

selectors.each do | selector |
if selector[:pacticipant_name].nil? && selector[:pacticipant_version_number].nil?
selectors.each do | s |
if s[:pacticipant_name].nil? && s[:pacticipant_version_number].nil?
error_messages << "Please specify the pacticipant name and version"
elsif selector[:pacticipant_name].nil?
elsif s[:pacticipant_name].nil?
error_messages << "Please specify the pacticipant name"
else
if s.key?(:pacticipant_version_number) && s.key?(:latest_tag)
error_messages << "A version and a latest tag cannot both be specified for #{s[:pacticipant_name]}"
end
end
end

selectors.collect{ |selector| selector[:pacticipant_name] }.compact.each do | pacticipant_name |
unless pacticipant_service.find_pacticipant_by_name(pacticipant_name)
error_messages << "Pacticipant '#{pacticipant_name}' not found"
error_messages << "Pacticipant #{pacticipant_name} not found"
end
end

if error_messages.empty?
selectors.each do | selector |
if selector[:pacticipant_version_number]
version = version_service.find_by_pacticipant_name_and_number(pacticipant_name: selector[:pacticipant_name], pacticipant_version_number: selector[:pacticipant_version_number])
error_messages << "No pact or verification found for #{selector[:pacticipant_name]} version #{selector[:pacticipant_version_number]}" if version.nil?
selectors.each do | s |
if s[:pacticipant_version_number]
version = version_service.find_by_pacticipant_name_and_number(pacticipant_name: s[:pacticipant_name], pacticipant_version_number: s[:pacticipant_version_number])
error_messages << "No pact or verification found for #{s[:pacticipant_name]} version #{s[:pacticipant_version_number]}" if version.nil?
elsif s[:latest_tag]
version = version_service.find_by_pacticpant_name_and_latest_tag(s[:pacticipant_name], s[:latest_tag])
error_messages << "No version of #{s[:pacticipant_name]} found with tag #{s[:latest_tag]}" if version.nil?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/versions/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def find_by_pacticipant_id_and_number pacticipant_id, number
PactBroker::Domain::Version.where(number: number, pacticipant_id: pacticipant_id).single_record
end

def find_by_latest_tag pacticipant_name, tag
def find_by_pacticpant_name_and_latest_tag pacticipant_name, tag
PactBroker::Domain::Version
.select_all_qualified
.join(:pacticipants, {id: :pacticipant_id}, {implicit_qualifier: :versions})
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/versions/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def self.find_by_pacticipant_name_and_number params
version_repository.find_by_pacticipant_name_and_number params.fetch(:pacticipant_name), params.fetch(:pacticipant_version_number)
end

def self.find_by_pacticpant_name_and_latest_tag(pacticipant_name, tag)
version_repository.find_by_pacticpant_name_and_latest_tag(pacticipant_name, tag)
end

def self.delete version
tag_repository.delete_by_version_id version.id
pact_repository.delete_by_version_id version.id
Expand Down
45 changes: 44 additions & 1 deletion spec/lib/pact_broker/matrix/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module Matrix
let(:selectors) { [{ pacticipant_name: "Foo", pacticipant_version_number: "1" }] }

it "returns error messages" do
expect(subject.first).to eq "Pacticipant 'Foo' not found"
expect(subject.first).to eq "Pacticipant Foo not found"
end
end

Expand Down Expand Up @@ -70,6 +70,49 @@ module Matrix
expect(subject.first).to eq "Please specify the pacticipant name and version"
end
end

context "when the latest_tag is used instead of a version" do
before do
td.create_pacticipant("Foo")
.create_version("1")
.create_tag("prod")
.create_pacticipant("Bar")
.create_version("2")
end

let(:selectors) { [{ pacticipant_name: "Foo", latest_tag: "prod" }, { pacticipant_name: "Bar", pacticipant_version_number: "2" }] }

context "when there is a version for the tag" do
it "returns no error messages" do
expect(subject).to eq []
end
end

context "when there is not a version for the tag" do

let(:selectors) { [{ pacticipant_name: "Foo", latest_tag: "wiffle" }, { pacticipant_name: "Bar", pacticipant_version_number: "2" }] }

it "returns an error message" do
expect(subject).to eq ["No version of Foo found with tag wiffle"]
end
end
end

context "when the latest_tag is used as well as a version" do
before do
td.create_pacticipant("Foo")
.create_version("1")
.create_tag("prod")
.create_pacticipant("Bar")
.create_version("2")
end

let(:selectors) { [{ pacticipant_name: "Foo", pacticipant_version_number: "1", latest_tag: "prod" }, { pacticipant_name: "Bar", pacticipant_version_number: "2" }] }

it "returns an error message" do
expect(subject).to eq ["A version and a latest tag cannot both be specified for Foo"]
end
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/pact_broker/versions/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Versions
let(:version_number) { "1.2.3" }


describe "#find_by_latest_tag" do
describe "#find_by_pacticpant_name_and_latest_tag" do
before do
td.create_consumer("Bar")
.create_consumer_version("2.3.4")
Expand All @@ -23,7 +23,7 @@ module Versions
.create_consumer_version("5.6.7")
end

subject { Repository.new.find_by_latest_tag("Foo", "prod") }
subject { Repository.new.find_by_pacticpant_name_and_latest_tag("Foo", "prod") }

it "returns the most recent version that has the specified tag" do

Expand Down

0 comments on commit 6da6e02

Please sign in to comment.