chore(pytest): Create and use fixture of Fresh DB instead of migrations for testing

ref: #770
This commit is contained in:
2025-05-23 14:30:01 +09:30
parent 95281d35eb
commit 06ad1fec5e
7 changed files with 300 additions and 1 deletions

View File

@ -1,7 +1,11 @@
import datetime
import pytest
import os
import sqlite3
import sys
from django.core.management import call_command
from django.conf import settings
from django.test import (
TestCase
)
@ -10,6 +14,32 @@ from access.models.tenant import Tenant
@pytest.fixture(scope="session", autouse = True)
def load_sqlite_fixture(django_db_setup, django_db_blocker):
"""
Load the SQLite database from an SQL dump file.
This runs during test setup and loads the schema and data via SQLite directly.
"""
db_path = settings.DATABASES['default']['NAME']
sql_file_path = os.path.join('app/fixtures', 'fresh_db.sql')
with django_db_blocker.unblock():
with open(sql_file_path, 'r') as f:
sql_script = f.read()
conn = sqlite3.connect(db_path)
try:
conn.executescript(sql_script)
finally:
conn.close()
with django_db_blocker.unblock():
call_command('loaddata','fresh_db')
def pytest_configure(config):
print("\n--- Pytest Launch Arguments ---")