Skip to content

Commit

Permalink
chore: Remove side-effects from tests with sideloads
Browse files Browse the repository at this point in the history
dummy_annotation was modified by a side-effect and the expected values
didn't need to add content which was confusing.
  • Loading branch information
asgeirrr committed Nov 8, 2023
1 parent cf56272 commit c3f8130
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions tests/elis_api_client/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ async def test_retrieve_annotation(self, elis_client, dummy_annotation):

async def test_retrieve_annotation_with_sideloads(self, elis_client, dummy_annotation):
client, http_client = elis_client
http_client.fetch_one.return_value = dummy_annotation
# Copy the annotation to prevent changing dummy_annotation by side effects
http_client.fetch_one.return_value = dummy_annotation.copy()
http_client.request_json.return_value = {"content": []}

aid = dummy_annotation["id"]
Expand All @@ -226,7 +227,8 @@ def is_imported(annotation):
client, http_client = elis_client
in_progress_annotation = {**dummy_annotation, "status": "importing"}
# First, return annotation in importing, than to_review state
http_client.fetch_one.side_effect = [in_progress_annotation, dummy_annotation]
# Copy the annotation to prevent changing dummy_annotation by side effects
http_client.fetch_one.side_effect = [in_progress_annotation, dummy_annotation.copy()]
# Return sideloaded content
http_client.request_json.return_value = {"content": []}

Expand All @@ -235,15 +237,16 @@ def is_imported(annotation):
dummy_annotation["id"], is_imported, sleep_s=2, sideloads=["content"]
)

assert annotation == Annotation(**dummy_annotation)
assert annotation == Annotation(**{**dummy_annotation, "content": []})

sleep_mock.assert_called_once_with(2)

async def test_poll_annotation_until_imported(self, elis_client, dummy_annotation):
client, http_client = elis_client
in_progress_annotation = {**dummy_annotation, "status": "importing"}
# First, return annotation in importing, than to_review state
http_client.fetch_one.side_effect = [in_progress_annotation, dummy_annotation]
# Copy the annotation to prevent changing dummy_annotation by side effects
http_client.fetch_one.side_effect = [in_progress_annotation, dummy_annotation.copy()]
# Return sideloaded content
http_client.request_json.return_value = {"content": []}

Expand All @@ -252,7 +255,7 @@ async def test_poll_annotation_until_imported(self, elis_client, dummy_annotatio
dummy_annotation["id"], sleep_s=2, sideloads=["content"]
)

assert annotation == Annotation(**dummy_annotation)
assert annotation == Annotation(**{**dummy_annotation, "content": []})
sleep_mock.assert_called_once_with(2)

async def test_upload_and_wait_until_imported(self, elis_client, dummy_annotation):
Expand All @@ -263,7 +266,8 @@ async def test_upload_and_wait_until_imported(self, elis_client, dummy_annotatio
{"results": [{"annotation": f"/annotation/{dummy_annotation['id']}"}]}
]
# First, return annotation in importing, than to_review state
http_client.fetch_one.side_effect = [in_progress_annotation, dummy_annotation]
# Copy the annotation to prevent changing dummy_annotation by side effects
http_client.fetch_one.side_effect = [in_progress_annotation, dummy_annotation.copy()]
# Return sideloaded content
http_client.request_json.return_value = {"content": []}

Expand All @@ -276,7 +280,7 @@ async def test_upload_and_wait_until_imported(self, elis_client, dummy_annotatio
sideloads=["content"],
)

assert annotation == Annotation(**dummy_annotation)
assert annotation == Annotation(**{**dummy_annotation, "content": []})

sleep_mock.assert_called_once_with(2)

Expand All @@ -287,7 +291,7 @@ async def test_start_annotation(self, elis_client, dummy_annotation):
aid = dummy_annotation["id"]

await client.start_annotation(aid)
http_client.request_json.assert_called_with("POST", "annotations/314528/start")
http_client.request_json.assert_called_with("POST", f"annotations/{aid}/start")

async def test_update_annotation(self, elis_client, dummy_annotation):
client, http_client = elis_client
Expand Down Expand Up @@ -328,7 +332,7 @@ async def test_bulk_update_annotation_data(self, elis_client, dummy_annotation):
await client.bulk_update_annotation_data(aid, operations)

http_client.request_json.assert_called_with(
"POST", "annotations/314528/content/operations", json={"operations": operations}
"POST", f"annotations/{aid}/content/operations", json={"operations": operations}
)

async def test_confirm_annotation(self, elis_client, dummy_annotation):
Expand All @@ -338,7 +342,7 @@ async def test_confirm_annotation(self, elis_client, dummy_annotation):
aid = dummy_annotation["id"]

await client.confirm_annotation(aid)
http_client.request_json.assert_called_with("POST", "annotations/314528/confirm")
http_client.request_json.assert_called_with("POST", f"annotations/{aid}/confirm")


class TestAnnotationsSync:
Expand Down Expand Up @@ -431,7 +435,9 @@ def test_retrieve_annotation(self, elis_client_sync, dummy_annotation):

def test_retrieve_annotation_with_sideloads(self, elis_client_sync, dummy_annotation):
client, http_client = elis_client_sync
http_client.fetch_one.return_value = dummy_annotation

# Copy the annotation to prevent changing dummy_annotation by side effects
http_client.fetch_one.return_value = dummy_annotation.copy()
http_client.request_json.return_value = {"content": []}

aid = dummy_annotation["id"]
Expand All @@ -448,7 +454,8 @@ def is_imported(annotation):
client, http_client = elis_client_sync
in_progress_annotation = {**dummy_annotation, "status": "importing"}
# First, return annotation in importing, than to_review state
http_client.fetch_one.side_effect = [in_progress_annotation, dummy_annotation, []]
# Copy the annotation to prevent changing dummy_annotation by side effects
http_client.fetch_one.side_effect = [in_progress_annotation, dummy_annotation.copy(), []]
# Return sideloaded content
http_client.request_json.return_value = {"content": []}

Expand All @@ -465,7 +472,8 @@ def test_poll_annotation_until_imported(self, elis_client_sync, dummy_annotation
client, http_client = elis_client_sync
in_progress_annotation = {**dummy_annotation, "status": "importing"}
# First, return annotation in importing, than to_review state
http_client.fetch_one.side_effect = [in_progress_annotation, dummy_annotation, []]
# Copy the annotation to prevent changing dummy_annotation by side effects
http_client.fetch_one.side_effect = [in_progress_annotation, dummy_annotation.copy(), []]
# Return sideloaded content
http_client.request_json.return_value = {"content": []}

Expand All @@ -486,7 +494,8 @@ def test_upload_and_wait_until_imported(self, elis_client_sync, dummy_annotation
{"results": [{"annotation": f"/annotation/{dummy_annotation['id']}"}]}
]
# First, return annotation in importing, than to_review state
http_client.fetch_one.side_effect = [in_progress_annotation, dummy_annotation]
# Copy the annotation to prevent changing dummy_annotation by side effects
http_client.fetch_one.side_effect = [in_progress_annotation, dummy_annotation.copy()]
# Return sideloaded content
http_client.request_json.return_value = {"content": []}

Expand All @@ -499,7 +508,7 @@ def test_upload_and_wait_until_imported(self, elis_client_sync, dummy_annotation
sideloads=["content"],
)

assert annotation == Annotation(**dummy_annotation)
assert annotation == Annotation(**{**dummy_annotation, "content": []})

sleep_mock.assert_called_once_with(2)

Expand All @@ -510,7 +519,7 @@ def test_start_annotation(self, elis_client_sync, dummy_annotation):
aid = dummy_annotation["id"]

client.start_annotation(aid)
http_client.request_json.assert_called_with("POST", "annotations/314528/start")
http_client.request_json.assert_called_with("POST", f"annotations/{aid}/start")

def test_update_annotation(self, elis_client_sync, dummy_annotation):
client, http_client = elis_client_sync
Expand Down Expand Up @@ -551,7 +560,7 @@ def test_bulk_update_annotation_data(self, elis_client_sync, dummy_annotation):
client.bulk_update_annotation_data(aid, operations)

http_client.request_json.assert_called_with(
"POST", "annotations/314528/content/operations", json={"operations": operations}
"POST", f"annotations/{aid}/content/operations", json={"operations": operations}
)

def test_confirm_annotation(self, elis_client_sync, dummy_annotation):
Expand All @@ -561,4 +570,4 @@ def test_confirm_annotation(self, elis_client_sync, dummy_annotation):
aid = dummy_annotation["id"]

client.confirm_annotation(aid)
http_client.request_json.assert_called_with("POST", "annotations/314528/confirm")
http_client.request_json.assert_called_with("POST", f"annotations/{aid}/confirm")

0 comments on commit c3f8130

Please sign in to comment.