|
| 1 | +"""Unit tests for idgen_random WOQL method""" |
| 2 | +from terminusdb_client.woqlquery.woql_query import WOQLQuery |
| 3 | + |
| 4 | + |
| 5 | +class TestIdgenRandom: |
| 6 | + """Test idgen_random WOQL method""" |
| 7 | + |
| 8 | + def test_idgen_random_basic(self): |
| 9 | + """Test basic idgen_random functionality""" |
| 10 | + woql = WOQLQuery().idgen_random("Person/", "v:PersonID") |
| 11 | + result = woql.to_dict() |
| 12 | + |
| 13 | + assert result["@type"] == "RandomKey" |
| 14 | + assert result["base"]["@type"] == "DataValue" |
| 15 | + assert result["base"]["data"]["@type"] == "xsd:string" |
| 16 | + assert result["base"]["data"]["@value"] == "Person/" |
| 17 | + assert result["uri"]["@type"] == "NodeValue" |
| 18 | + assert result["uri"]["variable"] == "PersonID" |
| 19 | + |
| 20 | + def test_idgen_random_with_prefix(self): |
| 21 | + """Test idgen_random with different prefix formats""" |
| 22 | + woql = WOQLQuery().idgen_random("http://example.org/Person/", "v:ID") |
| 23 | + result = woql.to_dict() |
| 24 | + |
| 25 | + assert result["base"]["data"]["@value"] == "http://example.org/Person/" |
| 26 | + assert result["uri"]["variable"] == "ID" |
| 27 | + |
| 28 | + def test_idgen_random_chaining(self): |
| 29 | + """Test idgen_random can be chained with other operations""" |
| 30 | + woql = (WOQLQuery() |
| 31 | + .triple("v:Person", "rdf:type", "@schema:Person") |
| 32 | + .idgen_random("Person/", "v:PersonID")) |
| 33 | + |
| 34 | + result = woql.to_dict() |
| 35 | + assert result["@type"] == "And" |
| 36 | + assert len(result["and"]) == 2 |
| 37 | + assert result["and"][0]["@type"] == "Triple" |
| 38 | + assert result["and"][1]["@type"] == "RandomKey" |
| 39 | + |
| 40 | + def test_idgen_random_multiple_calls(self): |
| 41 | + """Test multiple idgen_random calls in same query""" |
| 42 | + woql = (WOQLQuery() |
| 43 | + .idgen_random("Person/", "v:PersonID") |
| 44 | + .idgen_random("Order/", "v:OrderID")) |
| 45 | + |
| 46 | + result = woql.to_dict() |
| 47 | + assert result["@type"] == "And" |
| 48 | + assert result["and"][0]["@type"] == "RandomKey" |
| 49 | + assert result["and"][0]["base"]["data"]["@value"] == "Person/" |
| 50 | + assert result["and"][1]["@type"] == "RandomKey" |
| 51 | + assert result["and"][1]["base"]["data"]["@value"] == "Order/" |
| 52 | + |
| 53 | + def test_idgen_random_args_parameter(self): |
| 54 | + """Test idgen_random args parameter returns parameter list""" |
| 55 | + result = WOQLQuery().idgen_random("args", "v:ID") |
| 56 | + |
| 57 | + assert result == ["base", "uri"] |
| 58 | + |
| 59 | + def test_idgen_random_empty_prefix(self): |
| 60 | + """Test idgen_random with empty prefix""" |
| 61 | + woql = WOQLQuery().idgen_random("", "v:ID") |
| 62 | + result = woql.to_dict() |
| 63 | + |
| 64 | + assert result["@type"] == "RandomKey" |
| 65 | + assert result["base"]["data"]["@value"] == "" |
| 66 | + |
| 67 | + def test_idgen_random_variable_output(self): |
| 68 | + """Test idgen_random output variable format""" |
| 69 | + woql = WOQLQuery().idgen_random("Test/", "v:MyVar") |
| 70 | + result = woql.to_dict() |
| 71 | + |
| 72 | + assert result["uri"]["variable"] == "MyVar" |
| 73 | + |
| 74 | + def test_idgen_random_in_query_chain(self): |
| 75 | + """Test idgen_random in complex query chain""" |
| 76 | + woql = (WOQLQuery() |
| 77 | + .triple("v:Person", "rdf:type", "@schema:Person") |
| 78 | + .idgen_random("Person/", "v:PersonID") |
| 79 | + .triple("v:PersonID", "@schema:name", "v:Name")) |
| 80 | + |
| 81 | + result = woql.to_dict() |
| 82 | + assert result["@type"] == "And" |
| 83 | + # WOQLQuery chains create nested And structures |
| 84 | + # Verify RandomKey is present in the chain |
| 85 | + has_random_key = False |
| 86 | + |
| 87 | + def check_for_random_key(obj): |
| 88 | + nonlocal has_random_key |
| 89 | + if isinstance(obj, dict): |
| 90 | + if obj.get("@type") == "RandomKey": |
| 91 | + has_random_key = True |
| 92 | + for value in obj.values(): |
| 93 | + check_for_random_key(value) |
| 94 | + elif isinstance(obj, list): |
| 95 | + for item in obj: |
| 96 | + check_for_random_key(item) |
| 97 | + |
| 98 | + check_for_random_key(result) |
| 99 | + assert has_random_key, "RandomKey should be present in query chain" |
| 100 | + |
| 101 | + def test_idgen_random_matches_expected_json(self): |
| 102 | + """Test idgen_random produces expected WOQL JSON structure""" |
| 103 | + from terminusdb_client.tests.woqljson.woqlIdgenJson import WOQL_RANDOM_KEY_JSON |
| 104 | + |
| 105 | + woql = WOQLQuery().idgen_random("Person/", "v:Person_ID") |
| 106 | + result = woql.to_dict() |
| 107 | + |
| 108 | + assert result == WOQL_RANDOM_KEY_JSON |
| 109 | + |
| 110 | + def test_idgen_random_with_special_characters_in_prefix(self): |
| 111 | + """Test idgen_random handles special characters in prefix""" |
| 112 | + woql = WOQLQuery().idgen_random("Test/2024-11/", "v:ID") |
| 113 | + result = woql.to_dict() |
| 114 | + |
| 115 | + assert result["base"]["data"]["@value"] == "Test/2024-11/" |
| 116 | + |
| 117 | + def test_idgen_random_preserves_cursor_state(self): |
| 118 | + """Test idgen_random returns self for method chaining""" |
| 119 | + woql = WOQLQuery() |
| 120 | + result = woql.idgen_random("Test/", "v:ID") |
| 121 | + |
| 122 | + assert result is woql # Should return same object for chaining |
0 commit comments