| 1 | from graphrag_sdk.ontology import Ontology |
| 2 | from graphrag_sdk.entity import Entity |
| 3 | from graphrag_sdk.relation import Relation |
| 4 | from graphrag_sdk.helpers import ( |
| 5 | validate_cypher, |
| 6 | validate_cypher_entities_exist, |
| 7 | validate_cypher_relations_exist, |
| 8 | validate_cypher_relation_directions, |
| 9 | ) |
| 10 | import unittest |
| 11 | |
| 12 | |
| 13 | import logging |
| 14 | |
| 15 | logging.basicConfig(level=logging.DEBUG) |
| 16 | |
| 17 | |
| 18 | class TestValidateCypher1(unittest.TestCase): |
| 19 | """ |
| 20 | Test a valid cypher query |
| 21 | """ |
| 22 | |
| 23 | cypher = """ |
| 24 | MATCH (f:Fighter)-[r:FOUGHT_IN]->(fight:Fight) |
| 25 | RETURN f, count(fight) AS fight_count |
| 26 | ORDER BY fight_count DESC |
| 27 | LIMIT 1""" |
| 28 | |
| 29 | @classmethod |
| 30 | def setUpClass(cls): |
| 31 | |
| 32 | cls._ontology = Ontology() |
| 33 | |
| 34 | cls._ontology.add_entity( |
| 35 | Entity( |
| 36 | label="Fighter", |
| 37 | attributes=[], |
| 38 | ) |
| 39 | ) |
| 40 | |
| 41 | cls._ontology.add_entity( |
| 42 | Entity( |
| 43 | label="Fight", |
| 44 | attributes=[], |
| 45 | ) |
| 46 | ) |
| 47 | |
| 48 | cls._ontology.add_relation( |
| 49 | Relation( |
| 50 | label="FOUGHT_IN", |
| 51 | source="Fighter", |
| 52 | target="Fight", |
| 53 | attributes=[], |
| 54 | ) |
| 55 | ) |
| 56 | |
| 57 | def test_validate_cypher_entities_exist(self): |
| 58 | |
| 59 | errors = validate_cypher_entities_exist(self.cypher, self._ontology) |
| 60 | |
| 61 | assert len(errors) == 0 |
| 62 | |
| 63 | def test_validate_cypher_relations_exist(self): |
| 64 | |
| 65 | errors = validate_cypher_relations_exist(self.cypher, self._ontology) |
| 66 | |
| 67 | assert len(errors) == 0 |
| 68 | |
| 69 | def test_validate_cypher_relation_directions(self): |
| 70 | |
| 71 | errors = validate_cypher_relation_directions(self.cypher, self._ontology) |
| 72 | |
| 73 | assert len(errors) == 0 |
| 74 | |
| 75 | def test_validate_cypher(self): |
| 76 | errors = validate_cypher(self.cypher, self._ontology) |
| 77 | |
| 78 | assert errors is None |
| 79 | |
| 80 | |
| 81 | class TestValidateCypher2(unittest.TestCase): |
| 82 | """ |
| 83 | Test a cypher query with the wrong relation direction |
| 84 | """ |
| 85 | |
| 86 | cypher = """ |
| 87 | MATCH (f:Fighter)<-[r:FOUGHT_IN]-(fight:Fight) |
| 88 | RETURN f""" |
| 89 | |
| 90 | @classmethod |
| 91 | def setUpClass(cls): |
| 92 | |
| 93 | cls._ontology = Ontology([], []) |
| 94 | |
| 95 | cls._ontology.add_entity( |
| 96 | Entity( |
| 97 | label="Fighter", |
| 98 | attributes=[], |
| 99 | ) |
| 100 | ) |
| 101 | |
| 102 | cls._ontology.add_entity( |
| 103 | Entity( |
| 104 | label="Fight", |
| 105 | attributes=[], |
| 106 | ) |
| 107 | ) |
| 108 | |
| 109 | cls._ontology.add_relation( |
| 110 | Relation( |
| 111 | label="FOUGHT_IN", |
| 112 | source="Fighter", |
| 113 | target="Fight", |
| 114 | attributes=[], |
| 115 | ) |
| 116 | ) |
| 117 | |
| 118 | def test_validate_cypher_entities_exist(self): |
| 119 | |
| 120 | errors = validate_cypher_entities_exist(self.cypher, self._ontology) |
| 121 | |
| 122 | assert len(errors) == 0 |
| 123 | |
| 124 | def test_validate_cypher_relations_exist(self): |
| 125 | |
| 126 | errors = validate_cypher_relations_exist(self.cypher, self._ontology) |
| 127 | |
| 128 | assert len(errors) == 0 |
| 129 | |
| 130 | def test_validate_cypher_relation_directions(self): |
| 131 | |
| 132 | errors = validate_cypher_relation_directions(self.cypher, self._ontology) |
| 133 | |
| 134 | assert len(errors) == 1 |
| 135 | |
| 136 | def test_validate_cypher(self): |
| 137 | errors = validate_cypher(self.cypher, self._ontology) |
| 138 | |
| 139 | assert errors is not None |
| 140 | |
| 141 | |
| 142 | class TestValidateCypher3(unittest.TestCase): |
| 143 | """ |
| 144 | Test a cypher query with multiple right edge directions |
| 145 | """ |
| 146 | |
| 147 | cypher = """ |
| 148 | MATCH (a:Airline)-[:ACCEPTS]->(p:Pet), (r:Route)-[:ALLOWS]->(sd:Service_Dog) |
| 149 | RETURN a, p, r, sd |
| 150 | """ |
| 151 | |
| 152 | @classmethod |
| 153 | def setUpClass(cls): |
| 154 | |
| 155 | cls._ontology = Ontology([], []) |
| 156 | |
| 157 | cls._ontology.add_entity( |
| 158 | Entity( |
| 159 | label="Airline", |
| 160 | attributes=[], |
| 161 | ) |
| 162 | ) |
| 163 | |
| 164 | cls._ontology.add_entity( |
| 165 | Entity( |
| 166 | label="Pet", |
| 167 | attributes=[], |
| 168 | ) |
| 169 | ) |
| 170 | |
| 171 | cls._ontology.add_entity( |
| 172 | Entity( |
| 173 | label="Route", |
| 174 | attributes=[], |
| 175 | ) |
| 176 | ) |
| 177 | |
| 178 | cls._ontology.add_entity( |
| 179 | Entity( |
| 180 | label="Service_Dog", |
| 181 | attributes=[], |
| 182 | ) |
| 183 | ) |
| 184 | |
| 185 | cls._ontology.add_relation( |
| 186 | Relation( |
| 187 | label="ACCEPTS", |
| 188 | source="Airline", |
| 189 | target="Pet", |
| 190 | attributes=[], |
| 191 | ) |
| 192 | ) |
| 193 | |
| 194 | cls._ontology.add_relation( |
| 195 | Relation( |
| 196 | label="ALLOWS", |
| 197 | source="Route", |
| 198 | target="Service_Dog", |
| 199 | attributes=[], |
| 200 | ) |
| 201 | ) |
| 202 | |
| 203 | def test_validate_cypher_nodes_exist(self): |
| 204 | |
| 205 | errors = validate_cypher_entities_exist(self.cypher, self._ontology) |
| 206 | |
| 207 | assert len(errors) == 0 |
| 208 | |
| 209 | def test_validate_cypher_edges_exist(self): |
| 210 | |
| 211 | errors = validate_cypher_relations_exist(self.cypher, self._ontology) |
| 212 | |
| 213 | assert len(errors) == 0 |
| 214 | |
| 215 | def test_validate_cypher_edge_directions(self): |
| 216 | |
| 217 | errors = validate_cypher_relation_directions(self.cypher, self._ontology) |
| 218 | |
| 219 | assert len(errors) == 0 |
| 220 | |
| 221 | def test_validate_cypher(self): |
| 222 | errors = validate_cypher(self.cypher, self._ontology) |
| 223 | |
| 224 | assert errors is None or len(errors) == 0 |
| 225 | |
| 226 | |
| 227 | if __name__ == "__main__": |
| 228 | unittest.main() |