diff --git a/lib/pact_broker/domain/pact.rb b/lib/pact_broker/domain/pact.rb index c66cb214c..6bdea39ca 100644 --- a/lib/pact_broker/domain/pact.rb +++ b/lib/pact_broker/domain/pact.rb @@ -6,8 +6,12 @@ =end module PactBroker + class UnsetAttributeError < StandardError; end + class UnsetAttribute; end + module Domain class Pact + # The ID is the pact_publication ID attr_accessor :id, :provider, @@ -21,7 +25,8 @@ class Pact :latest_verification, :head_tag_names - def initialize attributes + def initialize attributes = {} + @latest_verification = UnsetAttribute.new attributes.each_pair do | key, value | self.send(key.to_s + "=", value) end @@ -47,6 +52,10 @@ def latest_consumer_version_tag_names= latest_consumer_version_tag_names @latest_consumer_version_tag_names = latest_consumer_version_tag_names end + def latest_verification + get_attribute_if_set :latest_verification + end + def to_s "Pact: consumer=#{consumer.name} provider=#{provider.name}" end @@ -88,6 +97,19 @@ def pending? def pact_version db_model.pact_version end + + # This class has various incarnations with different properties loaded. + # They should probably be different classes, but for now, raise an error if + # an attribute is called when it hasn't been set in the constuctor, because + # returning nil when there should be an object causes bugs. + def get_attribute_if_set attribute_name + val = instance_variable_get("@#{attribute_name}".to_sym) + if val.is_a?(UnsetAttribute) + raise UnsetAttributeError.new("Attribute #{attribute_name} not set") + else + val + end + end end end end diff --git a/lib/pact_broker/pacts/all_pact_publications.rb b/lib/pact_broker/pacts/all_pact_publications.rb index 261eb9292..60bb812bd 100644 --- a/lib/pact_broker/pacts/all_pact_publications.rb +++ b/lib/pact_broker/pacts/all_pact_publications.rb @@ -93,8 +93,10 @@ def to_domain_without_tags number: consumer_version_number, order: consumer_version_order, pacticipant: consumer, - tags: nil) - Domain::Pact.new(id: id, + tags: nil + ) + Domain::Pact.new( + id: id, consumer: consumer, consumer_version: consumer_version, provider: provider, @@ -103,7 +105,9 @@ def to_domain_without_tags pact_version_sha: pact_version_sha, created_at: created_at, head_tag_names: head_tag_names, - db_model: self) + latest_verification: pact_version.latest_verification, + db_model: self + ) end def head_tag_names diff --git a/lib/pact_broker/pacts/pact_publication.rb b/lib/pact_broker/pacts/pact_publication.rb index 3ca1aeba6..f09a5da5f 100644 --- a/lib/pact_broker/pacts/pact_publication.rb +++ b/lib/pact_broker/pacts/pact_publication.rb @@ -113,7 +113,7 @@ def to_domain revision_number: revision_number, json_content: pact_version.content, pact_version_sha: pact_version.sha, - latest_verification: nil, + latest_verification: pact_version.latest_verification, created_at: created_at, head_tag_names: [], db_model: self diff --git a/spec/lib/pact_broker/domain/pact_spec.rb b/spec/lib/pact_broker/domain/pact_spec.rb new file mode 100644 index 000000000..4c203be90 --- /dev/null +++ b/spec/lib/pact_broker/domain/pact_spec.rb @@ -0,0 +1,27 @@ +require 'pact_broker/domain/pact' + +module PactBroker + module Domain + describe Pact do + describe "latest_verification" do + context "when it has been set to an object" do + subject { Pact.new(latest_verification: 'verification') } + + its(:latest_verification) { is_expected.to eq 'verification' } + end + + context "when it has been set to nil" do + subject { Pact.new(latest_verification: nil) } + + its(:latest_verification) { is_expected.to eq nil } + end + + context "when it has not been set" do + it "raises an error" do + expect { Pact.new.latest_verification.foo }.to raise_error UnsetAttributeError + end + end + end + end + end +end diff --git a/spec/lib/pact_broker/pacts/pact_publication_spec.rb b/spec/lib/pact_broker/pacts/pact_publication_spec.rb index 528cea57c..e9263b383 100644 --- a/spec/lib/pact_broker/pacts/pact_publication_spec.rb +++ b/spec/lib/pact_broker/pacts/pact_publication_spec.rb @@ -3,6 +3,28 @@ module PactBroker module Pacts describe PactPublication do + describe "to_domain" do + before do + td.create_pact_with_verification("Foo", "1", "Bar", "2") + end + + subject { PactPublication.first.to_domain } + + its(:latest_verification) { is_expected.to_not be nil } + end + + describe "to_domain_lightweight" do + before do + td.create_pact_with_verification("Foo", "1", "Bar", "2") + end + + subject { PactPublication.first.to_domain_lightweight } + + it "raises an error if you try to access the latest_verification" do + expect { subject.latest_verification }.to raise_error PactBroker::UnsetAttributeError + end + end + describe "save and upsert" do before do td.create_consumer