|
| 1 | +"""Extended edge case tests for WOQL query functionality.""" |
| 2 | +import pytest |
| 3 | +from terminusdb_client.woqlquery.woql_query import WOQLQuery, Var, Doc, Vars |
| 4 | + |
| 5 | + |
| 6 | +class TestWOQLVocabHandling: |
| 7 | + """Test vocabulary handling in WOQL queries.""" |
| 8 | + |
| 9 | + def test_vocab_extraction_from_string_with_colon(self): |
| 10 | + """Test vocabulary extraction from strings with colons.""" |
| 11 | + query = WOQLQuery() |
| 12 | + # This should trigger line 706 - vocab extraction |
| 13 | + query.triple("schema:Person", "rdf:type", "owl:Class") |
| 14 | + # Vocab should be extracted from the prefixed terms |
| 15 | + assert query._query is not None |
| 16 | + |
| 17 | + def test_vocab_extraction_with_underscore_prefix(self): |
| 18 | + """Test that underscore-prefixed terms don't extract vocab.""" |
| 19 | + query = WOQLQuery() |
| 20 | + # _: prefix should not extract vocab (line 705 condition) |
| 21 | + query.triple("_:blank", "rdf:type", "owl:Class") |
| 22 | + assert query._query is not None |
| 23 | + |
| 24 | + |
| 25 | +class TestWOQLSelectEdgeCases: |
| 26 | + """Test select() method edge cases.""" |
| 27 | + |
| 28 | + def test_select_with_empty_list_directly(self): |
| 29 | + """Test select with empty list creates proper structure.""" |
| 30 | + query = WOQLQuery() |
| 31 | + # This tests line 786-787 - empty list handling |
| 32 | + result = query.select() |
| 33 | + |
| 34 | + assert result is query |
| 35 | + assert query._query["@type"] == "Select" |
| 36 | + |
| 37 | + def test_select_with_subquery_object(self): |
| 38 | + """Test select with a subquery that has to_dict method.""" |
| 39 | + query = WOQLQuery() |
| 40 | + subquery = WOQLQuery().triple("v:X", "rdf:type", "schema:Person") |
| 41 | + |
| 42 | + # This tests line 788-789 - hasattr to_dict check |
| 43 | + result = query.select("v:X", subquery) |
| 44 | + |
| 45 | + assert result is query |
| 46 | + assert "variables" in query._cursor |
| 47 | + assert "query" in query._cursor |
| 48 | + |
| 49 | + |
| 50 | +class TestWOQLAsEdgeCases: |
| 51 | + """Test as() method edge cases for uncovered lines.""" |
| 52 | + |
| 53 | + def test_as_with_list_of_pairs(self): |
| 54 | + """Test as() with list of [var, name] pairs.""" |
| 55 | + query = WOQLQuery() |
| 56 | + # This tests lines 1490-1495 - list handling |
| 57 | + result = query.woql_as([["v:X", "name"], ["v:Y", "age"]]) |
| 58 | + |
| 59 | + assert result is query |
| 60 | + assert len(query._query) == 2 |
| 61 | + |
| 62 | + def test_as_with_list_including_type(self): |
| 63 | + """Test as() with list including type specification.""" |
| 64 | + query = WOQLQuery() |
| 65 | + # This tests line 1493 - three-element list with type |
| 66 | + result = query.woql_as([["v:X", "name", "xsd:string"]]) |
| 67 | + |
| 68 | + assert result is query |
| 69 | + assert len(query._query) == 1 |
| 70 | + |
| 71 | + def test_as_with_xsd_type_string(self): |
| 72 | + """Test as() with xsd: prefixed type.""" |
| 73 | + query = WOQLQuery() |
| 74 | + # This tests lines 1500-1501 - xsd: prefix handling |
| 75 | + result = query.woql_as(0, "v:Value", "xsd:string") |
| 76 | + |
| 77 | + assert result is query |
| 78 | + assert len(query._query) == 1 |
| 79 | + |
| 80 | + def test_as_with_xdd_type_string(self): |
| 81 | + """Test as() with xdd: prefixed type.""" |
| 82 | + query = WOQLQuery() |
| 83 | + # This tests line 1500 - xdd: prefix handling |
| 84 | + result = query.woql_as(0, "v:Value", "xdd:coordinate") |
| 85 | + |
| 86 | + assert result is query |
| 87 | + assert len(query._query) == 1 |
| 88 | + |
| 89 | + def test_as_with_two_string_args(self): |
| 90 | + """Test as() with two string arguments.""" |
| 91 | + query = WOQLQuery() |
| 92 | + # This tests lines 1502-1503 - two arg handling without type |
| 93 | + result = query.woql_as("v:X", "name") |
| 94 | + |
| 95 | + assert result is query |
| 96 | + assert len(query._query) == 1 |
| 97 | + |
| 98 | + def test_as_with_single_arg(self): |
| 99 | + """Test as() with single argument.""" |
| 100 | + query = WOQLQuery() |
| 101 | + # This tests line 1505 - single arg handling |
| 102 | + result = query.woql_as("v:X") |
| 103 | + |
| 104 | + assert result is query |
| 105 | + assert len(query._query) == 1 |
| 106 | + |
| 107 | + def test_as_with_dict_arg(self): |
| 108 | + """Test as() with dictionary argument.""" |
| 109 | + query = WOQLQuery() |
| 110 | + # This tests lines 1509-1510 - dict handling |
| 111 | + result = query.woql_as({"@type": "Value", "variable": "X"}) |
| 112 | + |
| 113 | + assert result is query |
| 114 | + assert len(query._query) == 1 |
| 115 | + |
| 116 | + def test_as_with_object_having_to_dict(self): |
| 117 | + """Test as() with object that has to_dict method.""" |
| 118 | + query = WOQLQuery() |
| 119 | + var = Var("X") |
| 120 | + # This tests lines 1507-1508 - hasattr to_dict |
| 121 | + result = query.woql_as(var) |
| 122 | + |
| 123 | + assert result is query |
| 124 | + assert len(query._query) == 1 |
| 125 | + |
| 126 | + |
| 127 | +class TestWOQLCursorManagement: |
| 128 | + """Test cursor management edge cases.""" |
| 129 | + |
| 130 | + def test_wrap_cursor_with_and_when_already_and(self): |
| 131 | + """Test _wrap_cursor_with_and when cursor is already And type.""" |
| 132 | + query = WOQLQuery() |
| 133 | + # Set up cursor as And type with existing and array |
| 134 | + query._cursor["@type"] = "And" |
| 135 | + query._cursor["and"] = [{"@type": "Triple"}] |
| 136 | + |
| 137 | + # This should trigger lines 709-712 |
| 138 | + query._wrap_cursor_with_and() |
| 139 | + |
| 140 | + # After wrapping, the query structure should be valid |
| 141 | + assert query._query is not None or query._cursor is not None |
| 142 | + |
| 143 | + |
| 144 | +class TestWOQLArithmeticOperations: |
| 145 | + """Test arithmetic operations edge cases.""" |
| 146 | + |
| 147 | + def test_clean_arithmetic_value_with_string(self): |
| 148 | + """Test _clean_arithmetic_value with string input.""" |
| 149 | + query = WOQLQuery() |
| 150 | + |
| 151 | + # Test with a numeric string |
| 152 | + result = query._clean_arithmetic_value("42", "xsd:decimal") |
| 153 | + |
| 154 | + assert isinstance(result, dict) |
| 155 | + assert "@type" in result |
| 156 | + |
| 157 | + def test_clean_arithmetic_value_with_number(self): |
| 158 | + """Test _clean_arithmetic_value with numeric input.""" |
| 159 | + query = WOQLQuery() |
| 160 | + |
| 161 | + # Test with a number |
| 162 | + result = query._clean_arithmetic_value(42, "xsd:integer") |
| 163 | + |
| 164 | + assert isinstance(result, dict) |
| 165 | + assert "@type" in result |
| 166 | + |
| 167 | + |
| 168 | +class TestWOQLDocAndVarsClasses: |
| 169 | + """Test Doc and Vars classes for uncovered lines.""" |
| 170 | + |
| 171 | + def test_doc_with_none_value(self): |
| 172 | + """Test Doc class handles None values.""" |
| 173 | + doc = Doc({"key": None}) |
| 174 | + |
| 175 | + assert doc.encoded["@type"] == "Value" |
| 176 | + assert "dictionary" in doc.encoded |
| 177 | + |
| 178 | + def test_doc_with_nested_dict(self): |
| 179 | + """Test Doc class handles nested dictionaries.""" |
| 180 | + doc = Doc({"outer": {"inner": "value"}}) |
| 181 | + |
| 182 | + assert isinstance(doc.encoded, dict) |
| 183 | + assert doc.encoded["@type"] == "Value" |
| 184 | + |
| 185 | + def test_doc_with_empty_list(self): |
| 186 | + """Test Doc class handles empty lists.""" |
| 187 | + doc = Doc({"items": []}) |
| 188 | + |
| 189 | + assert doc.encoded["@type"] == "Value" |
| 190 | + # Check that structure is created |
| 191 | + assert "dictionary" in doc.encoded |
| 192 | + |
| 193 | + def test_vars_creates_multiple_variables(self): |
| 194 | + """Test Vars class creates multiple Var instances.""" |
| 195 | + vars_obj = Vars("var1", "var2", "var3") |
| 196 | + |
| 197 | + assert hasattr(vars_obj, "var1") |
| 198 | + assert hasattr(vars_obj, "var2") |
| 199 | + assert hasattr(vars_obj, "var3") |
| 200 | + assert isinstance(vars_obj.var1, Var) |
| 201 | + assert isinstance(vars_obj.var2, Var) |
| 202 | + assert isinstance(vars_obj.var3, Var) |
| 203 | + |
| 204 | + def test_var_to_dict_format(self): |
| 205 | + """Test Var to_dict returns correct format.""" |
| 206 | + var = Var("test_var") |
| 207 | + result = var.to_dict() |
| 208 | + |
| 209 | + assert result["@type"] == "Value" |
| 210 | + assert result["variable"] == "test_var" |
0 commit comments