TypeError: 'list' object is not callable - assertWarns()

TypeError: 'list' object is not callable - assertWarns()

我有以下功能:

def checking_remote_local_groups(data: List[dict], groups: List[dict]) -> List[dict]:
    step = 'GET_GROUPS_TO_CHECK'
    groups_to_check = []
    local_groups = [x['group'] for x in data]

    # Using set to get differences between local and AAD groups
    local_groups_set = set(local_groups)
   
    groups_set = set(x['name'] for x in groups)
    # If local AAD groups set are not in AAD groups, raise an Error on the workflow.
    if local_groups_set - groups_set:
        # SyncException is implemented somewhere.
        raise SyncException(
            f'{local_groups_set - groups_set} local group is not an AAD group\nCreate it via speckle-infra terraform',
            "We need to get in line, local and AAD groups. Additional {} group received from users.yaml".format(
                local_groups_set - groups_set
            ),
            step
        )
    # If remote groups set are not in local groups 
    if groups_set - local_groups_set:
        # Just notify it to the user
        warnings.warn(
            f"""
            {groups_set - local_groups_set} AAD group is not on users.yml as a local group
            Please add it if needed
            """
            )
    # If remote groups are the same than local groups
    elif local_groups_set == groups_set:
        print("AAD and local groups are inline"
            )
    return groups_to_check

我想通过使用 assertWarns 断言进行单元测试来检查第二个 if 部分代码的行为(如果远程组集不在本地组中),因为在那种情况下我会触发警告。所以我有:

class MyTestCase(unittest.TestCase):

    def test_warning_checking_remote_group_not_in_local_groups(self):
        # local groups
        data = [{'group': 'foobar'}, {'group': 'test'}]
        
        # remote groups
        groups = [{'name': 'foobar'}, {'name': 'test'}, {'name': 'missing'}]
        self.assertWarns(
            UserWarning,
            checking_aad_local_groups(data, groups),
            [{"name": "missing"}]
        )

我得到了这个输出:

test_sync.py .F...........                                               [100%]

=================================== FAILURES ===================================
______ GroupsTestCase.test_warning_checking_aad_group_not_in_local_groups ______

self = <test_sync.GroupsTestCase testMethod=test_warning_checking_remote_group_not_in_local_groups>

    def test_warning_checking_remote_group_not_in_local_groups(self):
        data = [{'group': 'foobar'}, {'group': 'test'}]
        groups = [{'name': 'foobar'}, {'name': 'test'}, {'name': 'missing'}]
>       self.assertWarns(
            UserWarning,
            checking_aad_local_groups(data, groups),
            [{"name": "missing"}]
        )
E       TypeError: 'list' object is not callable

test_sync.py:158: TypeError
=============================== warnings summary ===============================
test_sync.py::GroupsTestCase::test_warning_checking_aad_group_not_in_local_groups
  /home/runner/work/speckle-user-management/speckle-user-management/groups.py:31: UserWarning: 
              *********************** WARNING ... ***********************
              {'missing'} AAD group is not on users.yml as a local group
              Please add it if needed
              *********************** WARNING ... ***********************
              
    warnings.warn(

-- Docs: docs.pytest.org/en/latest/warnings.html
=========================== short test summary info ============================
FAILED test_sync.py::GroupsTestCase::test_warning_checking_remote_group_not_in_local_groups

我知道我作为参数发送的词典列表可能不被接受或不适用于 assertWarns() 期望的数据类型?

尽管如此,显示了我预期的消息 ({'missing'} AAD group is not on users.yml as a local group, Please add it if needed),但我希望通过此测试。

什么是最好的使用方式assertWarns()

如果我使用 assertFalse,测试通过,虽然我不确定是否是我可以使用的最多 specific/better 断言。

当您调用 assertWarns(UserWarning, checking_aad_local_groups(...)) 时,您正在调用 checking_aad_local_groups 并将结果传递给 assertWarns。您想给 assertWarns 一个可调用对象,以便它可以将其包装在警告检测代码中。

self.assertWarns(UserWarning, checking_aad_local_groups, (data, groups))

或者,您可以将其用作上下文管理器

with self.assertWarns() as w:
   checking_aad_local_groups(data, groups)