def _remove_file_from_exist(self, file): db = ExistDB() fname = path.split(file)[-1] exist_path= path.join(settings.EXISTDB_ROOT_COLLECTION, fname) # tests could remove fixtures, so an exception here is not a problem try: db.removeDocument(exist_path) except ExistDBException, e: # any way to determine if error ever needs to be reported? pass
def _fixture_teardown(self): if hasattr(self, 'exist_fixtures'): db = ExistDB() if 'index' in self.exist_fixtures: db.removeCollectionIndex(settings.EXISTDB_ROOT_COLLECTION) if 'directory' in self.exist_fixtures: for file in glob(path.join(self.exist_fixtures['directory'], '*.xml')): self._remove_file_from_exist(file) if 'files' in self.exist_fixtures: for file in self.exist_fixtures['files']: self._remove_file_from_exist(file) return super(TestCase, self)._fixture_teardown()
def _use_test_collection(sender, **kwargs): global _stored_default_collection _stored_default_collection = getattr(settings, "EXISTDB_ROOT_COLLECTION", None) if getattr(settings, "EXISTDB_TEST_COLLECTION", None): settings.EXISTDB_ROOT_COLLECTION = settings.EXISTDB_TEST_COLLECTION else: settings.EXISTDB_ROOT_COLLECTION = getattr(settings, "EXISTDB_ROOT_COLLECTION", "/default") + "_test" print "Creating eXist Test Collection: %s" % settings.EXISTDB_ROOT_COLLECTION # now that existdb root collection has been set to test collection, init db connection db = ExistDB() # create test collection (don't complain if collection already exists) db.createCollection(settings.EXISTDB_ROOT_COLLECTION, True)
def _fixture_setup(self): if hasattr(self, 'exist_fixtures'): db = ExistDB() # load index if 'index' in self.exist_fixtures: db.loadCollectionIndex(settings.EXISTDB_ROOT_COLLECTION, open(self.exist_fixtures['index'])) if 'directory' in self.exist_fixtures: for file in glob(path.join(self.exist_fixtures['directory'], '*.xml')): self._load_file_to_exist(file) if 'files' in self.exist_fixtures: for file in self.exist_fixtures['files']: self._load_file_to_exist(file) return super(TestCase, self)._fixture_setup()
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 _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
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)
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())
def _load_file_to_exist(self, file): db = ExistDB() fname = path.split(file)[-1] exist_path= path.join(settings.EXISTDB_ROOT_COLLECTION, fname) db.load(open(file), exist_path, True)