Skip to content

Commit

Permalink
feat(verifications): create and link provider version resource when v…
Browse files Browse the repository at this point in the history
…erification is published
  • Loading branch information
bethesque committed Oct 8, 2017
1 parent 1258099 commit 3014a8b
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 22 deletions.
12 changes: 12 additions & 0 deletions db/migrations/43_add_provider_version_to_verification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Sequel.migration do
change do
alter_table(:verifications) do
add_foreign_key(:provider_version_id, :versions, foreign_key_constraint_name: 'fk_verifications_versions')
end
end

# TODO
# alter_table(:verifications) do
# set_column_not_null(:provider_version_id)
# end
end
25 changes: 25 additions & 0 deletions db/migrations/44_populate_verifications_provider_version_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Sequel.migration do
up do
from(:verifications)
.select(Sequel[:verifications][:id], :provider_version, :provider_id, Sequel[:verifications][:created_at])
.join(:pact_versions, {id: :pact_version_id})
.each do | line |
version = from(:versions)
.where(number: line[:provider_version], pacticipant_id: line[:provider_id]).single_record
version_id = if version
version[:id]
else
from(:versions).insert(
number: line[:provider_version],
pacticipant_id: line[:provider_id],
created_at: line[:created_at],
updated_at: line[:created_at]
)
end
from(:verifications).where(id: line[:id]).update(provider_version_id: version_id)
end
end

down do
end
end
7 changes: 7 additions & 0 deletions db/migrations/45_set_verification_provider_number_nullable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Sequel.migration do
change do
alter_table(:verifications) do
set_column_allow_null(:provider_version)
end
end
end
18 changes: 18 additions & 0 deletions db/migrations/46_recreate_latest_verifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Sequel.migration do
up do
create_or_replace_view(:latest_verification_numbers,
"SELECT pact_version_id, MAX(number) latest_number
FROM verifications
GROUP BY pact_version_id")

# The most recent verification for each pact version
# provider_version is DEPRECATED, use provider_version_number
create_or_replace_view(:latest_verifications,
"SELECT v.id, v.number, v.success, s.number as provider_version, v.build_url, v.pact_version_id, v.execution_date, v.created_at, v.provider_version_id, s.number as provider_version_number
FROM verifications v
INNER JOIN latest_verification_numbers lv
ON v.pact_version_id = lv.pact_version_id AND v.number = lv.latest_number
INNER JOIN versions s on v.provider_version_id = s.id"
)
end
end
2 changes: 1 addition & 1 deletion lib/pact_broker/api/decorators/verification_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Decorators
class VerificationDecorator < BaseDecorator

property :provider_name, as: :providerName, writeable: :false
property :provider_version, as: :providerApplicationVersion
property :provider_version_number, as: :providerApplicationVersion, writeable: false
property :success
property :execution_date, as: :verificationDate
property :build_url, as: :buildUrl
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/domain/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def pact_changed_since_last_verification?
latest_verification.pact_version_sha != latest_pact.pact_version_sha
end

def latest_verification_provider_version
latest_verification.provider_version
def latest_verification_provider_version_number
latest_verification.provider_version.number
end

def pacticipants
Expand Down
6 changes: 5 additions & 1 deletion lib/pact_broker/domain/verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Verification < Sequel::Model

set_primary_key :id
associate(:many_to_one, :pact_version, class: "PactBroker::Pacts::PactVersion", key: :pact_version_id, primary_key: :id)
associate(:many_to_one, :provider_version, class: "PactBroker::Domain::Version", key: :provider_version_id, primary_key: :id)

def before_create
super
Expand Down Expand Up @@ -78,10 +79,13 @@ def provider
.limit(1).select(:provider_id))
end

def provider_version_number
provider_version.number
end

def latest_pact_publication
pact_version.latest_pact_publication
end

end

Verification.plugin :timestamps
Expand Down
6 changes: 3 additions & 3 deletions lib/pact_broker/ui/view_models/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ def warning?
def verification_tooltip
case @relationship.verification_status
when :success
"Successfully verified by #{provider_name} (v#{@relationship.latest_verification_provider_version})"
"Successfully verified by #{provider_name} (v#{@relationship.latest_verification_provider_version_number})"
when :stale
"Pact has changed since last successful verification by #{provider_name} (v#{@relationship.latest_verification_provider_version})"
"Pact has changed since last successful verification by #{provider_name} (v#{@relationship.latest_verification_provider_version_number})"
when :failed
"Verification by #{provider_name} (v#{@relationship.latest_verification_provider_version}) failed"
"Verification by #{provider_name} (v#{@relationship.latest_verification_provider_version_number}) failed"
else
nil
end
Expand Down
6 changes: 5 additions & 1 deletion lib/pact_broker/verifications/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ module Verifications
class Repository

include PactBroker::Repositories::Helpers
include PactBroker::Repositories

def create verification, pact
def create verification, provider_version_number, pact
provider = pacticipant_repository.find_by_name(pact.provider_name)
version = version_repository.find_by_pacticipant_id_and_number_or_create(provider.id, provider_version_number)
verification.pact_version_id = pact_version_id_for(pact)
verification.provider_version = version
verification.save
end

Expand Down
3 changes: 2 additions & 1 deletion lib/pact_broker/verifications/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ def next_number_for pact
def create next_verification_number, params, pact
PactBroker.logger.info "Creating verification #{next_verification_number} for pact_id=#{pact.id} from params #{params}"
verification = PactBroker::Domain::Verification.new
provider_version_number = params.fetch('providerApplicationVersion')
PactBroker::Api::Decorators::VerificationDecorator.new(verification).from_hash(params)
verification.number = next_verification_number
verification_repository.create(verification, pact)
verification_repository.create(verification, provider_version_number, pact)
end

def errors params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Decorators
instance_double('PactBroker::Domain::Verification',
number: 1,
success: true,
provider_version: "4.5.6",
provider_version_number: "4.5.6",
provider_name: 'Provider',
consumer_name: 'Consumer',
build_url: 'http://build-url',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Decorators
let(:verification) do
instance_double("PactBroker::Domain::Verification",
success: true, number: 1,
provider_version: '4.5.6',
provider_version_number: '4.5.6',
build_url: 'http://some-build',
provider_name: 'Provider',
consumer_name: 'Consumer',
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/ui/view_models/relationship_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module ViewDomain
instance_double("PactBroker::Domain::Relationship",
verification_status: verification_status,
provider_name: "Foo",
latest_verification_provider_version: "4.5.6")
latest_verification_provider_version_number: "4.5.6")
end
let(:ever_verified) { true }
let(:pact_changed) { false }
Expand Down
14 changes: 7 additions & 7 deletions spec/lib/pact_broker/verifications/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module Verifications
subject { Repository.new.find "Consumer1", "Provider1", pact.pact_version_sha, 2}

it "finds the latest verifications for the given consumer version" do
expect(subject.provider_version).to eq "3.7.4"
expect(subject.provider_version_number).to eq "3.7.4"
expect(subject.consumer_name).to eq "Consumer1"
expect(subject.provider_name).to eq "Provider1"
expect(subject.pact_version_sha).to eq pact.pact_version_sha
Expand Down Expand Up @@ -95,8 +95,8 @@ module Verifications
subject { Repository.new.find_latest_verifications_for_consumer_version("Consumer1", "1.2.3")}

it "finds the latest verifications for the given consumer version" do
expect(subject.first.provider_version).to eq "7.8.9"
expect(subject.last.provider_version).to eq "6.5.4"
expect(subject.first.provider_version_number).to eq "7.8.9"
expect(subject.last.provider_version_number).to eq "6.5.4"
end
end

Expand All @@ -116,7 +116,7 @@ module Verifications
subject { Repository.new.find_latest_verification_for("Consumer1", "Provider1")}

it "finds the latest verifications for the given consumer version" do
expect(subject.provider_version).to eq "7.8.9"
expect(subject.provider_version_number).to eq "7.8.9"
end
end

Expand All @@ -142,7 +142,7 @@ module Verifications
subject { Repository.new.find_latest_verification_for("Consumer1", "Provider1")}

it "finds the latest verifications for the given consumer version" do
expect(subject.provider_version).to eq "7.8.9"
expect(subject.provider_version_number).to eq "7.8.9"
end
end

Expand Down Expand Up @@ -173,7 +173,7 @@ module Verifications
subject { Repository.new.find_latest_verification_for("Consumer1", "Provider1", 'prod')}

it "finds the latest verifications for the given consumer version with the specified tag" do
expect(subject.provider_version).to eq "5.4.3"
expect(subject.provider_version_number).to eq "5.4.3"
end

context "when no verification exists" do
Expand Down Expand Up @@ -212,7 +212,7 @@ module Verifications
subject { Repository.new.find_latest_verification_for("Consumer1", "Provider1", :untagged)}

it "finds the latest verifications for the given consumer version with no tag" do
expect(subject.provider_version).to eq "5.4.3"
expect(subject.provider_version_number).to eq "5.4.3"
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/pact_broker/verifications/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ module Verifications
expect(verification.pact_version_id).to_not be_nil
expect(verification.pact_version).to_not be_nil
end

it "sets the provider version" do
verification = create_verification
expect(verification.provider_version).to_not be nil
expect(verification.provider_version_number).to eq '4.5.6'
end
end

describe "#errors" do
Expand Down
60 changes: 60 additions & 0 deletions spec/migrations/44_add_provider_version_to_verification_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
describe 'add provider version relationship to verification (migrate 42-44)', migration: true do
before do
PactBroker::Database.migrate(42)
end

let(:now) { DateTime.new(2018, 2, 2) }
let!(:consumer) { create(:pacticipants, {name: 'Consumer', created_at: now, updated_at: now}) }
let!(:provider) { create(:pacticipants, {name: 'Provider', created_at: now, updated_at: now}) }
let!(:provider) { create(:pacticipants, {name: 'Provider', created_at: now, updated_at: now}) }
let!(:consumer_version) { create(:versions, {number: '1.2.3', order: 1, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) }
let!(:pact_version) { create(:pact_versions, {content: {some: 'json'}.to_json, sha: '1234', consumer_id: consumer[:id], provider_id: provider[:id], created_at: now}) }
let!(:pact_publication) do
create(:pact_publications, {
consumer_version_id: consumer_version[:id],
provider_id: provider[:id],
revision_number: 1,
pact_version_id: pact_version[:id],
created_at: (now - 1)
})
end
let!(:verification) do
create(:verifications, {
number: 1,
success: true,
provider_version: '1.2.3',
pact_version_id: pact_version[:id],
execution_date: now,
created_at: now
})
end

subject { PactBroker::Database.migrate(46) }

it "creates a version object" do
expect { subject }.to change { PactBroker::Domain::Version.count }.by(1)
end

it "sets the foreign key to the new version" do
subject
expect(PactBroker::Domain::Verification.first.provider_version.number).to eq '1.2.3'
end

it "sets the created_at date of the new version to the created_at of the verification" do
subject
expect(PactBroker::Domain::Verification.first.provider_version.created_at).to eq now
end

context "when the version already exists" do
let!(:provider_version) { create(:versions, {number: '1.2.3', order: 1, pacticipant_id: provider[:id], created_at: now, updated_at: now}) }

it "does not create a version object" do
expect { subject }.to_not change { PactBroker::Domain::Version.count }
end

it "sets the foreign key to the existing version" do
subject
expect(PactBroker::Domain::Verification.first.provider_version.number).to eq '1.2.3'
end
end
end
9 changes: 6 additions & 3 deletions spec/support/test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,12 @@ def create_deprecated_webhook_execution params = {}
end

def create_verification parameters = {}
default_parameters = {success: true, provider_version: '4.5.6', number: 1}
verification = PactBroker::Domain::Verification.new(default_parameters.merge(parameters))
@verification = PactBroker::Verifications::Repository.new.create(verification, @pact)
provider_version_number = parameters[:provider_version] || '4.5.6'
default_parameters = {success: true, number: 1}
parameters = default_parameters.merge(parameters)
parameters.delete(:provider_version)
verification = PactBroker::Domain::Verification.new(parameters)
@verification = PactBroker::Verifications::Repository.new.create(verification, provider_version_number, @pact)
self
end

Expand Down

0 comments on commit 3014a8b

Please sign in to comment.