|
| 1 | +""" |
| 2 | +Integration tests for WOQL RangeMin and RangeMax predicates. |
| 3 | +
|
| 4 | +Tests finding minimum and maximum values in lists: |
| 5 | +- Integer lists |
| 6 | +- Date lists |
| 7 | +- Single element |
| 8 | +- Empty list |
| 9 | +- Equal elements |
| 10 | +""" |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from terminusdb_client import Client |
| 15 | +from terminusdb_client.woqlquery.woql_query import WOQLQuery |
| 16 | + |
| 17 | +test_user_agent = "terminusdb-client-python-tests" |
| 18 | + |
| 19 | + |
| 20 | +def intv(v): |
| 21 | + """Build an xsd:integer typed literal.""" |
| 22 | + return {"@type": "xsd:integer", "@value": v} |
| 23 | + |
| 24 | + |
| 25 | +def datv(v): |
| 26 | + """Build an xsd:date typed literal.""" |
| 27 | + return {"@type": "xsd:date", "@value": v} |
| 28 | + |
| 29 | + |
| 30 | +class TestRangeMin: |
| 31 | + """Integration tests for RangeMin.""" |
| 32 | + |
| 33 | + @pytest.fixture(autouse=True) |
| 34 | + def setup_teardown(self, docker_url): |
| 35 | + self.client = Client(docker_url, user_agent=test_user_agent) |
| 36 | + self.client.connect() |
| 37 | + self.db_name = "test_range_min" |
| 38 | + if self.db_name in self.client.list_databases(): |
| 39 | + self.client.delete_database(self.db_name) |
| 40 | + self.client.create_database(self.db_name) |
| 41 | + yield |
| 42 | + self.client.delete_database(self.db_name) |
| 43 | + |
| 44 | + def test_min_integers(self): |
| 45 | + """Minimum of [7, 2, 9, 1, 5] is 1.""" |
| 46 | + query = WOQLQuery().range_min( |
| 47 | + [intv(7), intv(2), intv(9), intv(1), intv(5)], "v:m" |
| 48 | + ) |
| 49 | + result = self.client.query(query) |
| 50 | + assert len(result["bindings"]) == 1 |
| 51 | + assert result["bindings"][0]["m"]["@value"] == 1 |
| 52 | + |
| 53 | + def test_min_single_element(self): |
| 54 | + """Single element list returns that element.""" |
| 55 | + query = WOQLQuery().range_min([intv(42)], "v:m") |
| 56 | + result = self.client.query(query) |
| 57 | + assert len(result["bindings"]) == 1 |
| 58 | + assert result["bindings"][0]["m"]["@value"] == 42 |
| 59 | + |
| 60 | + def test_min_empty_list(self): |
| 61 | + """Empty list yields no bindings.""" |
| 62 | + query = WOQLQuery().range_min([], "v:m") |
| 63 | + result = self.client.query(query) |
| 64 | + assert len(result["bindings"]) == 0 |
| 65 | + |
| 66 | + def test_min_dates(self): |
| 67 | + """Minimum of dates.""" |
| 68 | + query = WOQLQuery().range_min( |
| 69 | + [datv("2024-06-15"), datv("2024-01-01"), datv("2024-03-01")], "v:m" |
| 70 | + ) |
| 71 | + result = self.client.query(query) |
| 72 | + assert len(result["bindings"]) == 1 |
| 73 | + assert result["bindings"][0]["m"]["@value"] == "2024-01-01" |
| 74 | + |
| 75 | + def test_min_equal_elements(self): |
| 76 | + """All equal elements returns that element.""" |
| 77 | + query = WOQLQuery().range_min([intv(3), intv(3), intv(3)], "v:m") |
| 78 | + result = self.client.query(query) |
| 79 | + assert len(result["bindings"]) == 1 |
| 80 | + assert result["bindings"][0]["m"]["@value"] == 3 |
| 81 | + |
| 82 | + |
| 83 | +class TestRangeMax: |
| 84 | + """Integration tests for RangeMax.""" |
| 85 | + |
| 86 | + @pytest.fixture(autouse=True) |
| 87 | + def setup_teardown(self, docker_url): |
| 88 | + self.client = Client(docker_url, user_agent=test_user_agent) |
| 89 | + self.client.connect() |
| 90 | + self.db_name = "test_range_max" |
| 91 | + if self.db_name in self.client.list_databases(): |
| 92 | + self.client.delete_database(self.db_name) |
| 93 | + self.client.create_database(self.db_name) |
| 94 | + yield |
| 95 | + self.client.delete_database(self.db_name) |
| 96 | + |
| 97 | + def test_max_integers(self): |
| 98 | + """Maximum of [7, 2, 9, 1, 5] is 9.""" |
| 99 | + query = WOQLQuery().range_max( |
| 100 | + [intv(7), intv(2), intv(9), intv(1), intv(5)], "v:m" |
| 101 | + ) |
| 102 | + result = self.client.query(query) |
| 103 | + assert len(result["bindings"]) == 1 |
| 104 | + assert result["bindings"][0]["m"]["@value"] == 9 |
| 105 | + |
| 106 | + def test_max_dates(self): |
| 107 | + """Maximum of dates.""" |
| 108 | + query = WOQLQuery().range_max( |
| 109 | + [datv("2024-06-15"), datv("2024-01-01"), datv("2024-03-01")], "v:m" |
| 110 | + ) |
| 111 | + result = self.client.query(query) |
| 112 | + assert len(result["bindings"]) == 1 |
| 113 | + assert result["bindings"][0]["m"]["@value"] == "2024-06-15" |
0 commit comments