Skip to content

Commit

Permalink
Merge pull request #49 from saritasa-nest/feature/prepare-new-structu…
Browse files Browse the repository at this point in the history
…re-for-tests

Prepare new structure for tests
  • Loading branch information
Eg0ra authored Oct 7, 2024
2 parents 7ea1fa8 + 3e899f7 commit fa6335e
Show file tree
Hide file tree
Showing 42 changed files with 82 additions and 72 deletions.
10 changes: 7 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ htmlcov/
.cache
nosetests.xml
coverage.xml
coverage.lcov
*.cover
.hypothesis/
.pytest_cache/
Expand Down Expand Up @@ -106,6 +107,9 @@ ENV/
.idea/

# Test files
tests/media
tests/static
tests/db.sqlite3
test_project/media
test_project/static
test_project/db.sqlite3

# Database dumps
*.sql
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.append(str(pathlib.Path.cwd()))
sys.path.append(str(pathlib.Path("..").resolve()))
sys.path.append(str(pathlib.Path("../tests").resolve()))
sys.path.append(str(pathlib.Path("../test_project").resolve()))
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"

django.setup()
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ ignore = [
# https://docs.astral.sh/ruff/rules/line-too-long
"E501",
]
"**/tests/*" = [
"**/test_project/*" = [
# https://docs.astral.sh/ruff/rules/assert
"S101",
# https://docs.astral.sh/ruff/rules/hardcoded-password-func-arg
Expand Down Expand Up @@ -385,11 +385,11 @@ exclude = [
]

[[tool.mypy.overrides]]
module = "tests.*"
module = "test_project.*"
warn_unreachable = false

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "tests.settings"
DJANGO_SETTINGS_MODULE = "test_project.settings"
python_files = [
"tests.py",
"test_*.py",
Expand Down
8 changes: 4 additions & 4 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
"saritasa_invocations": saritasa_invocations.Config(
project_name="django-import-export-extensions",
celery=saritasa_invocations.CelerySettings(
app="tests.celery_app:app",
app="test_project.celery_app:app",
),
django=saritasa_invocations.DjangoSettings(
manage_file_path="tests/manage.py",
settings_path="tests.settings",
apps_path="tests",
manage_file_path="test_project/manage.py",
settings_path="test_project.settings",
apps_path="test_project",
),
github_actions=saritasa_invocations.GitHubActionsSettings(
hosts=("postgres", "redis"),
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/celery_app.py → test_project/celery_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from celery import Celery

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings")

app = Celery(
"import_export_extensions",
Expand Down
43 changes: 43 additions & 0 deletions test_project/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from django.conf import settings

import pytest


def pytest_configure() -> None:
"""Set up Django settings for tests.
`pytest` automatically calls this function once when tests are run.
"""
settings.TESTING = True


@pytest.fixture(scope="session", autouse=True)
def django_db_setup(django_db_setup): # noqa: PT004
"""Set up test db for testing."""


@pytest.fixture(autouse=True)
def enable_db_access_for_all_tests(django_db_setup, db): # noqa: PT004
"""Allow all tests to access DB."""


@pytest.fixture(scope="session", autouse=True)
def _temp_directory_for_media(tmpdir_factory):
"""Fixture that set temp directory for all media files.
This fixture changes DEFAULT_FILE_STORAGE or STORAGES variable
to filesystem and provides temp dir for media.
PyTest cleans up this temp dir by itself after few test runs
"""
if hasattr(settings, "STORAGES"):
settings.STORAGES["default"]["BACKEND"] = (
"django.core.files.storage.FileSystemStorage"
)
else:
settings.DEFAULT_FILE_STORAGE = (
"django.core.files.storage.FileSystemStorage"
)
media = tmpdir_factory.mktemp("tmp_media")
settings.MEDIA_ROOT = media
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from import_export_extensions.api import views
from tests.fake_app.resources import SimpleArtistResource

from ..resources import SimpleArtistResource


class ArtistExportViewSet(views.ExportJobViewSet):
Expand Down
2 changes: 1 addition & 1 deletion tests/fake_app/apps.py → test_project/fake_app/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
class IOExtensionsAppConfig(AppConfig):
"""Fake app config."""

name = "tests.fake_app"
name = "test_project.fake_app"
verbose_name = "Import Export Fake App"
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ArtistImportJobFactory(factory.django.DjangoModelFactory):
ArtistImportJobFactory(artists=[artist1, artist2])
"""

resource_path = "tests.fake_app.resources.SimpleArtistResource"
resource_path = "test_project.fake_app.resources.SimpleArtistResource"
resource_kwargs: dict[str, str] = {}

class Meta:
Expand Down Expand Up @@ -86,7 +86,7 @@ def data_file(self):
class ArtistExportJobFactory(factory.django.DjangoModelFactory):
"""Factory for creating ExportJob for Artist."""

resource_path = "tests.fake_app." "resources.SimpleArtistResource"
resource_path = "test_project.fake_app." "resources.SimpleArtistResource"
resource_kwargs: dict[str, str] = {}
file_format_path = "import_export.formats.base_formats.CSV"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django_filters import rest_framework as filters

from tests.fake_app.models import Artist
from .models import Artist


class ArtistFilterSet(filters.FilterSet):
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions tests/settings.py → test_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"django_extensions",
"import_export",
"import_export_extensions",
"tests.fake_app",
"test_project.fake_app",
]

MIDDLEWARE = [
Expand All @@ -43,7 +43,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "tests.urls"
ROOT_URLCONF = "test_project.urls"

TEMPLATES = [
{
Expand All @@ -63,7 +63,7 @@
},
]

WSGI_APPLICATION = "tests.wsgi.application"
WSGI_APPLICATION = "test_project.wsgi.application"

# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
Expand Down
File renamed without changes.
47 changes: 3 additions & 44 deletions tests/conftest.py → test_project/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile
Expand All @@ -8,29 +7,10 @@
import pytest

from import_export_extensions.models import ExportJob, ImportJob
from tests.fake_app.models import Artist, Band, Membership

from .fake_app import factories
from .fake_app.factories import ArtistImportJobFactory


def pytest_configure() -> None:
"""Set up Django settings for tests.
`pytest` automatically calls this function once when tests are run.
"""
settings.TESTING = True


@pytest.fixture(scope="session", autouse=True)
def django_db_setup(django_db_setup): # noqa: PT004
"""Set up test db for testing."""


@pytest.fixture(autouse=True)
def enable_db_access_for_all_tests(django_db_setup, db): # noqa: PT004
"""Allow all tests to access DB."""
from ..fake_app import factories
from ..fake_app.factories import ArtistImportJobFactory
from ..fake_app.models import Artist, Band, Membership


@pytest.fixture
Expand Down Expand Up @@ -119,27 +99,6 @@ def superuser():
)


@pytest.fixture(scope="session", autouse=True)
def _temp_directory_for_media(tmpdir_factory):
"""Fixture that set temp directory for all media files.
This fixture changes DEFAULT_FILE_STORAGE or STORAGES variable
to filesystem and provides temp dir for media.
PyTest cleans up this temp dir by itself after few test runs
"""
if hasattr(settings, "STORAGES"):
settings.STORAGES["default"]["BACKEND"] = (
"django.core.files.storage.FileSystemStorage"
)
else:
settings.DEFAULT_FILE_STORAGE = (
"django.core.files.storage.FileSystemStorage"
)
media = tmpdir_factory.mktemp("tmp_media")
settings.MEDIA_ROOT = media


@pytest.fixture
def api_client() -> test.APIClient:
"""Create api client."""
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_import_api_creates_import_job(
uploaded_file: SimpleUploadedFile,
):
"""Ensure import start api creates new import job."""
import_job_count = ImportJob.objects.count()
response = admin_api_client.post(
path=reverse("import-artist-start"),
data={
Expand All @@ -25,7 +26,7 @@ def test_import_api_creates_import_job(

assert response.status_code == status.HTTP_201_CREATED
assert response.data["import_status"] == "CREATED"
assert ImportJob.objects.first()
assert ImportJob.objects.count() == import_job_count + 1


@pytest.mark.django_db(transaction=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import pytest

from import_export_extensions.resources import CeleryModelResource
from tests.fake_app.factories import MembershipFactory
from tests.fake_app.models import Artist, Membership
from tests.fake_app.resources import ArtistResourceWithM2M

from ...fake_app.factories import MembershipFactory
from ...fake_app.models import Artist, Membership
from ...fake_app.resources import ArtistResourceWithM2M


@pytest.fixture
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_parse_data_import_errors(
"""
mocker.patch(
target="tests.fake_app.resources.SimpleArtistResource.import_field",
target="test_project.fake_app.resources.SimpleArtistResource.import_field",
side_effect=ValueError("Invalid data"),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
FileWidget,
IntermediateManyToManyWidget,
)
from tests.fake_app.factories import (

from ..fake_app.factories import (
ArtistFactory,
ArtistImportJobFactory,
BandFactory,
MembershipFactory,
)
from tests.fake_app.models import Band, Membership
from ..fake_app.models import Band, Membership


@pytest.fixture
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/wsgi.py → test_project/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings")

application = get_wsgi_application()
Empty file removed tests/test_widgets/__init__.py
Empty file.

0 comments on commit fa6335e

Please sign in to comment.