Skip to content

Commit

Permalink
feat: add _ids to interactions added via the TestDataBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Apr 26, 2019
1 parent 2b2a3f8 commit d21e709
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 64 deletions.
76 changes: 22 additions & 54 deletions lib/pact_broker/test/test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'pact_broker/domain/webhook_execution_result'
require 'pact_broker/pacts/repository'
require 'pact_broker/pacts/service'
require 'pact_broker/pacts/content'
require 'pact_broker/pacticipants/repository'
require 'pact_broker/pacticipants/service'
require 'pact_broker/versions/repository'
Expand Down Expand Up @@ -52,64 +53,22 @@ def comment *args
end

def create_pricing_service
@pricing_service_id = pacticipant_repository.create(:name => 'Pricing Service', :repository_url => 'git@git.realestate.com.au:business-systems/pricing-service').save(raise_on_save_failure: true).id
create_provider("Pricing Service", :repository_url => 'git@git.realestate.com.au:business-systems/pricing-service')
self
end

def create_contract_proposal_service
@contract_proposal_service_id = pacticipant_repository.create(:name => 'Contract Proposal Service', :repository_url => 'git@git.realestate.com.au:business-systems/contract-proposal-service').save(raise_on_save_failure: true).id
self
end

def create_contract_proposal_service_version number
@contract_proposal_service_version_id = version_repository.create(number: number, pacticipant_id: @contract_proposal_service_id).id
create_provider("Contract Proposal Service", :repository_url => 'git@git.realestate.com.au:business-systems/contract-proposal-service')
self
end

def create_contract_email_service
@contract_email_service_id = pacticipant_repository.create(:name => 'Contract Email Service', :repository_url => 'git@git.realestate.com.au:business-systems/contract-email-service').save(raise_on_save_failure: true).id
self
end

def create_contract_email_service_version number
@contract_email_service_version_id = version_repository.create(number: number, pacticipant_id: @contract_email_service_id).id
self
end

def create_ces_cps_pact
@pact_id = pact_repository.create(
version_id: @contract_email_service_version_id,
consumer_id: @contract_email_service_id,
provider_id: @contract_proposal_service_id,
json_content: default_json_content,
pact_version_sha: PactBroker.configuration.sha_generator.call(default_json_content)
).id
create_consumer("Contract Email Service", :repository_url => 'git@git.realestate.com.au:business-systems/contract-email-service')
self
end

def create_condor
@condor_id = pacticipant_repository.create(:name => 'Condor').save(raise_on_save_failure: true).id
self
end

def create_condor_version number
@condor_version_id = version_repository.create(number: number, pacticipant_id: @condor_id).id
self
end

def create_pricing_service_version number
@pricing_service_version_id = version_repository.create(number: number, pacticipant_id: @pricing_service_id).id
self
end

def create_condor_pricing_service_pact
@pact_id = pact_repository.create(
version_id: @condor_version_id,
consumer_id: @condor_id,
provider_id: @pricing_service_id,
json_content: default_json_content,
pact_version_sha: PactBroker.configuration.sha_generator.call(default_json_content)
).id
create_consumer("Condor")
self
end

Expand All @@ -136,13 +95,13 @@ def create_tag_with_hierarchy pacticipant_name, pacticipant_version, tag_name

def create_pacticipant pacticipant_name, params = {}
params.delete(:comment)
@pacticipant = PactBroker::Domain::Pacticipant.create(:name => pacticipant_name)
@pacticipant = PactBroker::Domain::Pacticipant.create({ :name => pacticipant_name }.merge(params))
self
end

def create_consumer consumer_name = "Consumer #{model_counter}", params = {}
params.delete(:comment)
create_pacticipant consumer_name
create_pacticipant consumer_name, params
@consumer = @pacticipant
self
end
Expand All @@ -155,7 +114,7 @@ def use_consumer consumer_name, params = {}

def create_provider provider_name = "Provider #{model_counter}", params = {}
params.delete(:comment)
create_pacticipant provider_name
create_pacticipant provider_name, params
@provider = @pacticipant
self
end
Expand Down Expand Up @@ -227,13 +186,13 @@ def create_label label_name
def create_pact params = {}
params.delete(:comment)
json_content = params[:json_content] || default_json_content
pact_version_sha = params[:pact_version_sha] || PactBroker.configuration.sha_generator.call(json_content)
pact_version_sha = params[:pact_version_sha] || generate_pact_version_sha(json_content)
@pact = PactBroker::Pacts::Repository.new.create(
version_id: @consumer_version.id,
consumer_id: @consumer.id,
provider_id: @provider.id,
json_content: json_content,
pact_version_sha: pact_version_sha
pact_version_sha: pact_version_sha,
json_content: prepare_json_content(json_content),
)
set_created_at_if_set params[:created_at], :pact_publications, {id: @pact.id}
set_created_at_if_set params[:created_at], :pact_versions, {sha: @pact.pact_version_sha}
Expand All @@ -250,9 +209,9 @@ def republish_same_pact params = {}

def revise_pact json_content = nil
json_content = json_content ? json_content : {random: rand}.to_json
pact_version_sha = PactBroker.configuration.sha_generator.call(json_content)
pact_version_sha = generate_pact_version_sha(json_content)
@pact = PactBroker::Pacts::Repository.new.update(@pact.id,
json_content: json_content,
json_content: prepare_json_content(json_content),
pact_version_sha: pact_version_sha
)
self
Expand Down Expand Up @@ -362,6 +321,15 @@ def and_return instance_variable_name

private

# Remember! This must be called before adding the IDs
def generate_pact_version_sha json_content
PactBroker.configuration.sha_generator.call(json_content)
end

def prepare_json_content(json_content)
PactBroker::Pacts::Content.from_json(json_content).with_ids.to_json
end

def set_created_at_if_set created_at, table_name, selector
if created_at
Sequel::Model.db[table_name].where(selector.keys.first => selector.values.first).update(created_at: created_at)
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/pact_broker/domain/order_versions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
before do
TestDataBuilder.new
.create_condor
.create_condor_version('1.5.0')
.create_condor_version('1.4.0')
.create_condor_version('1.3.0')
.create_condor_version('1.6.0')
.create_consumer_version('1.5.0')
.create_consumer_version('1.4.0')
.create_consumer_version('1.3.0')
.create_consumer_version('1.6.0')
end

let(:ordered_versions) { PactBroker::Domain::Version.order(:order).all.collect(&:number) }
Expand Down
12 changes: 6 additions & 6 deletions spec/service_consumers/provider_states_for_pact_broker_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@

provider_state "the 'Pricing Service' already exists in the pact-broker" do
set_up do
TestDataBuilder.new.create_pricing_service.create_pricing_service_version("1.3.0")
TestDataBuilder.new.create_pricing_service.create_provider_version("1.3.0")
end
end

Expand All @@ -149,9 +149,9 @@
set_up do
TestDataBuilder.new
.create_condor
.create_condor_version('1.3.0')
.create_consumer_version('1.3.0')
.create_pricing_service
.create_condor_pricing_service_pact
.create_pact
end
end

Expand All @@ -163,9 +163,9 @@
set_up do
TestDataBuilder.new
.create_condor
.create_condor_version('1.3.0')
.create_consumer_version('1.3.0')
.create_pricing_service
.create_condor_pricing_service_pact
.create_pact
end
end

Expand All @@ -177,7 +177,7 @@

provider_state "'Condor' exists in the pact-broker" do
set_up do
TestDataBuilder.new.create_condor.create_condor_version('1.3.0')
TestDataBuilder.new.create_condor.create_consumer_version('1.3.0')
end
end

Expand Down

0 comments on commit d21e709

Please sign in to comment.