From 53bea8b4a8d5b8933d68380bf50c8b2521670a19 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 23 Aug 2018 21:12:11 +1000 Subject: [PATCH] fix: gracefully handle attempt to create a duplicate tag --- lib/pact_broker/tags/repository.rb | 9 ++++- spec/lib/pact_broker/tags/repository_spec.rb | 35 +++++++++++++++++++- spec/support/test_data_builder.rb | 1 + 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/pact_broker/tags/repository.rb b/lib/pact_broker/tags/repository.rb index e66fe7b32..0a974bce4 100644 --- a/lib/pact_broker/tags/repository.rb +++ b/lib/pact_broker/tags/repository.rb @@ -9,7 +9,14 @@ class Repository include PactBroker::Repositories::Helpers def create args - Domain::Tag.new(name: args.fetch(:name), version: args.fetch(:version)).save + params = { + name: args.fetch(:name), + version_id: args.fetch(:version).id, + created_at: Sequel.datetime_class.now, + updated_at: Sequel.datetime_class.now + } + Domain::Tag.dataset.insert_ignore.insert(params) + Domain::Tag.find(name: args[:name], version_id: args[:version].id) end def find args diff --git a/spec/lib/pact_broker/tags/repository_spec.rb b/spec/lib/pact_broker/tags/repository_spec.rb index fc1c09b96..8006402f2 100644 --- a/spec/lib/pact_broker/tags/repository_spec.rb +++ b/spec/lib/pact_broker/tags/repository_spec.rb @@ -4,9 +4,42 @@ module PactBroker module Tags describe Repository do - let(:td) { TestDataBuilder.new } + describe ".create" do + before do + td.create_pacticipant("foo") + .create_version("1") + end + + let(:params) { { name: "prod", version: td.version } } + + subject { Repository.new.create(params) } + + it "returns a tag" do + expect(subject).to be_a(Domain::Tag) + end + + it "sets the properties" do + expect(subject.name).to eq "prod" + expect(subject.version.id).to eq td.version.id + end + + context "when the tag already exists" do + before do + td.create_tag("prod") + end + + it "does nothing" do + expect { subject }.to_not change { Domain::Tag.count } + end + + it "returns a tag" do + expect(subject).to be_a(Domain::Tag) + end + end + end + describe ".find" do let(:pacticipant_name) { "test_pacticipant" } diff --git a/spec/support/test_data_builder.rb b/spec/support/test_data_builder.rb index c0702266f..e169eba1b 100644 --- a/spec/support/test_data_builder.rb +++ b/spec/support/test_data_builder.rb @@ -35,6 +35,7 @@ class TestDataBuilder attr_reader :provider attr_reader :consumer_version attr_reader :provider_version + attr_reader :version attr_reader :pact attr_reader :verification attr_reader :webhook