From 43a7c6d07058e439726ae920ff46e2462ca22ca5 Mon Sep 17 00:00:00 2001 From: tdstein Date: Tue, 2 Apr 2024 15:49:36 -0400 Subject: [PATCH] style: replace unpacking usage with dict.update --- src/posit/connect/permissions.py | 15 +++++----- tests/posit/connect/test_content.py | 4 ++- tests/posit/connect/test_permissions.py | 8 ++---- tests/posit/connect/test_users.py | 37 +++++++++++++++++-------- 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/posit/connect/permissions.py b/src/posit/connect/permissions.py index fa21775f..48b06dbc 100644 --- a/src/posit/connect/permissions.py +++ b/src/posit/connect/permissions.py @@ -55,18 +55,17 @@ def update(self, *args, **kwargs) -> None: def update(self, *args, **kwargs) -> None: """Update the permission.""" - body = dict(*args, **kwargs) + body = { + "principal_guid": self.principal_guid, + "principal_type": self.principal_type, + "role": self.role, + } + body.update(*args, **kwargs) path = f"v1/content/{self.content_guid}/permissions/{self.id}" url = urls.append_path(self.config.url, path) response = self.session.put( url, - json={ - "principal_guid": self.principal_guid, - "principal_type": self.principal_type, - "role": self.role, - # shorthand to overwrite the above fields with method arguments - **body, - }, + json=body, ) super().update(**response.json()) diff --git a/tests/posit/connect/test_content.py b/tests/posit/connect/test_content.py index f9dc0263..3d7c90e6 100644 --- a/tests/posit/connect/test_content.py +++ b/tests/posit/connect/test_content.py @@ -19,9 +19,11 @@ def test_update(self): assert content.guid == guid new_name = "New Name" + fake_content = load_mock(f"v1/content/{guid}.json") + fake_content.update(name=new_name) responses.patch( f"https://connect.example/__api__/v1/content/{guid}", - json={**load_mock(f"v1/content/{guid}.json"), "name": new_name}, + json=fake_content, ) content.update(name=new_name) diff --git a/tests/posit/connect/test_permissions.py b/tests/posit/connect/test_permissions.py index 7ca2acb5..e6265297 100644 --- a/tests/posit/connect/test_permissions.py +++ b/tests/posit/connect/test_permissions.py @@ -96,17 +96,15 @@ def test_role_update(self): id = "94" content_guid = "f2f37341-e21d-3d80-c698-a935ad614066" - fake_permission = { - **load_mock(f"v1/content/{content_guid}/permissions/{id}.json"), - "role": old_role, - } + fake_permission = load_mock(f"v1/content/{content_guid}/permissions/{id}.json") + fake_permission.update(role=new_role) # define api behavior id = random.randint(0, 100) content_guid = str(uuid.uuid4()) responses.put( f"https://connect.example/__api__/v1/content/{content_guid}/permissions/{id}", - json={**fake_permission, "role": new_role}, + json=fake_permission, match=[ matchers.json_params_matcher( { diff --git a/tests/posit/connect/test_users.py b/tests/posit/connect/test_users.py index b30ce4e6..8073ef56 100644 --- a/tests/posit/connect/test_users.py +++ b/tests/posit/connect/test_users.py @@ -289,23 +289,24 @@ def test_default(self): @responses.activate def test_params(self): - # validate input params are propagated to the query params - params = {"key1": "value1", "key2": "value2", "key3": "value3"} - responses.get( + mock_get = responses.get( "https://connect.example/__api__/v1/users", match=[ responses.matchers.query_param_matcher( - {"page_size": 500, "page_number": 1, **params} + { + "page_size": 500, + "page_number": 1, + "key1": "value1", + "key2": "value2", + "key3": "value3", + } ) ], json=load_mock("v1/users?page_number=1&page_size=500.jsonc"), ) con = Client(api_key="12345", url="https://connect.example/") - con.users.find_one(**params) - responses.assert_call_count( - "https://connect.example/__api__/v1/users?key1=value1&key2=value2&key3=value3&page_number=1&page_size=500", - 1, - ) + con.users.find_one(key1="value1", key2="value2", key3="value3") + assert mock_get.call_count == 1 @responses.activate def test_empty_results(self): @@ -366,7 +367,13 @@ def test_params(self): "https://connect.example/__api__/v1/users", match=[ responses.matchers.query_param_matcher( - {"page_size": 500, "page_number": 1, **params} + { + "page_size": 500, + "page_number": 1, + "key1": "value1", + "key2": "value2", + "key3": "value3", + } ) ], json=load_mock("v1/users?page_number=1&page_size=500.jsonc"), @@ -375,13 +382,19 @@ def test_params(self): "https://connect.example/__api__/v1/users", match=[ responses.matchers.query_param_matcher( - {"page_size": 500, "page_number": 2, **params} + { + "page_size": 500, + "page_number": 2, + "key1": "value1", + "key2": "value2", + "key3": "value3", + } ) ], json=load_mock("v1/users?page_number=2&page_size=500.jsonc"), ) con = Client(api_key="12345", url="https://connect.example/") - con.users.find(**params) + con.users.find_one(key1="value1", key2="value2", key3="value3") responses.assert_call_count( "https://connect.example/__api__/v1/users?key1=value1&key2=value2&key3=value3&page_number=1&page_size=500", 1,