Skip to content

Commit

Permalink
Import refactoring to make python 2.6 happy
Browse files Browse the repository at this point in the history
  • Loading branch information
yakky committed Dec 7, 2014
1 parent c219228 commit 79ef348
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 70 deletions.
13 changes: 7 additions & 6 deletions djangocms_helper/base_test.py
@@ -1,14 +1,10 @@
# -*- coding: utf-8 -*-
from cms.utils import get_language_list
from cms.utils.compat.dj import get_user_model
from django.contrib.sites.models import Site
from django.http import SimpleCookie
from django.test import TestCase, RequestFactory
from django.utils.six import StringIO
from djangocms_helper.utils import create_user

User = get_user_model()


class BaseTestCase(TestCase):
"""
Expand Down Expand Up @@ -38,14 +34,19 @@ class BaseTestCase(TestCase):

@classmethod
def setUpClass(cls):
from django.contrib.sites.models import Site
cls.request_factory = RequestFactory()
cls.user = create_user('admin', 'admin@admin.com', 'admin', is_staff=True, is_superuser=True)
cls.user_staff = create_user('staff', 'staff@admin.com', 'staff', is_staff=True)
cls.user = create_user('admin', 'admin@admin.com', 'admin',
is_staff=True, is_superuser=True)
cls.user_staff = create_user('staff', 'staff@admin.com', 'staff',
is_staff=True)
cls.user_normal = create_user('normal', 'normal@admin.com', 'normal')
cls.site_1 = Site.objects.get(pk=1)

@classmethod
def tearDownClass(cls):
from cms.utils.compat.dj import get_user_model
User = get_user_model()
User.objects.all().delete()

def get_pages_data(self):
Expand Down
7 changes: 6 additions & 1 deletion djangocms_helper/default_settings.py
Expand Up @@ -51,6 +51,11 @@ def get_default_settings(CMS_APP, CMS_PROCESSORS, CMS_MIDDLEWARE,
('fullwidth.html', 'Fullwidth'),
('page.html', 'Normal page'),
),
'MIGRATION_MODULES': {}
'MIGRATION_MODULES': {},
'CACHES': {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}

}
11 changes: 6 additions & 5 deletions djangocms_helper/main.py
Expand Up @@ -7,14 +7,12 @@
import warnings

from docopt import docopt
from django.core.exceptions import DjangoRuntimeWarning, ImproperlyConfigured
from django.core.management import CommandError
from django.utils.encoding import force_text
from django.utils.importlib import import_module

from . import __version__
from .utils import (work_in, DJANGO_1_6, DJANGO_1_5, temp_dir, _make_settings,
create_user, _create_db, captured_output)
create_user, _create_db)

__doc__ = '''django CMS applications development helper script.
Expand Down Expand Up @@ -144,14 +142,16 @@ def makemigrations(application, merge=False):
"""
Generate migrations (for both south and django 1.7+)
"""
from django.core.exceptions import DjangoRuntimeWarning, ImproperlyConfigured
from django.core.management import call_command
from .utils import captured_output

if DJANGO_1_6:
from south.exceptions import NoMigrations
from south.migration import Migrations

if merge:
raise DjangoRuntimeWarning(u'Option not implemented for Django 1.6')
raise DjangoRuntimeWarning(u'Option not implemented for Django 1.6 and below')
try:
Migrations(application)
except NoMigrations:
Expand All @@ -176,9 +176,10 @@ def squashmigrations(application, migration):
"""
Squash Django 1.7+ migrations
"""
from django.core.exceptions import DjangoRuntimeWarning
from django.core.management import call_command
if DJANGO_1_6:
raise CommandError(u'Command not implemented for Django 1.6')
raise DjangoRuntimeWarning(u'Command not implemented for Django 1.6 and below')
else:
call_command('squashmigrations', application, migration)

Expand Down
95 changes: 52 additions & 43 deletions djangocms_helper/test_utils/example/tests/test_fake.py
@@ -1,44 +1,53 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from djangocms_helper.base_test import BaseTestCase


class FakeTests(BaseTestCase):
_pages_data = (
{'en': {'title': 'Page title', 'template': 'page.html', 'publish': True},
'fr': {'title': 'Titre', 'publish': True},
'it': {'title': 'Titolo pagina', 'publish': False}},
{'en': {'title': 'Second page', 'template': 'page.html', 'publish': True},
'fr': {'title': 'Deuxieme', 'publish': True},
'it': {'title': 'Seconda pagina', 'publish': False}},
)

def test_fake(self):
self.assertTrue(True)

def test_pages(self):
data = self.get_pages_data()
self.assertEqual(set(data[0].keys()), set(('en', 'fr', 'it')))

if 'cms' in settings.INSTALLED_APPS:
pages = self.get_pages()
self.assertEqual(len(pages), 2)

def test_requests(self):
if 'cms' in settings.INSTALLED_APPS:
pages = self.get_pages()

request = self.get_request(pages[1], 'en')
self.assertEqual(request.path, u'/second-page')
self.assertEqual(request.META['REQUEST_METHOD'], 'GET')

request = self.post_request(pages[1], 'en', data={'payload': 1})
self.assertEqual(request.path, u'/second-page')
self.assertEqual(request.META['REQUEST_METHOD'], 'POST')
self.assertEqual(request.POST.get('payload'), u'1')

request = self.get_page_request(pages[1], lang='en',
user=self.user_staff, edit=True,)
self.assertEqual(request.path, u'/en/second-page/')
self.assertTrue(request.toolbar)
self.assertEqual(request.META['REQUEST_METHOD'], 'GET')

try:
from djangocms_helper.base_test import BaseTestCase

class FakeTests(BaseTestCase):
_pages_data = (
{'en': {'title': 'Page title', 'template': 'page.html', 'publish': True},
'fr': {'title': 'Titre', 'publish': True},
'it': {'title': 'Titolo pagina', 'publish': False}},
{'en': {'title': 'Second page', 'template': 'page.html', 'publish': True},
'fr': {'title': 'Deuxieme', 'publish': True},
'it': {'title': 'Seconda pagina', 'publish': False}},
)

def test_fake(self):
self.assertTrue(True)

def test_pages(self):
from django.conf import settings
data = self.get_pages_data()
self.assertEqual(set(data[0].keys()), set(('en', 'fr', 'it')))

if 'cms' in settings.INSTALLED_APPS:
pages = self.get_pages()
self.assertEqual(len(pages), 2)

def test_requests(self):
from django.conf import settings
if 'cms' in settings.INSTALLED_APPS:
pages = self.get_pages()

request = self.get_request(pages[1], 'en')
self.assertEqual(request.path, u'/second-page')
self.assertEqual(request.META['REQUEST_METHOD'], 'GET')

request = self.post_request(pages[1], 'en', data={'payload': 1})
self.assertEqual(request.path, u'/second-page')
self.assertEqual(request.META['REQUEST_METHOD'], 'POST')
self.assertEqual(request.POST.get('payload'), u'1')

request = self.get_page_request(pages[1], lang='en',
user=self.user_staff, edit=True,)
self.assertEqual(request.path, u'/en/second-page/')
self.assertTrue(request.toolbar)
self.assertEqual(request.META['REQUEST_METHOD'], 'GET')
except Exception:
from unittest2 import TestCase

class FakeTests(TestCase):

def test_fake(self):
self.assertTrue(True)
29 changes: 19 additions & 10 deletions djangocms_helper/tests/test_commands.py
Expand Up @@ -2,7 +2,7 @@
from __future__ import print_function, with_statement
from copy import copy
from distutils.version import LooseVersion
from django.core.management import CommandError
from django.utils.encoding import force_text
import os.path
import shutil
import sys
Expand Down Expand Up @@ -48,6 +48,7 @@ class CommandTests(unittest.TestCase):
basedir = None
pofile = None
mofile = None
migration_dir = None

@classmethod
def setUpClass(cls):
Expand All @@ -64,15 +65,16 @@ def setUpClass(cls):
def setUp(self):
try:
os.unlink(self.pofile)
except OSError:
except (OSError, TypeError):
pass
try:
os.unlink(self.mofile)
except OSError:
except (OSError, TypeError):
pass
try:
shutil.rmtree(self.migration_dir)
except OSError:
if self.migration_dir:
shutil.rmtree(self.migration_dir)
except (OSError, TypeError):
pass
try:
del sys.modules['example.migrations']
Expand Down Expand Up @@ -124,33 +126,36 @@ def test_makemigrations(self):
self.assertTrue('Create model ExampleModel' in out.getvalue())

def test_makemigrations_merge(self):
from django.core.exceptions import DjangoRuntimeWarning
with work_in(self.basedir):
with captured_output() as (out, err):
args = copy(DEFAULT_ARGS)
args['makemigrations'] = True
args['--merge'] = True
if DJANGO_1_6:
with self.assertRaises(CommandError) as exit:
with self.assertRaises(DjangoRuntimeWarning) as exit:
core(args, self.application)
self.assertEqual(exit.exception.message, 'Option not implemented for Django 1.6')
self.assertEqual(force_text(exit.exception), 'Option not implemented for Django 1.6 and below')
else:
core(args, self.application)
self.assertTrue('No conflicts detected to merge' in out.getvalue())

def test_squashmigrations(self):
from django.core.exceptions import DjangoRuntimeWarning
from django.core.management import CommandError
with work_in(self.basedir):
with captured_output() as (out, err):
args = copy(DEFAULT_ARGS)
args['squashmigrations'] = True
args['<migration-name>'] = '0001_initial'
if DJANGO_1_6:
with self.assertRaises(CommandError) as exit:
with self.assertRaises(DjangoRuntimeWarning) as exit:
core(args, self.application)
self.assertEqual(exit.exception.message, 'Command not implemented for Django 1.6')
self.assertEqual(force_text(exit.exception), 'Command not implemented for Django 1.6 and below')
else:
with self.assertRaises(CommandError) as exit:
core(args, self.application)
self.assertTrue('squashmigrations on it makes no sense' in exit.exception.message)
self.assertTrue('squashmigrations on it makes no sense' in force_text(exit.exception))

def test_makemessages(self):
with work_in(self.basedir):
Expand Down Expand Up @@ -199,6 +204,8 @@ def test_pyflakes(self):
args['pyflakes'] = True
core(args, self.application)

@unittest.skipIf(sys.version_info < (2, 7),
reason="Example test non discoverable in Python 2.6")
def test_testrun(self):
with work_in(self.basedir):
with captured_output() as (out, err):
Expand All @@ -210,6 +217,8 @@ def test_testrun(self):
self.assertTrue('Ran 3 tests in' in err.getvalue())
self.assertEqual(exit.exception.code, 0)

@unittest.skipIf(sys.version_info < (2, 7),
reason="Example test non discoverable in Python 2.6")
def test_testrun_nocms(self):
with work_in(self.basedir):
with captured_output() as (out, err):
Expand Down
3 changes: 2 additions & 1 deletion djangocms_helper/utils.py
@@ -1,14 +1,15 @@
# -*- coding: utf-8 -*-
import contextlib
from distutils.version import LooseVersion
from django.core.management import call_command
import os
import random
import shutil
import stat
import sys
from tempfile import mkdtemp

import django
from django.core.management import call_command
from django.core.urlresolvers import clear_url_caches
from django.utils.datastructures import SortedDict
from django.utils.functional import empty
Expand Down
4 changes: 0 additions & 4 deletions setup.py
Expand Up @@ -8,10 +8,6 @@
test_suite = 'unittest2.collector'
else:
test_suite = 'djangocms_helper.tests'
try:
import unittest2
except ImportError:
pass

setuptools.setup(
name="djangocms-helper",
Expand Down

0 comments on commit 79ef348

Please sign in to comment.