Skip to content

Commit

Permalink
feat(matrix): add pact publication and verification dates to response
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 10, 2017
1 parent 136d8fb commit f0cc32b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
6 changes: 4 additions & 2 deletions lib/pact_broker/api/decorators/matrix_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ def provider_hash(line, provider, base_url)
}
}

if !line[:provider_version].nil?
hash[:version] = { number: line[:provider_version] }
if !line[:provider_version_number].nil?
hash[:version] = { number: line[:provider_version_number] }
end

hash
end

def pact_hash(line, base_url)
{
createdAt: line[:pact_created_at].to_datetime.xmlschema,
_links: {
self: {
href: pact_url_from_params(base_url, line)
Expand All @@ -96,6 +97,7 @@ def verification_hash(line, base_url)
if !line[:success].nil?
{
success: line[:success],
verifiedAt: line[:verification_executed_at].to_datetime.xmlschema,
_links: {
self: {
href: verification_url(OpenStruct.new(line), base_url)
Expand Down
30 changes: 17 additions & 13 deletions lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ class Repository
include PactBroker::Repositories

def find pacticipant_1_name, pacticipant_2_name
PactBroker::Pacts::LatestPactPublicationsByConsumerVersion
.left_outer_join(:latest_verifications, pact_version_id: :pact_version_id)
.pacticipants(pacticipant_1_name, pacticipant_2_name)
.reverse(:consumer_version_order)
.all
.collect(&:values)
version_ids = PactBroker::Domain::Version.select(Sequel[:versions][:id])
.join(:pacticipants, id: :pacticipant_id)
.where(Sequel[:pacticipants][:name] => [pacticipant_1_name, pacticipant_2_name])
find_for_version_ids(version_ids)
end

##
Expand All @@ -23,19 +21,25 @@ def find_compatible_pacticipant_versions criteria
version_repository.find_by_pacticipant_name_and_number(key, value).id
end

query = PactBroker::Pacts::LatestPactPublicationsByConsumerVersion
.select_append(:consumer_version_number, :provider_name, :consumer_name, :provider_version_id, :provider_version_number, :success, :execution_date)
find_for_version_ids(version_ids)
.group_by{|line| [line[:consumer_version_number], line[:provider_version_number]]}
.values
.collect(&:last)
.select{ |line | line[:success] }
end

def find_for_version_ids version_ids
PactBroker::Pacts::LatestPactPublicationsByConsumerVersion
.select_append(:consumer_version_number, :provider_name, :consumer_name, :provider_version_id, :provider_version_number, :success)
.select_append(Sequel[:latest_pact_publications_by_consumer_versions][:created_at].as(:pact_created_at))
.select_append(Sequel[:all_verifications][:number])
.select_append(Sequel[:all_verifications][:id].as(:verification_id))
.select_append(Sequel[:all_verifications][:execution_date].as(:verification_executed_at))
.left_outer_join(:all_verifications, pact_version_id: :pact_version_id)
.where(provider_version_id: version_ids)
.where(provider_version_id: version_ids).or(provider_version_id: nil)
.where(consumer_version_id: version_ids)
.order(:execution_date, :verification_id)
.collect(&:values)
.group_by{|line| [line[:consumer_version_number], line[:provider_version_number]]}
.values
.collect(&:last)
.select{ |line | line[:success] }
end
end
end
Expand Down
14 changes: 10 additions & 4 deletions spec/lib/pact_broker/api/decorators/matrix_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ module Api
module Decorators
describe MatrixPactDecorator do
describe "to_json" do
let(:verification_date) { DateTime.new(2017, 12, 31) }
let(:pact_created_at) { DateTime.new(2017, 1, 1) }
let(:line_1) do
{
consumer_name: "Consumer",
consumer_version_number: "1.0.0",
pact_version_sha: "1234",
provider_version: "4.5.6",
pact_created_at: pact_created_at,
provider_version_number: "4.5.6",
provider_name: "Provider",
success: true,
number: 1,
build_url: nil,
execution_date: DateTime.now
verification_executed_at: verification_date
}
end

Expand All @@ -24,12 +27,13 @@ module Decorators
consumer_name: "Consumer",
consumer_version_number: "1.0.0",
pact_version_sha: "1234",
provider_version: nil,
pact_created_at: pact_created_at,
provider_version_number: nil,
provider_name: "Provider",
success: nil,
number: nil,
build_url: nil,
execution_date: nil
verification_executed_at: nil
}
end

Expand Down Expand Up @@ -69,6 +73,7 @@ module Decorators
let(:verification_hash) do
{
success: true,
verifiedAt: "2017-12-31T00:00:00+00:00",
_links: {
self: {
href: "http://example.org/pacts/provider/Provider/consumer/Consumer/pact-version/1234/verification-results/1"
Expand All @@ -79,6 +84,7 @@ module Decorators

let(:pact_hash) do
{
createdAt: "2017-01-01T00:00:00+00:00",
_links: {
self: {
href: "http://example.org/pacts/provider/Provider/consumer/Consumer/version/1.0.0"
Expand Down
5 changes: 4 additions & 1 deletion spec/lib/pact_broker/matrix/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Matrix
.create_pact
end

subject { Repository.new.find "Consumer", "Provider" }
subject { Repository.new.find("Consumer", "Provider") }

it "returns the latest revision of each pact in reverse consumer_version_order" do
expect(subject.count).to eq 2
Expand Down Expand Up @@ -58,12 +58,15 @@ module Matrix
expect(subject.first[:provider_name]).to eq "B"
expect(subject.first[:provider_version_number]).to eq "2"
expect(subject.first[:number]).to eq 2
expect(subject.first[:pact_created_at]).to be_datey
expect(subject.first[:verification_executed_at]).to be_datey

expect(subject.last[:consumer_name]).to eq "B"
expect(subject.last[:consumer_version_number]).to eq "2"
expect(subject.last[:provider_name]).to eq "C"
expect(subject.last[:provider_version_number]).to eq "2"
expect(subject.last[:number]).to eq 1
expect(subject.last[:pact_created_at]).to be_datey

expect(subject.size).to eq 2
end
Expand Down

0 comments on commit f0cc32b

Please sign in to comment.