Skip to content

Commit

Permalink
feat: add interactions_count and messages_count to pact_versions table
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 12, 2021
1 parent 4c7cdee commit c17adbe
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Sequel.migration do
change do
alter_table(:pact_versions) do
add_column(:messages_count, Integer)
add_column(:interactions_count, Integer)
end
end
end
4 changes: 2 additions & 2 deletions lib/pact_broker/pacts/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ def content_that_affects_verification_results
end

def messages
pact_hash.is_a?(Hash) ? pact_hash["messages"] : nil
pact_hash.is_a?(Hash) && pact_hash["messages"].is_a?(Array) ? pact_hash["messages"] : nil
end

def interactions
pact_hash.is_a?(Hash) ? pact_hash["interactions"] : nil
pact_hash.is_a?(Hash) && pact_hash["interactions"].is_a?(Array) ? pact_hash["interactions"] : nil
end

def messages_or_interactions
Expand Down
5 changes: 4 additions & 1 deletion lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require "pact_broker/pacts/selectors"
require "pact_broker/feature_toggle"
require "pact_broker/pacts/pacts_for_verification_repository"
require "pact_broker/pacts/content"

module PactBroker
module Pacts
Expand Down Expand Up @@ -355,7 +356,9 @@ def create_pact_version consumer_id, provider_id, sha, json_content
provider_id: provider_id,
sha: sha,
content: json_content,
created_at: Sequel.datetime_class.now
created_at: Sequel.datetime_class.now,
interactions_count: Content.from_json(json_content).interactions&.count || 0,
messages_count: Content.from_json(json_content).messages&.count || 0
).upsert
end

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/pacts/content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module Pacts
end
end

context "with both messages and interactions, even though this should never happen" do
context "with both messages and interactions (v4)" do
let(:pact_hash) do
{
"ignored" => "foo",
Expand Down
12 changes: 11 additions & 1 deletion spec/lib/pact_broker/pacts/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Pacts
let(:provider) { Pacticipants::Repository.new.create name: "Provider" }
let(:version) { PactBroker::Versions::Repository.new.create number: "1.2.3", pacticipant_id: consumer.id }
let(:pact_version_sha) { "123" }
let(:json_content) { {some: "json"}.to_json }
let(:json_content) { { interactions: ["json"] }.to_json }

let(:params) do
{
Expand Down Expand Up @@ -63,6 +63,16 @@ module Pacts
expect(LatestPactPublicationIdForConsumerVersion.first.created_at).to eq PactBroker::Domain::Version.find(number: subject.consumer_version_number).created_at
end

it "sets the interactions count" do
subject
expect(PactVersion.order(:id).last.interactions_count).to eq 1
end

it "sets the messages count" do
subject
expect(PactVersion.order(:id).last.messages_count).to eq 0
end

context "when a pact already exists with exactly the same content" do
let(:another_version) { Versions::Repository.new.create number: "2.0.0", pacticipant_id: consumer.id }

Expand Down

0 comments on commit c17adbe

Please sign in to comment.