@@ -31,14 +31,26 @@ def sort
31
31
Content . from_hash ( SortContent . call ( pact_hash ) )
32
32
end
33
33
34
- def with_ids
34
+ def with_test_results ( test_results )
35
35
new_pact_hash = pact_hash . dup
36
36
if interactions && interactions . is_a? ( Array )
37
- new_pact_hash [ 'interactions' ] = add_ids ( interactions )
37
+ new_pact_hash [ 'interactions' ] = merge_verification_results ( interactions , test_results )
38
38
end
39
39
40
40
if messages && messages . is_a? ( Array )
41
- new_pact_hash [ 'messages' ] = add_ids ( messages )
41
+ new_pact_hash [ 'messages' ] = merge_verification_results ( messages , test_results )
42
+ end
43
+ Content . from_hash ( new_pact_hash )
44
+ end
45
+
46
+ def with_ids ( overwrite_existing_id = true )
47
+ new_pact_hash = pact_hash . dup
48
+ if interactions && interactions . is_a? ( Array )
49
+ new_pact_hash [ 'interactions' ] = add_ids ( interactions , overwrite_existing_id )
50
+ end
51
+
52
+ if messages && messages . is_a? ( Array )
53
+ new_pact_hash [ 'messages' ] = add_ids ( messages , overwrite_existing_id )
42
54
end
43
55
Content . from_hash ( new_pact_hash )
44
56
end
@@ -64,6 +76,10 @@ def interactions
64
76
pact_hash . is_a? ( Hash ) ? pact_hash [ 'interactions' ] : nil
65
77
end
66
78
79
+ def messages_or_interactions
80
+ messages || interactions
81
+ end
82
+
67
83
def pact_specification_version
68
84
maybe_pact_specification_version_1 = pact_hash [ 'metadata' ] [ 'pactSpecification' ] [ 'version' ] rescue nil
69
85
maybe_pact_specification_version_2 = pact_hash [ 'metadata' ] [ 'pact-specification' ] [ 'version' ] rescue nil
@@ -75,13 +91,17 @@ def pact_specification_version
75
91
76
92
attr_reader :pact_hash
77
93
78
- def add_ids ( interactions )
94
+ def add_ids ( interactions , overwrite_existing_id )
79
95
interactions . map do | interaction |
80
96
if interaction . is_a? ( Hash )
81
- # just in case there is a previous ID in there
82
- interaction_without_id = interaction . reject { |k , _ | k == "_id" }
83
- # make the _id the first key in the hash when rendered to JSON
84
- { "_id" => generate_interaction_sha ( interaction_without_id ) } . merge ( interaction )
97
+ if !interaction . key? ( "_id" ) || overwrite_existing_id
98
+ # just in case there is a previous ID in there
99
+ interaction_without_id = interaction . reject { |k , _ | k == "_id" }
100
+ # make the _id the first key in the hash when rendered to JSON
101
+ { "_id" => generate_interaction_sha ( interaction_without_id ) } . merge ( interaction )
102
+ else
103
+ interaction
104
+ end
85
105
else
86
106
interaction
87
107
end
0 commit comments