optimize-later/testproject/testapp/tests.py

38 lines
1.1 KiB
Python
Raw Normal View History

2017-08-13 18:50:47 -04:00
import imp
import sys
import uuid
from optimize_later.core import optimize_later, OptimizeReport
try:
import django
except ImportError:
pass
else:
from django.test import TestCase
from optimize_later import apps
class DjangoCallbackTest(TestCase):
def make_module_path(self, function):
name = 'id_%s' % (uuid.uuid4().hex,)
module = imp.new_module(name)
module.function = function
sys.modules[name] = module
return '%s.function' % (name,)
def test_no_callbacks(self):
apps.initialize_django_callbacks()
self.assertEqual(apps.django_callbacks, [])
def test_callbacks(self):
reports = []
with self.settings(OPTIMIZE_LATER_CALLBACKS=[
self.make_module_path(reports.append),
]):
apps.initialize_django_callbacks()
with optimize_later('test'):
pass
self.assertEqual(len(reports), 1)
self.assertIsInstance(reports[0], OptimizeReport)