Skip to content

Commit

Permalink
fix: gracefully handle attempt to create a duplicate tag
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Aug 23, 2018
1 parent 4af4ed1 commit 53bea8b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/pact_broker/tags/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion spec/lib/pact_broker/tags/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions spec/support/test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 53bea8b

Please sign in to comment.