|
| 1 | +"""Unit tests for pyTigerGraph.common.gsql helper functions.""" |
| 2 | + |
| 3 | +import unittest |
| 4 | + |
| 5 | +from pyTigerGraph.common.gsql import ( |
| 6 | + _wrap_gsql_result, |
| 7 | + _parse_graph_list, |
| 8 | + _GSQL_ERROR_PATTERNS, |
| 9 | +) |
| 10 | +from pyTigerGraph.common.exception import TigerGraphException |
| 11 | + |
| 12 | + |
| 13 | +class TestWrapGsqlResult(unittest.TestCase): |
| 14 | + |
| 15 | + def test_success_message(self): |
| 16 | + result = _wrap_gsql_result("Successfully created graph G1") |
| 17 | + self.assertEqual(result, {"error": False, "message": "Successfully created graph G1"}) |
| 18 | + |
| 19 | + def test_empty_result(self): |
| 20 | + result = _wrap_gsql_result("") |
| 21 | + self.assertEqual(result, {"error": False, "message": ""}) |
| 22 | + |
| 23 | + def test_none_result(self): |
| 24 | + result = _wrap_gsql_result(None) |
| 25 | + self.assertEqual(result, {"error": False, "message": ""}) |
| 26 | + |
| 27 | + def test_error_raises_by_default(self): |
| 28 | + for pattern in _GSQL_ERROR_PATTERNS: |
| 29 | + with self.subTest(pattern=pattern): |
| 30 | + with self.assertRaises(TigerGraphException): |
| 31 | + _wrap_gsql_result(f"Some output with {pattern} in it") |
| 32 | + |
| 33 | + def test_error_skipCheck_returns_dict(self): |
| 34 | + for pattern in _GSQL_ERROR_PATTERNS: |
| 35 | + with self.subTest(pattern=pattern): |
| 36 | + msg = f"Some output with {pattern} in it" |
| 37 | + result = _wrap_gsql_result(msg, skipCheck=True) |
| 38 | + self.assertTrue(result["error"]) |
| 39 | + self.assertEqual(result["message"], msg) |
| 40 | + |
| 41 | + def test_no_false_positive(self): |
| 42 | + clean_messages = [ |
| 43 | + "Successfully created query myQuery", |
| 44 | + "The query has been installed", |
| 45 | + "Schema changed successfully", |
| 46 | + "Graph dropped", |
| 47 | + "", |
| 48 | + ] |
| 49 | + for msg in clean_messages: |
| 50 | + with self.subTest(msg=msg): |
| 51 | + result = _wrap_gsql_result(msg) |
| 52 | + self.assertFalse(result["error"]) |
| 53 | + |
| 54 | + def test_specific_error_patterns(self): |
| 55 | + cases = [ |
| 56 | + ('Encountered "bad token" at line 1', True), |
| 57 | + ("SEMANTIC ERROR in query foo", True), |
| 58 | + ("Syntax Error: unexpected token", True), |
| 59 | + ("Failed to create schema", True), |
| 60 | + ("Vertex type 'Foo' does not exist", True), |
| 61 | + ("Edge type is not a valid identifier", True), |
| 62 | + ("Graph already exists in cluster", True), |
| 63 | + ("Invalid syntax near ';'", True), |
| 64 | + ("Query installed successfully", False), |
| 65 | + ] |
| 66 | + for msg, expect_error in cases: |
| 67 | + with self.subTest(msg=msg): |
| 68 | + if expect_error: |
| 69 | + with self.assertRaises(TigerGraphException): |
| 70 | + _wrap_gsql_result(msg) |
| 71 | + result = _wrap_gsql_result(msg, skipCheck=True) |
| 72 | + self.assertTrue(result["error"]) |
| 73 | + else: |
| 74 | + result = _wrap_gsql_result(msg) |
| 75 | + self.assertFalse(result["error"]) |
| 76 | + |
| 77 | + |
| 78 | +class TestParseGraphList(unittest.TestCase): |
| 79 | + |
| 80 | + def test_single_graph(self): |
| 81 | + output = "- Graph SocialNet(Person:v, KNOWS:e)" |
| 82 | + result = _parse_graph_list(output) |
| 83 | + self.assertEqual(len(result), 1) |
| 84 | + self.assertEqual(result[0]["GraphName"], "SocialNet") |
| 85 | + self.assertEqual(result[0]["VertexTypes"], ["Person"]) |
| 86 | + self.assertEqual(result[0]["EdgeTypes"], ["KNOWS"]) |
| 87 | + |
| 88 | + def test_multiple_graphs(self): |
| 89 | + output = ( |
| 90 | + "- Graph G1(V1:v, V2:v, E1:e)\n" |
| 91 | + "- Graph G2(Account:v, Transfer:e)\n" |
| 92 | + ) |
| 93 | + result = _parse_graph_list(output) |
| 94 | + self.assertEqual(len(result), 2) |
| 95 | + self.assertEqual(result[0]["GraphName"], "G1") |
| 96 | + self.assertEqual(result[0]["VertexTypes"], ["V1", "V2"]) |
| 97 | + self.assertEqual(result[0]["EdgeTypes"], ["E1"]) |
| 98 | + self.assertEqual(result[1]["GraphName"], "G2") |
| 99 | + self.assertEqual(result[1]["VertexTypes"], ["Account"]) |
| 100 | + self.assertEqual(result[1]["EdgeTypes"], ["Transfer"]) |
| 101 | + |
| 102 | + def test_empty_output(self): |
| 103 | + self.assertEqual(_parse_graph_list(""), []) |
| 104 | + self.assertEqual(_parse_graph_list(None), []) |
| 105 | + |
| 106 | + def test_graph_without_types(self): |
| 107 | + output = "- Graph EmptyGraph()" |
| 108 | + result = _parse_graph_list(output) |
| 109 | + self.assertEqual(len(result), 1) |
| 110 | + self.assertEqual(result[0]["GraphName"], "EmptyGraph") |
| 111 | + self.assertEqual(result[0]["VertexTypes"], []) |
| 112 | + self.assertEqual(result[0]["EdgeTypes"], []) |
| 113 | + |
| 114 | + def test_non_graph_lines_ignored(self): |
| 115 | + output = ( |
| 116 | + "Some header text\n" |
| 117 | + "- Graph G1(V:v)\n" |
| 118 | + "Other info\n" |
| 119 | + ) |
| 120 | + result = _parse_graph_list(output) |
| 121 | + self.assertEqual(len(result), 1) |
| 122 | + self.assertEqual(result[0]["GraphName"], "G1") |
| 123 | + |
| 124 | + def test_graph_star_ignored(self): |
| 125 | + output = "- Graph *\n- Graph RealGraph(V:v)" |
| 126 | + result = _parse_graph_list(output) |
| 127 | + self.assertEqual(len(result), 1) |
| 128 | + self.assertEqual(result[0]["GraphName"], "RealGraph") |
| 129 | + |
| 130 | + def test_multiple_vertex_edge_types(self): |
| 131 | + output = "- Graph Complex(Person:v, Product:v, Company:v, BOUGHT:e, WORKS_AT:e)" |
| 132 | + result = _parse_graph_list(output) |
| 133 | + self.assertEqual(len(result), 1) |
| 134 | + self.assertEqual(result[0]["VertexTypes"], ["Person", "Product", "Company"]) |
| 135 | + self.assertEqual(result[0]["EdgeTypes"], ["BOUGHT", "WORKS_AT"]) |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == '__main__': |
| 139 | + unittest.main() |
0 commit comments