Skip to content

Commit 34e9899

Browse files
committed
upated db
1 parent caf2d6b commit 34e9899

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

backend/apps/users/views/tests/test_main_view.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TestMainViews:
1515
"""Integration tests for main script execution endpoints using real Supabase"""
1616

1717
@pytest.fixture(autouse=True)
18-
def setup(self, test_django_user, test_admin_django_user, django_db_blocker):
18+
def setup(self, test_django_user, test_admin_django_user, django_db_blocker, ensure_test_tables):
1919
# Default script parameters
2020
self.script_params = {
2121
"parameters": {
@@ -49,19 +49,24 @@ def setup(self, test_django_user, test_admin_django_user, django_db_blocker):
4949
yield
5050

5151
with django_db_blocker.unblock():
52-
# Clean up after tests
53-
low_credit_auth_user = self.low_credit_user.user
54-
self.low_credit_user.delete()
55-
low_credit_auth_user.delete()
56-
CreditTransaction.objects.filter(user=low_credit_auth_user).delete()
57-
58-
# Reset credits for test users
59-
if hasattr(self.test_user, 'credits_balance'):
60-
self.test_user.credits_balance = 1000
61-
self.test_user.save()
62-
if hasattr(self.admin_user, 'credits_balance'):
63-
self.admin_user.credits_balance = 1000
64-
self.admin_user.save()
52+
# Clean up after tests - use try/except to handle missing tables
53+
try:
54+
# Clear transactions first to avoid foreign key constraint errors
55+
from apps.credits.models import CreditTransaction
56+
CreditTransaction.objects.filter(user=self.low_credit_user.user).delete()
57+
58+
# Then delete the users
59+
low_credit_auth_user = self.low_credit_user.user
60+
self.low_credit_user.delete()
61+
low_credit_auth_user.delete()
62+
63+
# Reset credits for test users
64+
if hasattr(self.test_user, 'credits_balance'):
65+
self.test_user.credits_balance = 1000
66+
self.test_user.save()
67+
except Exception as e:
68+
# Log the error but don't fail the test
69+
print(f"Cleanup error: {str(e)}")
6570

6671
def test_main_script_execution_success(self, monkeypatch, django_db_blocker):
6772
"""Test successful script execution with real Supabase auth"""

backend/conftest.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import sys
55
import django
6+
import pytest
67

78
# Add the project root to the Python path
89
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
@@ -33,3 +34,28 @@ def pytest_configure(config):
3334

3435
# Also filter out RuntimeWarnings about coroutines not being awaited
3536
warnings.filterwarnings("ignore", message="coroutine .* was never awaited")
37+
38+
# Add fixture for properly creating tables in test database
39+
@pytest.fixture(scope='function')
40+
def ensure_test_tables(django_db_setup, django_db_blocker):
41+
"""Ensure all required tables exist in the test database before tests run"""
42+
with django_db_blocker.unblock():
43+
from django.core.management import call_command
44+
# Migrate specific apps that might cause issues
45+
call_command('migrate', 'credits', verbosity=0)
46+
call_command('migrate', 'users', verbosity=0)
47+
48+
# Override default databases to include supabase in test isolation
49+
@pytest.fixture(scope='function')
50+
def django_db_setup(request, django_db_setup, django_db_blocker):
51+
"""Custom database setup that adds supabase to available databases"""
52+
from django.test import override_settings
53+
54+
# Get the existing databases
55+
from django.conf import settings
56+
databases = list(settings.DATABASES.keys())
57+
if 'supabase' not in databases:
58+
databases.append('supabase')
59+
60+
with override_settings(DATABASE_ROUTERS=[]):
61+
yield

0 commit comments

Comments
 (0)