https://github.com/mohdluqmancse/purge_glue/blob/main/tests/test_s3_objects.py
mock_check_s3_uri_exists.side_effect = lambda bucket, key: key not in ("prefix2/prefix3/file1")
The above line fails with the below error message.
but works if I use != instead of not in.
(venv) purge_glue>pytest tests
======================================================================================================= test session starts =======================================================================================================
platform win32 -- Python 3.12.3, pytest-8.3.2, pluggy-1.5.0
rootdir: purge_glue
collected 5 items
teststest_s3_objects.py ....F [100%]
============================================================================================================ FAILURES =============================================================================================================
_______________________________________________________________________________ TestTreeFunctions.test_traverse_and_mark_for_deletion_breadth_first _______________________________________________________________________________
self = <tests.test_s3_objects.TestTreeFunctions testMethod=test_traverse_and_mark_for_deletion_breadth_first>, mock_check_s3_uri_exists = <MagicMock name='check_s3_uri_exists' id='2190747349648'>
@patch("scripts_test.s3_objects.check_s3_uri_exists")
def test_traverse_and_mark_for_deletion_breadth_first(self, mock_check_s3_uri_exists):
# Define the bucket name
bucket_name = "bucket-name"
# Mock S3 existence checks
# not in clause is not working properly when you have forward slashes
mock_check_s3_uri_exists.side_effect = lambda bucket, key: key not in ("prefix2/prefix3/file1")
#mock_check_s3_uri_exists.side_effect = lambda bucket, key: key != "prefix2/prefix3/file1"
# Traverse and clean the tree
nodes_to_delete = traverse_and_mark_for_deletion_breadth_first(self.tree, bucket_name)
prefixes = [f"{node[1]}" for node in nodes_to_delete]
expected_nodes_to_delete = ["prefix2/prefix3/file1"]
> assert set(prefixes) == set(expected_nodes_to_delete), f"Expected {expected_nodes_to_delete} but got {prefixes}"
E AssertionError: Expected ['prefix2/prefix3/file1'] but got ['prefix2']
E assert {'prefix2'} == {'prefix2/prefix3/file1'}
E
E Extra items in the left set:
E 'prefix2'
E Extra items in the right set:
E 'prefix2/prefix3/file1'
E Use -v to get more diff
teststest_s3_objects.py:87: AssertionError
===================================================================================================== short test summary info =====================================================================================================
FAILED tests/test_s3_objects.py::TestTreeFunctions::test_traverse_and_mark_for_deletion_breadth_first - AssertionError: Expected ['prefix2/prefix3/file1'] but got ['prefix2']
=================================================================================================== 1 failed, 4 passed in 5.10s ===================================================================================================
Explained above. != works but not in doesn’t work.
New contributor
Mohammed Luqman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.