Skip to content

Commit

Permalink
Setup pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanw committed Oct 27, 2022
1 parent 0595a5a commit 3e2f19f
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dist/
tmp/
example_project/
node_modules/
htmlcov/
fc-env/
.env
*.sqlite3
Expand All @@ -16,3 +17,4 @@ data/
static/
.venv/
build
.coverage
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
DJANGO_SETTINGS_MODULE=test_project.settings
python_files = tests.py test_*.py
48 changes: 45 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,17 +1,59 @@
[wheel]
universal = 1

[aliases]
test=pytest

[flake8]
extend-ignore = E203,E501,C901
max-line-length = 88
extend-ignore = E203,E501,C901,W503,B950
select = C,E,F,W,B,B950
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist,migrations
max-complexity = 10

[isort]
profile = black
src_paths = froide
src_paths = filingcabinet
default_section = THIRDPARTY
known_first_party = filingcabinet
known_django = django
sections = FUTURE,STDLIB,DJANGO,THIRDPARTY,FIRSTPARTY,LOCALFOLDER

[coverage:run]
branch = true
source = filingcabinet
omit =
*/migrations/*
*/wsgi.py
plugins =
django_coverage_plugin

[coverage:report]
show_missing = True
skip_covered = True
exclude_lines =
pragma: no cover

[mypy]
python_version = 3.10
plugins =
mypy_django_plugin.main,
mypy_drf_plugin.main

check_untyped_defs = True
disallow_any_generics = True
disallow_untyped_calls = True
disallow_untyped_decorators = True
ignore_errors = False
ignore_missing_imports = True
implicit_reexport = False
strict_optional = True
strict_equality = True
no_implicit_optional = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
warn_unreachable = True
warn_no_return = True

[mypy.plugins.django-stubs]
django_settings_module = "test_project.settings"
25 changes: 24 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ def find_version(*file_paths):
raise RuntimeError("Unable to find version string.")


tests_require = [
"black",
"coverage",
"django-coverage-plugin",
"django-stubs",
"djangorestframework-stubs",
"factory_boy",
"flake8-bugbear",
"flake8",
"isort",
"monkeytype",
"mypy-extensions",
"mypy",
"pre-commit",
"pycodestyle",
"pytest-django",
"pytest-factoryboy",
"pytest-playwright",
"pytest",
]


setup(
name="django-filingcabinet",
version=find_version("filingcabinet", "__init__.py"),
Expand Down Expand Up @@ -57,8 +79,9 @@ def find_version(*file_paths):
"annotate": [
"fcdocs-annotate @ https://github.com/okfde/fcdocs-annotate/archive/refs/heads/main.zip"
],
"test": tests_require,
},
test_requires=["factory_boy", "whitenoise"],
tests_require=tests_require,
include_package_data=True,
classifiers=[
"Development Status :: 3 - Alpha",
Expand Down
57 changes: 57 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os

import pytest
from pytest_factoryboy import register

from .factories import (
DocumentCollectionFactory,
DocumentFactory,
PageFactory,
UserFactory,
)

register(UserFactory)
register(DocumentFactory)
register(DocumentCollectionFactory)
register(PageFactory)

os.environ.setdefault("DJANGO_ALLOW_ASYNC_UNSAFE", "true")


@pytest.fixture
def dummy_user():
yield UserFactory(username="dummy")


@pytest.fixture()
def page(browser):
context = browser.new_context(locale="en")
page = context.new_page()
yield page
page.close()


@pytest.fixture()
def processed_document():
path = "docs/ef/39/5b/ef395b666014488aa551e431e653a1d9"
doc = DocumentFactory(
uid="ef395b66-6014-488a-a551-e431e653a1d9",
pdf_file=f"{path}/example.pdf",
file_size=260082,
pending=False,
num_pages=4,
language="de",
public=True,
)
for i in range(1, 5):
PageFactory(
document=doc,
number=i,
width=2481,
height=3508,
image=f"{path}/page-p{i}-original.png",
image_large=f"{path}/page-p{i}-large.png",
image_normal=f"{path}/page-p{i}-normal.png",
image_small=f"{path}/page-p{i}-small.png",
)
yield doc
40 changes: 40 additions & 0 deletions tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.conf import settings
from django.template.defaultfilters import slugify

import factory

from filingcabinet import get_document_model, get_documentcollection_model
from filingcabinet.models import Page

Document = get_document_model()
DocumentCollection = get_documentcollection_model()


class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = settings.AUTH_USER_MODEL


class DocumentFactory(factory.django.DjangoModelFactory):
class Meta:
model = Document

title = factory.Sequence(lambda n: "Document {}".format(n))
slug = factory.LazyAttribute(lambda o: slugify(o.title))


class PageFactory(factory.django.DjangoModelFactory):
class Meta:
model = Page

document = factory.SubFactory(DocumentFactory)
number = factory.Sequence(lambda n: n)


class DocumentCollectionFactory(factory.django.DjangoModelFactory):
class Meta:
model = DocumentCollection

title = factory.Sequence(lambda n: "DocumentCollection {}".format(n))
slug = factory.LazyAttribute(lambda o: slugify(o.title))
user = factory.SubFactory(UserFactory)

0 comments on commit 3e2f19f

Please sign in to comment.