mirror of
https://github.com/DRYTRIX/TimeTracker.git
synced 2025-12-30 15:49:44 -06:00
Fixed tests
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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'))
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user