def test_weird_specs(self): spec = ['a->a->a->a b '] nds, eds = GraphParser(spec).parse() self.assertSetEqual(nds, {'a', 'b'}) self.assertSetEqual(eds, {Edge('a', 'a')}) spec = ['a b', 'c'] nds, eds = GraphParser(spec).parse() self.assertSetEqual(nds, {'a', 'b', 'c'}) self.assertSetEqual(eds, set()) spec = ['a->b c->b'] nds, eds = GraphParser(spec).parse() self.assertSetEqual(nds, {'a', 'b', 'c'}) self.assertSetEqual(eds, {Edge('a', 'b'), Edge('c', 'b')})
def path_helper(self, spec, s, t, expected): nodes, edges = GraphParser(spec).parse() ga = GraphAnalyzer(nodes, edges) ga._all_paths(s, t) expected = set([','.join(p) for p in expected]) actual = set([','.join(p) for p in ga.paths]) self.assertSetEqual(actual, expected)
def test_construct_paths(self): # normal spec = ['a->b->c', 'b->d'] expected = [['a', 'b', 'c'], ['a', 'b', 'd']] self.cp_helper(spec, expected) # single node spec = ['a'] expected = [['a']] self.cp_helper(spec, expected) # multiple sources and targets spec = ['a->b->c', 'a2->b->c2'] expected = [['a', 'b', 'c'], ['a2', 'b', 'c2'], ['a', 'b', 'c2'], ['a2', 'b', 'c']] self.cp_helper(spec, expected) # disconnected spec = ['a->b->c', 'e->f'] expected = [['a', 'b', 'c'], ['e', 'f']] self.cp_helper(spec, expected) # cyclic spec = ['a->b->c->a'] nodes, edges = GraphParser(spec).parse() ga = GraphAnalyzer(nodes, edges) with self.assertRaises(InvalidGraphError): ga._construct_paths()
def cp_helper(self, spec, expected): nodes, edges = GraphParser(spec).parse() ga = GraphAnalyzer(nodes, edges) ga._construct_paths() expected = set([','.join(p) for p in expected]) actual = set([','.join(p) for p in ga.paths]) self.assertSetEqual(actual, expected)
def test_syntax_error(self): spec = ['my_first_node -> my_second_node'] nds, eds = GraphParser(spec).parse() self.assertSetEqual(nds, {'my_first_node', 'my_second_node'}) self.assertSetEqual(eds, {Edge('my_first_node', 'my_second_node')}) spec = ['_start -> _end'] with self.assertRaisesRegex(ParseError, '(?i)cannot handle character'): GraphParser(spec).parse() spec = ['-> B'] with self.assertRaisesRegex(ParseError, '(?i)source node'): GraphParser(spec).parse() spec = ['A -> B ->'] with self.assertRaisesRegex(ParseError, '(?i)target node'): GraphParser(spec).parse() spec = ['A - B'] with self.assertRaises(ParseError): GraphParser(spec).parse() spec = ['A->B->C, B->D'] with self.assertRaises(ParseError): GraphParser(spec).parse()
def source_helper(self, spec, exp_source, exp_target): nodes, edges = GraphParser(spec).parse() ga = GraphAnalyzer(nodes, edges) self.assertSetEqual(ga._get_source(), exp_source) self.assertSetEqual(ga._get_target(), exp_target)
def test_good_specs(self): spec = ['A -> B -> C1', 'B->C2'] nodes, edges = GraphParser(spec).parse() self.assertSetEqual(nodes, {'A', 'B', 'C1', 'C2'}) exp_edges = {Edge('A', 'B'), Edge('B', 'C1'), Edge('B', 'C2')} self.assertSetEqual(edges, exp_edges)