Skip to content

Commit

Permalink
[HOTFIX] Fixing a bug where response headers are not being correctly …
Browse files Browse the repository at this point in the history
…identified (#345)

* fixing a bug where response headers were not being correctly identified

* Added test

* typo

* Update darwin/client.py

Co-authored-by: Andrea Azzini <[email protected]>

Co-authored-by: Andrea Azzini <[email protected]>
  • Loading branch information
Fl4m3Ph03n1x and andreaazzini authored Feb 14, 2022
1 parent 60dea1d commit e7e5506
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
4 changes: 2 additions & 2 deletions darwin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def import_annotation(self, item_id: int, payload: Dict[str, Any]) -> None:

def fetch_remote_attributes(self, dataset_id: int) -> List[Dict[str, Any]]:
"""
Fetches all attributes remotly.
Fetches all attributes remotely.
Parameters
----------
Expand Down Expand Up @@ -1080,7 +1080,7 @@ def _raise_if_known_error(self, response: Response, endpoint: str) -> None:
raise InsufficientStorage()

def _has_json_response(self, response: Response) -> bool:
return response.headers.get("content-type") == "application/json"
return "application/json" in response.headers.get("content-type", "")

def _get_response_debug_text(self, response: Response) -> str:
if self._has_json_response(response):
Expand Down
32 changes: 29 additions & 3 deletions tests/darwin/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from darwin.config import Config
from darwin.dataset.remote_dataset import RemoteDataset
from darwin.datatypes import Feature
from darwin.exceptions import NotFound
from darwin.exceptions import NameTaken, NotFound


@pytest.fixture
Expand Down Expand Up @@ -110,7 +110,12 @@ def it_returns_the_dataset(darwin_client: Client):

actual_dataset = darwin_client.get_remote_dataset("v7/dataset-slug-1")
expected_dataset = RemoteDataset(
team="v7", name="dataset-name-1", slug="dataset-slug-1", dataset_id=1, item_count=1, client=darwin_client,
team="v7",
name="dataset-name-1",
slug="dataset-slug-1",
dataset_id=1,
item_count=1,
client=darwin_client,
)

assert_dataset(actual_dataset, expected_dataset)
Expand All @@ -134,11 +139,32 @@ def it_returns_the_created_dataset(darwin_client: Client):

actual_dataset = darwin_client.create_dataset("my-dataset", "v7")
expected_dataset = RemoteDataset(
team="v7", name="my-dataset", slug="my-dataset", dataset_id=1, item_count=1, client=darwin_client,
team="v7",
name="my-dataset",
slug="my-dataset",
dataset_id=1,
item_count=1,
client=darwin_client,
)

assert_dataset(actual_dataset, expected_dataset)

@responses.activate
def it_raises_if_name_is_taken(darwin_client: Client):
endpoint: str = "/datasets"
json_response: Dict[str, Any] = {"errors": {"name": ["has already been taken"]}}

responses.add(
responses.POST,
darwin_client.url + endpoint,
json=json_response,
status=422,
adding_headers={"content-type": "utf-8"},
)

with pytest.raises(NameTaken):
darwin_client.create_dataset("my-dataset", "v7")


@pytest.mark.usefixtures("file_read_write_test")
def describe_fetch_remote_files():
Expand Down

0 comments on commit e7e5506

Please sign in to comment.