Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/fixtures/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,29 @@ def setup(self):

return self

def reset_test_data(self):
"""Reset test storage for per-test isolation.

This method clears all test storage (SQLite in-memory DB, memory collections)
and re-initializes services. Use this in tearDown() for per-test isolation.

WARNING: This clears auth credentials, so tests using bearer tokens will need
to re-create their tokens after calling this method.

For tests that use authentication, consider using unique identifiers per test
(e.g., filtering by created_by) instead of full reset, or re-create the token
in setUp() after calling this in tearDown().
"""
import campus.storage.testing

# Reset storage (clears SQLite in-memory DB and memory collections)
campus.storage.testing.reset_test_storage()

# Re-initialize auth and yapper services
# These are idempotent and will recreate necessary tables/collections
auth.init()
yapper.init()

def close(self):
"""Clean up service instances and resources.

Expand Down
47 changes: 25 additions & 22 deletions tests/integration/api/test_assignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,32 @@ def setUpClass(cls):
raise RuntimeError("Expected Flask app from service manager")

cls.app = api_app
cls.user_id = schema.UserID("test.user@campus.test")

# Initialize credentials storage (needed for token creation)
# This may have been cleared by a previous test class
from campus.auth import resources as auth_resources
auth_resources.credentials.init_storage()
auth_resources.user.init_storage()
def setUp(self):
"""Set up test environment before each test."""
self.client = self.app.test_client()

# Set up test context
self.app_context = self.app.app_context()
self.app_context.push()

# Create test user token for bearer auth
cls.user_id = schema.UserID("test.user@campus.test")
cls.token = create_test_token(cls.user_id)
cls.auth_headers = get_bearer_auth_headers(cls.token)
# Re-create before each test since tearDown() resets storage
self.token = create_test_token(self.user_id)
self.auth_headers = get_bearer_auth_headers(self.token)

def tearDown(self):
"""Clean up after each test.

Resets storage for per-test isolation, ensuring tests don't pollute
each other's state. This eliminates the need for test_00_* prefixes.
"""
self.app_context.pop()

# Reset storage for per-test isolation
if hasattr(self, 'service_manager'):
self.service_manager.reset_test_data()

@classmethod
def tearDownClass(cls):
Expand All @@ -49,20 +64,8 @@ def tearDownClass(cls):
import campus.storage.testing
campus.storage.testing.reset_test_storage()

def setUp(self):
"""Set up test environment before each test."""
self.client = self.app.test_client()

# Set up test context
self.app_context = self.app.app_context()
self.app_context.push()

def tearDown(self):
"""Clean up after each test."""
self.app_context.pop()

def test_00_list_assignments_empty(self):
"""GET /assignments should return empty list initially. (Named test_00_* to run first for isolation)"""
def test_list_assignments_empty(self):
"""GET /assignments should return empty list initially."""
response = self.client.get('/api/v1/assignments/', headers=self.auth_headers)

assert response.status_code == 200
Expand Down