django-testscenarios-0.10/0000755000175000017500000000000013215723413015461 5ustar neilneil00000000000000django-testscenarios-0.10/setup.cfg0000644000175000017500000000015213215723413017300 0ustar neilneil00000000000000[upload_docs] upload-dir = build/sphinx/html [upload] sign = True [egg_info] tag_build = tag_date = 0 django-testscenarios-0.10/.gitreview0000644000175000017500000000011513215716540017467 0ustar neilneil00000000000000[gerrit] host=review.linaro.org port=29418 project=lava/django-testscenarios django-testscenarios-0.10/Makefile0000644000175000017500000000073712226044732017131 0ustar neilneil00000000000000coverage_report coverage_report_log.txt: $(shell find -name '*.py') rm -rf coverage_report mkdir -p coverage_report PYTHONPATH=. COVERAGE_REPORT_HTML_OUTPUT_DIR="$(CURDIR)/coverage_report" python test_project/manage.py test_coverage django_testscenarios -v0 2>&1 | tee coverage_report_log.txt on_commit:: coverage_report rsync -a coverage_report zero-kelvin.pl:/var/www/www.zero-kelvin.pl/django_testscenarios/ clean: rm -rf coverage_report rm -f coverage_report_log.txt django-testscenarios-0.10/PKG-INFO0000644000175000017500000000143513215723413016561 0ustar neilneil00000000000000Metadata-Version: 1.1 Name: django-testscenarios Version: 0.10 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-Content-Type: UNKNOWN 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: Programming Language :: Python :: 3.5 Classifier: Topic :: Software Development :: Testing django-testscenarios-0.10/COPYING0000644000175000017500000001672712226044732016532 0ustar neilneil00000000000000 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.10/setup.py0000755000175000017500000000353313215716553017211 0ustar neilneil00000000000000#!/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.10", 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", "Programming Language :: Python :: 3.5", "Topic :: Software Development :: Testing", ], zip_safe=True, packages=[ 'django_testscenarios', ], install_requires=[ 'django >= 1.7', 'django-testproject >= 0.1.3', 'testtools >= 0.9.2', 'testscenarios >= 0.1', ], ) django-testscenarios-0.10/django_testscenarios.egg-info/0000755000175000017500000000000013215723413023363 5ustar neilneil00000000000000django-testscenarios-0.10/django_testscenarios.egg-info/PKG-INFO0000644000175000017500000000143513215723413024463 0ustar neilneil00000000000000Metadata-Version: 1.1 Name: django-testscenarios Version: 0.10 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-Content-Type: UNKNOWN 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: Programming Language :: Python :: 3.5 Classifier: Topic :: Software Development :: Testing django-testscenarios-0.10/django_testscenarios.egg-info/SOURCES.txt0000644000175000017500000000140313215723413025245 0ustar neilneil00000000000000.gitignore .gitreview COPYING MANIFEST.in Makefile 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.pycdjango-testscenarios-0.10/django_testscenarios.egg-info/top_level.txt0000644000175000017500000000002513215723413026112 0ustar neilneil00000000000000django_testscenarios django-testscenarios-0.10/django_testscenarios.egg-info/dependency_links.txt0000644000175000017500000000000113215723413027431 0ustar neilneil00000000000000 django-testscenarios-0.10/django_testscenarios.egg-info/zip-safe0000644000175000017500000000000112327527512025020 0ustar neilneil00000000000000 django-testscenarios-0.10/django_testscenarios.egg-info/requires.txt0000644000175000017500000000011213215723413025755 0ustar neilneil00000000000000django-testproject>=0.1.3 django>=1.7 testscenarios>=0.1 testtools>=0.9.2 django-testscenarios-0.10/MANIFEST.in0000644000175000017500000000016313215716540017222 0ustar neilneil00000000000000include COPYING include django_testscenarios/test_project/* recursive-include django_testproject/templates *.html django-testscenarios-0.10/.gitignore0000644000175000017500000000011213215716540017446 0ustar neilneil00000000000000*.egg *.egg-info *.pyc build coverage_report coverage_report_log.txt dist django-testscenarios-0.10/django_testscenarios/0000755000175000017500000000000013215723413021671 5ustar neilneil00000000000000django-testscenarios-0.10/django_testscenarios/test_project/0000755000175000017500000000000013215723413024376 5ustar neilneil00000000000000django-testscenarios-0.10/django_testscenarios/test_project/manage.py0000755000175000017500000000244113215716540026207 0ustar neilneil00000000000000#!/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.10/django_testscenarios/test_project/settings.pyc0000644000175000017500000000062412327533173026762 0ustar neilneil00000000000000ó ;fUSc@s6ddlmZeƒjeddddgƒƒdS(iÿÿÿÿ(t gen_settingst SECRET_KEYtneeded_but_not_securedtINSTALLED_APPStdjango_testscenariosN(tdjango_testproject.settingsRtlocalstupdate(((sW/home/neil/code/lava/django-testscenarios/django_testscenarios/test_project/settings.pyts django-testscenarios-0.10/django_testscenarios/test_project/__init__.pyc0000644000175000017500000000025612327533173026662 0ustar neilneil00000000000000ó ÚIXRc@sdS(N((((sW/home/neil/code/lava/django-testscenarios/django_testscenarios/test_project/__init__.pytsdjango-testscenarios-0.10/django_testscenarios/test_project/__init__.py0000644000175000017500000000136512226044732026515 0ustar neilneil00000000000000# 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.10/django_testscenarios/test_project/tests.pyc0000644000175000017500000000103512327533173026261 0ustar neilneil00000000000000ó ÚIXRc@s3ddlmZd„Zedkr/eƒndS(iÿÿÿÿ(t run_tests_forcCs tdƒS(Ns*django_testscenarios.test_project.settings(R(((sT/home/neil/code/lava/django-testscenarios/django_testscenarios/test_project/tests.pyt run_testsst__main__N(tdjango_testproject.testsRRt__name__(((sT/home/neil/code/lava/django-testscenarios/django_testscenarios/test_project/tests.pyts  django-testscenarios-0.10/django_testscenarios/test_project/settings.py0000644000175000017500000000165713215716540026624 0ustar neilneil00000000000000# 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.10/django_testscenarios/test_project/tests.py0000644000175000017500000000166112226044732026117 0ustar neilneil00000000000000# 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.10/django_testscenarios/__init__.py0000644000175000017500000000000013215716540023773 0ustar neilneil00000000000000django-testscenarios-0.10/django_testscenarios/models.py0000644000175000017500000000136512226044732023534 0ustar neilneil00000000000000# 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.10/django_testscenarios/ubertest.py0000644000175000017500000001704713215716540024114 0ustar neilneil00000000000000# 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 __hash__(self): return hash(repr(self)) 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.10/django_testscenarios/tests.py0000644000175000017500000002336713215716540023423 0ustar neilneil00000000000000# 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 """ def __hash__(self): return hash(repr(self)) 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) """ def __hash__(self): return hash(repr(self)) 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 """ def __hash__(self): return hash(repr(self)) # Transaction tests class TransactionsWorkWithPlainDjangoTestCase(DjangoTransactionTestCase, PlainDatabaseChecks, TransactionChecks): """ Test class that is using: * database transactions * django test case (with transaction support) """ def __hash__(self): return hash(repr(self)) class TransactionsWorkWithTestToolsTestCase(TransactionTestCase, PlainDatabaseChecks, TransactionChecks): """ Test class that is using: * database transactions * test tools test case (with transaction support) """ def __hash__(self): return hash(repr(self)) 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) """ def __hash__(self): return hash(repr(self)) 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 """ def __hash__(self): return hash(repr(self)) 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 __hash__(self): return hash(repr(self)) def runTest(self, result): pass class PlainScenarios(TestCaseWithScenarios): def __hash__(self): return hash(repr(self)) def runTest(self, result): pass class Transaction(TransactionTestCase): """" Empty test class inheriting from TransactionTestCase """ def __hash__(self): return hash(repr(self)) def runTest(self, result): pass class TransactionScenarios(TransactionTestCaseWithScenarios): """" Empty test class inheriting from TransactionTestCase """ def __hash__(self): return hash(repr(self)) 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(list(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])