def test_on_buitlin(cls, full_name): """ Tests get_full_class_name method :param cls: class :param full_name: full class name """ assert get_full_class_name(cls) == full_name
def test_passenger_generation(): """Tests passenger generation""" config_name = get_full_class_name(Config) with patch(config_name + '.graph_dict', new_callable=PropertyMock) as mock_graph_dict: with patch(config_name + '.lines_dict', new_callable=PropertyMock) as mock_lines_dict: with patch(config_name + '.traffic_data_dict', new_callable=PropertyMock) as mock_traffic_dict: mock_graph_dict.return_value = { 'A': [('B', 1)], 'B': [('A', 1)] } mock_lines_dict.return_value = { 0: { 'id': 0, 'bus_capacity': 1, 'frequency1': 10000000, 'frequency2': 1000000000, 'route1': ['B', 'A'], 'route2': ['A', 'B'] } } config = Config(["A", "B"], {}, {}, {}, 1.0) mock_traffic_dict.return_value = { 'A': { 'A': 0, 'B': 120 }, 'B': { 'A': 0, 'B': 0 } } generated = [] model = [] simulation = Simulation(config) print(config.lines_dict) simulation.refresh() simulation.refresh() for i in range(100000): simulation.refresh() if simulation.stops['A'].passengers: generated.append( simulation.stops['A'].passengers[0].count) for i in range(len(generated) - 1, 0, -1): generated[i] -= generated[i - 1] model.append(np.random.poisson(2)) model.append(np.random.poisson(2)) generated = np.sort(np.array(generated)) model = np.sort(np.array(model)) res = sum(np.abs(generated - model) != 0) assert res / len( generated) <= 0.02, " jak nie dziolo to odpalic od nowa i nie narzekac, " \ "bo tak naprawde dziala tylko czasem nie"
def test_graph_and_lines(self): """Tests simulation with graph and lines""" config_name = get_full_class_name(Config) with patch(config_name+'.graph_dict', new_callable=PropertyMock) as mock_graph_dict: with patch(config_name+'.lines_dict', new_callable=PropertyMock) as mock_lines_dict: with patch(config_name+'.traffic_data_dict', new_callable=PropertyMock) as mock_traffic_dict: mock_graph_dict.return_value = {'A': [('B', 7), ('D', 2)], 'B': [('A', 7), ('C', 1), ('E', 2)], 'C': [('B', 1), ('D', 3)], 'D': [('A', 2), ('C', 3)], 'E': [('B', 2), ('F', 2)], 'F': [('E', 2)]} mock_lines_dict.return_value = { 0: {'id': 0, 'bus_capacity': 20, 'frequency1': 17, 'frequency2': 17, 'route1': ['A', 'D', 'C', 'B', 'E', 'F'], 'route2': ['F', 'E', 'B', 'A']}} config = Config(["A", "B", "C", "D", "E", "F"], {}, {}, {}, 1.0) mock_traffic_dict.return_value = {'E': {'E': 0, 'F': 0, 'D': 0, 'A': 0, 'C': 0, 'B': 0}, 'F': {'E': 0, 'F': 0, 'D': 0, 'A': 0, 'C': 0, 'B': 0}, 'D': {'E': 0, 'F': 0, 'D': 0, 'A': 0, 'C': 0, 'B': 0}, 'A': {'E': 0, 'F': 0, 'D': 0, 'A': 0, 'C': 0, 'B': 0}, 'C': {'E': 0, 'F': 0, 'D': 0, 'A': 0, 'C': 0, 'B': 0}, 'B': {'E': 0, 'F': 0, 'D': 0, 'A': 0, 'C': 0, 'B': 0}} simulation = Simulation(config) def mocked_update(mocked_self): """Mocked update """ for bus in mocked_self.buses: if bus.route == 0: if bus.id not in mocked_self.mocked_dict.keys(): mocked_self.mocked_dict[bus.id] = [] if bus.time_to_next_stop == 0: mocked_self.mocked_dict[bus.id].append(bus.current_stop_name) else: mocked_self.mocked_dict[bus.id].append(bus.current_stop_name + bus.next_stop_name) def finished(mocked_self): mocked_self.mocked_update() return False add_property(simulation, "finished", finished) from types import MethodType simulation.mocked_update = MethodType(mocked_update, simulation) add_variable(simulation, "count_finished", 0) add_variable(simulation, "mocked_dict", {}) count = 0 while count < 35: count += 1 simulation.refresh() paths = ['PA', 'A', 'AD', 'AD', 'D', 'DC', 'DC', 'DC', 'C', 'CB', 'B', 'BE', 'BE', 'E', 'EF', 'EF', 'F'] self.assertEqual(len(simulation.mocked_dict), 2) for path in simulation.mocked_dict.values(): self.assertEqual(path, paths)
def test_coun_one_group(dest, count): """Tests if count is being calculated correctly""" stop = Stop("") name = get_full_class_name(PassengersGroup) with mock.patch(name + ".destination", new_callable=PropertyMock) as mocked_destination: mocked_destination.return_value = dest group = PassengersGroup("a", 0) group.count = count stop.passengers.append(group) assert stop.count(dest) == count
def test_graph_and_lines_transfer_2(self): """Tests simulation with graph and lines - duplication""" config_name = get_full_class_name(Config) with patch(config_name+'.graph_dict', new_callable=PropertyMock) as mock_graph_dict: with patch(config_name+'.lines_dict', new_callable=PropertyMock) as mock_lines_dict: with patch(config_name+'.traffic_data_dict', new_callable=PropertyMock) as mock_traffic_dict: class MockedGenerator: def __init__(self, empty_argument): self.done = False def generate(self, src, dest): if not self.done and src == 'C' and dest == 'F': self.done = True return 1 return 0 mock_graph_dict.return_value = {'A': [('B', 2), ('D', 2)], 'B': [('A', 2), ('C', 2), ('E', 2)], 'C': [('B', 2), ('D', 2)], 'D': [('A', 2), ('C', 2)], 'E': [('B', 2), ('F', 2)], 'F': [('E', 2)]} mock_lines_dict.return_value = { 0: {'id': 0, 'bus_capacity': 20, 'frequency1': 1000, 'frequency2': 1000, 'route1': ['B', 'A', 'D', 'C'], 'route2': ['C', 'D', 'A', 'B']}, 1: {'id': 1, 'bus_capacity': 20, 'frequency1': 1000, 'frequency2': 1000, 'route1': ['C', 'B', 'E', 'F'], 'route2': ['F', 'E', 'B', 'C']}} config = Config(["A", "B", "C", "D", "E", "F"], {}, {}, {}, 1.0) mock_traffic_dict.return_value = {'E': {'E': 0, 'F': 0, 'D': 0, 'A': 0, 'C': 0, 'B': 0}, 'F': {'E': 0, 'F': 0, 'D': 0, 'A': 0, 'C': 0, 'B': 0}, 'D': {'E': 0, 'F': 0, 'D': 0, 'A': 0, 'C': 0, 'B': 0}, 'A': {'E': 0, 'F': 0, 'D': 0, 'A': 0, 'C': 0, 'B': 0}, 'C': {'E': 0, 'F': 1, 'D': 0, 'A': 0, 'C': 0, 'B': 0}, 'B': {'E': 0, 'F': 0, 'D': 0, 'A': 0, 'C': 0, 'B': 0}} simulation = Simulation(config, MockedGenerator) simulation.refresh() self.are_lists_equal(simulation.stops['C'].passengers, [PassengersGroup('F', 1)], passenger_group_equality) simulation.refresh() self.are_lists_equal(simulation.stops['C'].passengers, [], passenger_group_equality) k = 0 for bus in simulation.buses: k += bus.count self.are_equal(k, 1)
def graph_mock_graph_dict(self, graph_dict_input): """ Tests assertions :param graph_dict_input: value returned from config graph_dict :param assertions: methods asserting graph :type assertions: list[lambda] :rtype: Graph :return: Filled graph """ name = get_full_class_name(Config) with mock.patch(name + ".graph_dict", new_callable=PropertyMock) as graph_dict: graph_dict.return_value = graph_dict_input graph = Graph.from_config(graph_dict_input) return graph
def test_bus_generation(): """Tests bus generation""" config_name = get_full_class_name(Config) with patch(config_name + '.graph_dict', new_callable=PropertyMock) as mock_graph_dict: with patch(config_name + '.lines_dict', new_callable=PropertyMock) as mock_lines_dict: with patch(config_name + '.traffic_data_dict', new_callable=PropertyMock) as mock_traffic_dict: mock_graph_dict.return_value = { 'A': [('B', 1)], 'B': [('A', 1)] } mock_lines_dict.return_value = { 0: { 'id': 0, 'bus_capacity': 20, 'frequency1': 10, 'frequency2': 1000, 'route1': ['B', 'A'], 'route2': ['A', 'B'] } } config = Config(["A", "B"], {}, {}, {}, 1.0) mock_traffic_dict.return_value = { 'A': { 'A': 0, 'B': 0 }, 'B': { 'A': 0, 'B': 0 } } simulation = Simulation(config) assert Bus.BUS_COUNTER == 0 simulation.refresh() simulation.refresh() assert Bus.BUS_COUNTER == 2 for i in range(1, 10): for _ in range(10): simulation.refresh() assert Bus.BUS_COUNTER == 2 + i
def test_on_buitlin(cls, type_name): """Checks if builtin types are recognized properly""" builtin = "builtins." if sys.version_info[0] >= 3 else "__builtin__." assert get_full_class_name(cls) == builtin + type_name
def get_group(count, name="!"): """ Creates PassengersGroup with parameters :param count: group count :param name: group name :return: Passengers Group :rtype: PassengersGroup """ group = PassengersGroup(name, count) group.name = name group.count = count return group LINE_NAME = get_full_class_name(Line) def test_create(): """ Checks if bus is created properly """ with mock.patch(LINE_NAME + ".routes", new_callable=PropertyMock) as mocked_routes: mocked_routes.return_value = [[LineStop('A', 0), LineStop('', 0)], [LineStop('B', 0), LineStop('', 0)]] line = get_empty_line() bus = Bus(line, 0) bus2 = Bus(line, 1) assert bus.passengers == [] assert bus.line == line assert bus.time_to_next_stop == 0 assert bus.current_stop_name == 'A'