def test_remote_auth(mocked_requests): woql_client = WOQLClient("http://localhost:6363") auth_setting = {"type": "jwt", "user": "******", "key": "<token>"} woql_client.connect( user="******", team="admin", key="root", remote_auth=auth_setting ) result = woql_client._remote_auth assert result == auth_setting
def test_connection(mocked_requests): woql_client = WOQLClient("http://localhost:6363") # before connect it connection is empty woql_client.connect(key="root", team="admin", user="******") requests.get.assert_called_once_with( "http://localhost:6363/api/", auth=("admin", "root"), headers={"user-agent": f"terminusdb-client-python/{__version__}"}, )
def test_set_db(mocked_requests): woql_client = WOQLClient("http://localhost:6363") with pytest.raises(InterfaceError): woql_client.set_db("myDBName") woql_client.connect() woql_client.set_db("myDBName") assert woql_client.db == "myDBName" assert woql_client.repo == "local"
def test_connected_flag(mocked_requests): woql_client = WOQLClient("http://localhost:6363") assert not woql_client._connected woql_client.connect(key="root", team="admin", user="******") assert woql_client._connected woql_client.close() assert not woql_client._connected
def test_full_replace_fail(mocked_requests): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(db="myDBName") with pytest.raises(ValueError): woql_client.insert_document( [{"not_context": "no context provided"}], full_replace=True )
def test_delete_database(mocked_requests, mocked_requests2): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", key="root", team="admin") woql_client.create_database( "myFirstTerminusDB", "admin", label="my first db", description="my first db comment", include_schema=False, ) with pytest.raises(UserWarning): woql_client.delete_database()
def test_get_triples(mocked_requests): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", team="admin", key="root", db="myDBName") woql_client.get_triples("instance") requests.get.assert_called_with( "http://localhost:6363/api/triples/admin/myDBName/local/branch/main/instance", auth=("admin", "root"), headers={"user-agent": f"terminusdb-client-python/{__version__}"}, )
def test_crazy_branch(mocked_requests, mocked_requests2): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", team="amazing admin", key="root", db="my DB") woql_client.create_branch("my new branch") requests.post.assert_called_once_with( "http://localhost:6363/api/branch/amazing%20admin/my%20DB/local/branch/my%20new%20branch", auth=("admin", "root"), json={"origin": "amazing admin/my DB/local/branch/main"}, headers={"user-agent": f"terminusdb-client-python/{__version__}"}, )
def test_create_database_with_schema(mocked_requests, mocked_requests2): woql_client = WOQLClient("http://localhost:6363") woql_client.connect() woql_client.create_database( "myFirstTerminusDB", "admin", label="my first db", description="my first db comment", include_schema=True, ) requests.post.assert_called_once_with( "http://localhost:6363/api/db/admin/myFirstTerminusDB", auth=("admin", "root"), json={"label": "my first db", "comment": "my first db comment", "schema": True}, headers={"user-agent": f"terminusdb-client-python/{__version__}"}, )
def test_create_database_and_change_team(mocked_requests, mocked_requests2): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", team="admin", key="root") woql_client.create_database( "myFirstTerminusDB", "my_new_team", label="my first db", description="my first db comment", include_schema=False, ) requests.post.assert_called_once_with( "http://localhost:6363/api/db/my_new_team/myFirstTerminusDB", auth=("admin", "root"), json={"label": "my first db", "comment": "my first db comment"}, headers={"user-agent": f"terminusdb-client-python/{__version__}"}, )
def test_query(mocked_requests, mocked_requests2): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", team="admin", key="root", db="myDBName") # WoqlStar is the query in json-ld woql_client.query(WoqlStar, commit_msg="commit msg") requests.post.assert_called_once_with( "http://localhost:6363/api/woql/admin/myDBName/local/branch/main", auth=("admin", "root"), json={ "commit_info": { "author": "admin", "message": "commit msg", }, "query": WoqlStar, }, headers={"user-agent": f"terminusdb-client-python/{__version__}"}, )
def test_query_nodb(mocked_requests): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", team="admin", key="root") with pytest.raises(InterfaceError): woql_client.query(WoqlStar)
def test_basic_auth(mocked_requests): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", team="admin", key="root") assert woql_client._key == "root" assert woql_client.team == "admin" assert woql_client.user == "admin"
def test_copy_client(): woql_client = WOQLClient("http://localhost:6363") copy_client = woql_client.copy() assert id(woql_client) != copy_client
def test_rollback(mocked_requests): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", team="admin", key="root") with pytest.raises(NotImplementedError): woql_client.rollback()
return match def generateInsertClause(code, type, i): insert = WOQLQuery().woql_and( WOQLQuery().insert("v:ID_"+str(i), type, label="v:Label_"+str(i)) ) return insert def generateMultiInsertQuery(codes, type): matches = [] inserts = [] index = 0 for code in codes: matches.append(generateMatchClause(code, type, index)) inserts.append(generateInsertClause(code, type, index)) index = index+1 return WOQLQuery().when( WOQLQuery().woql_and(*matches), WOQLQuery().woql_and(*inserts) ) db_id = "pyplane" client = WOQLClient(server_url = "https://127.0.0.1:6363") client.connect(key="root", account="admin", user="******") existing = client.get_metadata(db_id, client.uid()) if not existing: client.create_database(db_id, "admin", label="Flights Graph", description="Create a graph with Open Flights data") else: client.db(db_id) create_schema(client)
hus_obj = WOQLQuery().insert("hus"+str(hus["Id"]), "House").label(hus["Name"]) if hus["Region"] is not None: hus_obj.property("region", "doc:Region_"+hus["Region"]) for seat in hus["Seats"]: hus_obj.property("seats", "doc:Seats_"+seat) if hus["Founder"] is not None: hus_obj.property("founder", "doc:Person_"+str(hus["Founder"])) if hus["Words"] is not None: data_obj = {"@value" : hus["Words"], "@type" : "xsd:string"} hus_obj.property("words", data_obj) if hus["Heir"] is not None: hus_obj.property("heir", "doc:Person_"+str(hus["Heir"])) if hus["Overlord"] is not None: hus_obj.property("overlord", "doc:Person_"+str(hus["Overlord"])) results.append(hus_obj) return WOQLQuery().woql_and(*results).execute(client, "Adding data for Game of Thrones.") db_id = "game_of_thrones" client = WOQLClient(server_url = "http://localhost:6363") client.connect(key="root", account="admin", user="******") existing = client.get_metadata(db_id, client.uid()) if not existing: client.create_database(db_id, "admin", label="Game of Thrones Graph", description="Create a graph with Game of Thrones data") else: client.db(db_id) create_schema(client) load_data(client, houses, characters)
def test_insert_woqlschema_fail(mocked_requests): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(db="myDBName") with pytest.raises(InterfaceError): woql_client.insert_document(WOQLSchema(), graph_type="instance")
def test_delete_document(mocked_requests, mocked_requests2, test_schema): woql_client = WOQLClient( "http://localhost:6363", user="******", key="root", team="admin" ) woql_client.connect(db="myDBName") woql_client.delete_document(["id1", "id2"]) requests.delete.assert_called_with( "http://localhost:6363/api/document/admin/myDBName/local/branch/main", auth=("admin", "root"), json=["id1", "id2"], headers={"user-agent": f"terminusdb-client-python/{__version__}"}, params={ "author": "admin", "graph_type": "instance", "message": f"Commit via python client {__version__}", }, ) woql_client.delete_document("id1") requests.delete.assert_called_with( "http://localhost:6363/api/document/admin/myDBName/local/branch/main", auth=("admin", "root"), json=["id1"], headers={"user-agent": f"terminusdb-client-python/{__version__}"}, params={ "author": "admin", "graph_type": "instance", "message": f"Commit via python client {__version__}", }, ) my_schema = test_schema Coordinate = my_schema.object.get("Coordinate") home = Coordinate(_id="Coordinate/home", x=123.431, y=342.435) woql_client.delete_document(home) requests.delete.assert_called_with( "http://localhost:6363/api/document/admin/myDBName/local/branch/main", auth=("admin", "root"), json=["Coordinate/home"], headers={"user-agent": f"terminusdb-client-python/{__version__}"}, params={ "author": "admin", "graph_type": "instance", "message": f"Commit via python client {__version__}", }, ) woql_client.delete_document(home._obj_to_dict()) requests.delete.assert_called_with( "http://localhost:6363/api/document/admin/myDBName/local/branch/main", auth=("admin", "root"), json=["Coordinate/home"], headers={"user-agent": f"terminusdb-client-python/{__version__}"}, params={ "author": "admin", "graph_type": "instance", "message": f"Commit via python client {__version__}", }, )
def test_query_commit_made(mocked_execute, mocked_requests): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", team="admin", key="root", db="myDBName") result = woql_client.query(WoqlStar) assert result == "Commit successfully made."
propteries = pd.read_csv("all-layers-properties.csv") propteries["QueryObjects"] = propteries.apply(construction_schema_objects, axis=1) propteries["QueryObjects_DR"] = propteries.apply(construct_prop_dr, axis=1) propteries["QueryAddOnObj"] = propteries.apply( construction_schema_addon_property, axis=1, type_list=list(types["id"])) server_url = "https://127.0.0.1:6363" user = "******" account = "admin" key = "root" dbid = "schema_tutorial" label = "Schema Tutorial" description = "Create a graph with Schema.org data" client = WOQLClient(server_url) client.connect(user=user, account=account, key=key, db=dbid) try: client.create_database(dbid, user, label=label, description=description) except Exception as E: error_obj = E.errorObj if "api:DatabaseAlreadyExists" == error_obj.get("api:error", {}).get("@type", None): print(f'Warning: Database "{dbid}" already exists!\n') else: raise (E) print("create schema for types") create_schema_objects(client, list(types["QueryObjects"])) print("create schema relations for simple types")
data_type = data['type'] WOQLObj = WOQLQuery().insert('doc:'+id, data_type) if data_type == 'http://schema.org/DateTime': date_value = {"@value" : data['value'], "@type" : "xsd:dateTime"} execution_queue.append(WOQLObj.property(data_type+'Value', date_value)) return for prop in data['properties']: extract_data(data['properties'][prop], id+prop+'/') WOQLObj = WOQLObj.property('http://schema.org/'+prop, 'doc:'+id+prop+'/') execution_queue.append(WOQLObj) else: if '://' in data: data_type = 'http://schema.org/URL' else: data_type = 'http://schema.org/Text' WOQLObj = WOQLQuery().insert('doc:'+id, data_type) data_obj = {"@value" : data, "@type" : "xsd:string"} execution_queue.append(WOQLObj.property(data_type+'Value',data_obj)) extract_data(data['microdata'][0]) db_id = "schema_tutorial" client = WOQLClient(server_url = "http://localhost:6363") client.connect(key="root", account="admin", user="******") existing = client.get_metadata(db_id, client.uid()) if not existing: client.create_database(db_id, "admin", { "label": "Schema.org Graph", "comment": "Create a graph with Schema.org data"}) else: client.db(db_id) WOQLQuery().woql_and(*execution_queue).execute(client)