def test_set_db(mocked_requests): woql_client = WOQLClient("http://localhost:6363") with pytest.raises(InterfaceError): woql_client.set_db("my_db") woql_client.connect() woql_client.set_db("my_db") assert woql_client._db == "my_db" assert woql_client.db() == "my_db" assert woql_client.repo() == "local"
def test_create_graph(docker_url): client = WOQLClient(docker_url) assert not client._connected # test connect client.connect() assert client._connected # test create db client.create_database("test_graph") client.create_graph("instance", "test-one-more-graph", "create test graph") client.delete_graph("instance", "test-one-more-graph", "delete test graph")
def test_connection(mocked_requests, monkeypatch): woql_client = WOQLClient("http://localhost:6363") # before connect it connection is empty woql_client.connect(key="root", account="admin", user="******") requests.get.assert_called_once_with( "http://localhost:6363/", headers={"Authorization": "Basic YWRtaW46cm9vdA=="} )
def test_terminusx(terminusx_token): testdb = ("test_happy_" + str(dt.datetime.now()).replace(" ", "") + "_" + str(random())) endpoint = "https://cloud-dev.dcm.ist/TerminusDBTest/" client = WOQLClient(endpoint) client.connect(use_token=True, team="TerminusDBTest") assert client._connected # test create db client.create_database(testdb) assert client.db == testdb assert testdb in client.list_databases() client.delete_database(testdb, "TerminusDBTest") assert client.db is None assert testdb not in client.list_databases()
def test_remote_auth(mocked_requests): woql_client = WOQLClient("http://localhost:6363") result = woql_client.remote_auth() assert result is None woql_client.connect(user="******", account="admin", key="root") result = woql_client.remote_auth() assert result is None auth_setting = {"type": "jwt", "user": "******", "key": "<token>"} woql_client.remote_auth(auth_setting) result = woql_client.remote_auth() assert result == auth_setting
def test_jwt(docker_url_jwt): # create client url = docker_url_jwt[0] token = docker_url_jwt[1] client = WOQLClient(url) assert not client._connected # test connect client.connect(use_token=True, jwt_token=token) assert client._connected # test create db client.create_database("test_happy_path") assert client.db == "test_happy_path" assert "test_happy_path" in client.list_databases() client.delete_database("test_happy_path", "admin") assert client.db is None assert "test_happy_path" not in client.list_databases()
def test_basic_auth(mocked_requests): woql_client = WOQLClient("http://localhost:6363") result = woql_client.basic_auth() assert result is None woql_client.connect(user="******", account="admin", key="root") result = woql_client.basic_auth() assert result == "admin:root" assert woql_client.account() == "admin" assert woql_client.user() == "admin"
def test_datetime_backend(docker_url): datetime_obj = dt.datetime(2019, 5, 18, 15, 17, 8, 132263) delta = dt.timedelta( days=50, seconds=27, microseconds=10, milliseconds=29000, minutes=5, hours=8, weeks=2, ) test_obj = CheckDatetime(datetime=datetime_obj, duration=delta) client = WOQLClient(docker_url) client.connect() client.create_database("test_datetime") client.insert_document(CheckDatetime, graph_type="schema") client.insert_document(test_obj)
def test_create_database(mocked_requests, mocked_requests2): woql_client = WOQLClient("http://localhost:6363", user="******", key="root", account="admin") woql_client.connect() assert woql_client.basic_auth() == "admin:root" woql_client.create_database( "myFirstTerminusDB", "admin", label="my first db", description="my first db comment", include_schema=False, ) requests.post.assert_called_once_with( "http://localhost:6363/api/db/admin/myFirstTerminusDB", headers={"Authorization": "Basic YWRtaW46cm9vdA=="}, verify=False, json={ "label": "my first db", "comment": "my first db comment" }, )
def test_create_database_and_change_account(mocked_requests, mocked_requests2): woql_client = WOQLClient( "http://localhost:6363", user="******", key="root", account="admin" ) woql_client.connect() woql_client.create_database( "myFirstTerminusDB", "my_new_account", label="my first db", description="my first db comment", include_schema=False, ) requests.post.assert_called_once_with( "http://localhost:6363/db/my_new_account/myFirstTerminusDB", headers={ "Authorization": "Basic YWRtaW46cm9vdA==", "content-type": "application/json", }, json={ "label": "my first db", "comment": "my first db comment", "prefixes": { "scm": "terminusdb://my_new_account/myFirstTerminusDB/schema#", "doc": "terminusdb://my_new_account/myFirstTerminusDB/data/", }, }, ) assert woql_client.basic_auth() == "admin:root"
def test_query_commit_made(mocked_execute, mocked_requests): # mocked_execute.return_value = MOCK_CAPABILITIES woql_client = WOQLClient("http://*****:*****@type": "api:WoqlResponse", "api:status": "api:success", "api:variable_names": [], "bindings": [{}], "deletes": 0, "inserts": 1, "transaction_retry_count": 0, } result = woql_client.query(WoqlStar) assert result == "Commit successfully made."
def test_execute_method(self, mocked_requests, mocked_requests2): woql_client = WOQLClient("http://*****:*****@context"] = json_context requests.post.assert_called_once_with( "http://localhost:6363/woql/admin/myDBName/local/branch/master", headers={ "Authorization": "Basic YWRtaW46cm9vdA==", "content-type": "application/json", }, json={"query": mycon}, )
def test_delete_database(mocked_requests, mocked_requests2): woql_client = WOQLClient( "http://localhost:6363", user="******", key="root", account="admin" ) woql_client.connect() assert woql_client.basic_auth() == "admin:root" 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_connected_flag(mocked_requests): woql_client = WOQLClient("http://localhost:6363") assert not woql_client._connected woql_client.connect(key="root", account="admin", user="******") assert woql_client._connected woql_client.close() assert not woql_client._connected
def test_wrong_graph_type(mocked_requests, mocked_requests2): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", account="admin", key="root", db="myDBName") with pytest.raises(ValueError): woql_client.create_graph("wrong_graph_name", "mygraph", "add a new graph")
def test_get_triples(mocked_requests): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", account="admin", key="root", db="myDBName") woql_client.get_triples("instance", "mygraph") requests.get.assert_called_with( "http://localhost:6363/triples/admin/myDBName/local/branch/master/instance/mygraph", headers={"Authorization": "Basic YWRtaW46cm9vdA=="}, )
def test_getting_and_deleting_cheuk(docker_url): assert "cheuk" not in globals() assert "cheuk" not in locals() client = WOQLClient(docker_url) client.connect(db="test_docapi") new_schema = WOQLSchema() new_schema.from_db(client) cheuk = new_schema.import_objects( client.get_documents_by_type("Employee", as_list=True))[0] result = cheuk._obj_to_dict() assert result["address_of"]["postal_code"] == "A12 345" assert result["address_of"]["street"] == "123 Abc Street" assert result["name"] == "Cheuk" assert result["age"] == 21 assert result["contact_number"] == "07777123456" assert result.get("@id") client.delete_document(cheuk) assert client.get_documents_by_type("Employee", as_list=True) == []
def test_branch(mocked_requests, mocked_requests2): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", account="admin", key="root", db="myDBName") woql_client.branch("my_new_branch") requests.post.assert_called_once_with( "http://localhost:6363/api/branch/admin/myDBName/local/branch/my_new_branch", headers={"Authorization": "Basic YWRtaW46cm9vdA=="}, verify=False, json={"origin": "admin/myDBName/local/branch/main"}, )
def test_delete_graph(mocked_requests, mocked_requests2): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", account="admin", key="root", db="myDBName") woql_client.delete_graph("instance", "mygraph", "add a new graph") requests.delete.assert_called_once_with( "http://localhost:6363/graph/admin/myDBName/local/branch/master/instance/mygraph?author=admin&message=add+a+new+graph", headers={"Authorization": "Basic YWRtaW46cm9vdA=="}, )
def test_create_schema(docker_url, test_schema): my_schema = test_schema client = WOQLClient(docker_url) client.connect() client.create_database("test_docapi") client.insert_document(my_schema, commit_msg="I am checking in the schema", graph_type="schema") result = client.get_all_documents(graph_type="schema") for item in result: if "@id" in item: assert item["@id"] in [ "Employee", "Person", "Address", "Team", "Country", "Coordinate", "Role", ] elif "@type" in item: assert item["@type"] == "@context" else: raise AssertionError()
def test_create_database_with_schema( mocked_requests, mocked_requests2, create_schema_obj ): woql_client = WOQLClient( "http://localhost:6363", user="******", key="root", account="admin" ) woql_client.connect() assert woql_client.basic_auth() == "admin:root" woql_client.create_database( "myFirstTerminusDB", "admin", label="my first db", description="my first db comment", ) WOQLClient.create_graph.assert_called_once_with( "schema", "main", f"Python client {__version__} message: Creating schema graph" )
def test_query(mocked_requests, mocked_requests2): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", account="admin", key="root", db="myDBName") # WoqlStar is the query in json-ld woql_client.query(WoqlStar) requests.post.assert_called_once_with( "http://localhost:6363/woql/admin/myDBName/local/branch/master", headers={ "Authorization": "Basic YWRtaW46cm9vdA==", "content-type": "application/json", }, json={"query": WoqlStar}, )
def test_delete_database(mocked_requests, mocked_requests2, monkeypatch): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", account="admin", key="root") monkeypatch.setattr(woql_client.conCapabilities, "capabilities_permit", mock_func_with_1arg) monkeypatch.setattr(woql_client.conCapabilities, "remove_db", mock_func_with_2arg) woql_client.delete_database("myFirstTerminusDB") requests.delete.assert_called_once_with( "http://localhost:6363/db/admin/myFirstTerminusDB", headers={"Authorization": "Basic YWRtaW46cm9vdA=="}, )
def test_query(mocked_requests, mocked_requests2): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", account="admin", key="root", db="myDBName") # WoqlStar is the query in json-ld woql_client.query(WoqlStar) requests.post.assert_called_once_with( "http://localhost:6363/api/woql/admin/myDBName/local/branch/main", headers={"Authorization": "Basic YWRtaW46cm9vdA=="}, verify=False, json={ "commit_info": { "author": "admin", "message": f"Commit via python client {__version__}", }, "query": WoqlStar, }, )
def test_create_graph(mocked_requests, mocked_requests2): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", account="admin", key="root", db="myDBName") woql_client.create_graph("instance", "mygraph", "add a new graph") requests.post.assert_called_once_with( "http://localhost:6363/graph/admin/myDBName/local/branch/master/instance/mygraph", headers={ "Authorization": "Basic YWRtaW46cm9vdA==", "content-type": "application/json", }, json={ "commit_info": { "author": "admin", "message": "add a new graph" } }, )
from terminusdb_client.woqlquery.woql_query import WOQLQuery as WQ from terminusdb_client.woqlclient.woqlClient import WOQLClient server_url = "https://127.0.0.1:6363" user = "******" account = "admin" key = "root" dbid = "unwarbled_widgets" repository = "local" label = "Unwarbled Widgets" description = "The database for storage of the unwarbled widgets" result_csv = '/app/local_files/unwarbled_widgets.csv' widgets_url = 'http://terminusdb.github.io/terminusdb-web-assets/tutorials/episode_1/items.csv' 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) # Add the schema (there is no harm in adding repeatedly as it is idempotent) WQ().woql_and( WQ().doctype("scm:Widget") .label("Widget")
def test_happy_path(docker_url): # create client client = WOQLClient(docker_url) assert not client._connected # test connect client.connect() assert client._connected # test create db client.create_database("test_happy_path", prefixes={"doc": "foo://"}) init_commit = client._get_current_commit() assert client._db == "test_happy_path" assert "test_happy_path" in client.list_databases() assert client._context.get("doc") == "foo://" assert len(client.get_commit_history()) == 1 # test adding doctype WOQLQuery().doctype("Station").execute(client) first_commit = client._get_current_commit() assert first_commit != init_commit commit_history = client.get_commit_history() assert len(commit_history) == 2 assert len(client.get_commit_history(2)) == 2 assert len(client.get_commit_history(1)) == 1 assert len(client.get_commit_history(0)) == 1 assert commit_history[0]["commit"] == first_commit.split("_")[-1] assert commit_history[1]["commit"] == init_commit.split("_")[-1] client.delete_database("test_happy_path", "admin") assert client._db is None assert "test_happy_path" not in client.list_databases()
def test_csv_handeling(docker_url): client = WOQLClient(docker_url) assert not client._connected # test connect client.connect() assert client._connected # test create db client.create_database("test_csv") client._get_current_commit() assert client._db == "test_csv" assert "test_csv" in client.list_databases() csv_file_path = _generate_csv() # create testing csv try: client.insert_csv(csv_file_path) client.get_csv(csv_file_path, csv_output_name="new_" + csv_file_path) assert filecmp.cmp(csv_file_path, "new_" + csv_file_path) finally: _file_clean_up(csv_file_path) _file_clean_up("new_" + csv_file_path)
def test_rollback(mocked_requests): woql_client = WOQLClient("http://localhost:6363") woql_client.connect(user="******", account="admin", key="root") with pytest.raises(NotImplementedError): woql_client.rollback()
def test_copy_client(): woql_client = WOQLClient("http://localhost:6363") copy_client = woql_client.copy() assert id(woql_client) != copy_client