“not in”不适用于 python 中具有单个值的情况

问题描述 投票:0回答:1

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")

上面的行失败并显示以下错误消息。

但是如果我使用 != 而不是“不在”,则可以。

(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                                                                                                                                                                                                                  

tests\test_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

tests\test_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 =================================================================================================== 

上面已经解释过了。 “!=”有效,但“不在”无效。

python lambda notin
1个回答
0
投票

@Barmar 回答了这个问题。

("prefix2/prefix3/file1") 应该是 ("prefix2/prefix3/file1",) 使其成为一个只有一个元素的元组。

© www.soinside.com 2019 - 2024. All rights reserved.