|
| 1 | +""" |
| 2 | +Unit tests for WOQL slice operator |
| 3 | +
|
| 4 | +Tests the Python client binding for slice(input_list, result, start, end=None) |
| 5 | +""" |
| 6 | + |
| 7 | +from terminusdb_client.woqlquery.woql_query import WOQLQuery |
| 8 | + |
| 9 | +from .woqljson.woqlSliceJson import WOQL_SLICE_JSON |
| 10 | + |
| 11 | + |
| 12 | +class TestWOQLSlice: |
| 13 | + """Test cases for the slice operator""" |
| 14 | + |
| 15 | + def test_basic_slice(self): |
| 16 | + """Basic slicing - slice([a,b,c,d], result, 1, 3) returns [b,c]""" |
| 17 | + woql_object = WOQLQuery().slice(["a", "b", "c", "d"], "v:Result", 1, 3) |
| 18 | + assert woql_object.to_dict() == WOQL_SLICE_JSON["basicSlice"] |
| 19 | + |
| 20 | + def test_negative_indices(self): |
| 21 | + """Negative indices - slice([a,b,c,d], result, -2, -1) returns [c]""" |
| 22 | + woql_object = WOQLQuery().slice(["a", "b", "c", "d"], "v:Result", -2, -1) |
| 23 | + assert woql_object.to_dict() == WOQL_SLICE_JSON["negativeIndices"] |
| 24 | + |
| 25 | + def test_without_end(self): |
| 26 | + """Optional end - slice([a,b,c,d], result, 1) returns [b,c,d]""" |
| 27 | + woql_object = WOQLQuery().slice(["a", "b", "c", "d"], "v:Result", 1) |
| 28 | + assert woql_object.to_dict() == WOQL_SLICE_JSON["withoutEnd"] |
| 29 | + |
| 30 | + def test_variable_list(self): |
| 31 | + """Variable list input - slice(v:MyList, result, 0, 2)""" |
| 32 | + woql_object = WOQLQuery().slice("v:MyList", "v:Result", 0, 2) |
| 33 | + assert woql_object.to_dict() == WOQL_SLICE_JSON["variableList"] |
| 34 | + |
| 35 | + def test_variable_indices(self): |
| 36 | + """Variable indices - slice([x,y,z], result, v:Start, v:End)""" |
| 37 | + woql_object = WOQLQuery().slice(["x", "y", "z"], "v:Result", "v:Start", "v:End") |
| 38 | + assert woql_object.to_dict() == WOQL_SLICE_JSON["variableIndices"] |
| 39 | + |
| 40 | + def test_empty_list(self): |
| 41 | + """Empty list - slice([], result, 0, 1) returns []""" |
| 42 | + woql_object = WOQLQuery().slice([], "v:Result", 0, 1) |
| 43 | + assert woql_object.to_dict() == WOQL_SLICE_JSON["emptyList"] |
| 44 | + |
| 45 | + def test_slice_type(self): |
| 46 | + """Verify the @type is correctly set to 'Slice'""" |
| 47 | + woql_object = WOQLQuery().slice(["a", "b"], "v:Result", 0, 1) |
| 48 | + assert woql_object.to_dict()["@type"] == "Slice" |
| 49 | + |
| 50 | + def test_chaining_with_and(self): |
| 51 | + """Test that slice works with method chaining via woql_and""" |
| 52 | + woql_object = WOQLQuery().woql_and( |
| 53 | + WOQLQuery().eq("v:MyList", ["a", "b", "c"]), |
| 54 | + WOQLQuery().slice("v:MyList", "v:Result", 1, 3), |
| 55 | + ) |
| 56 | + result = woql_object.to_dict() |
| 57 | + assert result["@type"] == "And" |
| 58 | + assert len(result["and"]) == 2 |
| 59 | + assert result["and"][1]["@type"] == "Slice" |
| 60 | + |
| 61 | + def test_single_element_slice(self): |
| 62 | + """Single element - slice([a,b,c,d], result, 1, 2) returns [b]""" |
| 63 | + woql_object = WOQLQuery().slice(["a", "b", "c", "d"], "v:Result", 1, 2) |
| 64 | + result = woql_object.to_dict() |
| 65 | + assert result["@type"] == "Slice" |
| 66 | + assert result["start"]["data"]["@value"] == 1 |
| 67 | + assert result["end"]["data"]["@value"] == 2 |
| 68 | + |
| 69 | + def test_full_range(self): |
| 70 | + """Full range - slice([a,b,c,d], result, 0, 4) returns [a,b,c,d]""" |
| 71 | + woql_object = WOQLQuery().slice(["a", "b", "c", "d"], "v:Result", 0, 4) |
| 72 | + result = woql_object.to_dict() |
| 73 | + assert result["@type"] == "Slice" |
| 74 | + assert result["start"]["data"]["@value"] == 0 |
| 75 | + assert result["end"]["data"]["@value"] == 4 |
0 commit comments