fix(rnd): Fix flaky CurrentDateAndTimeBlock test (#7741)

### Background

CurrentDateAndTimeBlock would fail if the test is not complete within 1-second wall-time.
In the case a test started at the second 01:59:59, it becomes flaky.

We can change the test to only assert the type. But this is also a good chance to add more assertion options for Block: a custom function.

### Changes 🏗️

Change assertion for the time block using an additional margin of error.
pull/7728/head^2
Zamil Majdy 2024-08-08 08:44:47 +04:00 committed by GitHub
parent f8d07a27af
commit 81adf84032
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 4 deletions

View File

@ -51,7 +51,11 @@ class CurrentDateBlock(Block):
{"trigger": "Hello", "format": "{date}", "offset": "7"},
],
test_output=[
("date", (datetime.now() - timedelta(days=7)).strftime("%Y-%m-%d")),
(
"date",
lambda t: abs(datetime.now() - datetime.strptime(t, "%Y-%m-%d"))
< timedelta(days=8), # 7 days difference + 1 day error margin.
),
],
)
@ -82,7 +86,13 @@ class CurrentDateAndTimeBlock(Block):
{"trigger": "Hello", "format": "{date_time}"},
],
test_output=[
("date_time", time.strftime("%Y-%m-%d %H:%M:%S")),
(
"date_time",
lambda t: abs(
datetime.now() - datetime.strptime(t, "%Y-%m-%d %H:%M:%S")
)
< timedelta(seconds=10), # 10 seconds error margin.
),
],
)

View File

@ -113,10 +113,14 @@ def execute_block_test(block: Block):
ex_output_name, ex_output_data = block.test_output[output_index]
def compare(data, expected_data):
if isinstance(expected_data, type):
if data == expected_data:
is_matching = True
elif isinstance(expected_data, type):
is_matching = isinstance(data, expected_data)
elif callable(expected_data):
is_matching = expected_data(data)
else:
is_matching = data == expected_data
is_matching = False
mark = "" if is_matching else ""
log(f"{prefix} {mark} comparing `{data}` vs `{expected_data}`")