<commit_before>import sys
import types
import unittest

from core.configuration import Config
from core.simulation import Simulation
from utils.helpers import add_property, add_variable

if sys.version_info[0] >= 3:
    from unittest.mock import PropertyMock, patch
else:
    from mock import PropertyMock, patch


class SimulationTest(unittest.TestCase):
    @staticmethod
    def copy_func(f, name=None):
        return types.FunctionType(f.func_code, f.func_globals, name or f.func_name,
                                  f.func_defaults, f.func_closure)

    def test_graph_and_lines(self):
        with patch('core.configuration.Config.graph_dict', new_callable=PropertyMock) as mock_graph_dict:
            with patch('core.configuration.Config.lines_dict', new_callable=PropertyMock) as mock_lines_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({}, {}, {})
                config.stops = ["A", "B", "C", "D", "E", "F"]
                simulation = Simulation(config)

                def mocked_update(self):
                    for bus in self._buses:
                        if bus.route == 0:
                            if bus.id not in self.fajny_slownik.keys():
                                self.fajny_slownik[bus.id] = []
                            if bus.time_to_next_stop == 0:
                                self.fajny_slownik[bus.id].append(bus.current_stop_name)
                            else:
                                self.fajny_slownik[bus.id].append(bus.current_stop_name + bus.next_stop_name)

                def finished(q):
                    if q.count_finished == 35:
                        return True
                    q.count_finished += 1
                    q.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, "fajny_slownik", {})
                simulation.mainloop()
                def empty():
                    pass
                simulation._print = empty
                pathsy = ['PA', 'A', 'AD', 'AD', 'D', 'DC', 'DC', 'DC', 'C', 'CB', 'B', 'BE', 'BE', 'E', 'EF', 'EF','F']
                print(pathsy)
                self.assertEqual(len(simulation.fajny_slownik), 2)
                for path in simulation.fajny_slownik.values():
                    print(path)
                    self.assertEqual(path, pathsy)<commit_msg>faster tests<commit_after>import sys
import types
import unittest
import time

from core.configuration import Config
from core.simulation import Simulation
from utils.helpers import add_property, add_variable

if sys.version_info[0] >= 3:
    from unittest.mock import PropertyMock, patch
else:
    from mock import PropertyMock, patch


class SimulationTest(unittest.TestCase):
    @staticmethod
    def copy_func(f, name=None):
        return types.FunctionType(f.func_code, f.func_globals, name or f.func_name,
                                  f.func_defaults, f.func_closure)

    def test_graph_and_lines(self):
        with patch('core.configuration.Config.graph_dict', new_callable=PropertyMock) as mock_graph_dict:
            with patch('core.configuration.Config.lines_dict', new_callable=PropertyMock) as mock_lines_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({}, {}, {})
                config.stops = ["A", "B", "C", "D", "E", "F"]
                simulation = Simulation(config)

                def mocked_update(self):
                    for bus in self._buses:
                        if bus.route == 0:
                            if bus.id not in self.fajny_slownik.keys():
                                self.fajny_slownik[bus.id] = []
                            if bus.time_to_next_stop == 0:
                                self.fajny_slownik[bus.id].append(bus.current_stop_name)
                            else:
                                self.fajny_slownik[bus.id].append(bus.current_stop_name + bus.next_stop_name)


                def finished(q):
                    time.sleep = lambda x: None
                    if q.count_finished == 35:
                        return True
                    q.count_finished += 1
                    q.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, "fajny_slownik", {})
                simulation.mainloop()
                def empty():
                    pass
                simulation._print = empty
                pathsy = ['PA', 'A', 'AD', 'AD', 'D', 'DC', 'DC', 'DC', 'C', 'CB', 'B', 'BE', 'BE', 'E', 'EF', 'EF','F']
                print(pathsy)
                self.assertEqual(len(simulation.fajny_slownik), 2)
                for path in simulation.fajny_slownik.values():
                    print(path)
                    self.assertEqual(path, pathsy)
{'fraction_non_alphanumeric': 0.12952352231604342, 'fraction_numerical': 0.009951749095295536, 'mean_word_length': 1.4058759521218716, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1681273', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1771, 'n_words': 416}