Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional name in assertion message #1200

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions esrally/driver/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def smaller_than_or_equal(self, expected, actual):
def equal(self, expected, actual):
return actual == expected

def check_assertion(self, assertion, properties):
def check_assertion(self, op_name, assertion, properties):
path = assertion["property"]
predicate_name = assertion["condition"]
expected_value = assertion["value"]
Expand All @@ -362,18 +362,24 @@ def check_assertion(self, assertion, properties):
predicate = self.predicates[predicate_name]
success = predicate(expected_value, actual_value)
if not success:
raise exceptions.RallyTaskAssertionError(
f"Expected [{path}] to be {predicate_name} [{expected_value}] but was [{actual_value}].")
if op_name:
msg = f"Expected [{path}] in [{op_name}] to be {predicate_name} [{expected_value}] but was [{actual_value}]."
else:
msg = f"Expected [{path}] to be {predicate_name} [{expected_value}] but was [{actual_value}]."

raise exceptions.RallyTaskAssertionError(msg)

async def __call__(self, *args):
params = args[1]
return_value = await self.delegate(*args)
if AssertingRunner.assertions_enabled and "assertions" in params:
op_name = params.get("name")
if isinstance(return_value, dict):
for assertion in params["assertions"]:
self.check_assertion(assertion, return_value)
self.check_assertion(op_name, assertion, return_value)
else:
self.logger.debug("Skipping assertion check as [%s] does not return a dict.", repr(self.delegate))
self.logger.debug("Skipping assertion check in [%s] as [%s] does not return a dict.",
op_name, repr(self.delegate))
return return_value

def __repr__(self, *args, **kwargs):
Expand Down
5 changes: 4 additions & 1 deletion tests/driver/runner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ async def test_asserts_equal_succeeds(self):
r = runner.AssertingRunner(delegate)
async with r:
final_response = await r(es, {
"name": "test-task",
"assertions": [
{
"property": "hits.hits.value",
Expand Down Expand Up @@ -193,9 +194,10 @@ async def test_asserts_equal_fails(self):
delegate.return_value = as_future(response)
r = runner.AssertingRunner(delegate)
with self.assertRaisesRegex(exceptions.RallyTaskAssertionError,
r"Expected \[hits.hits.relation\] to be == \[eq\] but was \[gte\]."):
r"Expected \[hits.hits.relation\] in \[test-task\] to be == \[eq\] but was \[gte\]."):
async with r:
await r(es, {
"name": "test-task",
"assertions": [
{
"property": "hits.hits.value",
Expand All @@ -219,6 +221,7 @@ async def test_skips_asserts_for_non_dicts(self):
r = runner.AssertingRunner(delegate)
async with r:
final_response = await r(es, {
"name": "test-task",
"assertions": [
{
"property": "hits.hits.value",
Expand Down