From bb77e076ee7a754b703b95ae40be78aca22c4781 Mon Sep 17 00:00:00 2001 From: Dries Peeters Date: Thu, 13 Nov 2025 11:27:48 +0100 Subject: [PATCH] Fixed tests --- app/models/time_entry.py | 4 +++- tests/conftest.py | 8 ++++++- tests/test_admin_settings_logo.py | 1 + tests/test_admin_users.py | 9 +++++++ tests/test_currency_display.py | 39 ++++++++++++++++++------------- 5 files changed, 43 insertions(+), 18 deletions(-) diff --git a/app/models/time_entry.py b/app/models/time_entry.py index 5333af7..a49b123 100644 --- a/app/models/time_entry.py +++ b/app/models/time_entry.py @@ -73,7 +73,9 @@ class TimeEntry(db.Model): self.calculate_duration() def __repr__(self): - return f'' + user_name = self.user.username if self.user else 'deleted_user' + project_name = self.project.name if self.project else 'deleted_project' + return f'' @property def is_active(self): diff --git a/tests/conftest.py b/tests/conftest.py index 9088cc4..07a5c4b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -337,7 +337,13 @@ def time_entry(app, user, project): db.session.add(entry) db.session.commit() - db.session.refresh(entry) + # Refresh entry, but handle case where related objects might be deleted + try: + db.session.refresh(entry) + except Exception: + # If refresh fails, just return the entry as-is + # This can happen if user/project are deleted before this fixture is used + pass return entry diff --git a/tests/test_admin_settings_logo.py b/tests/test_admin_settings_logo.py index 8efe470..8cda560 100644 --- a/tests/test_admin_settings_logo.py +++ b/tests/test_admin_settings_logo.py @@ -24,6 +24,7 @@ def authenticated_admin_client(client, admin_user): """Create an authenticated admin client.""" with client.session_transaction() as sess: sess['_user_id'] = str(admin_user.id) + sess['_fresh'] = True return client diff --git a/tests/test_admin_users.py b/tests/test_admin_users.py index fcefe77..284e0ad 100644 --- a/tests/test_admin_users.py +++ b/tests/test_admin_users.py @@ -25,6 +25,7 @@ class TestAdminUserList: # Login as admin with client.session_transaction() as sess: sess['_user_id'] = str(admin_user.id) + sess['_fresh'] = True response = client.get(url_for('admin.list_users')) assert response.status_code == 200 @@ -418,6 +419,7 @@ class TestAdminUserDeletionCascading: with client: with client.session_transaction() as sess: sess['_user_id'] = str(admin_user.id) + sess['_fresh'] = True response = client.get(url_for('admin.list_users')) assert response.status_code == 200 @@ -431,6 +433,7 @@ class TestAdminUserDeletionCascading: with client: with client.session_transaction() as sess: sess['_user_id'] = str(admin_user.id) + sess['_fresh'] = True response = client.get(url_for('admin.list_users')) assert response.status_code == 200 @@ -461,6 +464,7 @@ class TestUserDeletionSmokeTests: with client: with client.session_transaction() as sess: sess['_user_id'] = str(admin_user.id) + sess['_fresh'] = True # Delete the user response = client.post( @@ -496,6 +500,7 @@ class TestUserDeletionSmokeTests: with client: with client.session_transaction() as sess: sess['_user_id'] = str(admin_user.id) + sess['_fresh'] = True # Try to delete response = client.post( @@ -517,6 +522,7 @@ class TestUserDeletionSmokeTests: with client: with client.session_transaction() as sess: sess['_user_id'] = str(admin_user.id) + sess['_fresh'] = True # Try to delete the only admin response = client.post( @@ -538,6 +544,7 @@ class TestUserDeletionSmokeTests: with client: with client.session_transaction() as sess: sess['_user_id'] = str(admin_user.id) + sess['_fresh'] = True response = client.get(url_for('admin.list_users')) @@ -576,6 +583,7 @@ class TestUserDeletionSmokeTests: with client: with client.session_transaction() as sess: sess['_user_id'] = str(admin_user.id) + sess['_fresh'] = True response = client.get(url_for('admin.list_users')) @@ -599,6 +607,7 @@ class TestUserDeletionSmokeTests: with client: with client.session_transaction() as sess: sess['_user_id'] = str(admin_user.id) + sess['_fresh'] = True # Step 2: View user list (should show user) response = client.get(url_for('admin.list_users')) diff --git a/tests/test_currency_display.py b/tests/test_currency_display.py index 5b43625..13d91fd 100644 --- a/tests/test_currency_display.py +++ b/tests/test_currency_display.py @@ -30,6 +30,7 @@ def test_client_with_auth(app, client, admin_user): """Return authenticated client.""" with client.session_transaction() as sess: sess['_user_id'] = str(admin_user.id) + sess['_fresh'] = True return client @@ -45,27 +46,33 @@ def usd_settings(app): @pytest.fixture def sample_client(app): """Create a sample client.""" - client = Client( - name='Test Client', - email='test@example.com' - ) - db.session.add(client) - db.session.commit() - return client + with app.app_context(): + client = Client( + name='Test Client', + email='test@example.com' + ) + db.session.add(client) + db.session.commit() + db.session.refresh(client) + return client @pytest.fixture def sample_project(app, sample_client): """Create a sample project.""" - project = Project( - name='Test Project', - client_id=sample_client.id, - status='active', - hourly_rate=Decimal('100.00') - ) - db.session.add(project) - db.session.commit() - return project + with app.app_context(): + # Store client_id before accessing relationship + client_id = sample_client.id + project = Project( + name='Test Project', + client_id=client_id, + status='active', + hourly_rate=Decimal('100.00') + ) + db.session.add(project) + db.session.commit() + db.session.refresh(project) + return project @pytest.fixture