Best Python code snippet using autotest_python
test_graphlib.py
Source:test_graphlib.py
...7 def _test_graph(self, graph, expected):8 def static_order_with_groups(ts):9 ts.prepare()10 while ts.is_active():11 nodes = ts.get_ready()12 for node in nodes:13 ts.done(node)14 yield nodes15 ts = graphlib.TopologicalSorter(graph)16 self.assertEqual(list(static_order_with_groups(ts)), list(expected))17 ts = graphlib.TopologicalSorter(graph)18 self.assertEqual(list(ts.static_order()), list(chain(*expected)))19 def _assert_cycle(self, graph, cycle):20 ts = graphlib.TopologicalSorter()21 for node, dependson in graph.items():22 ts.add(node, *dependson)23 try:24 ts.prepare()25 except graphlib.CycleError as e:26 msg, seq = e.args27 self.assertIn(" ".join(map(str, cycle)), " ".join(map(str, seq * 2)))28 else:29 raise30 def test_simple_cases(self):31 self._test_graph(32 {2: {11}, 9: {11, 8}, 10: {11, 3}, 11: {7, 5}, 8: {7, 3}},33 [(3, 5, 7), (11, 8), (2, 10, 9)],34 )35 self._test_graph({1: {}}, [(1,)])36 self._test_graph(37 {x: {x + 1} for x in range(10)}, [(x,) for x in range(10, -1, -1)]38 )39 self._test_graph(40 {2: {3}, 3: {4}, 4: {5}, 5: {1}, 11: {12}, 12: {13}, 13: {14}, 14: {15}},41 [(1, 15), (5, 14), (4, 13), (3, 12), (2, 11)],42 )43 self._test_graph(44 {45 0: [1, 2],46 1: [3],47 2: [5, 6],48 3: [4],49 4: [9],50 5: [3],51 6: [7],52 7: [8],53 8: [4],54 9: [],55 },56 [(9,), (4,), (3, 8), (1, 5, 7), (6,), (2,), (0,)],57 )58 self._test_graph({0: [1, 2], 1: [], 2: [3], 3: []}, [(1, 3), (2,), (0,)])59 self._test_graph(60 {0: [1, 2], 1: [], 2: [3], 3: [], 4: [5], 5: [6], 6: []},61 [(1, 3, 6), (2, 5), (0, 4)],62 )63 def test_no_dependencies(self):64 self._test_graph({1: {2}, 3: {4}, 5: {6}}, [(2, 4, 6), (1, 3, 5)])65 self._test_graph({1: set(), 3: set(), 5: set()}, [(1, 3, 5)])66 def test_the_node_multiple_times(self):67 # Test same node multiple times in dependencies68 self._test_graph({1: {2}, 3: {4}, 0: [2, 4, 4, 4, 4, 4]}, [(2, 4), (1, 3, 0)])69 # Test adding the same dependency multiple times70 ts = graphlib.TopologicalSorter()71 ts.add(1, 2)72 ts.add(1, 2)73 ts.add(1, 2)74 self.assertEqual([*ts.static_order()], [2, 1])75 def test_graph_with_iterables(self):76 dependson = (2 * x + 1 for x in range(5))77 ts = graphlib.TopologicalSorter({0: dependson})78 self.assertEqual(list(ts.static_order()), [1, 3, 5, 7, 9, 0])79 def test_add_dependencies_for_same_node_incrementally(self):80 # Test same node multiple times81 ts = graphlib.TopologicalSorter()82 ts.add(1, 2)83 ts.add(1, 3)84 ts.add(1, 4)85 ts.add(1, 5)86 ts2 = graphlib.TopologicalSorter({1: {2, 3, 4, 5}})87 self.assertEqual([*ts.static_order()], [*ts2.static_order()])88 def test_empty(self):89 self._test_graph({}, [])90 def test_cycle(self):91 # Self cycle92 self._assert_cycle({1: {1}}, [1, 1])93 # Simple cycle94 self._assert_cycle({1: {2}, 2: {1}}, [1, 2, 1])95 # Indirect cycle96 self._assert_cycle({1: {2}, 2: {3}, 3: {1}}, [1, 3, 2, 1])97 # not all elements involved in a cycle98 self._assert_cycle({1: {2}, 2: {3}, 3: {1}, 5: {4}, 4: {6}}, [1, 3, 2, 1])99 # Multiple cycles100 self._assert_cycle({1: {2}, 2: {1}, 3: {4}, 4: {5}, 6: {7}, 7: {6}}, [1, 2, 1])101 # Cycle in the middle of the graph102 self._assert_cycle({1: {2}, 2: {3}, 3: {2, 4}, 4: {5}}, [3, 2])103 def test_calls_before_prepare(self):104 ts = graphlib.TopologicalSorter()105 with self.assertRaisesRegex(ValueError, r"prepare\(\) must be called first"):106 ts.get_ready()107 with self.assertRaisesRegex(ValueError, r"prepare\(\) must be called first"):108 ts.done(3)109 with self.assertRaisesRegex(ValueError, r"prepare\(\) must be called first"):110 ts.is_active()111 def test_prepare_multiple_times(self):112 ts = graphlib.TopologicalSorter()113 ts.prepare()114 with self.assertRaisesRegex(ValueError, r"cannot prepare\(\) more than once"):115 ts.prepare()116 def test_invalid_nodes_in_done(self):117 ts = graphlib.TopologicalSorter()118 ts.add(1, 2, 3, 4)119 ts.add(2, 3, 4)120 ts.prepare()121 ts.get_ready()122 with self.assertRaisesRegex(ValueError, "node 2 was not passed out"):123 ts.done(2)124 with self.assertRaisesRegex(ValueError, r"node 24 was not added using add\(\)"):125 ts.done(24)126 def test_done(self):127 ts = graphlib.TopologicalSorter()128 ts.add(1, 2, 3, 4)129 ts.add(2, 3)130 ts.prepare()131 self.assertEqual(ts.get_ready(), (3, 4))132 # If we don't mark anything as done, get_ready() returns nothing133 self.assertEqual(ts.get_ready(), ())134 ts.done(3)135 # Now 2 becomes available as 3 is done136 self.assertEqual(ts.get_ready(), (2,))137 self.assertEqual(ts.get_ready(), ())138 ts.done(4)139 ts.done(2)140 # Only 1 is missing141 self.assertEqual(ts.get_ready(), (1,))142 self.assertEqual(ts.get_ready(), ())143 ts.done(1)144 self.assertEqual(ts.get_ready(), ())145 self.assertFalse(ts.is_active())146 def test_is_active(self):147 ts = graphlib.TopologicalSorter()148 ts.add(1, 2)149 ts.prepare()150 self.assertTrue(ts.is_active())151 self.assertEqual(ts.get_ready(), (2,))152 self.assertTrue(ts.is_active())153 ts.done(2)154 self.assertTrue(ts.is_active())155 self.assertEqual(ts.get_ready(), (1,))156 self.assertTrue(ts.is_active())157 ts.done(1)158 self.assertFalse(ts.is_active())159 def test_not_hashable_nodes(self):160 ts = graphlib.TopologicalSorter()161 self.assertRaises(TypeError, ts.add, dict(), 1)162 self.assertRaises(TypeError, ts.add, 1, dict())163 self.assertRaises(TypeError, ts.add, dict(), dict())164 def test_order_of_insertion_does_not_matter_between_groups(self):165 def get_groups(ts):166 ts.prepare()167 while ts.is_active():168 nodes = ts.get_ready()169 ts.done(*nodes)170 yield set(nodes)171 ts = graphlib.TopologicalSorter()172 ts.add(3, 2, 1)173 ts.add(1, 0)174 ts.add(4, 5)175 ts.add(6, 7)176 ts.add(4, 7)177 ts2 = graphlib.TopologicalSorter()178 ts2.add(1, 0)179 ts2.add(3, 2, 1)180 ts2.add(4, 7)181 ts2.add(6, 7)182 ts2.add(4, 5)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!