Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plugins check #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion djangosanetesting/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def clear_db(cache):
from django.db import connection
cursor = connection.cursor()
cursor = connection.cursor() # @UndefinedVariable
cursor.execute('DELETE FROM %s' % cache._table)

def clear_filebased(cache):
Expand Down
97 changes: 47 additions & 50 deletions djangosanetesting/cases.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import re
import sys
import urllib2

from django.template import Context, Template, TemplateSyntaxError
from nose.tools import (
assert_equals,
assert_almost_equals,
assert_not_equals,
assert_raises,
assert_true,
assert_false,
)
from nose import SkipTest

from djangosanetesting.utils import twill_patched_go, twill_xpath_go, extract_django_traceback, get_live_server_path
from djangosanetesting.utils import twill_patched_go, twill_xpath_go, extract_django_traceback

from djangosanetesting import MULTIDB_SUPPORT

try:
from django.db import DEFAULT_DB_ALIAS
MULTIDB_SUPPORT = True
except ImportError:
DEFAULT_DB_ALIAS = 'default'
MULTIDB_SUPPORT = False

__all__ = ("UnitTestCase", "DatabaseTestCase", "DestructiveDatabaseTestCase",
"HttpTestCase", "SeleniumTestCase", "TemplateTagTestCase")


class SaneTestCase(object):
""" Common ancestor we're using our own hierarchy """
start_live_server = False
Expand All @@ -33,58 +21,60 @@ class SaneTestCase(object):
selenium_start = False
no_database_interaction = False
make_translations = True


required_sane_plugins = None

SkipTest = SkipTest

failureException = AssertionError

def __new__(type, *args, **kwargs):
def __new__(cls, *args, **kwargs):
"""
When constructing class, add assert* methods from unittest(2),
both camelCase and pep8-ify style.

"""
obj = super(SaneTestCase, type).__new__(type, *args, **kwargs)
obj = super(SaneTestCase, cls).__new__(cls, *args, **kwargs)

caps = re.compile('([A-Z])')

from django.test import TestCase

##########
### Scraping heavily inspired by nose testing framework, (C) by Jason Pellerin
### and respective authors.
##########

class Dummy(TestCase):
def att():
def test_meth(self):
pass
t = Dummy('att')
dummy_test_case = Dummy('test_meth')

def pepify(name):
return caps.sub(lambda m: '_' + m.groups()[0].lower(), name)
def scrape(t):
for a in [at for at in dir(t) if at.startswith('assert') and not '_' in at]:
v = getattr(t, a)
setattr(obj, a, v)
setattr(obj, pepify(a), v)
scrape(t)

def scrape(dummy_test_case):
for attr in [at for at in dir(dummy_test_case) if at.startswith('assert') and not '_' in at]:
val = getattr(dummy_test_case, attr)
setattr(obj, attr, val)
setattr(obj, pepify(attr), val)

scrape(dummy_test_case)

try:
from unittest2 import TestCase
except ImportError:
pass
else:
class Dummy(TestCase):
def att():
class Dummy(TestCase): # pylint: disable=E0102
def test_meth(self):
pass
t = Dummy('att')
scrape(t)
dummy_test_case = Dummy('test_meth')
scrape(dummy_test_case)

return obj


def _check_plugins(self):
if getattr(self, 'required_sane_plugins', False):
for plugin in self.required_sane_plugins:
Expand All @@ -100,14 +90,14 @@ def setUp(self):
self._check_plugins()
if getattr(self, 'multi_db', False) and not MULTIDB_SUPPORT:
raise self.SkipTest("I need multi db support to run, skipping..")

def is_skipped(self):
if getattr(self, 'multi_db', False) and not MULTIDB_SUPPORT:
return True
try:
self._check_skipped()
self._check_plugins()
except self.SkipTest, e:
except self.SkipTest:
return True
else:
return False
Expand All @@ -118,6 +108,7 @@ def fail(self, *args, **kwargs):
def tearDown(self):
pass


class UnitTestCase(SaneTestCase):
"""
This class is a unittest, i.e. do not interact with database et al
Expand All @@ -126,6 +117,9 @@ class UnitTestCase(SaneTestCase):
no_database_interaction = True
test_type = "unit"

def __init__(self, *args, **kwargs):
super(UnitTestCase, self).__init__(*args, **kwargs)
self._django_client = None

# undocumented client: can be only used for views that are *guaranteed*
# not to interact with models
Expand All @@ -144,7 +138,7 @@ def set_django_client(self, value):
class DatabaseTestCase(SaneTestCase):
"""
Tests using database for models in simple: rollback on teardown and we're out.

However, we must check for fixture difference, if we're using another fixture, we must flush database anyway.
"""
database_single_transaction = True
Expand All @@ -153,15 +147,19 @@ class DatabaseTestCase(SaneTestCase):
django_plugin_started = False
test_type = "database"

def __init__(self, *args, **kwargs):
super(DatabaseTestCase, self).__init__(*args, **kwargs)
self._django_client = None

def get_django_client(self):
from django.test import Client
if not getattr(self, '_django_client', False):
self._django_client = Client()
return self._django_client

def set_django_client(self, value):
self._django_client = value

client = property(fget=get_django_client, fset=set_django_client)


Expand Down Expand Up @@ -221,12 +219,10 @@ def __init__(self, *args, **kwargs):
def get_twill(self):
if not self._twill:
try:
import twill
from twill import get_browser
except ImportError:
raise SkipTest("Twill must be installed if you want to use it")

from twill import get_browser

self._twill = get_browser()
self._twill.go = twill_patched_go(browser=self._twill, original_go=self._twill.go)
self._twill.go_xpath = twill_xpath_go(browser=self._twill, original_go=self._twill.go)
Expand All @@ -241,7 +237,7 @@ def get_twill(self):
def get_spynner(self):
if not self._spynner:
try:
import spynner
import spynner # @UnresolvedImport pylint: disable=F0401
except ImportError:
raise SkipTest("Spynner must be installed if you want to use it")

Expand All @@ -255,7 +251,7 @@ def get_spynner(self):
def assert_code(self, code):
self.assert_equals(int(code), self.twill.get_code())

def urlopen(self, *args, **kwargs):
def urlopen(self, *args, **kwargs): # pylint: disable=R0201
"""
Wrap for the urlopen function from urllib2
prints django's traceback if server responds with 500
Expand All @@ -274,6 +270,7 @@ def tearDown(self):

super(HttpTestCase, self).tearDown()


class SeleniumTestCase(HttpTestCase):
"""
Connect to selenium RC and provide it as instance attribute.
Expand Down
8 changes: 4 additions & 4 deletions djangosanetesting/management/commands/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@


if 'south' in settings.INSTALLED_APPS:
from south.management.commands.test import Command
from south.management.commands.test import Command as OriginalCommand # @UnresolvedImport @UnusedImport pylint: disable=F0401
else:
from django.core.management.commands.test import Command
from django.core.management.commands.test import Command as OriginalCommand # @Reimport

TestRunner = get_runner(settings)

Expand All @@ -21,5 +21,5 @@
extra_options = []


class Command(Command):
option_list = Command.option_list + tuple(extra_options)
class Command(OriginalCommand):
option_list = OriginalCommand.option_list + tuple(extra_options)
Loading