diff options
Diffstat (limited to 'testsuite/common.py')
-rw-r--r-- | testsuite/common.py | 66 |
1 files changed, 42 insertions, 24 deletions
diff --git a/testsuite/common.py b/testsuite/common.py index 4c7337e0d..e0ff3ed19 100644 --- a/testsuite/common.py +++ b/testsuite/common.py @@ -15,7 +15,10 @@ import codecs import lxml.etree import Bcfg2.Options import Bcfg2.Utils -from mock import patch, MagicMock, _patch, DEFAULT +try: + from mock.mock import patch, MagicMock, _patch, DEFAULT +except ImportError: + from mock import patch, MagicMock, _patch, DEFAULT try: from unittest2 import skip, skipIf, skipUnless, TestCase except ImportError: @@ -64,6 +67,7 @@ try: set_setup_default("time_zone") set_setup_default("web_debug", False) set_setup_default("web_prefix") + set_setup_default("django_settings") import Bcfg2.DBSettings Bcfg2.DBSettings.finalize_django_config() @@ -163,7 +167,7 @@ class Bcfg2TestCase(TestCase): sys.stderr = cls._stderr if hasattr(TestCase, "assertCountEqual"): - assertItemsEqual = assertCountEqual + assertItemsEqual = TestCase.assertCountEqual def assertXMLEqual(self, el1, el2, msg=None): """ Test that the two XML trees given are equal. """ @@ -171,21 +175,22 @@ class Bcfg2TestCase(TestCase): msg = "XML trees are not equal: %s" else: msg += ": %s" - fullmsg = msg + "\nFirst: %s" % lxml.etree.tostring(el1) + \ + msg += "\n%s" + fullmsg = "First: %s" % lxml.etree.tostring(el1) + \ "\nSecond: %s" % lxml.etree.tostring(el2) - self.assertEqual(el1.tag, el2.tag, msg=fullmsg % "Tags differ") + self.assertEqual(el1.tag, el2.tag, msg=msg % ("Tags differ", fullmsg)) if el1.text is not None and el2.text is not None: self.assertEqual(el1.text.strip(), el2.text.strip(), - msg=fullmsg % "Text content differs") + msg=msg % ("Text content differs", fullmsg)) else: self.assertEqual(el1.text, el2.text, - msg=fullmsg % "Text content differs") + msg=msg % ("Text content differs", fullmsg)) self.assertItemsEqual(el1.attrib.items(), el2.attrib.items(), - msg=fullmsg % "Attributes differ") + msg=msg % ("Attributes differ", fullmsg)) self.assertEqual(len(el1.getchildren()), len(el2.getchildren()), - msg=fullmsg % "Different numbers of children") + msg=msg % ("Different numbers of children", fullmsg)) matched = [] for child1 in el1.getchildren(): for child2 in el2.xpath(child1.tag): @@ -199,10 +204,10 @@ class Bcfg2TestCase(TestCase): continue else: assert False, \ - fullmsg % ("Element %s is missing from second" % - lxml.etree.tostring(child1)) + msg % ("Element %s is missing from second" % + lxml.etree.tostring(child1), fullmsg) self.assertItemsEqual(el2.getchildren(), matched, - msg=fullmsg % "Second has extra element(s)") + msg=msg % ("Second has extra element(s)", fullmsg)) class DBModelTestCase(Bcfg2TestCase): @@ -214,12 +219,33 @@ class DBModelTestCase(Bcfg2TestCase): def test_syncdb(self): """ Create the test database and sync the schema """ if self.models: + import django import django.core.management - django.core.management.call_command("syncdb", interactive=False, - verbosity=0) - self.assertTrue( - os.path.exists( - django.conf.settings.DATABASES['default']['NAME'])) + from django.core.exceptions import ImproperlyConfigured + + dbfile = django.conf.settings.DATABASES['default']['NAME'] + + # Close all connections to the old database + if django.VERSION[0] == 1 and django.VERSION[1] >= 7: + for connection in django.db.connections.all(): + connection.close() + else: + django.db.close_connection() + + # Remove old database + if os.path.exists(dbfile): + os.unlink(dbfile) + self.assertFalse(os.path.exists(dbfile)) + + # Create new + if django.VERSION[0] == 1 and django.VERSION[1] < 7: + django.core.management.call_command('syncdb', interactive=False, + verbosity=1) + django.core.management.call_command('migrate', interactive=False, + verbosity=1) + + # Check if database exists now + self.assertTrue(os.path.exists(dbfile)) @skipUnless(has_django, "Django not found, skipping") def test_cleandb(self): @@ -230,14 +256,6 @@ class DBModelTestCase(Bcfg2TestCase): self.assertItemsEqual(list(model.objects.all()), []) -def syncdb(modeltest): - """ Given an instance of a :class:`DBModelTestCase` object, sync - and clean the database """ - inst = modeltest(methodName='test_syncdb') - inst.test_syncdb() - inst.test_cleandb() - - # in order for patchIf() to decorate a function in the same way as # patch(), we override the default behavior of __enter__ and __exit__ # on the _patch() object to basically be noops. |