From 691ddae5915b1ad782ab1eb4c8d0201660119933 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Fri, 6 Sep 2024 11:15:59 +0700 Subject: [PATCH 1/2] fix: Fix can't compare 2 eachKey matching rules, 2 eachValue matching rules --- rust/pact_models/src/matchingrules/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust/pact_models/src/matchingrules/mod.rs b/rust/pact_models/src/matchingrules/mod.rs index 606766df..81b03d68 100644 --- a/rust/pact_models/src/matchingrules/mod.rs +++ b/rust/pact_models/src/matchingrules/mod.rs @@ -537,6 +537,8 @@ impl PartialEq for MatchingRule { (MatchingRule::Include(str1), MatchingRule::Include(str2)) => str1 == str2, (MatchingRule::ContentType(str1), MatchingRule::ContentType(str2)) => str1 == str2, (MatchingRule::ArrayContains(variants1), MatchingRule::ArrayContains(variants2)) => variants1 == variants2, + (MatchingRule::EachKey(definition1), MatchingRule::EachKey(definition2)) => definition1 == definition2, + (MatchingRule::EachValue(definition1), MatchingRule::EachValue(definition2)) => definition1 == definition2, _ => mem::discriminant(self) == mem::discriminant(other) } } From c2793764ad303c234e7bbc09d560d204e867a9a7 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Fri, 6 Sep 2024 11:40:19 +0700 Subject: [PATCH 2/2] test: Add test for eachKey and eachValue matching rules comparation --- .../src/patterns/special_rules.rs | 6 +++--- .../src/matchingrules/expressions.rs | 2 +- rust/pact_models/src/matchingrules/mod.rs | 20 ++++++++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/rust/pact_consumer/src/patterns/special_rules.rs b/rust/pact_consumer/src/patterns/special_rules.rs index 64329669..13a611f6 100644 --- a/rust/pact_consumer/src/patterns/special_rules.rs +++ b/rust/pact_consumer/src/patterns/special_rules.rs @@ -567,7 +567,7 @@ fn object_matching_test() { MatchingRule::EachKey(MatchingRuleDefinition::new("key1".to_string(), ValueType::String, MatchingRule::Regex("[a-z]{3}[0-9]".to_string()), None)), MatchingRule::EachValue(MatchingRuleDefinition::new("\"value1\"".to_string(), - ValueType::Unknown, MatchingRule::Type, None)) + ValueType::String, MatchingRule::Type, None)) ] }, rules); } @@ -786,7 +786,7 @@ fn each_value_is_pattern() { matchable.extract_matching_rules(DocPath::root(), &mut rules); expect!(rules).to(be_equal_to(matchingrules_list! { "body"; "$" => [ - MatchingRule::EachValue(MatchingRuleDefinition::new("100".to_string(), ValueType::String, + MatchingRule::EachValue(MatchingRuleDefinition::new("\"100\"".to_string(), ValueType::String, MatchingRule::Regex("\\d+".to_string()), None)) ] })); @@ -820,7 +820,7 @@ fn each_value_test() { result.extract_matching_rules(DocPath::root(), &mut rules); expect!(rules).to(be_equal_to(matchingrules_list! { "body"; "$" => [ - MatchingRule::EachValue(MatchingRuleDefinition::new("value1".to_string(), ValueType::Unknown, + MatchingRule::EachValue(MatchingRuleDefinition::new("\"value1\"".to_string(), ValueType::String, MatchingRule::Regex("[a-z]{5}[0-9]".to_string()), None)) ] })); diff --git a/rust/pact_models/src/matchingrules/expressions.rs b/rust/pact_models/src/matchingrules/expressions.rs index 80f39cc6..aae5c1fc 100644 --- a/rust/pact_models/src/matchingrules/expressions.rs +++ b/rust/pact_models/src/matchingrules/expressions.rs @@ -1676,7 +1676,7 @@ mod test { value_type: ValueType::Unknown, rules: vec![ Either::Left(MatchingRule::EachKey(MatchingRuleDefinition { value: "$.test.one".to_string(), value_type: ValueType::String, rules: vec![Either::Left(MatchingRule::Regex("\\$(\\.\\w+)+".to_string()))], generator: None } )), - Either::Left(MatchingRule::EachValue(MatchingRuleDefinition { value: "".to_string(), value_type: ValueType::Unknown, rules: vec![Either::Left(MatchingRule::Type)], generator: None } )) + Either::Left(MatchingRule::EachValue(MatchingRuleDefinition { value: "".to_string(), value_type: ValueType::String, rules: vec![Either::Left(MatchingRule::Type)], generator: None } )) ], generator: None })); diff --git a/rust/pact_models/src/matchingrules/mod.rs b/rust/pact_models/src/matchingrules/mod.rs index 81b03d68..21c0bed2 100644 --- a/rust/pact_models/src/matchingrules/mod.rs +++ b/rust/pact_models/src/matchingrules/mod.rs @@ -2160,7 +2160,7 @@ mod tests { ] }); expect!(MatchingRule::from_json(&json)).to(be_ok().value( - MatchingRule::EachValue(MatchingRuleDefinition::new("{\"price\": 1.23}".to_string(), + MatchingRule::EachValue(MatchingRuleDefinition::new("{\"price\":1.23}".to_string(), ValueType::Unknown, MatchingRule::Decimal, None))) ); } @@ -2635,4 +2635,22 @@ mod tests { } ) } + + #[test] + #[should_panic] + fn each_value_matching_rule_comparation_test() { + assert_eq!( + matchingrules_list!{"body"; "$.array_values" => [MatchingRule::EachValue(MatchingRuleDefinition::new("[\"string value\"]".to_string(), ValueType::Unknown, MatchingRule::Type, None))]}, + matchingrules_list!{"body"; "$.array_values" => [MatchingRule::EachValue(MatchingRuleDefinition::new("[\"something else\"]".to_string(), ValueType::Unknown, MatchingRule::Type, None))]} + ) + } + + #[test] + #[should_panic] + fn each_key_matching_rule_comparation_test() { + assert_eq!( + matchingrules_list!{"body"; "$.array_values" => [MatchingRule::EachKey(MatchingRuleDefinition::new("a_key".to_string(), ValueType::Unknown, MatchingRule::Type, None))]}, + matchingrules_list!{"body"; "$.array_values" => [MatchingRule::EachKey(MatchingRuleDefinition::new("another_key".to_string(), ValueType::Unknown, MatchingRule::Type, None))]} + ) + } }