Skip to content

Commit 533a7d1

Browse files
author
Charles Larivier
committed
feat: add support for MISSING kwargs in UpdateResource
1 parent b9c23ab commit 533a7d1

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

metabase/resource.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from metabase import Metabase
55
from requests import HTTPError
66

7+
from missing import MISSING
8+
79

810
class Resource:
911
ENDPOINT: str
@@ -65,7 +67,8 @@ def create(cls, **kwargs):
6567
class UpdateResource(Resource):
6668
def update(self, **kwargs) -> None:
6769
"""Update an instance."""
68-
response = self.connection().put(self.ENDPOINT + f"/{getattr(self, self.PRIMARY_KEY)}", json=kwargs)
70+
params = {k: v for k, v in kwargs.items() if v != MISSING}
71+
response = self.connection().put(self.ENDPOINT + f"/{getattr(self, self.PRIMARY_KEY)}", json=params)
6972

7073
if response.status_code != 200:
7174
raise HTTPError(response.json())

tests/test_resource.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from unittest import TestCase
2+
from unittest.mock import call, patch
3+
from requests import HTTPError
24

35
from exceptions import NotFoundError
46
from metabase.metabase import Metabase
57
from metabase.resource import CreateResource, DeleteResource, GetResource, ListResource, Resource, UpdateResource
8+
from missing import MISSING
69

710
from tests.helpers import IntegrationTestCase
811

@@ -92,6 +95,27 @@ class Collection(CreateResource, GetResource, UpdateResource):
9295
# metabase was updated
9396
self.assertEqual("My New Collection", Collection.get(collection.id).name)
9497

98+
def test_update_missing(self):
99+
"""Ensure UpdateResource.update() ignores arguments equal to MISSING."""
100+
class Collection(UpdateResource):
101+
ENDPOINT = "/api/collection"
102+
103+
test_matrix = [
104+
({"a": "A", "b": "B"}, {"a": "A", "b": "B"}),
105+
({"a": "A", "b": MISSING}, {"a": "A"}),
106+
({"a": MISSING, "b": MISSING}, {}),
107+
]
108+
109+
for kwargs, expected in test_matrix:
110+
with patch("metabase.resource.Metabase.put") as mock:
111+
try:
112+
Collection(id=1).update(**kwargs)
113+
except HTTPError:
114+
pass
115+
116+
self.assertTrue(mock.called)
117+
self.assertIsNone(mock.assert_called_with("/api/collection/1", json=expected))
118+
95119

96120
class DeleteResourceTests(IntegrationTestCase):
97121
def test_delete(self):

0 commit comments

Comments
 (0)