Fixed tests

This commit is contained in:
Dries Peeters
2025-11-13 11:27:48 +01:00
parent cc44c1a09e
commit bb77e076ee
5 changed files with 43 additions and 18 deletions

View File

@@ -73,7 +73,9 @@ class TimeEntry(db.Model):
self.calculate_duration()
def __repr__(self):
return f'<TimeEntry {self.id}: {self.user.username} on {self.project.name}>'
user_name = self.user.username if self.user else 'deleted_user'
project_name = self.project.name if self.project else 'deleted_project'
return f'<TimeEntry {self.id}: {user_name} on {project_name}>'
@property
def is_active(self):

View File

@@ -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

View File

@@ -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

View File

@@ -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'))

View File

@@ -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