Skip to content

Commit 8301176

Browse files
committed
refactor test suites
1 parent 2e0d4b8 commit 8301176

File tree

4 files changed

+149
-67
lines changed

4 files changed

+149
-67
lines changed

tests/integration/synapseclient/models/async/test_wiki_async.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Integration tests for the synapseclient.models.wiki module."""
22

3+
import asyncio
34
import gzip
45
import os
56
import tempfile
@@ -34,7 +35,6 @@ async def wiki_page_fixture(
3435
markdown=wiki_markdown,
3536
)
3637
root_wiki = await wiki_page.store_async(synapse_client=syn)
37-
schedule_for_cleanup(root_wiki.id)
3838
return root_wiki
3939

4040

@@ -240,7 +240,7 @@ async def test_get_attachment_preview_url(
240240
# GIVEN a wiki page with an attachment
241241
wiki_page, attachment_name = wiki_page_with_attachment
242242
# Sleep for 0.5 minutes to ensure the attachment preview is created
243-
time.sleep(0.5 * 60)
243+
await asyncio.sleep(0.5 * 60)
244244
# WHEN getting attachment preview URL
245245
preview_url = await wiki_page.get_attachment_preview_async(
246246
file_name=attachment_name, download_file=False, synapse_client=self.syn
@@ -649,9 +649,11 @@ async def test_wiki_page_history(
649649
# GIVEN a wiki page with multiple versions
650650
sub_wiki = wiki_page_with_multiple_versions
651651
# WHEN getting wiki history
652-
history = await WikiHistorySnapshot.get_async(
652+
history = []
653+
async for item in WikiHistorySnapshot.get_async(
653654
owner_id=project_model.id, id=sub_wiki.id, synapse_client=self.syn
654-
)
655+
):
656+
history.append(item)
655657
# THEN history should be returned
656658
assert len(history) == 3
657659
schedule_for_cleanup(history)
@@ -690,9 +692,11 @@ async def test_get_wiki_header_tree(
690692
self, project_model: Project, schedule_for_cleanup: Callable[..., None]
691693
) -> None:
692694
# WHEN getting the wiki header tree
693-
headers = await WikiHeader.get_async(
695+
headers = []
696+
async for header in WikiHeader.get_async(
694697
owner_id=project_model.id, synapse_client=self.syn
695-
)
698+
):
699+
headers.append(header)
696700

697701
# THEN headers should be returned
698702
assert len(headers) == 8
@@ -727,9 +731,11 @@ async def test_store_wiki_order_hint(
727731
self, project_model: Project, schedule_for_cleanup: Callable[..., None]
728732
) -> None:
729733
# Get headers
730-
headers = await WikiHeader.get_async(
734+
headers = []
735+
async for header in WikiHeader.get_async(
731736
owner_id=project_model.id, synapse_client=self.syn
732-
)
737+
):
738+
headers.append(header)
733739
# Get the ids of the headers
734740
header_ids = [header.id for header in headers]
735741
# Get initial order hint
@@ -749,3 +755,9 @@ async def test_store_wiki_order_hint(
749755
schedule_for_cleanup(retrieved_order_hint)
750756
assert retrieved_order_hint.id_list == header_ids
751757
assert len(retrieved_order_hint.id_list) == 8
758+
759+
# clean up the wiki pages for other tests in the same session
760+
async def test_cleanup_wiki_pages(self, wiki_page_fixture: WikiPage):
761+
root_wiki = wiki_page_fixture
762+
await root_wiki.delete_async(synapse_client=self.syn)
763+
assert True

tests/integration/synapseclient/models/synchronous/test_wiki.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,9 +644,11 @@ def test_wiki_page_history(
644644
# GIVEN a wiki page with multiple versions
645645
sub_wiki = wiki_page_with_multiple_versions
646646
# WHEN getting wiki history
647-
history = WikiHistorySnapshot.get(
647+
history = []
648+
for item in WikiHistorySnapshot.get(
648649
owner_id=project_model.id, id=sub_wiki.id, synapse_client=self.syn
649-
)
650+
):
651+
history.append(item)
650652
# THEN history should be returned
651653
assert len(history) == 3
652654
schedule_for_cleanup(history)
@@ -688,7 +690,11 @@ def test_get_wiki_header_tree(
688690
schedule_for_cleanup: Callable[..., None],
689691
) -> None:
690692
# WHEN getting the wiki header tree
691-
headers = WikiHeader.get(owner_id=project_model.id, synapse_client=self.syn)
693+
headers = []
694+
for header in WikiHeader.get(
695+
owner_id=project_model.id, synapse_client=self.syn
696+
):
697+
headers.append(header)
692698

693699
# THEN headers should be returned
694700
assert len(headers) == 8
@@ -743,3 +749,9 @@ def test_store_wiki_order_hint(
743749
schedule_for_cleanup(retrieved_order_hint)
744750
assert retrieved_order_hint.id_list == header_ids
745751
assert len(retrieved_order_hint.id_list) == 8
752+
753+
# clean up the wiki pages for other tests in the same session
754+
def test_cleanup_wiki_pages(self, wiki_page_fixture: WikiPage):
755+
root_wiki = wiki_page_fixture
756+
root_wiki.delete(synapse_client=self.syn)
757+
assert True

tests/unit/synapseclient/models/async/unit_test_wiki_async.py

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,15 @@ async def mock_async_generator(
199199
"synapseclient.models.wiki.get_wiki_history",
200200
return_value=mock_async_generator(mock_responses),
201201
) as mocked_get:
202-
results = await WikiHistorySnapshot.get_async(
202+
results = []
203+
async for item in WikiHistorySnapshot.get_async(
203204
owner_id="syn123",
204205
id="wiki1",
205206
offset=0,
206207
limit=20,
207208
synapse_client=self.syn,
208-
)
209+
):
210+
results.append(item)
209211
# THEN the API should be called with correct parameters
210212
mocked_get.assert_called_once_with(
211213
owner_id="syn123",
@@ -242,11 +244,12 @@ async def test_get_async_missing_owner_id(self) -> None:
242244
) as mocked_get, pytest.raises(
243245
ValueError, match="Must provide owner_id to get wiki history."
244246
):
245-
await WikiHistorySnapshot.get_async(
247+
async for _ in WikiHistorySnapshot.get_async(
246248
owner_id=None,
247249
id="wiki1",
248250
synapse_client=self.syn,
249-
)
251+
):
252+
pass
250253
# THEN the API should not be called
251254
mocked_get.assert_not_called()
252255

@@ -257,11 +260,12 @@ async def test_get_async_missing_id(self) -> None:
257260
) as mocked_get, pytest.raises(
258261
ValueError, match="Must provide id to get wiki history."
259262
):
260-
await WikiHistorySnapshot.get_async(
263+
async for _ in WikiHistorySnapshot.get_async(
261264
owner_id="syn123",
262265
id=None,
263266
synapse_client=self.syn,
264-
)
267+
):
268+
pass
265269
# THEN the API should not be called
266270
mocked_get.assert_not_called()
267271

@@ -319,13 +323,14 @@ async def mock_async_generator(
319323
"synapseclient.models.wiki.get_wiki_header_tree",
320324
return_value=mock_async_generator(mock_responses),
321325
) as mocked_get:
322-
results = await WikiHeader.get_async(
326+
results = []
327+
async for item in WikiHeader.get_async(
323328
owner_id="syn123",
324329
synapse_client=self.syn,
325330
offset=0,
326331
limit=20,
327-
)
328-
332+
):
333+
results.append(item)
329334
# THEN the API should be called with correct parameters
330335
mocked_get.assert_called_once_with(
331336
owner_id="syn123",
@@ -349,7 +354,8 @@ async def test_get_async_missing_owner_id(self) -> None:
349354
) as mocked_get, pytest.raises(
350355
ValueError, match="Must provide owner_id to get wiki header tree."
351356
):
352-
await WikiHeader.get_async(owner_id=None, synapse_client=self.syn)
357+
async for _ in WikiHeader.get_async(owner_id=None, synapse_client=self.syn):
358+
pass
353359
# THEN the API should not be called
354360
mocked_get.assert_not_called()
355361

@@ -603,6 +609,28 @@ def test_reformat_attachment_file_name(self, file_name: str, expected: str) -> N
603609
# THEN the result should be the reformatted file name
604610
assert result == expected
605611

612+
@pytest.mark.parametrize(
613+
"file_name,expected",
614+
[
615+
("test.png", False),
616+
("test.jpg", False),
617+
("test.jpeg", False),
618+
("test.txt.gz", False),
619+
("test.txt", True),
620+
],
621+
)
622+
def test_should_gzip_file(self, file_name: str, expected: bool) -> None:
623+
# WHEN I call `_should_gzip_file` with a file name
624+
result = WikiPage._should_gzip_file(file_name)
625+
# THEN the result should match the expected value
626+
assert result == expected
627+
628+
def test_should_gzip_file_with_invalid_content(self) -> None:
629+
# WHEN I call `_should_gzip_file` with invalid content (non-string)
630+
# THEN it should raise an AttributeError
631+
with pytest.raises(AttributeError):
632+
WikiPage._should_gzip_file(123)
633+
606634
async def test_get_markdown_file_handle_success_with_markdown(self) -> WikiPage:
607635
with patch(
608636
"synapseclient.models.wiki.WikiPage._to_gzip_file",
@@ -931,9 +959,12 @@ async def test_determine_wiki_action_create_root(self) -> None:
931959
)
932960

933961
async def test_determine_wiki_action_create_sub(self) -> None:
962+
async def mock_get_async(*args, **kwargs) -> AsyncGenerator[WikiHeader, None]:
963+
yield WikiHeader(id="wiki1", title="Test Wiki Page")
964+
934965
with patch(
935966
"synapseclient.models.wiki.WikiHeader.get_async",
936-
side_effect=SynapseHTTPError(response=Mock(status_code=404)),
967+
side_effect=mock_get_async,
937968
) as mock_get_header:
938969
# GIVEN a WikiPage with a parent_id
939970
wiki_page = WikiPage(
@@ -950,9 +981,12 @@ async def test_determine_wiki_action_create_sub(self) -> None:
950981
mock_get_header.assert_not_called()
951982

952983
async def test_determine_wiki_action_update_existing_root(self) -> None:
984+
async def mock_get_async(*args, **kwargs) -> AsyncGenerator[WikiHeader, None]:
985+
yield WikiHeader(id="wiki1", title="Test Wiki Page")
986+
953987
with patch(
954988
"synapseclient.models.wiki.WikiHeader.get_async",
955-
return_value=WikiHeader(id="wiki1", title="Test Wiki Page"),
989+
side_effect=mock_get_async,
956990
) as mock_get_header:
957991
# GIVEN a WikiPage with an id
958992
wiki_page = WikiPage(
@@ -974,9 +1008,12 @@ async def test_determine_wiki_action_update_existing_root(self) -> None:
9741008
async def test_determine_wiki_action_update_existing_without_passing_id(
9751009
self,
9761010
) -> None:
1011+
async def mock_get_async(*args, **kwargs) -> AsyncGenerator[WikiHeader, None]:
1012+
yield WikiHeader(id="wiki1", title="Test Wiki Page")
1013+
9771014
with patch(
9781015
"synapseclient.models.wiki.WikiHeader.get_async",
979-
return_value=WikiHeader(id="wiki1", title="Test Wiki Page"),
1016+
side_effect=mock_get_async,
9801017
) as mock_get_header:
9811018
# GIVEN a WikiPage with an id and parent_id
9821019
wiki_page = WikiPage(

0 commit comments

Comments
 (0)