Ejemplo n.º 1
0
class TestFactorGraphCreation(unittest.TestCase):
    def setUp(self):
        self.graph = FactorGraph()

    def test_class_init_without_data(self):
        self.assertIsInstance(self.graph, FactorGraph)

    def test_class_init_data_string(self):
        self.graph = FactorGraph([('a', 'phi1'), ('b', 'phi1')])
        self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'phi1'])
        self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
                             [['a', 'phi1'], ['b', 'phi1']])

    def test_add_single_node(self):
        self.graph.add_node('phi1')
        self.assertEqual(self.graph.nodes(), ['phi1'])

    def test_add_multiple_nodes(self):
        self.graph.add_nodes_from(['a', 'b', 'phi1'])
        self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'phi1'])

    def test_add_single_edge(self):
        self.graph.add_edge('a', 'phi1')
        self.assertListEqual(sorted(self.graph.nodes()), ['a', 'phi1'])
        self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
                             [['a', 'phi1']])

    def test_add_multiple_edges(self):
        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1')])
        self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'phi1'])
        self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
                             [['a', 'phi1'], ['b', 'phi1']])

    def test_add_self_loop_raises_error(self):
        self.assertRaises(ValueError, self.graph.add_edge, 'a', 'a')

    def test_add_edge_between_variable_nodes_raises_error(self):
        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1')])
        self.assertRaises(ValueError, self.graph.add_edge, 'a', 'b')

    def tearDown(self):
        del self.graph
Ejemplo n.º 2
0
class TestFactorGraphCreation(unittest.TestCase):
    def setUp(self):
        self.graph = FactorGraph()

    def test_class_init_without_data(self):
        self.assertIsInstance(self.graph, FactorGraph)

    def test_class_init_data_string(self):
        self.graph = FactorGraph([("a", "phi1"), ("b", "phi1")])
        self.assertListEqual(sorted(self.graph.nodes()), ["a", "b", "phi1"])
        self.assertListEqual(
            hf.recursive_sorted(self.graph.edges()), [["a", "phi1"], ["b", "phi1"]]
        )

    def test_add_single_node(self):
        self.graph.add_node("phi1")
        self.assertEqual(list(self.graph.nodes()), ["phi1"])

    def test_add_multiple_nodes(self):
        self.graph.add_nodes_from(["a", "b", "phi1"])
        self.assertListEqual(sorted(self.graph.nodes()), ["a", "b", "phi1"])

    def test_add_single_edge(self):
        self.graph.add_edge("a", "phi1")
        self.assertListEqual(sorted(self.graph.nodes()), ["a", "phi1"])
        self.assertListEqual(hf.recursive_sorted(self.graph.edges()), [["a", "phi1"]])

    def test_add_multiple_edges(self):
        self.graph.add_edges_from([("a", "phi1"), ("b", "phi1")])
        self.assertListEqual(sorted(self.graph.nodes()), ["a", "b", "phi1"])
        self.assertListEqual(
            hf.recursive_sorted(self.graph.edges()), [["a", "phi1"], ["b", "phi1"]]
        )

    def test_add_self_loop_raises_error(self):
        self.assertRaises(ValueError, self.graph.add_edge, "a", "a")

    def tearDown(self):
        del self.graph