-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PY-576][PY-581] Create + Update of properties (#756)
* create update scaffold * create + property changes * linting * linting * typing * create + update: including e2e tests * removing unnecessary import * removing bad comments * test cleanup * linting * unit tests
- Loading branch information
1 parent
a2b61f6
commit dc3f887
Showing
18 changed files
with
731 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from darwin.future.core.properties.create import create_property | ||
from darwin.future.core.properties.get import ( | ||
get_property_by_id, | ||
get_team_full_properties, | ||
get_team_properties, | ||
) | ||
from darwin.future.core.properties.update import update_property, update_property_value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import Optional, Union | ||
|
||
from pydantic import parse_obj_as | ||
|
||
from darwin.future.core.client import ClientCore | ||
from darwin.future.core.types.common import JSONDict | ||
from darwin.future.data_objects.properties import FullProperty | ||
|
||
|
||
def create_property( | ||
client: ClientCore, | ||
params: Union[FullProperty, JSONDict], | ||
team_slug: Optional[str] = None, | ||
) -> FullProperty: | ||
""" | ||
Creates a property for the specified team slug. | ||
Parameters: | ||
client (ClientCore): The client to use for the request. | ||
team_slug (Optional[str]): The slug of the team to get. If not specified, the | ||
default team from the client's config will be used. | ||
params (Optional[JSONType]): The JSON data to use for the request. | ||
Returns: | ||
FullProperty: FullProperty object for the created property. | ||
Raises: | ||
HTTPError: If the response status code is not in the 200-299 range. | ||
""" | ||
if not team_slug: | ||
team_slug = client.config.default_team | ||
if isinstance(params, FullProperty): | ||
params = params.to_create_endpoint() | ||
response = client.post(f"/v2/teams/{team_slug}/properties", data=params) | ||
assert isinstance(response, dict) | ||
return parse_obj_as(FullProperty, response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from typing import Optional, Union | ||
|
||
from pydantic import parse_obj_as | ||
|
||
from darwin.future.core.client import ClientCore | ||
from darwin.future.core.types.common import JSONDict | ||
from darwin.future.data_objects.properties import FullProperty, PropertyValue | ||
|
||
|
||
def update_property( | ||
client: ClientCore, | ||
params: Union[FullProperty, JSONDict], | ||
team_slug: Optional[str] = None, | ||
) -> FullProperty: | ||
""" | ||
Updates a property for the specified team slug. | ||
Parameters: | ||
client (ClientCore): The client to use for the request. | ||
team_slug (Optional[str]): The slug of the team to get. If not specified, the | ||
default team from the client's config will be used. | ||
params (Optional[JSONType]): The JSON data to use for the request. | ||
Returns: | ||
FullProperty: FullProperty object for the created property. | ||
Raises: | ||
HTTPError: If the response status code is not in the 200-299 range. | ||
""" | ||
if not team_slug: | ||
team_slug = client.config.default_team | ||
if isinstance(params, FullProperty): | ||
id, params = params.to_update_endpoint() | ||
else: | ||
id = params.get("id") | ||
del params["id"] | ||
response = client.put(f"/v2/teams/{team_slug}/properties/{id}", data=params) | ||
assert isinstance(response, dict) | ||
return parse_obj_as(FullProperty, response) | ||
|
||
|
||
def update_property_value( | ||
client: ClientCore, | ||
params: Union[PropertyValue, JSONDict], | ||
item_id: str, | ||
team_slug: Optional[str] = None, | ||
) -> PropertyValue: | ||
""" | ||
Updates a property value for the specified property id. | ||
Parameters: | ||
client (ClientCore): The client to use for the request. | ||
team_slug (Optional[str]): The slug of the team to get. If not specified, the | ||
default team from the client's config will be used. | ||
params (Optional[JSONType]): The JSON data to use for the request. | ||
Returns: | ||
FullProperty: FullProperty object for the created property. | ||
Raises: | ||
HTTPError: If the response status code is not in the 200-299 range. | ||
""" | ||
if not team_slug: | ||
team_slug = client.config.default_team | ||
if isinstance(params, PropertyValue): | ||
id, params = params.to_update_endpoint() | ||
else: | ||
id = params.get("id") | ||
del params["id"] | ||
response = client.put( | ||
f"/v2/teams/{team_slug}/properties/{item_id}/property_values/{id}", data=params | ||
) | ||
assert isinstance(response, dict) | ||
return parse_obj_as(PropertyValue, response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import responses | ||
|
||
from darwin.future.core.client import ClientCore | ||
from darwin.future.core.properties import create_property | ||
from darwin.future.data_objects.properties import FullProperty | ||
from darwin.future.tests.core.fixtures import * | ||
|
||
|
||
@responses.activate | ||
def test_create_property( | ||
base_client: ClientCore, base_property_object: FullProperty | ||
) -> None: | ||
# Mocking the response using responses library | ||
responses.add( | ||
responses.POST, | ||
f"{base_client.config.base_url}api/v2/teams/{base_client.config.default_team}/properties", | ||
json=base_property_object.dict(), | ||
status=200, | ||
) | ||
# Call the function being tested | ||
property = create_property( | ||
base_client, | ||
params=base_property_object, | ||
team_slug=base_client.config.default_team, | ||
) | ||
|
||
# Assertions | ||
assert isinstance(property, FullProperty) | ||
assert property == base_property_object | ||
|
||
|
||
@responses.activate | ||
def test_create_property_from_json( | ||
base_client: ClientCore, base_property_object: FullProperty | ||
) -> None: | ||
json = base_property_object.to_create_endpoint() | ||
# Mocking the response using responses library | ||
responses.add( | ||
responses.POST, | ||
f"{base_client.config.base_url}api/v2/teams/{base_client.config.default_team}/properties", | ||
json=base_property_object.dict(), | ||
status=200, | ||
) | ||
# Call the function being tested | ||
property = create_property( | ||
base_client, params=json, team_slug=base_client.config.default_team | ||
) | ||
|
||
# Assertions | ||
assert isinstance(property, FullProperty) | ||
assert property == base_property_object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import responses | ||
|
||
from darwin.future.core.client import ClientCore | ||
from darwin.future.core.properties import update_property, update_property_value | ||
from darwin.future.data_objects.properties import FullProperty, PropertyValue | ||
from darwin.future.tests.core.fixtures import * | ||
|
||
|
||
@responses.activate | ||
def test_update_property( | ||
base_client: ClientCore, base_property_object: FullProperty | ||
) -> None: | ||
# Mocking the response using responses library | ||
responses.add( | ||
responses.PUT, | ||
f"{base_client.config.base_url}api/v2/teams/{base_client.config.default_team}/properties/{base_property_object.id}", | ||
json=base_property_object.dict(), | ||
status=200, | ||
) | ||
# Call the function being tested | ||
property = update_property( | ||
base_client, | ||
params=base_property_object, | ||
team_slug=base_client.config.default_team, | ||
) | ||
|
||
# Assertions | ||
assert isinstance(property, FullProperty) | ||
assert property == base_property_object | ||
|
||
|
||
@responses.activate | ||
def test_update_property_value( | ||
base_client: ClientCore, base_property_object: FullProperty | ||
) -> None: | ||
# Mocking the response using responses library | ||
item_id = base_property_object.id | ||
assert item_id | ||
assert base_property_object.property_values | ||
pv = base_property_object.property_values[0] | ||
responses.add( | ||
responses.PUT, | ||
f"{base_client.config.base_url}api/v2/teams/{base_client.config.default_team}/properties/{item_id}/property_values/{pv.id}", | ||
json=pv.dict(), | ||
status=200, | ||
) | ||
# Call the function being tested | ||
property_value = update_property_value( | ||
base_client, | ||
params=pv, | ||
item_id=item_id, | ||
team_slug=base_client.config.default_team, | ||
) | ||
|
||
# Assertions | ||
assert isinstance(property_value, PropertyValue) | ||
assert property_value == pv |
Oops, something went wrong.