|
| 1 | +from cog.torque import Graph |
| 2 | +import unittest |
| 3 | +import os |
| 4 | +import shutil |
| 5 | + |
| 6 | +DIR_NAME = "TorqueExtensionsTest" |
| 7 | + |
| 8 | + |
| 9 | +def ordered(obj): |
| 10 | + if isinstance(obj, dict): |
| 11 | + return sorted((k, ordered(v)) for k, v in list(obj.items())) |
| 12 | + if isinstance(obj, list): |
| 13 | + return sorted(ordered(x) for x in obj) |
| 14 | + else: |
| 15 | + return obj |
| 16 | + |
| 17 | + |
| 18 | +class TorqueExtensionsTest(unittest.TestCase): |
| 19 | + """ |
| 20 | + Tests for new Torque traversal methods: |
| 21 | + - both(): bidirectional traversal |
| 22 | + - is_(): filter to specific nodes |
| 23 | + - unique(): remove duplicates |
| 24 | + - limit(): limit results |
| 25 | + - skip(): skip results |
| 26 | + - back(): return to tagged position |
| 27 | + """ |
| 28 | + maxDiff = None |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def setUpClass(cls): |
| 32 | + if os.path.exists("/tmp/" + DIR_NAME): |
| 33 | + shutil.rmtree("/tmp/" + DIR_NAME) |
| 34 | + os.mkdir("/tmp/" + DIR_NAME) |
| 35 | + |
| 36 | + cls.g = Graph(graph_name="test_graph", cog_home=DIR_NAME) |
| 37 | + # Create a simple test graph: |
| 38 | + # alice -> bob -> charlie -> alice (cycle) |
| 39 | + # alice -> dani |
| 40 | + # bob has status "cool" |
| 41 | + cls.g.put("alice", "follows", "bob") |
| 42 | + cls.g.put("bob", "follows", "charlie") |
| 43 | + cls.g.put("charlie", "follows", "alice") |
| 44 | + cls.g.put("alice", "follows", "dani") |
| 45 | + cls.g.put("bob", "status", "cool") |
| 46 | + cls.g.put("dani", "status", "cool") |
| 47 | + print(">>> TorqueExtensionsTest setup complete.\n") |
| 48 | + |
| 49 | + # =========== both() tests =========== |
| 50 | + |
| 51 | + def test_both_follows_from_bob(self): |
| 52 | + """both() should return vertices connected by edges in either direction.""" |
| 53 | + result = self.g.v("bob").both("follows").all() |
| 54 | + ids = {r['id'] for r in result['result']} |
| 55 | + # bob follows charlie, alice follows bob |
| 56 | + self.assertIn("charlie", ids) |
| 57 | + self.assertIn("alice", ids) |
| 58 | + |
| 59 | + def test_both_no_predicate(self): |
| 60 | + """both() with no predicate should follow all edge types.""" |
| 61 | + result = self.g.v("bob").both().all() |
| 62 | + ids = {r['id'] for r in result['result']} |
| 63 | + # bob -> charlie (follows), alice -> bob (follows), bob -> cool (status) |
| 64 | + self.assertIn("charlie", ids) |
| 65 | + self.assertIn("alice", ids) |
| 66 | + self.assertIn("cool", ids) |
| 67 | + |
| 68 | + # =========== is_() tests =========== |
| 69 | + |
| 70 | + def test_is_single_node(self): |
| 71 | + """is_() should filter to only the specified node.""" |
| 72 | + result = self.g.v("alice").out("follows").is_("bob").all() |
| 73 | + self.assertEqual(len(result['result']), 1) |
| 74 | + self.assertEqual(result['result'][0]['id'], "bob") |
| 75 | + |
| 76 | + def test_is_multiple_nodes(self): |
| 77 | + """is_() should accept multiple nodes.""" |
| 78 | + result = self.g.v("alice").out("follows").is_("bob", "dani").all() |
| 79 | + ids = {r['id'] for r in result['result']} |
| 80 | + self.assertEqual(ids, {"bob", "dani"}) |
| 81 | + |
| 82 | + def test_is_no_match(self): |
| 83 | + """is_() should return empty if no nodes match.""" |
| 84 | + result = self.g.v("alice").out("follows").is_("nonexistent").all() |
| 85 | + self.assertEqual(result['result'], []) |
| 86 | + |
| 87 | + def test_is_with_list(self): |
| 88 | + """is_() should accept a list of nodes.""" |
| 89 | + result = self.g.v("alice").out("follows").is_(["bob", "dani"]).all() |
| 90 | + ids = {r['id'] for r in result['result']} |
| 91 | + self.assertEqual(ids, {"bob", "dani"}) |
| 92 | + |
| 93 | + # =========== unique() tests =========== |
| 94 | + |
| 95 | + def test_unique_removes_duplicates(self): |
| 96 | + """unique() should remove duplicate vertices.""" |
| 97 | + # Get all followers' statuses - "cool" appears twice (bob and dani) |
| 98 | + result_without_unique = self.g.v("alice").out("follows").out("status").all() |
| 99 | + result_with_unique = self.g.v("alice").out("follows").out("status").unique().all() |
| 100 | + |
| 101 | + # Without unique, we should have duplicates |
| 102 | + ids_without = [r['id'] for r in result_without_unique['result']] |
| 103 | + self.assertEqual(ids_without.count("cool"), 2) |
| 104 | + |
| 105 | + # With unique, no duplicates |
| 106 | + ids_with = [r['id'] for r in result_with_unique['result']] |
| 107 | + self.assertEqual(ids_with.count("cool"), 1) |
| 108 | + |
| 109 | + def test_unique_preserves_order(self): |
| 110 | + """unique() should preserve the order of first occurrence.""" |
| 111 | + result = self.g.v().unique().all() |
| 112 | + # Should have vertices in order of first appearance |
| 113 | + self.assertTrue(len(result['result']) > 0) |
| 114 | + |
| 115 | + # =========== limit() tests =========== |
| 116 | + |
| 117 | + def test_limit_returns_n_results(self): |
| 118 | + """limit() should return at most N vertices.""" |
| 119 | + result = self.g.v().limit(2).all() |
| 120 | + self.assertEqual(len(result['result']), 2) |
| 121 | + |
| 122 | + def test_limit_more_than_available(self): |
| 123 | + """limit() with N larger than result set should return all.""" |
| 124 | + all_result = self.g.v().all() |
| 125 | + limited_result = self.g.v().limit(1000).all() |
| 126 | + self.assertEqual(len(all_result['result']), len(limited_result['result'])) |
| 127 | + |
| 128 | + def test_limit_zero(self): |
| 129 | + """limit(0) should return empty.""" |
| 130 | + result = self.g.v().limit(0).all() |
| 131 | + self.assertEqual(result['result'], []) |
| 132 | + |
| 133 | + # =========== skip() tests =========== |
| 134 | + |
| 135 | + def test_skip_skips_n_results(self): |
| 136 | + """skip() should skip the first N vertices.""" |
| 137 | + all_result = self.g.v().all() |
| 138 | + skipped_result = self.g.v().skip(2).all() |
| 139 | + self.assertEqual(len(skipped_result['result']), len(all_result['result']) - 2) |
| 140 | + |
| 141 | + def test_skip_more_than_available(self): |
| 142 | + """skip() with N larger than result set should return empty.""" |
| 143 | + result = self.g.v().skip(1000).all() |
| 144 | + self.assertEqual(result['result'], []) |
| 145 | + |
| 146 | + def test_limit_and_skip_pagination(self): |
| 147 | + """limit() and skip() together enable pagination.""" |
| 148 | + all_result = self.g.v().all() |
| 149 | + page1 = self.g.v().limit(2).all() |
| 150 | + page2 = self.g.v().skip(2).limit(2).all() |
| 151 | + |
| 152 | + # Pages should not overlap |
| 153 | + page1_ids = {r['id'] for r in page1['result']} |
| 154 | + page2_ids = {r['id'] for r in page2['result']} |
| 155 | + self.assertEqual(len(page1_ids & page2_ids), 0) |
| 156 | + |
| 157 | + # =========== back() tests =========== |
| 158 | + |
| 159 | + def test_back_returns_to_tagged_vertex(self): |
| 160 | + """back() should return to the previously tagged vertex.""" |
| 161 | + result = self.g.v("alice").tag("start").out("follows").back("start").all() |
| 162 | + # Should return to alice |
| 163 | + ids = {r['id'] for r in result['result']} |
| 164 | + self.assertEqual(ids, {"alice"}) |
| 165 | + |
| 166 | + def test_back_preserves_tags(self): |
| 167 | + """back() should preserve existing tags.""" |
| 168 | + result = self.g.v("alice").tag("origin").out("follows").tag("middle").back("origin").all() |
| 169 | + for r in result['result']: |
| 170 | + self.assertIn("origin", r) |
| 171 | + self.assertIn("middle", r) |
| 172 | + |
| 173 | + def test_back_with_invalid_tag(self): |
| 174 | + """back() with non-existent tag should return empty.""" |
| 175 | + result = self.g.v("alice").out("follows").back("nonexistent").all() |
| 176 | + self.assertEqual(result['result'], []) |
| 177 | + |
| 178 | + def test_back_after_filter(self): |
| 179 | + """back() should work with filtered results.""" |
| 180 | + result = self.g.v("alice").tag("start").out("follows").has("status", "cool").back("start").all() |
| 181 | + # Only bob has status cool, so we should get back to alice (who follows bob) |
| 182 | + ids = {r['id'] for r in result['result']} |
| 183 | + self.assertEqual(ids, {"alice"}) |
| 184 | + |
| 185 | + @classmethod |
| 186 | + def tearDownClass(cls): |
| 187 | + cls.g.close() |
| 188 | + shutil.rmtree("/tmp/" + DIR_NAME) |
| 189 | + print("*** TorqueExtensionsTest cleanup complete.") |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == '__main__': |
| 193 | + unittest.main() |
0 commit comments