Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
Improve tests that require Django instance by moving settings into ac…
Browse files Browse the repository at this point in the history
…tual settings module
  • Loading branch information
rowanseymour committed Mar 17, 2017
1 parent 87ffdcf commit cdb442d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 40 deletions.
8 changes: 8 additions & 0 deletions hamlpy/test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import unicode_literals

import django
import os


os.environ['DJANGO_SETTINGS_MODULE'] = 'hamlpy.test.settings'
django.setup()
34 changes: 34 additions & 0 deletions hamlpy/test/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import unicode_literals

import django

from distutils.version import StrictVersion

DEBUG = True

DATABASES = {}

INSTALLED_APPS = ('hamlpy',)

TEMPLATE_DIR = 'hamlpy/test/templates'
TEMPLATE_LOADERS = [
'hamlpy.template.loaders.HamlPyFilesystemLoader',
'hamlpy.template.loaders.HamlPyAppDirectoriesLoader',
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader'
]

if StrictVersion(django.get_version()) >= StrictVersion('1.8'):
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'OPTIONS': {'loaders': TEMPLATE_LOADERS, 'debug': True}
}
]
else:
TEMPLATE_DIRS = [TEMPLATE_DIR]
TEMPLATE_LOADERS = TEMPLATE_LOADERS
TEMPLATE_DEBUG = True

SECRET_KEY = 'tots top secret'
45 changes: 12 additions & 33 deletions hamlpy/test/test_loader.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,37 @@
from __future__ import print_function, unicode_literals

import django
import mock
import pytest
import unittest

from distutils.version import StrictVersion
from django.conf import settings
from django.template import TemplateDoesNotExist
from django.template.loader import render_to_string
from django.test import SimpleTestCase
from django.test.utils import override_settings
from six.moves import reload_module

from hamlpy.compiler import Compiler
from hamlpy.template import loaders


TEMPLATE_DIR = 'hamlpy/test/templates'
TEMPLATE_LOADERS = [
'hamlpy.template.loaders.HamlPyFilesystemLoader',
'hamlpy.template.loaders.HamlPyAppDirectoriesLoader',
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader'
]

if StrictVersion(django.get_version()) >= StrictVersion('1.8'):
settings.configure(TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'OPTIONS': {'loaders': TEMPLATE_LOADERS, 'debug': True}
}
])
else:
settings.configure(TEMPLATE_DIRS=[TEMPLATE_DIR], TEMPLATE_LOADERS=TEMPLATE_LOADERS, TEMPLATE_DEBUG=True)

django.setup()

class LoaderTest(SimpleTestCase):
def setUp(self):
super(LoaderTest, self).setUp()

class LoaderTest(unittest.TestCase):
def tearDown(self):
from hamlpy.template import loaders
reload_module(loaders)

@mock.patch('hamlpy.compiler.Compiler', wraps=Compiler)
def test_compiler_settings(self, mock_compiler_class):
@mock.patch('hamlpy.template.loaders.Compiler', wraps=Compiler)
def test_compiler_default_settings(self, mock_compiler_class):
render_to_string('simple.hamlpy')

mock_compiler_class.assert_called_once_with(options={})
mock_compiler_class.reset_mock()

with override_settings(HAMLPY_ATTR_WRAPPER='"', HAMLPY_DJANGO_INLINE_STYLE=False):
from hamlpy.template import loaders
reload_module(loaders)
@override_settings(HAMLPY_ATTR_WRAPPER='"', HAMLPY_DJANGO_INLINE_STYLE=False)
def test_compiler_settings(self):

reload_module(loaders)

with mock.patch('hamlpy.template.loaders.Compiler', wraps=Compiler) as mock_compiler_class:
rendered = render_to_string('simple.hamlpy')

mock_compiler_class.assert_called_once_with(options={
Expand Down
9 changes: 2 additions & 7 deletions hamlpy/test/test_templatize.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
from __future__ import unicode_literals

import unittest
from django.test import SimpleTestCase

from hamlpy import Config


class TemplatizeTest(unittest.TestCase):
class TemplatizeTest(SimpleTestCase):

def test_templatize(self):
# patching of Django's templatize happens when our app is loaded
Config.ready(None)

from django.utils.translation import templatize

# test regular Django tags
Expand Down

0 comments on commit cdb442d

Please sign in to comment.