Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created June 28, 2016 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/ffc6f96e4688dfb53810f4e4d6ba4d92 to your computer and use it in GitHub Desktop.
Save podhmo/ffc6f96e4688dfb53810f4e4d6ba4d92 to your computer and use it in GitHub Desktop.
# -*- coding:utf-8 -*-
import django
from django.conf import settings
from django.test.utils import get_runner
from django.test import TestCase
"""
TestCase.setUpTestData() is sometimes helpful. (worst case)
"""
settings.configure(
DEBUG=True,
DATABASES={"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:"
}},
INSTALLED_APPS=[
'django.contrib.auth',
'django.contrib.contenttypes',
__name__,
],
)
django.setup()
class Tests(TestCase):
def setUp(self):
from django.contrib.auth.models import User
for i in range(100):
User.objects.create_superuser("admin{}".format(i), "myemail{}@example.com".format(i), '')
def test_query(self):
from django.contrib.auth.models import User
with self.assertNumQueries(1):
actual = User.objects.filter(username__startswith="admin1").count()
self.assertEqual(actual, 11)
def test_query1(self):
from django.contrib.auth.models import User
with self.assertNumQueries(1):
actual = User.objects.filter(username__startswith="admin1").count()
self.assertEqual(actual, 11)
def test_query2(self):
from django.contrib.auth.models import User
with self.assertNumQueries(1):
actual = User.objects.filter(username__startswith="admin1").count()
self.assertEqual(actual, 11)
if __name__ == "__main__":
from django.apps import apps
for config in apps.get_app_configs():
config.models_module = __name__
factory = get_runner(settings)
test_runner = factory()
status = test_runner.run_tests([__name__])
# -*- coding:utf-8 -*-
import django
from django.conf import settings
from django.test.utils import get_runner
from django.test import TestCase
"""
TestCase.setUpTestData() is sometimes helpful.
"""
settings.configure(
DEBUG=True,
DATABASES={"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:"
}},
INSTALLED_APPS=[
'django.contrib.auth',
'django.contrib.contenttypes',
__name__,
]
)
django.setup()
class Tests(TestCase):
@classmethod
def setUpTestData(cls):
from django.contrib.auth.models import User
for i in range(100):
User.objects.create_superuser("admin{}".format(i), "myemail{}@example.com".format(i), '')
def test_query(self):
from django.contrib.auth.models import User
with self.assertNumQueries(1):
actual = User.objects.filter(username__startswith="admin1").count()
self.assertEqual(actual, 11)
def test_query1(self):
from django.contrib.auth.models import User
with self.assertNumQueries(1):
actual = User.objects.filter(username__startswith="admin1").count()
self.assertEqual(actual, 11)
def test_query2(self):
from django.contrib.auth.models import User
with self.assertNumQueries(1):
actual = User.objects.filter(username__startswith="admin1").count()
self.assertEqual(actual, 11)
if __name__ == "__main__":
from django.apps import apps
for config in apps.get_app_configs():
config.models_module = __name__
factory = get_runner(settings)
test_runner = factory()
status = test_runner.run_tests([__name__])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment