Merge pull request #197 from Orange-OpenSource/feature/count-equals-predicate-error

Improve error message for countEquals predicate
This commit is contained in:
Fabrice Reix 2021-06-11 09:10:01 +02:00 committed by GitHub
commit 3204970172
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 37 deletions

View File

@ -87,3 +87,11 @@ error: Assert Failure
| expected: not something
|
error: Assert Failure
--> tests/error_predicate.hurl:16:0
|
16 | jsonpath "$.list" countEquals 2
| actual: 3
| expected: 2
|

View File

@ -1 +1 @@
<div class="hurl-file"><div class="hurl-entry"><div class="request"><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/predicate/error/type</span></span></div><div class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="status">200</span></span><span class="line section-header">[Asserts]</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.status"</span> <span class="predicate-type">equals</span> <span class="string">"true"</span></span><span class="line"><span class="comment">#jsonpath "$.count" equals "0"</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.count"</span> <span class="predicate-type">equals</span> <span class="number">0</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.message"</span> <span class="predicate-type">equals</span> <span class="number">0</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.empty"</span> <span class="predicate-type">equals</span> <span class="number">0</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.number"</span> <span class="predicate-type">equals</span> <span class="number">1.1</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.message"</span> <span class="predicate-type">startsWith</span> <span class="string">"hi"</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.message"</span> <span class="predicate-type">contains</span> <span class="string">"hi"</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.message"</span> <span class="predicate-type">matches</span> <span class="string">"hi"</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.message"</span> <span class="predicate-type">equals</span> <span class="number">1</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.toto"</span> <span class="predicate-type">exists</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.message"</span> not <span class="predicate-type">exists</span></span></div></div></div>
<div class="hurl-file"><div class="hurl-entry"><div class="request"><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/predicate/error/type</span></span></div><div class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="status">200</span></span><span class="line section-header">[Asserts]</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.status"</span> <span class="predicate-type">equals</span> <span class="string">"true"</span></span><span class="line"><span class="comment">#jsonpath "$.count" equals "0"</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.count"</span> <span class="predicate-type">equals</span> <span class="number">0</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.message"</span> <span class="predicate-type">equals</span> <span class="number">0</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.empty"</span> <span class="predicate-type">equals</span> <span class="number">0</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.number"</span> <span class="predicate-type">equals</span> <span class="number">1.1</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.message"</span> <span class="predicate-type">startsWith</span> <span class="string">"hi"</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.message"</span> <span class="predicate-type">contains</span> <span class="string">"hi"</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.message"</span> <span class="predicate-type">matches</span> <span class="string">"hi"</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.message"</span> <span class="predicate-type">equals</span> <span class="number">1</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.toto"</span> <span class="predicate-type">exists</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.message"</span> not <span class="predicate-type">exists</span></span><span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.list"</span> <span class="predicate-type">equals</span> <span class="number">2</span></span></div></div></div>

View File

@ -13,3 +13,4 @@ jsonpath "$.message" matches "hi"
jsonpath "$.message" countEquals 1
jsonpath "$.toto" exists
jsonpath "$.message" not exists
jsonpath "$.list" countEquals 2

View File

@ -1 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/predicate/error/type"},"response":{"version":"HTTP/1.0","status":200,"asserts":[{"query":{"type":"jsonpath","expr":"$.status"},"predicate":{"type":"equal","value":"true"}},{"query":{"type":"jsonpath","expr":"$.count"},"predicate":{"type":"equal","value":0}},{"query":{"type":"jsonpath","expr":"$.message"},"predicate":{"type":"equal","value":0}},{"query":{"type":"jsonpath","expr":"$.empty"},"predicate":{"type":"equal","value":0}},{"query":{"type":"jsonpath","expr":"$.number"},"predicate":{"type":"equal","value":1.1}},{"query":{"type":"jsonpath","expr":"$.message"},"predicate":{"type":"start-with","value":"hi"}},{"query":{"type":"jsonpath","expr":"$.message"},"predicate":{"type":"contain","value":"hi"}},{"query":{"type":"jsonpath","expr":"$.message"},"predicate":{"type":"match","value":"hi"}},{"query":{"type":"jsonpath","expr":"$.message"},"predicate":{"type":"count","value":1}},{"query":{"type":"jsonpath","expr":"$.toto"},"predicate":{"type":"exist"}},{"query":{"type":"jsonpath","expr":"$.message"},"predicate":{"not":true,"type":"exist"}}]}}]}
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/predicate/error/type"},"response":{"version":"HTTP/1.0","status":200,"asserts":[{"query":{"type":"jsonpath","expr":"$.status"},"predicate":{"type":"equal","value":"true"}},{"query":{"type":"jsonpath","expr":"$.count"},"predicate":{"type":"equal","value":0}},{"query":{"type":"jsonpath","expr":"$.message"},"predicate":{"type":"equal","value":0}},{"query":{"type":"jsonpath","expr":"$.empty"},"predicate":{"type":"equal","value":0}},{"query":{"type":"jsonpath","expr":"$.number"},"predicate":{"type":"equal","value":1.1}},{"query":{"type":"jsonpath","expr":"$.message"},"predicate":{"type":"start-with","value":"hi"}},{"query":{"type":"jsonpath","expr":"$.message"},"predicate":{"type":"contain","value":"hi"}},{"query":{"type":"jsonpath","expr":"$.message"},"predicate":{"type":"match","value":"hi"}},{"query":{"type":"jsonpath","expr":"$.message"},"predicate":{"type":"count","value":1}},{"query":{"type":"jsonpath","expr":"$.toto"},"predicate":{"type":"exist"}},{"query":{"type":"jsonpath","expr":"$.message"},"predicate":{"not":true,"type":"exist"}},{"query":{"type":"jsonpath","expr":"$.list"},"predicate":{"type":"count","value":2}}]}}]}

View File

@ -2,4 +2,11 @@ from tests import app
@app.route("/predicate/error/type")
def predicate_error_type():
return '{ "status": true, "message": "0", "count": 1, "empty": "", "number": 1.0 }'
return '''{
"status": true,
"message": "0",
"count": 1,
"empty": "",
"number": 1.0,
"list": [1,2,3]
}'''

View File

@ -379,30 +379,26 @@ fn eval_something(
PredicateFuncValue::CountEqual {
value: expected_value,
..
} => {
let actual = value.clone().display();
let expected = format!("count equals to <{}>", expected_value);
match value {
Value::List(values) => Ok(AssertResult {
success: values.len() as u64 == expected_value,
actual,
expected,
type_mismatch: false,
}),
Value::Nodeset(n) => Ok(AssertResult {
success: n as u64 == expected_value,
actual,
expected,
type_mismatch: false,
}),
_ => Ok(AssertResult {
success: false,
actual,
expected,
type_mismatch: true,
}),
}
}
} => match value {
Value::List(values) => Ok(AssertResult {
success: values.len() as u64 == expected_value,
actual: values.len().to_string(),
expected: expected_value.to_string(),
type_mismatch: false,
}),
Value::Nodeset(n) => Ok(AssertResult {
success: n as u64 == expected_value,
actual: n.to_string(),
expected: expected_value.to_string(),
type_mismatch: false,
}),
_ => Ok(AssertResult {
success: false,
actual: value.clone().display(),
expected: format!("count equals to <{}>", expected_value),
type_mismatch: true,
}),
},
// starts with string
PredicateFuncValue::StartWith {
@ -1303,8 +1299,8 @@ mod tests {
.unwrap();
assert_eq!(assert_result.success, false);
assert_eq!(assert_result.type_mismatch, false);
assert_eq!(assert_result.actual.as_str(), "[]");
assert_eq!(assert_result.expected.as_str(), "count equals to <1>");
assert_eq!(assert_result.actual.as_str(), "0");
assert_eq!(assert_result.expected.as_str(), "1");
let assert_result = eval_something(
PredicateFunc {
@ -1320,8 +1316,8 @@ mod tests {
.unwrap();
assert_eq!(assert_result.success, false);
assert_eq!(assert_result.type_mismatch, false);
assert_eq!(assert_result.actual.as_str(), "nodeset of size <3>");
assert_eq!(assert_result.expected.as_str(), "count equals to <1>");
assert_eq!(assert_result.actual.as_str(), "3");
assert_eq!(assert_result.expected.as_str(), "1");
}
#[test]
@ -1345,8 +1341,8 @@ mod tests {
.unwrap();
assert_eq!(assert_result.success, true);
assert_eq!(assert_result.type_mismatch, false);
assert_eq!(assert_result.actual.as_str(), "[int <1>]");
assert_eq!(assert_result.expected.as_str(), "count equals to <1>");
assert_eq!(assert_result.actual.as_str(), "1");
assert_eq!(assert_result.expected.as_str(), "1");
let assert_result = eval_something(
PredicateFunc {
@ -1362,8 +1358,8 @@ mod tests {
.unwrap();
assert_eq!(assert_result.success, true);
assert_eq!(assert_result.type_mismatch, false);
assert_eq!(assert_result.actual.as_str(), "nodeset of size <1>");
assert_eq!(assert_result.expected.as_str(), "count equals to <1>");
assert_eq!(assert_result.actual.as_str(), "1");
assert_eq!(assert_result.expected.as_str(), "1");
}
#[test]

View File

@ -304,8 +304,8 @@ mod tests {
predicate_result: Some(Err(Error {
source_info: SourceInfo::init(1, 0, 1, 0),
inner: RunnerError::AssertFailure {
actual: "nodeset of size <2>".to_string(),
expected: "count equals to <3>".to_string(),
actual: "2".to_string(),
expected: "3".to_string(),
type_mismatch: false,
},
assert: true,