Skip to content

Commit

Permalink
[PY-624][PY-625] Add creating/updating a property - backported to cli…
Browse files Browse the repository at this point in the history
…ent (#755)

* add client.create_property & client.update_property

* add correct API calls + update tests

* update tests - using base_property_object
  • Loading branch information
saurbhc authored Dec 19, 2023
1 parent dc3f887 commit 7cee58c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
23 changes: 23 additions & 0 deletions darwin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@
ValidationError,
)
from darwin.future.core.client import ClientCore, DarwinConfig
from darwin.future.core.properties import create_property as create_property_future
from darwin.future.core.properties import (
get_team_full_properties as get_team_full_properties_future,
)
from darwin.future.core.properties import (
get_team_properties as get_team_properties_future,
)
from darwin.future.core.properties import update_property as update_property_future
from darwin.future.core.types.common import JSONDict
from darwin.future.data_objects.properties import FullProperty
from darwin.item import DatasetItem
from darwin.utils import (
Expand Down Expand Up @@ -1499,3 +1502,23 @@ def get_team_properties(
client=future_client,
team_slug=team_slug or self.default_team,
)

def create_property(self, team_slug: Optional[str], params: Union[FullProperty, JSONDict]) -> FullProperty:
darwin_config = DarwinConfig.from_old(self.config)
future_client = ClientCore(darwin_config)

return create_property_future(
client=future_client,
team_slug=team_slug or self.default_team,
params=params,
)

def update_property(self, team_slug: Optional[str], params: Union[FullProperty, JSONDict]) -> FullProperty:
darwin_config = DarwinConfig.from_old(self.config)
future_client = ClientCore(darwin_config)

return update_property_future(
client=future_client,
team_slug=team_slug or self.default_team,
params=params,
)
70 changes: 70 additions & 0 deletions tests/darwin/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from darwin.dataset.remote_dataset_v2 import RemoteDatasetV2
from darwin.datatypes import Feature, JSONFreeForm
from darwin.exceptions import NameTaken, NotFound
from darwin.future.data_objects.properties import FullProperty
from darwin.future.tests.core.fixtures import * # noqa: F401, F403
from tests.fixtures import * # noqa: F401, F403


Expand Down Expand Up @@ -464,3 +466,71 @@ def test_get_team_properties(self, darwin_client: Client) -> None:
status=200,
)
assert len(darwin_client.get_team_properties()) == 1


@pytest.mark.usefixtures("file_read_write_test")
class TestCreateProperty:
@responses.activate
def test_create_property(self, darwin_client: Client, base_property_object: FullProperty) -> None:
responses.add(
responses.POST,
"http://localhost/apiv2/teams/v7-darwin-json-v1/properties",
json=base_property_object.dict(),
status=200,
)
_property = darwin_client.create_property(
team_slug="v7-darwin-json-v1",
params=base_property_object
)
assert isinstance(_property, FullProperty)
assert _property == base_property_object

@responses.activate
def test_create_property_from_json(self, darwin_client: Client, base_property_object: FullProperty) -> None:
responses.add(
responses.POST,
"http://localhost/apiv2/teams/v7-darwin-json-v1/properties",
json=base_property_object.dict(),
status=200,
)
_property = darwin_client.create_property(
team_slug="v7-darwin-json-v1",
params=base_property_object.dict()
)
assert isinstance(_property, FullProperty)
assert _property == base_property_object


@pytest.mark.usefixtures("file_read_write_test")
class TestUpdateProperty:
@responses.activate
def test_update_property(self, darwin_client: Client, base_property_object: FullProperty) -> None:
property_id = base_property_object.id
responses.add(
responses.PUT,
f"http://localhost/apiv2/teams/v7-darwin-json-v1/properties/{property_id}",
json=base_property_object.dict(),
status=200,
)
_property = darwin_client.update_property(
team_slug="v7-darwin-json-v1",
params=base_property_object
)
assert isinstance(_property, FullProperty)
assert _property == base_property_object

@responses.activate
def test_update_property_from_json(self, darwin_client: Client, base_property_object: FullProperty) -> None:
property_id = base_property_object.id
responses.add(
responses.PUT,
f"http://localhost/apiv2/teams/v7-darwin-json-v1/properties/{property_id}",
json=base_property_object.dict(),
status=200,
)
_property = darwin_client.update_property(
team_slug="v7-darwin-json-v1",
params=base_property_object.dict()
)
assert isinstance(_property, FullProperty)
assert _property == base_property_object

0 comments on commit 7cee58c

Please sign in to comment.