Skip to content

Commit

Permalink
Merge pull request #19 from tb1337/develop
Browse files Browse the repository at this point in the history
Add tests for iterate methods
  • Loading branch information
tb1337 authored Dec 19, 2023
2 parents 79cde27 + 5c84e37 commit 8aaacac
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pypaperless/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"correspondents": {
"count": 89,
"next": "http://localhost:8000/api/correspondents/?page=2",
"next": null,
"previous": null,
"all": [
49,
Expand Down Expand Up @@ -191,7 +191,7 @@
},
"custom_fields": {
"count": 8,
"next": "dummy next page for tests :P",
"next": null,
"previous": null,
"all": [
8,
Expand Down
9 changes: 9 additions & 0 deletions tests/pypaperless/test_consumption_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions tests/pypaperless/test_correspondents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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]):
Expand Down
9 changes: 9 additions & 0 deletions tests/pypaperless/test_custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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]):
Expand Down
9 changes: 9 additions & 0 deletions tests/pypaperless/test_document_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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]):
Expand Down
9 changes: 9 additions & 0 deletions tests/pypaperless/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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]):
Expand Down
9 changes: 9 additions & 0 deletions tests/pypaperless/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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]):
Expand Down
9 changes: 9 additions & 0 deletions tests/pypaperless/test_mail_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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]):
Expand Down
9 changes: 9 additions & 0 deletions tests/pypaperless/test_mail_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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]):
Expand Down
9 changes: 9 additions & 0 deletions tests/pypaperless/test_saved_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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]):
Expand Down
9 changes: 9 additions & 0 deletions tests/pypaperless/test_storage_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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]):
Expand Down
9 changes: 9 additions & 0 deletions tests/pypaperless/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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]):
Expand Down
7 changes: 7 additions & 0 deletions tests/pypaperless/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]):
Expand Down
9 changes: 9 additions & 0 deletions tests/pypaperless/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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]):
Expand Down

0 comments on commit 8aaacac

Please sign in to comment.