Topic 15 / 40
APITestCase & Database Isolation with Savepoints
1. Deep Architecture
Django test runs run inside database transactions. Using TestCase, Django wraps every test method in an atomic block, rolling back writes at the end. For nested transactions, database Savepoints are used. This prevents database state from leaking between tests, ensuring tests remain isolated.
2. The Feynman Gatekeeper
[KNOWLEDGE CHECK] How do transaction savepoints allow rollback isolation in tests without dropping tables between runs?
3. The Code
from rest_framework.test import APITestCase
from sanjaya_core.models import Video
class VideoTest(APITestCase):
def test_list_videos(self):
# Database reads are isolated here
response = self.client.get('/api/videos/')
self.assertEqual(response.status_code, 200)
4. The Funnel
Stat Level-Up: Isolation Guard (Lvl 1).
Sanjaya Integration: Write isolated tests to verify that video analysis states do not contaminate tenant statistics.