|
| 1 | +from common import * |
| 2 | + |
| 3 | +class testMathematicalOperationsFlow(FlowTestsBase): |
| 4 | + def __init__(self): |
| 5 | + self.env = Env(decodeResponses=True) |
| 6 | + self.redis_con = self.env.getConnection() |
| 7 | + self.graph = Graph(self.redis_con, "G") |
| 8 | + self.populate_graph() |
| 9 | + |
| 10 | + def populate_graph(self): |
| 11 | + people = ["Roi", "Alon", "Ailon", "Boaz"] |
| 12 | + nodes = {} |
| 13 | + # Create entities |
| 14 | + for idx, p in enumerate(people): |
| 15 | + if idx % 2 == 0: |
| 16 | + labels = ["person"] |
| 17 | + else: |
| 18 | + labels = ["person", "student"] |
| 19 | + node = Node(label=labels, properties={"name": p, "val": idx}) |
| 20 | + self.graph.add_node(node) |
| 21 | + nodes[p] = node |
| 22 | + |
| 23 | + # Fully connected graph |
| 24 | + for src in nodes: |
| 25 | + for dest in nodes: |
| 26 | + if src != dest: |
| 27 | + edge = Edge(nodes[src], "know", nodes[dest]) |
| 28 | + self.graph.add_edge(edge) |
| 29 | + |
| 30 | + for src in nodes: |
| 31 | + for dest in nodes: |
| 32 | + if src != dest: |
| 33 | + edge = Edge(nodes[src], "works_with", nodes[dest]) |
| 34 | + self.graph.add_edge(edge) |
| 35 | + |
| 36 | + self.graph.commit() |
| 37 | + query = """MATCH (a)-[:know]->(b) CREATE (a)-[:know]->(b)""" |
| 38 | + self.graph.query(query) |
| 39 | + |
| 40 | + def expect_error(self, query, expected_err_msg): |
| 41 | + try: |
| 42 | + self.graph.query(query) |
| 43 | + assert(False) |
| 44 | + except redis.exceptions.ResponseError as e: |
| 45 | + self.env.assertIn(expected_err_msg, str(e)) |
| 46 | + |
| 47 | + def expect_type_error(self, query): |
| 48 | + self.expect_error(query, "Type mismatch") |
| 49 | + |
| 50 | + # GitHub Issue #2291. |
| 51 | + # Ensure that a node can be added to a list. |
| 52 | + def test01_node_add_to_array(self): |
| 53 | + query = """MATCH (a) WITH collect(a) AS as RETURN as[0] + [true, 2, 3]""" |
| 54 | + actual_result = self.graph.query(query) |
| 55 | + expected_result = [Node(label='person', properties = {'name': 'Roi', 'val': 0}), True, 2, 3] |
| 56 | + self.env.assertEquals(actual_result.result_set[0][0], expected_result) |
| 57 | + |
| 58 | + # Ensure that an edge can be added to a list. |
| 59 | + def test02_edge_add_to_array(self): |
| 60 | + query = """MATCH (a)-[r]->(b) WITH collect(r) AS rs RETURN rs[0] + [true, 2, 3]""" |
| 61 | + actual_result = self.graph.query(query) |
| 62 | + expected_result = [True, 2, 3] |
| 63 | + self.env.assertTrue(isinstance(actual_result.result_set[0][0][0], Edge)) |
| 64 | + self.env.assertEquals(actual_result.result_set[0][0][1:], expected_result) |
| 65 | + |
| 66 | + # Ensure that a path can be added to a list. |
| 67 | + def test03_path_add_to_array(self): |
| 68 | + query = """MATCH path = (a)-[r]->(b) RETURN path + [true, 2, 3]""" |
| 69 | + actual_result = self.graph.query(query) |
| 70 | + expected_result = [True, 2, 3] |
| 71 | + self.env.assertTrue(isinstance(actual_result.result_set[0][0][0], Path)) |
| 72 | + self.env.assertEquals(actual_result.result_set[0][0][1:], expected_result) |
| 73 | + |
| 74 | + # Ensure that a point can be added to a list. |
| 75 | + def test04_point_add_to_array(self): |
| 76 | + query = """MATCH () RETURN point({latitude:1, longitude:2}) + [true, 2, 3] LIMIT 1""" |
| 77 | + actual_result = self.graph.query(query) |
| 78 | + expected_result = [{'latitude': 1, 'longitude': 2}, True, 2, 3] |
| 79 | + self.env.assertEquals(actual_result.result_set[0][0], expected_result) |
| 80 | + |
| 81 | + # Ensure that complex objects can't be used in an addition operation. |
| 82 | + def test05_adding_complex_objects_with_not_self_causes_error(self): |
| 83 | + args = [ |
| 84 | + '2', |
| 85 | + '2.0', |
| 86 | + 'true', |
| 87 | + 'timestamp()', |
| 88 | + '"string"', |
| 89 | + ] |
| 90 | + query_templates = [ |
| 91 | + """MATCH (a) RETURN a + %s LIMIT 1""", |
| 92 | + """MATCH (a) RETURN %s + a LIMIT 1""", |
| 93 | + """MATCH ()-[r]->() RETURN r + %s LIMIT 1""", |
| 94 | + """MATCH ()-[r]->() RETURN %s + r LIMIT 1""", |
| 95 | + """MATCH path = ()-[r]->() RETURN path + %s LIMIT 1""", |
| 96 | + """MATCH path = ()-[r]->() RETURN %s + path LIMIT 1""", |
| 97 | + """MATCH () RETURN point({latitude:1, longitude:2}) + %s LIMIT 1""", |
| 98 | + """MATCH () RETURN %s + point({latitude:1, longitude:2}) LIMIT 1""", |
| 99 | + ] |
| 100 | + for template in query_templates: |
| 101 | + for arg in args: |
| 102 | + self.expect_type_error(template % arg) |
0 commit comments