def test_fireNode(self): """ Generates a list of nodes and weights, and a test node and their initial value. Then adds the list as successors to the node and calls fire. Checks that all values are calculated correctly. Also checks that firing multiple times sums the values. :return: """ # TODO: check for transformation function value_list = [self.rand.uniform(0, 1) for _ in range(100)] node_list = [(Node(), Fraction.from_float(val)) for val in value_list] test_node = Node(float(Fraction(1, 2))) test_node.add_successors(node_list) # Creates a copy of the node list and manually calculates their values # after firing the test node fired_node_list = node_list.copy() for node, value in fired_node_list: node.add_value(0.5 * value) test_node.fire() self.assertListEqual(node_list, fired_node_list) # manually calculates values again for node, value in fired_node_list: node.add_value(0.5 * value) test_node.fire() self.assertListEqual(node_list, fired_node_list)
def test_addSuccessors(self): """ Generates a list of nodes, appends it to a test node and checks, if add_successor was called for every node. :return: """ test_node = Node() test_node.add_successor = MagicMock() node_list = [(Node(), float(Fraction(0))) for _ in range(10)] test_node.add_successors(node_list) for elem in node_list: self.assertIn((elem,), test_node.add_successor.call_args_list)
def test_addSuccessorsOfWhichOneAlreadyExists(self): """ Generates a list of nodes and duplicates some of them. Then checks, if appending the list to a node raises an Exception. :return: """ test_node = Node() duplicate_node = Node() node_list = [(duplicate_node, float(Fraction(0)))] node_list.extend([(Node(), float(Fraction(0))) for _ in range(10)]) node_list.append((duplicate_node, float(Fraction(0)))) with self.assertRaises(Exception): test_node.add_successors(node_list)