def test_init_old_format(self): # support init with old-style xmlrpc url optionally including # username and password user = '******' password = '******' url = 'example.com:8080/exist' test_db = db.ExistDB('http://%s:%s@%s/xmlrpc' % (user, password, url)) self.assertEqual(user, test_db.username) self.assertEqual(password, test_db.password) self.assertEqual('http://%s' % url, test_db.exist_url) # specified username/password args shoudl override xmlrpc url override_user = '******' override_password = '******' test_db = db.ExistDB('http://%s:%s@%s/xmlrpc' % (user, password, url), username=override_user, password=override_password) self.assertEqual(override_user, test_db.username) self.assertEqual(override_password, test_db.password) self.assertEqual('http://%s' % url, test_db.exist_url) # no username/password test_db = db.ExistDB('http://%s/xmlrpc' % (url)) self.assertEqual(None, test_db.username) self.assertEqual(None, test_db.password) self.assertEqual('http://%s' % url, test_db.exist_url) # no port, ssl url = 'example.com/exist' test_db = db.ExistDB('https://%s/xmlrpc' % (url)) self.assertEqual('https://%s' % url, test_db.exist_url)
def test_timeout(self): # set timeout low to confirm timeout exception is raised timeoutdb = db.ExistDB(server_url=EXISTDB_SERVER_URL, timeout=1) self.assertRaises(ExistDBTimeout, timeoutdb.query, 'util:wait(10000)') # use mocks to test initialization/timeout parameters with patch('pyexistdb.db.socket') as mocksocket: timeout_server = 'http://nonexistent/exist:8080/timeout' # default timeout timeoutdb = db.ExistDB(server_url=timeout_server) mocksocket.getdefaulttimeout.assert_called_with() mocksocket.reset_mock() # timeout specified with patch('pyexistdb.db.requests') as mockrequests: timeout = 3 timeoutdb = db.ExistDB(server_url=timeout_server, timeout=timeout) mocksocket.getdefaulttimeout.assert_not_called() # session options used for rest api should have timeout set self.assertEqual(timeout, timeoutdb.session_opts['timeout']) # xmlrpc transport should use requested timeout xmlrpc_session = mockrequests.Session.return_value # requires a valid xmlrpc response or unmarshalling fails xmlrpc_session.post.return_value.text = '''<methodResponse> <params><param/></params></methodResponse>''' timeoutdb.server.ping() args, kwargs = xmlrpc_session.post.call_args self.assertEqual(timeout, kwargs['timeout'])
def pruebaDELETE(): conex = db.ExistDB("http://*****:*****@localhost:8080/exist") with open('delete.xquery', 'r') as file: archivo = file.read() conex.executeQuery(archivo)
def pruebaINSERT(): conex = db.ExistDB("http://*****:*****@localhost:8080/exist") with open('insertar.xquery', 'r') as file: archivo = file.read() conex.executeQuery(archivo)
def test_failed_authentication_from_settings(self): """Check that initializing ExistDB with invalid django settings raises exception""" # passwords can be specified in localsettings.py # test bad credentials to ensure that authentication fails with override_settings(EXISTDB_SERVER_URLEXISTDB_SERVER_USER='******', EXISTDB_SERVER_PASSWORD='******'): test_db = db.ExistDB() self.assertRaises(db.ExistDBException, test_db.hasCollection, self.COLLECTION)
def test_failed_authentication(self): """Check that connecting with invalid user credentials raises an exception""" parts = urlsplit(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)) test_db = db.ExistDB(server_url=bad_uri) self.assertRaises(db.ExistDBException, test_db.hasCollection, self.COLLECTION)
def cargarDatos(): conex = db.ExistDB("http://*****:*****@localhost:8080/exist") if conex.hasCollection("coleccion"): print("encontrado") else: conex.createCollection("coleccion") print("creada") with open('actividades_gim.xml', 'r') as file1: archivo1 = file1.read() with open('socios_gim.xml', 'r') as file2: archivo2 = file2.read() with open('uso_gimnasio.xml', 'r') as file3: archivo3 = file3.read() with open('consulta1.xquery', 'r') as file4: consulta = file4.read() with open('consulta2.xquery', 'r') as file5: consulta2 = file5.read() conex.load(archivo1, "/db/coleccion/actividades_gim.xml") conex.load(archivo2, "/db/coleccion/socios_gim.xml") conex.load(archivo3, "/db/coleccion/uso_gimnasio.xml") resultID = conex.executeQuery(consulta) num = conex.getHits(resultID) result = "" for pos in range(0, num): result += conex.retrieve_text(resultID, pos) conex.load("<resultado>" + result + "</resultado>", "/db/coleccion/resultado.xml") resultID = conex.executeQuery(consulta2) num = conex.getHits(resultID) result = "" for pos in range(0, num): result += conex.retrieve_text(resultID, pos) conex.load("<resultado2>" + result + "</resultado2>", "/db/coleccion/resultado2.xml")
def setUp(self): self.db = db.ExistDB(server_url=EXISTDB_SERVER_URL, username=EXISTDB_SERVER_USER, password=EXISTDB_SERVER_PASSWORD) # separate existdb instance with dba credentials self.db_admin = db.ExistDB(server_url=EXISTDB_SERVER_URL, username=EXISTDB_SERVER_ADMIN_USER, password=EXISTDB_SERVER_ADMIN_PASSWORD) self.db.createCollection(self.COLLECTION, True) self.db.load("<hello>World</hello>", self.COLLECTION + "/hello.xml") xml = '<root><element name="one">One</element><element name="two">Two</element><element name="two">Three</element></root>' self.db.load(xml.encode("utf-8"), self.COLLECTION + '/xqry_test.xml') xml = '<root><field name="one">One</field><field name="two">Two</field><field name="three">Three</field><field name="four">Four</field><unicode> ϔ ϕ ϖ Ϛ Ϝ Ϟ Ϡ Ϣ ڡ ڢ ڣ ڤ ༀ </unicode></root>' self.db.load(xml.encode("utf-8"), self.COLLECTION + '/xqry_test2.xml') self.test_groups = [] self.test_users = [] xml = '<root xml:id="A"/>' self.db.load(xml.encode("utf-8"), self.COLLECTION + '/xqry_test3.xml') self.db.load(xml.encode("utf-8"), self.COLLECTION + '/xqry_test4.xml')
def test_django_timeout(self): # use mocks to test initialization/timeout parameters with patch('pyexistdb.db.socket') as mocksocket: timeout_server = 'http://nonexistent/exist:8080/timeout' with patch('pyexistdb.db.requests') as mockrequests: # test init via django settings django_timeout = 4 with override_settings(EXISTDB_TIMEOUT=django_timeout): # no timeout specified, should use settings timeoutdb = db.ExistDB(server_url=timeout_server) mocksocket.getdefaulttimeout.assert_not_called() # xmlrpc transport should use requested timeout xmlrpc_session = mockrequests.Session.return_value # requires a valid xmlrpc response or unmarshalling fails xmlrpc_session.post.return_value.text = '''<methodResponse> <params><param/></params></methodResponse>''' # session used for rest api should use django timeout self.assertEqual(django_timeout, timeoutdb.session_opts['timeout']) # xmlrpc transport should use django timeout timeoutdb.server.ping() args, kwargs = xmlrpc_session.post.call_args self.assertEqual(django_timeout, kwargs['timeout']) # explicit None should take precedence over django settings no_timeout = None timeoutdb = db.ExistDB(server_url=EXISTDB_SERVER_URL, timeout=no_timeout) self.assertEqual(no_timeout, timeoutdb.session_opts['timeout']) timeoutdb.server.ping() args, kwargs = xmlrpc_session.post.call_args self.assertEqual(no_timeout, kwargs['timeout'])