Write correct Django tests — TestCase vs TransactionTestCase, setUpTestData, factory-boy, assertNumQueries, mock.patch placement, and DRF APITestCase patterns
99
99%
Does it follow best practices?
Impact
99%
1.33xAverage score across 2 eval scenarios
Passed
No known issues
Write tests for a Django blog API. The app has the following models:
title (CharField), body (TextField), author (ForeignKey to User), created_at (DateTimeField auto_now_add), published (BooleanField default=False)post (ForeignKey to Post), author (ForeignKey to User), body (TextField), created_at (DateTimeField auto_now_add)name (CharField unique) with a ManyToManyField on Post called tagsThe API exposes these endpoints via Django REST Framework:
GET /api/posts/ -- list published posts with their tags (paginated)POST /api/posts/ -- create a new post (authenticated)GET /api/posts/<id>/ -- retrieve a single postPUT /api/posts/<id>/ -- update a post (author only)DELETE /api/posts/<id>/ -- delete a post (author only)GET /api/posts/<id>/comments/ -- list comments for a postPOST /api/posts/<id>/comments/ -- add a comment (authenticated)GET /api/tags/ -- list all tagsProduce a test file at blog/tests/test_blog_api.py with comprehensive test coverage for the above endpoints. Include any supporting factory files you need. Assume the models and views already exist; you are only writing the tests.