def test_triple_method(self, triple_opt): woql_object = WOQLQuery().triple("a", "b", "c") woql_object_opt = WOQLQuery().triple("a", "b", "c", opt=True) assert woql_object.to_dict() == WOQL_JSON["tripleJson"] assert woql_object_opt.to_dict() == triple_opt
def test_star_method(self): woql_object = WOQLQuery().star() assert woql_object.to_dict() == WOQL_STAR
def test_eval_method(self): woql_object = WOQLQuery().eval("1+2", "b") assert woql_object.to_dict() == WOQL_MATH_JSON["evalJson"]
def test_bank_tutorial(docker_url): user = "******" account = "admin" key = "root" dbid = "bank_balance_example" repository = "local" label = "Bank Balance Example" description = "An example database for playing with bank accounts" client = WOQLClient(docker_url) client.connect(user=user, account=account, key=key) client.create_database(dbid, user, label=label, description=description) # Add the schema (there is no harm in adding repeatedly as it is idempotent) WOQLQuery().woql_and(WOQLQuery().doctype("scm:BankAccount").label( "Bank Account").description("A bank account").property( "scm:owner", "xsd:string").label("owner").cardinality(1).property( "scm:balance", "xsd:nonNegativeInteger").label("owner")).execute( client, "Adding bank account object to schema") # Fix bug in schema WOQLQuery().woql_and( WOQLQuery().delete_quad("scm:balance", "label", "owner", "schema/main"), WOQLQuery().add_quad("scm:balance", "label", "balance", "schema/main"), ).execute(client, "Label for balance was wrong") # Add the data from csv to the main branch (again idempotent as widget ids are chosen from sku) WOQLQuery().woql_and(WOQLQuery().insert( "doc:mike", "scm:BankAccount").property("scm:owner", "mike").property( "scm:balance", 123)).execute(client, "Add mike") # try to make mike go below zero balance, new_balance = WOQLQuery().vars("Balance", "New Balance") with pytest.raises(DatabaseError): WOQLQuery().woql_and( WOQLQuery().triple("doc:mike", "scm:balance", balance), WOQLQuery().delete_triple("doc:mike", "scm:balance", balance), WOQLQuery().eval(WOQLQuery().minus(balance, 130), new_balance), WOQLQuery().add_triple("doc:mike", "scm:balance", new_balance), ).execute(client, "Update mike") # Subtract less WOQLQuery().woql_and( WOQLQuery().triple("doc:mike", "scm:balance", balance), WOQLQuery().delete_triple("doc:mike", "scm:balance", balance), WOQLQuery().eval(WOQLQuery().minus(balance, 110), new_balance), WOQLQuery().add_triple("doc:mike", "scm:balance", new_balance), ).execute(client, "Update mike") # Make the "branch_office" branch branch = "branch_office" client.branch(branch) # Add some data to the branch client.checkout(branch) WOQLQuery().woql_and(WOQLQuery().insert( "doc:jim", "scm:BankAccount").property("owner", "jim").property( "balance", 8)).execute(client, "Adding Jim") # Return to the 'main' branch and add Jane client.checkout("main") WOQLQuery().woql_and(WOQLQuery().insert( "doc:jane", "scm:BankAccount").property("owner", "jane").property( "balance", 887)).execute(client, "Adding Jane") client.rebase( rebase_source=f"{user}/{dbid}/{repository}/branch/{branch}", author=user, message="Merging jim in from branch_office", ) result = WOQLQuery().triple("doc:jim", "scm:balance", 8).execute(client) assert ( len(result.get("bindings")) == 1 ) # if jim is there, the length of the binding should be 1, if not it will be 0
def test_when_method(self): woql_object = WOQLQuery().when(True, WOQLQuery().add_class("id")) woql_object_chain = WOQLQuery().when(True).add_class("id") assert woql_object.to_dict() == WOQL_WHEN_JSON assert woql_object_chain.to_dict() == WOQL_WHEN_JSON
def test_min(self, property_min): woql_object = WOQLQuery().add_property("P", "string").domain("A").min(2) assert woql_object.to_dict() == property_min
def test_chain2(self, chain_doctype): woql_object = (WOQLQuery().doctype("MyDoc").label("abc").description( "abcd").property("prop", "dateTime").label("aaa").property( "prop2", "integer").label("abe")) assert woql_object.to_dict() == chain_doctype
def test_delete_method(self): woql_object = WOQLQuery().delete_object("doc:x") json_obj = {"@type": "woql:DeleteObject", "woql:document_uri": "doc:x"} assert woql_object.to_dict() == json_obj
def test_delete_triple_method(self): woql_object = WOQLQuery().delete_triple("a", "b", "c") assert woql_object.to_dict() == WOQL_JSON["deleteTripleJson"]
def test_sub_method(self): woql_object = WOQLQuery().sub("ClassA", "ClassB") assert woql_object.to_dict() == WOQL_JSON["subsumptionJson"]
def test_isa_method(self): woql_object = WOQLQuery().isa("instance", "Class") assert woql_object.to_dict() == WOQL_JSON["isAJson"]
def test_delete_class_method(self): woql_object = WOQLQuery().delete_class("id") assert woql_object.to_dict() == WOQL_DELETE_JSON
def test_add_class_method(self): woql_object = WOQLQuery().add_class("id") assert woql_object.to_dict() == WOQL_JSON["addClassJson"]
def test_quad_method(self, quad_opt): woql_object = WOQLQuery().quad("a", "b", "c", "d") woql_object_opt = WOQLQuery().quad("a", "b", "c", "d", opt=True) assert woql_object.to_dict() == WOQL_JSON["quadJson"] assert woql_object_opt.to_dict() == quad_opt
def test_node_parent_method(self): woql_object = WOQLQuery().node("doc:x", "add_quad").parent("classParentName") assert woql_object.to_dict() == WOQL_JSON["nodeParentJson"]
def test_delete_quad_method(self): woql_object = WOQLQuery().delete_quad("a", "b", "c", "d") assert woql_object.to_dict() == WOQL_JSON["deleteQuadJson"]
def test_class_description_method(self): woql_object = (WOQLQuery().add_class("NewClass").description( "A new class object.")) assert woql_object.to_dict() == WOQL_JSON["addClassDescJson"]
def test_add_triple_method(self): woql_object = WOQLQuery().add_triple("a", "b", "c") assert woql_object.to_dict() == WOQL_JSON["addTripleJson"]
def test_card(self, property_cardinalty): woql_object = WOQLQuery().add_property( "P", "string").domain("A").cardinality(3) assert woql_object.to_dict() == property_cardinalty
def test_add_quad_method(self): woql_object = WOQLQuery().add_quad("a", "b", "c", "d") assert woql_object.to_dict() == WOQL_JSON["addQuadJson"]
def test_vars(self): single_vars = WOQLQuery().vars("a") vars1, vars2, vars3 = WOQLQuery().vars("a", "b", "c") assert single_vars == "v:a" assert (vars1, vars2, vars3) == ("v:a", "v:b", "v:c")
def test_delete_property_method(self): woql_object = WOQLQuery().delete_property("some_property", "string") assert woql_object.to_dict() == WOQL_JSON["deletePropertyJson"]
def test_woql_or_method(self): woql_object = WOQLQuery().woql_or(WOQLQuery().triple("a", "b", "c"), WOQLQuery().triple("1", "2", "3")) assert woql_object.to_dict() == WOQL_OR_JSON
def test_node_method(self): woql_object = WOQLQuery().node("some_node", "add_quad") assert woql_object.to_dict() == {}
def test_opt_method(self): woql_object = WOQLQuery().opt(WOQLQuery().star()) woql_object_chain = WOQLQuery().opt().star() json_obj = {"@type": "woql:Optional", "woql:query": WOQL_STAR} assert woql_object.to_dict() == json_obj assert woql_object_chain.to_dict() == json_obj
def test_abstract_method(self): woql_object = WOQLQuery().node("doc:x", "add_quad").abstract() assert woql_object.to_dict() == WOQL_JSON["nodeAbstractJson"]
def test_trim_method(self): woql_object = WOQLQuery().trim("a", "b") assert woql_object.to_dict() == WOQL_TRIM_JSON
def test_node_property_method(self): woql_object = (WOQLQuery().node("doc:x", "add_triple").property( "myprop", "value")) assert woql_object.to_dict() == WOQL_JSON["addNodePropJson"]
def test_minus_method(self): woql_object = WOQLQuery().minus("2", "1") assert woql_object.to_dict() == WOQL_MATH_JSON["minusJson"]
def test_double_grouped_path_query(self): woql_object = WOQLQuery().path( "v:X", "((<scm:hop,scm:hop>)|(<scm:hop2,scm:hop2>))+", "v:Y", "v:Path") json_object = { "@type": "woql:Path", "woql:subject": { "@type": "woql:Variable", "woql:variable_name": { "@value": "X", "@type": "xsd:string" }, }, "woql:path_pattern": { "@type": "woql:PathPlus", "woql:path_pattern": { "@type": "woql:PathOr", "woql:path_left": { "@type": "woql:PathSequence", "woql:path_first": { "@type": "woql:InvertedPathPredicate", "woql:path_predicate": { "@id": "scm:hop" }, }, "woql:path_second": { "@type": "woql:PathPredicate", "woql:path_predicate": { "@id": "scm:hop" }, }, }, "woql:path_right": { "@type": "woql:PathSequence", "woql:path_first": { "@type": "woql:InvertedPathPredicate", "woql:path_predicate": { "@id": "scm:hop2" }, }, "woql:path_second": { "@type": "woql:PathPredicate", "woql:path_predicate": { "@id": "scm:hop2" }, }, }, }, }, "woql:object": { "@type": "woql:Variable", "woql:variable_name": { "@value": "Y", "@type": "xsd:string" }, }, "woql:path": { "@type": "woql:Variable", "woql:variable_name": { "@value": "Path", "@type": "xsd:string" }, }, } assert woql_object.to_dict() == json_object