django-testscenarios-0.8/ 0000755 0001750 0001750 00000000000 12625563020 015410 5 ustar neil neil 0000000 0000000 django-testscenarios-0.8/setup.cfg 0000644 0001750 0001750 00000000177 12625563020 017236 0 ustar neil neil 0000000 0000000 [upload_docs]
upload-dir = build/sphinx/html
[upload]
sign = True
[egg_info]
tag_build =
tag_date = 0
tag_svn_revision = 0
django-testscenarios-0.8/PKG-INFO 0000644 0001750 0001750 00000001310 12625563020 016500 0 ustar neil neil 0000000 0000000 Metadata-Version: 1.1
Name: django-testscenarios
Version: 0.8
Summary: Test scenarios for Django
Home-page: https://git.linaro.org/lava/django-testscenarios.git
Author: Linaro Limited
Author-email: lava-team@linaro.org
License: LGPLv3
Description: UNKNOWN
Keywords: django,testing,scenarios
Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 2.7
Classifier: Topic :: Software Development :: Testing
django-testscenarios-0.8/COPYING 0000644 0001750 0001750 00000016727 12226044732 016461 0 ustar neil neil 0000000 0000000 GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
django-testscenarios-0.8/setup.py 0000755 0001750 0001750 00000003451 12622326260 017130 0 ustar neil neil 0000000 0000000 #!/usr/bin/env python
# Copyright (C) 2010 Linaro Limited
#
# Author: Zygmunt Krynicki
#
# This file is part of django-testscenarios.
#
# django-testscenarios is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# django-testscenarios is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with django-testscenarios. If not, see .
from setuptools import setup
setup(
name='django-testscenarios',
version="0.8",
author="Linaro Limited",
author_email="lava-team@linaro.org",
description="Test scenarios for Django",
url='https://git.linaro.org/lava/django-testscenarios.git',
test_suite='django_testscenarios.test_project.tests.run_tests',
license='LGPLv3',
keywords=['django', 'testing', 'scenarios'],
classifiers=[
"Development Status :: 4 - Beta",
'Environment :: Web Environment',
'Framework :: Django',
'Intended Audience :: Developers',
"License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)",
"Operating System :: OS Independent",
"Programming Language :: Python :: 2.7",
"Topic :: Software Development :: Testing",
],
zip_safe=True,
packages=[
'django_testscenarios',
],
install_requires=[
'django >= 1.7',
'django-testproject >= 0.1.1',
'testtools >= 0.9.2',
'testscenarios >= 0.1',
],
)
django-testscenarios-0.8/django_testscenarios.egg-info/ 0000755 0001750 0001750 00000000000 12625563020 023312 5 ustar neil neil 0000000 0000000 django-testscenarios-0.8/django_testscenarios.egg-info/PKG-INFO 0000644 0001750 0001750 00000001310 12625563020 024402 0 ustar neil neil 0000000 0000000 Metadata-Version: 1.1
Name: django-testscenarios
Version: 0.8
Summary: Test scenarios for Django
Home-page: https://git.linaro.org/lava/django-testscenarios.git
Author: Linaro Limited
Author-email: lava-team@linaro.org
License: LGPLv3
Description: UNKNOWN
Keywords: django,testing,scenarios
Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 2.7
Classifier: Topic :: Software Development :: Testing
django-testscenarios-0.8/django_testscenarios.egg-info/SOURCES.txt 0000644 0001750 0001750 00000001344 12625563020 025200 0 ustar neil neil 0000000 0000000 COPYING
MANIFEST.in
setup.cfg
setup.py
django_testscenarios/__init__.py
django_testscenarios/models.py
django_testscenarios/tests.py
django_testscenarios/ubertest.py
django_testscenarios.egg-info/PKG-INFO
django_testscenarios.egg-info/SOURCES.txt
django_testscenarios.egg-info/dependency_links.txt
django_testscenarios.egg-info/requires.txt
django_testscenarios.egg-info/top_level.txt
django_testscenarios.egg-info/zip-safe
django_testscenarios/test_project/__init__.py
django_testscenarios/test_project/__init__.pyc
django_testscenarios/test_project/manage.py
django_testscenarios/test_project/settings.py
django_testscenarios/test_project/settings.pyc
django_testscenarios/test_project/tests.py
django_testscenarios/test_project/tests.pyc django-testscenarios-0.8/django_testscenarios.egg-info/top_level.txt 0000644 0001750 0001750 00000000025 12625563020 026041 0 ustar neil neil 0000000 0000000 django_testscenarios
django-testscenarios-0.8/django_testscenarios.egg-info/dependency_links.txt 0000644 0001750 0001750 00000000001 12625563020 027360 0 ustar neil neil 0000000 0000000
django-testscenarios-0.8/django_testscenarios.egg-info/zip-safe 0000644 0001750 0001750 00000000001 12327527512 024747 0 ustar neil neil 0000000 0000000
django-testscenarios-0.8/django_testscenarios.egg-info/requires.txt 0000644 0001750 0001750 00000000122 12625563020 025705 0 ustar neil neil 0000000 0000000 django >= 1.7
django-testproject >= 0.1.1
testtools >= 0.9.2
testscenarios >= 0.1
django-testscenarios-0.8/MANIFEST.in 0000644 0001750 0001750 00000000163 12325263073 017150 0 ustar neil neil 0000000 0000000 include COPYING
include django_testscenarios/test_project/*
recursive-include django_testproject/templates *.html
django-testscenarios-0.8/django_testscenarios/ 0000755 0001750 0001750 00000000000 12625563020 021620 5 ustar neil neil 0000000 0000000 django-testscenarios-0.8/django_testscenarios/test_project/ 0000755 0001750 0001750 00000000000 12625563020 024325 5 ustar neil neil 0000000 0000000 django-testscenarios-0.8/django_testscenarios/test_project/manage.py 0000755 0001750 0001750 00000002441 12325263073 026135 0 ustar neil neil 0000000 0000000 #!/usr/bin/env python
# Copyright (C) 2010, 2011 Linaro Limited
#
# Author: Zygmunt Krynicki
#
# This file is part of django-testscenarios.
#
# django-testscenarios is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# django-testscenarios is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with django-testscenarios. If not, see .
import os
import sys
def find_sources():
base_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "../..")
if os.path.exists(os.path.join(base_path, "django_testscenarios")):
sys.path.insert(0, base_path)
if __name__ == "__main__":
find_sources()
settings_module = "django_testscenarios.test_project.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings_module)
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
django-testscenarios-0.8/django_testscenarios/test_project/settings.pyc 0000644 0001750 0001750 00000000624 12327533173 026711 0 ustar neil neil 0000000 0000000 ó
;fUSc @ s6 d d l m Z e ƒ j e d d d d g ƒ ƒ d S( iÿÿÿÿ( t gen_settingst
SECRET_KEYt needed_but_not_securedt INSTALLED_APPSt django_testscenariosN( t django_testproject.settingsR t localst update( ( ( sW /home/neil/code/lava/django-testscenarios/django_testscenarios/test_project/settings.pyt s django-testscenarios-0.8/django_testscenarios/test_project/__init__.pyc 0000644 0001750 0001750 00000000256 12327533173 026611 0 ustar neil neil 0000000 0000000 ó
ÚIXRc @ s d S( N( ( ( ( sW /home/neil/code/lava/django-testscenarios/django_testscenarios/test_project/__init__.pyt s django-testscenarios-0.8/django_testscenarios/test_project/__init__.py 0000644 0001750 0001750 00000001365 12226044732 026444 0 ustar neil neil 0000000 0000000 # Copyright (C) 2010, 2011 Linaro Limited
#
# Author: Zygmunt Krynicki
#
# This file is part of django-testscenarios.
#
# django-testscenarios is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# django-testscenarios is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with django-testscenarios. If not, see .
django-testscenarios-0.8/django_testscenarios/test_project/tests.pyc 0000644 0001750 0001750 00000001035 12327533173 026210 0 ustar neil neil 0000000 0000000 ó
ÚIXRc @ s3 d d l m Z d „ Z e d k r/ e ƒ n d S( iÿÿÿÿ( t
run_tests_forc C s
t d ƒ S( Ns* django_testscenarios.test_project.settings( R ( ( ( sT /home/neil/code/lava/django-testscenarios/django_testscenarios/test_project/tests.pyt run_tests s t __main__N( t django_testproject.testsR R t __name__( ( ( sT /home/neil/code/lava/django-testscenarios/django_testscenarios/test_project/tests.pyt s django-testscenarios-0.8/django_testscenarios/test_project/settings.py 0000644 0001750 0001750 00000001657 12325263073 026552 0 ustar neil neil 0000000 0000000 # Copyright (C) 2010, 2011 Linaro Limited
#
# Author: Zygmunt Krynicki
#
# This file is part of django-testscenarios.
#
# django-testscenarios is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# django-testscenarios is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with django-testscenarios. If not, see .
from django_testproject.settings import gen_settings
locals().update(
gen_settings(
SECRET_KEY='needed_but_not_secured',
INSTALLED_APPS=['django_testscenarios']))
django-testscenarios-0.8/django_testscenarios/test_project/tests.py 0000644 0001750 0001750 00000001661 12226044732 026046 0 ustar neil neil 0000000 0000000 # Copyright (C) 2010, 2011 Linaro Limited
#
# Author: Zygmunt Krynicki
#
# This file is part of django-testscenarios.
#
# django-testscenarios is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# django-testscenarios is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with django-testscenarios. If not, see .
from django_testproject.tests import run_tests_for
def run_tests():
return run_tests_for("django_testscenarios.test_project.settings")
if __name__ == '__main__':
run_tests()
django-testscenarios-0.8/django_testscenarios/__init__.py 0000644 0001750 0001750 00000000000 12327531401 023714 0 ustar neil neil 0000000 0000000 django-testscenarios-0.8/django_testscenarios/models.py 0000644 0001750 0001750 00000001365 12226044732 023463 0 ustar neil neil 0000000 0000000 # Copyright (C) 2010, 2011 Linaro Limited
#
# Author: Zygmunt Krynicki
#
# This file is part of django-testscenarios.
#
# django-testscenarios is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# django-testscenarios is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with django-testscenarios. If not, see .
django-testscenarios-0.8/django_testscenarios/ubertest.py 0000644 0001750 0001750 00000016756 12327532742 024054 0 ustar neil neil 0000000 0000000 # Copyright (C) 2010, 2011 Linaro Limited
#
# Author: Zygmunt Krynicki
#
# This file is part of django-testscenarios.
#
# django-testscenarios is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# django-testscenarios is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with django-testscenarios. If not, see .
"""
An uber-test class for Django that mixes:
* testtools.TestCase
* testscenarios.TestWithScenarios
with django.test.TestCase or TransactionTestCase
This module contains two base classes for django aware unit testing with
scenario support. They change the way __call__ and run() are implemented
to let each test case generated for a scenario participate in django
database setup mechanics.
"""
import django.test
import testscenarios
import testtools
import unittest
class ScenarioAwareTestCaseIdStrReprMixIn(object):
"""
Helper mix-in that makes the id(), str() and repr() methods sensible
again. It uses code from unittest.TestCase as starting point and
adds the scenario name where appropriate.
It's needed to undo the changes that testtools and testscenarios
make which somewhat collide with django test runner. The code has
been tuned to make output match normal django test code.
"""
_testScenarioName = None
def id(self):
value = unittest.TestCase.id(self)
if self._testScenarioName is not None:
value += ' (%s)' % self._testScenarioName
return value
def __str__(self):
value = unittest.TestCase.__str__(self)
if self._testScenarioName is not None:
value += ' (%s)' % self._testScenarioName
return value
def __repr__(self):
if self._testScenarioName is not None:
return unittest.TestCase.__repr__(self)
else:
return "<%s.%s testMethod=%s testScenario=%s>" % (
self.__class__.__module__,
self.__class__.__name__,
self._testMethodName,
self._testScenarioName)
def shortDescription(self):
# XXX this makes setup.py test and test_project/manage.py test
# output consistent but I have not managed to understand why
# it's needed. Perhaps something along the way sets
# shortDescription() in the non-pure-unittest code path and then
# it gets used by TextTestRunner's getDescription() method.
return str(self)
class TransactionTestCase(ScenarioAwareTestCaseIdStrReprMixIn,
testtools.TestCase,
django.test.TransactionTestCase):
"""
Django TransactionTestCase with testtools power.
"""
class TestCase(django.test.TestCase,
TransactionTestCase):
# XXX: Inheritance order is very important!
# See comment next to TestCaseWithScenarios
"""
Django TestCase with testtools power.
"""
class TransactionTestCaseWithScenarios(ScenarioAwareTestCaseIdStrReprMixIn,
testtools.TestCase,
testscenarios.TestWithScenarios,
django.test.TransactionTestCase):
"""
Django TransactionTestCase with testtools power and scenario support.
"""
def __call__(self, result=None):
"""
Wrapper around default __call__ method to perform common Django test
set up. This means that user-defined Test Cases aren't required to
include a call to super().setUp().
This wrapper is made scenario-aware
"""
scenarios = self._get_scenarios()
if scenarios:
# Note, we call our implementation of run() to create
# scenarios and give each a chance to initialize.
self.run(result)
else:
# Without scenarios we just call the django __call__ version
# to let it initialize the test database
return django.test.TransactionTestCase.__call__(self, result)
def run(self, result=None):
"""
Run test case generating additional scenarios if needed
"""
scenarios = self._get_scenarios()
if scenarios:
# We can get the scenario name like that because
# generate_scenarios iterates the list of scenarios in order
for scenario_info, test in zip(scenarios,
testscenarios.scenarios.generate_scenarios(self)):
# Monkey-patch id() back into the cloned test class This
# will use our id (which resembles stock TestCase.id()
# but it will hold extra information about test scenario
# that was applied. This is necessary because
# generate_scenarios() removes any information about the
# actual scenario and hard-codes the id() function to
# return something that we cannot control.
test._testScenarioName = scenario_info[0]
test.id = lambda: self.__class__.id(test)
# Note, we call __call__() on the test case instance to
# give Django.test.TestCase.__call__() a chance to run.
# The code there will actually setup all the database
# mechanics. If we would simply call run here it'd loop
# back to our implementation instead which would go to
# the else clause and call
# Django.test.TransationTestCase.run(). The problem with
# this code is that it depends on __call__ being called
# earlier which normally happens when the test framework
# calls into the test (the very first call to a
# unittest.TestCase subclass is __call__, not run().
test.__call__(result)
return
else:
return super(TransactionTestCaseWithScenarios, self).run(result)
class TestCaseWithScenarios(django.test.TestCase,
TransactionTestCaseWithScenarios):
# XXX: As in Django we mimic important implementation detail.
# Inheritance order is important for non-obvious reasons.
# Django pays attention to base test class (TestCase vs
# TransactionTestCase) and _sorts_ tests inside suites to always run
# _all_ non-transaction tests before first transaction test. This is
# necessary because of the optimisation in implementation of
# database cleanup code. Any other ordering will allow each
# non-transaction test occurring immediately after a transaction
# test to observe past state. This is caused by Django _not_ doing
# cleanup _after_ a transaction test case. This works correctly
# since (after sorting test cases) subsequent transaction test case
# startup code will purge the database before running user code.
#
# Hence, our implementation makes the TransactionTestCase the base
# class and the plain TestCase derived class.
"""
Django TestCase with testtools power and scenario support.
"""
__all__ = [
"TestCase",
"TestCaseWithScenarios",
"TransactionTestCase",
"TransactionTestCaseWithScenarios"]
django-testscenarios-0.8/django_testscenarios/tests.py 0000644 0001750 0001750 00000022151 12625550174 023343 0 ustar neil neil 0000000 0000000 # Copyright (C) 2010, 2011, 2015 Linaro Limited
#
# Author: Zygmunt Krynicki
# Neil Williams
#
# This file is part of django-testscenarios.
#
# django-testscenarios is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# django-testscenarios is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with django-testscenarios. If not, see .
import unittest
import django
from django.db import models, transaction
from django.test import (
TestCase as DjangoTestCase,
TransactionTestCase as DjangoTransactionTestCase)
from django_testscenarios.ubertest import (
TestCase,
TestCaseWithScenarios,
TransactionTestCase,
TransactionTestCaseWithScenarios)
if hasattr(django, 'setup'):
django.setup()
class TestModel(models.Model):
"""
Model for testing database configuration/initialization
"""
field = models.CharField(max_length=10, null=True)
broken = models.IntegerField(default=1)
class ScenarioParametersAreVisibleChecks(object):
scenarios = [
('scenario_a', {'attr': 'foo'}),
('scenario_b', {'attr': 'bar'}),
]
def test_attr_is_set(self):
self.assertNotEqual(getattr(self, 'attr'), None)
class PlainDatabaseChecks(object):
def _do_check_for_database_state(self):
self.assertEqual(TestModel.objects.all().count(), 0)
obj = TestModel.objects.create()
self.assertEqual(TestModel.objects.all().count(), 1)
obj.delete()
self.assertEqual(TestModel.objects.all().count(), 0)
def test_database_is_empty_at_start_of_test_first(self):
self._do_check_for_database_state()
def test_database_is_empty_at_start_of_test_second(self):
self._do_check_for_database_state()
class TransactionChecks(object):
@transaction.atomic
def _create_object(self):
self.obj = TestModel.objects.create(field=None)
self.pk = self.obj.pk
def _reload_object(self):
self.obj = TestModel.objects.get(pk=self.pk)
def test_transaction_handling(self):
with transaction.atomic():
self._create_object()
self._reload_object()
self.assertEqual(self.obj.field, None)
self.obj.field = "something"
self.obj.save()
try:
self.obj.broken = 'not an integer'
except ValueError:
pass
try:
with transaction.atomic():
self.obj.save()
except ValueError:
pass
self._reload_object()
self.assertEqual(self.obj.broken, 1)
self.assertNotEqual(self.obj.field, None)
# Non-transaction tests
class TestsWorkWithPlainDjangoTestCase(DjangoTestCase,
PlainDatabaseChecks):
"""
Test class that is using:
* plain database checks
* django test case
"""
class TestsWorkWithTestToolsTestCase(TestCase,
PlainDatabaseChecks):
"""
Test class that is using:
* plain database checks
* test tools test case
"""
class TestsWorkWithTestScenariosTestCaseAndNoScenarios(TestCaseWithScenarios,
PlainDatabaseChecks):
"""
Test class that is using:
* plain database checks
* test tools test case
* test scenarios test case
* no actual scenarios (short-circuited fast path)
"""
class TestsWorkWithTestScenariosTestCaseAndSomeScenarios(TestCaseWithScenarios,
ScenarioParametersAreVisibleChecks,
PlainDatabaseChecks):
"""
Test class that is using:
* database transactions
* test tools test case
* test scenarios test case
* two dummy scenarios so that multiple test cases get generated
"""
# Transaction tests
class TransactionsWorkWithPlainDjangoTestCase(DjangoTransactionTestCase,
PlainDatabaseChecks,
TransactionChecks):
"""
Test class that is using:
* database transactions
* django test case (with transaction support)
"""
class TransactionsWorkWithTestToolsTestCase(TransactionTestCase,
PlainDatabaseChecks,
TransactionChecks):
"""
Test class that is using:
* database transactions
* test tools test case (with transaction support)
"""
class TransactionsWorkWithTestScenariosTestCaseAndNoScenarios(TransactionTestCaseWithScenarios,
PlainDatabaseChecks,
TransactionChecks):
"""
Test class that is using:
* database transactions
* test tools test case (with transaction support)
* test scenarios test case (with transaction support)
* no actual scenarios (short-circuited fast path)
"""
class TransactionsWorkWithTestScenariosTestCaseAndSomeScenarios(TransactionTestCaseWithScenarios,
ScenarioParametersAreVisibleChecks,
PlainDatabaseChecks,
TransactionChecks):
"""
Test class that is using:
* database transactions
* test tools test case (with transaction support)
* test scenarios test case (with transaction support)
* two dummy scenarios so that multiple test cases get generated
"""
class TestReorderingNotBroken(DjangoTestCase):
"""
Test that test suite reordering done inside DjangoTestSuiteRunner
class (to optimize database setup/tear down code) is not going to
reorder our improved test classes.
"""
class Plain(TestCase):
"""" Empty test class inheriting from TestCase """
def runTest(self, result):
pass
class PlainScenarios(TestCaseWithScenarios):
def runTest(self, result):
pass
class Transaction(TransactionTestCase):
"""" Empty test class inheriting from TransactionTestCase """
def runTest(self, result):
pass
class TransactionScenarios(TransactionTestCaseWithScenarios):
"""" Empty test class inheriting from TransactionTestCase """
def runTest(self, result):
pass
def ensure_proper_order(self, initial_order, proper_order):
"""
Create a TestSuite with test cases in initial_order, reorder
them using the same logic that Django applies and verify that
the order is proper_order
"""
from django.test.runner import reorder_suite
suite = unittest.TestSuite()
for cls in initial_order:
suite.addTest(cls())
reordered_suite = reorder_suite(suite, (DjangoTestCase,))
self.assertEqual(map(type, reordered_suite._tests), proper_order)
def test_transaction_test_case_stays_last(self):
self.ensure_proper_order(
[self.Plain, self.Transaction],
[self.Plain, self.Transaction])
def test_transaction_test_case_is_moved_to_be_last(self):
self.ensure_proper_order(
[self.Transaction, self.Plain],
[self.Plain, self.Transaction])
def test_transaction_scenarios_test_case_stays_last(self):
self.ensure_proper_order(
[self.Plain, self.TransactionScenarios],
[self.Plain, self.TransactionScenarios])
def test_transaction_scenarios_test_case_is_moved_to_be_last(self):
self.ensure_proper_order(
[self.TransactionScenarios, self.Plain],
[self.Plain, self.TransactionScenarios])
def test_plain_test_case_stays_first(self):
self.ensure_proper_order(
[self.Plain, self.Transaction],
[self.Plain, self.Transaction])
def test_plain_test_case_is_moved_to_be_first(self):
self.ensure_proper_order(
[self.Transaction, self.Plain],
[self.Plain, self.Transaction])
def test_plain_test_case_stays_first(self):
self.ensure_proper_order(
[self.Plain, self.Transaction],
[self.Plain, self.Transaction])
def test_plain_scenarios_test_case_is_moved_to_be_first(self):
self.ensure_proper_order(
[self.Transaction, self.PlainScenarios],
[self.PlainScenarios, self.Transaction])
def test_plain_scenarios_test_case_stays_first(self):
self.ensure_proper_order(
[self.PlainScenarios, self.Transaction],
[self.PlainScenarios, self.Transaction])