Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Last active August 25, 2020 06:15
Show Gist options
  • Save velotiotech/c9b38c604648b4ce25267617fb094fee to your computer and use it in GitHub Desktop.
Save velotiotech/c9b38c604648b4ce25267617fb094fee to your computer and use it in GitHub Desktop.
from django.urls import reverse
from django_seed import Seed
from core.models import Author, Book
from rest_framework.test import APITestCase
seeder = Seed.seeder()
class BooksTestCase(APITestCase):
def test_list_books(self):
# Add dummy data to the Author and Book Table
seeder.add_entity(Author, 5)
seeder.add_entity(Book, 10)
seeder.execute()
# we expect the result in 1 query
with self.assertNumQueries(1):
response = self.client.get(reverse("book_list"), format="json")
# test output
$ ./manage.py test
.
.
.
AssertionError: 11 != 1 : 11 queries executed, 1 expected
Captured queries were:
1. SELECT "core_book"."id", "core_book"."name", "core_book"."author_id", "core_book"."created_at" FROM "core_book"
2. SELECT "core_author"."id", "core_author"."name" FROM "core_author" WHERE "core_author"."id" = 4 LIMIT 21
3. SELECT "core_author"."id", "core_author"."name" FROM "core_author" WHERE "core_author"."id" = 1 LIMIT 21
4. SELECT "core_author"."id", "core_author"."name" FROM "core_author" WHERE "core_author"."id" = 4 LIMIT 21
5. SELECT "core_author"."id", "core_author"."name" FROM "core_author" WHERE "core_author"."id" = 4 LIMIT 21
6. SELECT "core_author"."id", "core_author"."name" FROM "core_author" WHERE "core_author"."id" = 5 LIMIT 21
7. SELECT "core_author"."id", "core_author"."name" FROM "core_author" WHERE "core_author"."id" = 5 LIMIT 21
8. SELECT "core_author"."id", "core_author"."name" FROM "core_author" WHERE "core_author"."id" = 1 LIMIT 21
9. SELECT "core_author"."id", "core_author"."name" FROM "core_author" WHERE "core_author"."id" = 3 LIMIT 21
10. SELECT "core_author"."id", "core_author"."name" FROM "core_author" WHERE "core_author"."id" = 3 LIMIT 21
11. SELECT "core_author"."id", "core_author"."name" FROM "core_author" WHERE "core_author"."id" = 5 LIMIT 21
----------------------------------------------------------------------
Ran 1 test in 0.027s
FAILED (failures=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment