Ejemplo n.º 1
0
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())