def _restore_root_collection(sender, **kwargs): global _stored_default_collection # if use_test_collection didn't run, don't change anything if _stored_default_collection is None: print "eXist test-start handler does not appear to have run; not restoring eXist Root Collection" print "Is 'eulcore.django.existdb' in your installed apps?" else: print "Removing eXist Test Collection: %s" % settings.EXISTDB_ROOT_COLLECTION # before restoring existdb non-test root collection, init db connection db = ExistDB() try: # remove test collection db.removeCollection(settings.EXISTDB_ROOT_COLLECTION) except ExistDBException, e: print "Error removing collection ", settings.EXISTDB_ROOT_COLLECTION, ': ', e print "Restoring eXist Root Collection: %s" % _stored_default_collection settings.EXISTDB_ROOT_COLLECTION = _stored_default_collection
class ModelTest(unittest.TestCase): COLLECTION = settings.EXISTDB_TEST_COLLECTION def setUp(self): self.db = ExistDB() self.db.createCollection(self.COLLECTION, True) module_path = os.path.split(__file__)[0] fixture = os.path.join(module_path, 'exist_fixtures', 'goodbye-english.xml') self.db.load(open(fixture), self.COLLECTION + '/goodbye-english.xml', True) fixture = os.path.join(module_path, 'exist_fixtures', 'goodbye-french.xml') self.db.load(open(fixture), self.COLLECTION + '/goodbye-french.xml', True) def tearDown(self): self.db.removeCollection(self.COLLECTION) def test_manager(self): partings = Parting.objects.all() self.assertEquals(2, partings.count())
class ExistDBTest(unittest.TestCase): COLLECTION = settings.EXISTDB_TEST_COLLECTION def setUp(self): self.db = ExistDB() self.db.createCollection(self.COLLECTION, True) # rudimentary example of loading exist fixture from a file module_path = os.path.split(__file__)[0] fixture = os.path.join(module_path, 'exist_fixtures', 'hello.xml') self.db.load(open(fixture), self.COLLECTION + '/hello.xml', True) # save exist configurations modified by some tests self._EXISTDB_SERVER_URL = getattr(settings, 'EXISTDB_SERVER_URL', None) self._EXISTDB_SERVER_USER = getattr(settings, 'EXISTDB_SERVER_USER', None) self._EXISTDB_SERVER_PASSWORD = getattr(settings, 'EXISTDB_SERVER_PASSWORD', None) def tearDown(self): self.db.removeCollection(self.COLLECTION) # restore exist settings setattr(settings, 'EXISTDB_SERVER_URL', self._EXISTDB_SERVER_URL) setattr(settings, 'EXISTDB_SERVER_USER', self._EXISTDB_SERVER_USER) setattr(settings, 'EXISTDB_SERVER_PASSWORD', self._EXISTDB_SERVER_PASSWORD) def test_init(self): self.assert_(isinstance(self.db, nondjangoexistdb.db.ExistDB)) self.assert_(isinstance(self.db, ExistDB)) def test_getDocument(self): """Retrieve document loaded via file fixture""" xml = self.db.getDocument(self.COLLECTION + "/hello.xml") self.assertEquals(xml, "<hello>World</hello>") def test_failed_authentication_from_settings(self): """Check that initializing ExistDB with invalid django settings raises exception""" try: #passwords can be specified in localsettings.py # overwrite (and then restore) to ensure that authentication fails server_url = settings.EXISTDB_SERVER_URL parts = urlsplit(settings.EXISTDB_SERVER_URL) netloc = 'bad_user:bad_password@' + parts.hostname if parts.port: netloc += ':' + str(parts.port) bad_uri = urlunsplit((parts.scheme, netloc, parts.path, parts.query, parts.fragment)) settings.EXISTDB_SERVER_URL = bad_uri test_db = ExistDB() self.assertRaises(nondjangoexistdb.db.ExistDBException, test_db.hasCollection, self.COLLECTION) finally: settings.EXISTDB_SERVER_URL = server_url def test_get_exist_url(self): # test constructing url based on multiple possible configurations user = settings.EXISTDB_SERVER_USER pwd = settings.EXISTDB_SERVER_PASSWORD scheme, sep, host = settings.EXISTDB_SERVER_URL.partition('//') # with username & password self.assertEqual(scheme + sep + user + ':' + pwd + '@' + host, self.db._get_exist_url()) # username but no password delattr(settings, 'EXISTDB_SERVER_PASSWORD') self.assertEqual(scheme + sep + user + '@' + host, self.db._get_exist_url()) # no credentials delattr(settings, 'EXISTDB_SERVER_USER') self.assertEqual(settings.EXISTDB_SERVER_URL, self.db._get_exist_url())