Skip to content

Commit

Permalink
Merge pull request #34 from tb1337/develop
Browse files Browse the repository at this point in the history
Add tests for CRUD operations
  • Loading branch information
tb1337 authored Dec 23, 2023
2 parents e5df382 + 19433a3 commit dea0ad5
Show file tree
Hide file tree
Showing 20 changed files with 339 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pre-commit-updater.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
body: |
Update versions of tools in pre-commit
configs to latest version
labels: dependencies
labels: chore
3 changes: 2 additions & 1 deletion pypaperless/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ async def generate_request(
url = endpoint if endpoint.startswith("http") else f"http://{self.host}/api/{endpoint}"
url = url.rstrip("/") + "/" # check and add trailing slash

kwargs.update(self._request_opts)
if isinstance(self._request_opts, dict):
kwargs.update(self._request_opts)

if "headers" not in kwargs:
kwargs["headers"] = {}
Expand Down
4 changes: 2 additions & 2 deletions pypaperless/api/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ async def create(self, obj: DocumentPost) -> str:
"document_type",
"archive_serial_number",
):
if obj[field]:
form.add_field(field, obj[field])
if getattr(obj, field):
form.add_field(field, getattr(obj, field))

if obj.tags and isinstance(obj.tags, list):
for tag in obj.tags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

from unittest.mock import patch

import pytest

from pypaperless import Paperless
from pypaperless.api import ConsumptionTemplatesEndpoint
from pypaperless.api.base import BaseEndpointCrudMixin, PaginatedResult
from pypaperless.models import ConsumptionTemplate
from pypaperless.models.shared import ConsumptionTemplateSource


@pytest.fixture(scope="module")
def dataset(data):
"""Represent current data."""
return data["consumption_templates"]


async def test_endpoint(paperless: Paperless) -> None:
"""Test endpoint."""
assert isinstance(paperless.consumption_templates, ConsumptionTemplatesEndpoint)
assert not isinstance(paperless.consumption_templates, BaseEndpointCrudMixin)


async def test_list_and_get(paperless: Paperless, data):
async def test_list_and_get(paperless: Paperless, dataset):
"""Test list."""
with patch.object(paperless, "request_json", return_value=data["consumption_templates"]):
with patch.object(paperless, "request_json", return_value=dataset):
result = await paperless.consumption_templates.list()

assert isinstance(result, list)
Expand All @@ -32,18 +40,16 @@ async def test_list_and_get(paperless: Paperless, data):
assert isinstance(page.items.pop(), ConsumptionTemplate)


async def test_iterate(paperless: Paperless, data):
async def test_iterate(paperless: Paperless, dataset):
"""Test iterate."""
with patch.object(paperless, "request_json", return_value=data["consumption_templates"]):
with patch.object(paperless, "request_json", return_value=dataset):
async for item in paperless.consumption_templates.iterate():
assert isinstance(item, ConsumptionTemplate)


async def test_one(paperless: Paperless, data):
async def test_one(paperless: Paperless, dataset):
"""Test one."""
with patch.object(
paperless, "request_json", return_value=data["consumption_templates"]["results"][0]
):
with patch.object(paperless, "request_json", return_value=dataset["results"][0]):
item = await paperless.consumption_templates.one(72)

assert isinstance(item, ConsumptionTemplate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

from unittest.mock import patch

import pytest

from pypaperless import Paperless
from pypaperless.api import CorrespondentsEndpoint
from pypaperless.api.base import BaseEndpointCrudMixin, PaginatedResult
from pypaperless.models import Correspondent
from pypaperless.models.shared import MatchingAlgorithm


@pytest.fixture(scope="module")
def dataset(data):
"""Represent current data."""
return data["correspondents"]


async def test_endpoint(paperless: Paperless) -> None:
"""Test endpoint."""
assert isinstance(paperless.correspondents, CorrespondentsEndpoint)
assert isinstance(paperless.correspondents, BaseEndpointCrudMixin)


async def test_list_and_get(paperless: Paperless, data):
async def test_list_and_get(paperless: Paperless, dataset):
"""Test list."""
with patch.object(paperless, "request_json", return_value=data["correspondents"]):
with patch.object(paperless, "request_json", return_value=dataset):
result = await paperless.correspondents.list()

assert isinstance(result, list)
Expand All @@ -32,16 +40,16 @@ async def test_list_and_get(paperless: Paperless, data):
assert isinstance(page.items.pop(), Correspondent)


async def test_iterate(paperless: Paperless, data):
async def test_iterate(paperless: Paperless, dataset):
"""Test iterate."""
with patch.object(paperless, "request_json", return_value=data["correspondents"]):
with patch.object(paperless, "request_json", return_value=dataset):
async for item in paperless.correspondents.iterate():
assert isinstance(item, Correspondent)


async def test_one(paperless: Paperless, data):
async def test_one(paperless: Paperless, dataset):
"""Test one."""
with patch.object(paperless, "request_json", return_value=data["correspondents"]["results"][0]):
with patch.object(paperless, "request_json", return_value=dataset["results"][0]):
item = await paperless.correspondents.one(72)

assert isinstance(item, Correspondent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

from unittest.mock import patch

import pytest

from pypaperless import Paperless
from pypaperless.api import CustomFieldEndpoint
from pypaperless.api.base import BaseEndpointCrudMixin, PaginatedResult
from pypaperless.models import CustomField
from pypaperless.models.shared import CustomFieldType


@pytest.fixture(scope="module")
def dataset(data):
"""Represent current data."""
return data["custom_fields"]


async def test_endpoint(paperless: Paperless) -> None:
"""Test endpoint."""
assert isinstance(paperless.custom_fields, CustomFieldEndpoint)
assert isinstance(paperless.custom_fields, BaseEndpointCrudMixin)


async def test_list_and_get(paperless: Paperless, data):
async def test_list_and_get(paperless: Paperless, dataset):
"""Test list."""
with patch.object(paperless, "request_json", return_value=data["custom_fields"]):
with patch.object(paperless, "request_json", return_value=dataset):
result = await paperless.custom_fields.list()

assert isinstance(result, list)
Expand All @@ -32,16 +40,16 @@ async def test_list_and_get(paperless: Paperless, data):
assert isinstance(page.items.pop(), CustomField)


async def test_iterate(paperless: Paperless, data):
async def test_iterate(paperless: Paperless, dataset):
"""Test iterate."""
with patch.object(paperless, "request_json", return_value=data["custom_fields"]):
with patch.object(paperless, "request_json", return_value=dataset):
async for item in paperless.custom_fields.iterate():
assert isinstance(item, CustomField)


async def test_one(paperless: Paperless, data):
async def test_one(paperless: Paperless, dataset):
"""Test one."""
with patch.object(paperless, "request_json", return_value=data["custom_fields"]["results"][0]):
with patch.object(paperless, "request_json", return_value=dataset["results"][0]):
item = await paperless.custom_fields.one(72)

assert isinstance(item, CustomField)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

from unittest.mock import patch

import pytest

from pypaperless import Paperless
from pypaperless.api import DocumentTypesEndpoint
from pypaperless.api.base import BaseEndpointCrudMixin, PaginatedResult
from pypaperless.models import DocumentType
from pypaperless.models.shared import MatchingAlgorithm


@pytest.fixture(scope="module")
def dataset(data):
"""Represent current data."""
return data["document_types"]


async def test_endpoint(paperless: Paperless) -> None:
"""Test endpoint."""
assert isinstance(paperless.document_types, DocumentTypesEndpoint)
assert isinstance(paperless.document_types, BaseEndpointCrudMixin)


async def test_list_and_get(paperless: Paperless, data):
async def test_list_and_get(paperless: Paperless, dataset):
"""Test list."""
with patch.object(paperless, "request_json", return_value=data["document_types"]):
with patch.object(paperless, "request_json", return_value=dataset):
result = await paperless.document_types.list()

assert isinstance(result, list)
Expand All @@ -32,16 +40,16 @@ async def test_list_and_get(paperless: Paperless, data):
assert isinstance(page.items.pop(), DocumentType)


async def test_iterate(paperless: Paperless, data):
async def test_iterate(paperless: Paperless, dataset):
"""Test iterate."""
with patch.object(paperless, "request_json", return_value=data["document_types"]):
with patch.object(paperless, "request_json", return_value=dataset):
async for item in paperless.document_types.iterate():
assert isinstance(item, DocumentType)


async def test_one(paperless: Paperless, data):
async def test_one(paperless: Paperless, dataset):
"""Test one."""
with patch.object(paperless, "request_json", return_value=data["document_types"]["results"][0]):
with patch.object(paperless, "request_json", return_value=dataset["results"][0]):
item = await paperless.document_types.one(72)

assert isinstance(item, DocumentType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

from unittest.mock import patch

import pytest

from pypaperless import Paperless
from pypaperless.api import DocumentsEndpoint
from pypaperless.api.base import BaseEndpointCrudMixin, PaginatedResult
from pypaperless.models import Document, DocumentNote
from pypaperless.models.custom_fields import CustomFieldValue


@pytest.fixture(scope="module")
def dataset(data):
"""Represent current data."""
return data["documents"]


async def test_endpoint(paperless: Paperless) -> None:
"""Test endpoint."""
assert isinstance(paperless.documents, DocumentsEndpoint)
assert isinstance(paperless.documents, BaseEndpointCrudMixin)


async def test_list_and_get(paperless: Paperless, data):
async def test_list_and_get(paperless: Paperless, dataset):
"""Test list."""
with patch.object(paperless, "request_json", return_value=data["documents"]):
with patch.object(paperless, "request_json", return_value=dataset):
result = await paperless.documents.list()

assert isinstance(result, list)
Expand All @@ -32,16 +40,16 @@ async def test_list_and_get(paperless: Paperless, data):
assert isinstance(page.items.pop(), Document)


async def test_iterate(paperless: Paperless, data):
async def test_iterate(paperless: Paperless, dataset):
"""Test iterate."""
with patch.object(paperless, "request_json", return_value=data["documents"]):
with patch.object(paperless, "request_json", return_value=dataset):
async for item in paperless.documents.iterate():
assert isinstance(item, Document)


async def test_one(paperless: Paperless, data):
async def test_one(paperless: Paperless, dataset):
"""Test one."""
with patch.object(paperless, "request_json", return_value=data["documents"]["results"][0]):
with patch.object(paperless, "request_json", return_value=dataset["results"][0]):
item = await paperless.documents.one(72)

assert isinstance(item, Document)
Expand Down
20 changes: 14 additions & 6 deletions tests/pypaperless/test_groups.py → tests/api/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@

from unittest.mock import patch

import pytest

from pypaperless import Paperless
from pypaperless.api import GroupsEndpoint
from pypaperless.api.base import BaseEndpointCrudMixin, PaginatedResult
from pypaperless.models import Group


@pytest.fixture(scope="module")
def dataset(data):
"""Represent current data."""
return data["groups"]


async def test_endpoint(paperless: Paperless) -> None:
"""Test endpoint."""
assert isinstance(paperless.groups, GroupsEndpoint)
assert not isinstance(paperless.groups, BaseEndpointCrudMixin)


async def test_list_and_get(paperless: Paperless, data):
async def test_list_and_get(paperless: Paperless, dataset):
"""Test list."""
with patch.object(paperless, "request_json", return_value=data["groups"]):
with patch.object(paperless, "request_json", return_value=dataset):
result = await paperless.groups.list()

assert isinstance(result, list)
Expand All @@ -31,16 +39,16 @@ async def test_list_and_get(paperless: Paperless, data):
assert isinstance(page.items.pop(), Group)


async def test_iterate(paperless: Paperless, data):
async def test_iterate(paperless: Paperless, dataset):
"""Test iterate."""
with patch.object(paperless, "request_json", return_value=data["groups"]):
with patch.object(paperless, "request_json", return_value=dataset):
async for item in paperless.groups.iterate():
assert isinstance(item, Group)


async def test_one(paperless: Paperless, data):
async def test_one(paperless: Paperless, dataset):
"""Test one."""
with patch.object(paperless, "request_json", return_value=data["groups"]["results"][0]):
with patch.object(paperless, "request_json", return_value=dataset["results"][0]):
item = await paperless.groups.one(72)

assert isinstance(item, Group)
Loading

0 comments on commit dea0ad5

Please sign in to comment.