From 8d2b028f77f2a5bdf0f4ea81c9a6512166d3ffc7 Mon Sep 17 00:00:00 2001 From: tb1337 Date: Tue, 19 Dec 2023 06:09:23 +0100 Subject: [PATCH 1/3] Add tests for iteration methods --- tests/pypaperless/test_consumption_templates.py | 9 +++++++++ tests/pypaperless/test_correspondents.py | 9 +++++++++ tests/pypaperless/test_custom_fields.py | 9 +++++++++ tests/pypaperless/test_document_types.py | 9 +++++++++ tests/pypaperless/test_documents.py | 9 +++++++++ tests/pypaperless/test_groups.py | 9 +++++++++ tests/pypaperless/test_mail_accounts.py | 9 +++++++++ tests/pypaperless/test_mail_rules.py | 9 +++++++++ tests/pypaperless/test_saved_views.py | 9 +++++++++ tests/pypaperless/test_storage_paths.py | 9 +++++++++ tests/pypaperless/test_tags.py | 9 +++++++++ tests/pypaperless/test_tasks.py | 7 +++++++ tests/pypaperless/test_users.py | 9 +++++++++ 13 files changed, 115 insertions(+) diff --git a/tests/pypaperless/test_consumption_templates.py b/tests/pypaperless/test_consumption_templates.py index b4d20ba..29c538c 100644 --- a/tests/pypaperless/test_consumption_templates.py +++ b/tests/pypaperless/test_consumption_templates.py @@ -21,6 +21,8 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(result, list) assert len(result) > 0 + for item in result: + assert isinstance(item, int) page = await paperless.consumption_templates.get() @@ -29,6 +31,13 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(page.items.pop(), ConsumptionTemplate) +async def test_iterate(paperless: Paperless, data): + """Test iterate.""" + with patch.object(paperless, "request", return_value=data["consumption_templates"]): + async for item in paperless.consumption_templates.iterate(): + assert isinstance(item, ConsumptionTemplate) + + async def test_one(paperless: Paperless, data): """Test one.""" with patch.object( diff --git a/tests/pypaperless/test_correspondents.py b/tests/pypaperless/test_correspondents.py index 3a1f882..907ba38 100644 --- a/tests/pypaperless/test_correspondents.py +++ b/tests/pypaperless/test_correspondents.py @@ -21,6 +21,8 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(result, list) assert len(result) > 0 + for item in result: + assert isinstance(item, int) page = await paperless.correspondents.get() @@ -29,6 +31,13 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(page.items.pop(), Correspondent) +async def test_iterate(paperless: Paperless, data): + """Test iterate.""" + with patch.object(paperless, "request", return_value=data["correspondents"]): + async for item in paperless.correspondents.iterate(): + assert isinstance(item, Correspondent) + + async def test_one(paperless: Paperless, data): """Test one.""" with patch.object(paperless, "request", return_value=data["correspondents"]["results"][0]): diff --git a/tests/pypaperless/test_custom_fields.py b/tests/pypaperless/test_custom_fields.py index 16ba46d..fc5c402 100644 --- a/tests/pypaperless/test_custom_fields.py +++ b/tests/pypaperless/test_custom_fields.py @@ -21,6 +21,8 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(result, list) assert len(result) > 0 + for item in result: + assert isinstance(item, int) page = await paperless.custom_fields.get() @@ -29,6 +31,13 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(page.items.pop(), CustomField) +async def test_iterate(paperless: Paperless, data): + """Test iterate.""" + with patch.object(paperless, "request", return_value=data["custom_fields"]): + async for item in paperless.custom_fields.iterate(): + assert isinstance(item, CustomField) + + async def test_one(paperless: Paperless, data): """Test one.""" with patch.object(paperless, "request", return_value=data["custom_fields"]["results"][0]): diff --git a/tests/pypaperless/test_document_types.py b/tests/pypaperless/test_document_types.py index 4b5d504..bd46c74 100644 --- a/tests/pypaperless/test_document_types.py +++ b/tests/pypaperless/test_document_types.py @@ -21,6 +21,8 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(result, list) assert len(result) > 0 + for item in result: + assert isinstance(item, int) page = await paperless.document_types.get() @@ -29,6 +31,13 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(page.items.pop(), DocumentType) +async def test_iterate(paperless: Paperless, data): + """Test iterate.""" + with patch.object(paperless, "request", return_value=data["document_types"]): + async for item in paperless.document_types.iterate(): + assert isinstance(item, DocumentType) + + async def test_one(paperless: Paperless, data): """Test one.""" with patch.object(paperless, "request", return_value=data["document_types"]["results"][0]): diff --git a/tests/pypaperless/test_documents.py b/tests/pypaperless/test_documents.py index dbd60cd..ff9d54c 100644 --- a/tests/pypaperless/test_documents.py +++ b/tests/pypaperless/test_documents.py @@ -21,6 +21,8 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(result, list) assert len(result) > 0 + for item in result: + assert isinstance(item, int) page = await paperless.documents.get() @@ -29,6 +31,13 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(page.items.pop(), Document) +async def test_iterate(paperless: Paperless, data): + """Test iterate.""" + with patch.object(paperless, "request", return_value=data["documents"]): + async for item in paperless.documents.iterate(): + assert isinstance(item, Document) + + async def test_one(paperless: Paperless, data): """Test one.""" with patch.object(paperless, "request", return_value=data["documents"]["results"][0]): diff --git a/tests/pypaperless/test_groups.py b/tests/pypaperless/test_groups.py index bee3658..60d0f40 100644 --- a/tests/pypaperless/test_groups.py +++ b/tests/pypaperless/test_groups.py @@ -20,6 +20,8 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(result, list) assert len(result) > 0 + for item in result: + assert isinstance(item, int) page = await paperless.groups.get() @@ -28,6 +30,13 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(page.items.pop(), Group) +async def test_iterate(paperless: Paperless, data): + """Test iterate.""" + with patch.object(paperless, "request", return_value=data["groups"]): + async for item in paperless.groups.iterate(): + assert isinstance(item, Group) + + async def test_one(paperless: Paperless, data): """Test one.""" with patch.object(paperless, "request", return_value=data["groups"]["results"][0]): diff --git a/tests/pypaperless/test_mail_accounts.py b/tests/pypaperless/test_mail_accounts.py index 35482a7..52ff187 100644 --- a/tests/pypaperless/test_mail_accounts.py +++ b/tests/pypaperless/test_mail_accounts.py @@ -20,6 +20,8 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(result, list) assert len(result) > 0 + for item in result: + assert isinstance(item, int) page = await paperless.mail_accounts.get() @@ -28,6 +30,13 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(page.items.pop(), MailAccount) +async def test_iterate(paperless: Paperless, data): + """Test iterate.""" + with patch.object(paperless, "request", return_value=data["mail_accounts"]): + async for item in paperless.mail_accounts.iterate(): + assert isinstance(item, MailAccount) + + async def test_one(paperless: Paperless, data): """Test one.""" with patch.object(paperless, "request", return_value=data["mail_accounts"]["results"][0]): diff --git a/tests/pypaperless/test_mail_rules.py b/tests/pypaperless/test_mail_rules.py index 2e6c477..c8d91bf 100644 --- a/tests/pypaperless/test_mail_rules.py +++ b/tests/pypaperless/test_mail_rules.py @@ -20,6 +20,8 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(result, list) assert len(result) > 0 + for item in result: + assert isinstance(item, int) page = await paperless.mail_rules.get() @@ -28,6 +30,13 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(page.items.pop(), MailRule) +async def test_iterate(paperless: Paperless, data): + """Test iterate.""" + with patch.object(paperless, "request", return_value=data["mail_rules"]): + async for item in paperless.mail_rules.iterate(): + assert isinstance(item, MailRule) + + async def test_one(paperless: Paperless, data): """Test one.""" with patch.object(paperless, "request", return_value=data["mail_rules"]["results"][0]): diff --git a/tests/pypaperless/test_saved_views.py b/tests/pypaperless/test_saved_views.py index 814b0a7..94f4c60 100644 --- a/tests/pypaperless/test_saved_views.py +++ b/tests/pypaperless/test_saved_views.py @@ -20,6 +20,8 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(result, list) assert len(result) > 0 + for item in result: + assert isinstance(item, int) page = await paperless.saved_views.get() @@ -28,6 +30,13 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(page.items.pop(), SavedView) +async def test_iterate(paperless: Paperless, data): + """Test iterate.""" + with patch.object(paperless, "request", return_value=data["saved_views"]): + async for item in paperless.saved_views.iterate(): + assert isinstance(item, SavedView) + + async def test_one(paperless: Paperless, data): """Test one.""" with patch.object(paperless, "request", return_value=data["saved_views"]["results"][0]): diff --git a/tests/pypaperless/test_storage_paths.py b/tests/pypaperless/test_storage_paths.py index c3b1a34..2fac5dd 100644 --- a/tests/pypaperless/test_storage_paths.py +++ b/tests/pypaperless/test_storage_paths.py @@ -21,6 +21,8 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(result, list) assert len(result) > 0 + for item in result: + assert isinstance(item, int) page = await paperless.storage_paths.get() @@ -29,6 +31,13 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(page.items.pop(), StoragePath) +async def test_iterate(paperless: Paperless, data): + """Test iterate.""" + with patch.object(paperless, "request", return_value=data["storage_paths"]): + async for item in paperless.storage_paths.iterate(): + assert isinstance(item, StoragePath) + + async def test_one(paperless: Paperless, data): """Test one.""" with patch.object(paperless, "request", return_value=data["storage_paths"]["results"][0]): diff --git a/tests/pypaperless/test_tags.py b/tests/pypaperless/test_tags.py index 3f100a6..bff465b 100644 --- a/tests/pypaperless/test_tags.py +++ b/tests/pypaperless/test_tags.py @@ -21,6 +21,8 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(result, list) assert len(result) > 0 + for item in result: + assert isinstance(item, int) page = await paperless.tags.get() @@ -29,6 +31,13 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(page.items.pop(), Tag) +async def test_iterate(paperless: Paperless, data): + """Test iterate.""" + with patch.object(paperless, "request", return_value=data["tags"]): + async for item in paperless.tags.iterate(): + assert isinstance(item, Tag) + + async def test_one(paperless: Paperless, data): """Test one.""" with patch.object(paperless, "request", return_value=data["tags"]["results"][0]): diff --git a/tests/pypaperless/test_tasks.py b/tests/pypaperless/test_tasks.py index 74e5b9a..1b507cb 100644 --- a/tests/pypaperless/test_tasks.py +++ b/tests/pypaperless/test_tasks.py @@ -28,6 +28,13 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(tasks[0], Task) +async def test_iterate(paperless: Paperless, data): + """Test iterate.""" + with patch.object(paperless, "request", return_value=data["tasks"]): + async for item in paperless.tasks.iterate(): + assert isinstance(item, Task) + + async def test_one(paperless: Paperless, data): """Test one.""" with patch.object(paperless, "request", return_value=data["tasks"]): diff --git a/tests/pypaperless/test_users.py b/tests/pypaperless/test_users.py index 5bc6b91..3b6a9d6 100644 --- a/tests/pypaperless/test_users.py +++ b/tests/pypaperless/test_users.py @@ -20,6 +20,8 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(result, list) assert len(result) > 0 + for item in result: + assert isinstance(item, int) page = await paperless.users.get() @@ -28,6 +30,13 @@ async def test_list_and_get(paperless: Paperless, data): assert isinstance(page.items.pop(), User) +async def test_iterate(paperless: Paperless, data): + """Test iterate.""" + with patch.object(paperless, "request", return_value=data["users"]): + async for item in paperless.users.iterate(): + assert isinstance(item, User) + + async def test_one(paperless: Paperless, data): """Test one.""" with patch.object(paperless, "request", return_value=data["users"]["results"][0]): From eb9c84d1b4111509512d5552f474de9d5ff0861e Mon Sep 17 00:00:00 2001 From: tb1337 Date: Tue, 19 Dec 2023 06:15:37 +0100 Subject: [PATCH 2/3] Remove dummy next pages in fixtures to prevent endless loops --- tests/fixtures/data.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/fixtures/data.json b/tests/fixtures/data.json index 1d788e1..2469ff7 100644 --- a/tests/fixtures/data.json +++ b/tests/fixtures/data.json @@ -18,7 +18,7 @@ }, "correspondents": { "count": 89, - "next": "http://localhost:8000/api/correspondents/?page=2", + "next": null, "previous": null, "all": [ 49, @@ -191,7 +191,7 @@ }, "custom_fields": { "count": 8, - "next": "dummy next page for tests :P", + "next": null, "previous": null, "all": [ 8, From 5c84e373f23f89b3270c789a39aa351c558a34fd Mon Sep 17 00:00:00 2001 From: tb1337 Date: Tue, 19 Dec 2023 06:16:27 +0100 Subject: [PATCH 3/3] Add iterate override for tasks --- pypaperless/api.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pypaperless/api.py b/pypaperless/api.py index f0b9cc2..b4be780 100644 --- a/pypaperless/api.py +++ b/pypaperless/api.py @@ -290,6 +290,15 @@ async def get( res = await self._paperless.request("get", self._endpoint, params=kwargs) return [dataclass_from_dict(self.endpoint_cls, item) for item in res] + async def iterate( + self, + **kwargs: dict[str, Any], + ) -> Generator[Task, None, None]: + """Iterate pages and yield every entity.""" + res = await self.get(**kwargs) + for item in res: + yield item + async def one(self, idx: str) -> T: """Request exactly one entity by id.""" res = await self._paperless.request("get", self._endpoint, params={"task_id": idx})