-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
1,110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
"""Pytest configuration for example project.""" | ||
|
||
from pathlib import Path | ||
|
||
import pytest | ||
from django.conf import settings | ||
from django.db import connection | ||
from django.db.migrations.recorder import MigrationRecorder | ||
|
||
|
||
class MigrationTracker: | ||
"""Tracks migration files created during tests.""" | ||
|
||
def __init__(self): | ||
self.initial_migrations = set() | ||
|
||
def snapshot_migrations(self): | ||
"""Take a snapshot of existing migration files.""" | ||
self.initial_migrations.clear() | ||
for app_config in settings.INSTALLED_APPS: | ||
if "." in app_config: | ||
app_name = app_config.split(".")[-1] | ||
migrations_dir = Path(settings.BASE_DIR) / "example_project" / app_name / "migrations" | ||
if migrations_dir.exists(): | ||
self.initial_migrations.update(str(f.absolute()) for f in migrations_dir.glob("[0-9]*.py")) | ||
|
||
def get_new_migrations(self): | ||
"""Get list of migration files created since last snapshot.""" | ||
current_migrations = set() | ||
for app_config in settings.INSTALLED_APPS: | ||
if "." in app_config: | ||
app_name = app_config.split(".")[-1] | ||
migrations_dir = Path(settings.BASE_DIR) / "example_project" / app_name / "migrations" | ||
if migrations_dir.exists(): | ||
current_migrations.update(str(f.absolute()) for f in migrations_dir.glob("[0-9]*.py")) | ||
return current_migrations - self.initial_migrations | ||
|
||
|
||
# Create a single instance to use across tests | ||
migration_tracker = MigrationTracker() | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def django_db_setup(django_db_setup, django_db_blocker): | ||
"""Setup database for testing and handle migration cleanup.""" | ||
with django_db_blocker.unblock(): | ||
# Store initial migration state | ||
initial_db_migrations = set( | ||
(migration.app, migration.name) for migration in MigrationRecorder.Migration.objects.all() | ||
) | ||
|
||
# Take snapshot of migration files | ||
migration_tracker.snapshot_migrations() | ||
|
||
yield | ||
|
||
# Clean up migrations created during testing | ||
with connection.cursor() as cursor: | ||
# Get current migrations | ||
final_migrations = set( | ||
(migration.app, migration.name) for migration in MigrationRecorder.Migration.objects.all() | ||
) | ||
|
||
# Find and remove new migrations from database | ||
new_migrations = final_migrations - initial_db_migrations | ||
for app, name in new_migrations: | ||
cursor.execute("DELETE FROM django_migrations WHERE app = %s AND name = %s", [app, name]) | ||
|
||
# Clean up new migration files | ||
new_migration_files = migration_tracker.get_new_migrations() | ||
for migration_file in new_migration_files: | ||
try: | ||
Path(migration_file).unlink() | ||
except FileNotFoundError: | ||
pass # File was already deleted | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def clean_test_migrations(): | ||
"""Track and clean migrations for each test.""" | ||
# Take snapshot before test | ||
migration_tracker.snapshot_migrations() | ||
|
||
yield | ||
|
||
# Clean up new migration files after test | ||
new_migration_files = migration_tracker.get_new_migrations() | ||
for migration_file in new_migration_files: | ||
try: | ||
Path(migration_file).unlink() | ||
except FileNotFoundError: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Test cases for management commands with migration cleanup.""" | ||
|
||
from io import StringIO | ||
|
||
import pytest | ||
from django.core.management import call_command | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestManagementCommands: | ||
"""Test cases for the management commands.""" | ||
|
||
def test_listoptions_command(self): | ||
"""Test the listoptions command.""" | ||
out = StringIO() | ||
call_command("listoptions", stdout=out) | ||
output = out.getvalue() | ||
assert "Model: TaskPriorityOption" in output | ||
assert "Model: TaskStatusOption" in output | ||
|
||
def test_syncoptions_command(self): | ||
"""Test the syncoptions command.""" | ||
out = StringIO() | ||
call_command("syncoptions", stdout=out) | ||
output = out.getvalue() | ||
assert "Model: TaskPriorityOption" in output | ||
assert "Model: TaskStatusOption" in output | ||
|
||
def test_maketriggers_command(self): | ||
"""Test the maketriggers command.""" | ||
out = StringIO() | ||
call_command("maketriggers", stdout=out) | ||
output = out.getvalue() | ||
assert "Creating migration for taskpriorityselection" in output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Test cases for the django-tenant-options package.""" | ||
|
||
from django.apps import apps | ||
from django.conf import settings | ||
|
||
|
||
def test_succeeds() -> None: | ||
"""It exits with a status code of zero.""" | ||
assert 0 == 0 | ||
|
||
|
||
def test_settings() -> None: | ||
"""It exits with a status code of zero.""" | ||
assert settings.USE_TZ is True | ||
|
||
|
||
def test_apps() -> None: | ||
"""It exits with a status code of zero.""" | ||
assert "django_tenant_options" in apps.get_app_config("django_tenant_options").name |
Oops, something went wrong.