From 81adf84032e44994d0bfb17706c133a421ed335f Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Thu, 8 Aug 2024 08:44:47 +0400 Subject: [PATCH] fix(rnd): Fix flaky CurrentDateAndTimeBlock test (#7741) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### 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. --- .../autogpt_server/blocks/time_blocks.py | 14 ++++++++++++-- rnd/autogpt_server/autogpt_server/util/test.py | 8 ++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/rnd/autogpt_server/autogpt_server/blocks/time_blocks.py b/rnd/autogpt_server/autogpt_server/blocks/time_blocks.py index e3f48bc22..7e375717b 100644 --- a/rnd/autogpt_server/autogpt_server/blocks/time_blocks.py +++ b/rnd/autogpt_server/autogpt_server/blocks/time_blocks.py @@ -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. + ), ], ) diff --git a/rnd/autogpt_server/autogpt_server/util/test.py b/rnd/autogpt_server/autogpt_server/util/test.py index ac9268d35..47095c9a3 100644 --- a/rnd/autogpt_server/autogpt_server/util/test.py +++ b/rnd/autogpt_server/autogpt_server/util/test.py @@ -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}`")