I can't use TransactionTestCase.assertNumQueries obediently because I messed with the test DB connection. In case.
# coding: utf-8
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
from django.conf import settings
from django.db import connections
import unittest, pprint
from collections import deque
from core.models import Kvs, Hoge
class TestKvs(unittest.TestCase):
def setUp(self):
settings.DEBUG = True
def tearDown(self):
# django.db. reset_queries are fine
connections['default'].queries_log = deque()
settings.DEBUG = False
def test_simple(self):
hoge = list(Hoge.objects.all())[0]
# select
self.assertEqual(len(connections['default'].queries), 1)
Kvs.set(hoge.id, hoge)
# select, begin, update
self.assertEqual(len(connections['default'].queries), 4)
got = Kvs.get(hoge.id)
# select
self.assertEqual(got.id, hoge.id)
self.assertEqual(len(connections['default'].queries), 5)
if __name__ == "__main__":
unittest.main()
Reference: [Django: is there a way to count SQL queries from an unit test?](Http://stackoverflow.com/questions/1254170/django-is-there-a-way-to-count-sql-queries- from-an-unit-test)
Recommended Posts