class TimeTableTest(unittest.TestCase): def setUp(self) -> None: self.time_table = TimeTable( (Hour('11:15'), Hour('17:23'), Hour('23:10'))) def tearDown(self) -> None: del self.time_table def test_should_return_departure_times(self): self.assertNotEqual((), self.time_table.get_departure_times()) def test_time_should_be_in_correct_format(self): time_index = randint(0, len(self.time_table.get_departure_times()) - 1) seconds_in_day = 24 * 60 * 60 result_time = self.time_table.get_departure_times()[time_index] self.assertTrue(type(result_time) is int, msg='Should be from Clock') self.assertGreaterEqual( self.time_table.get_departure_times()[time_index], 0) self.assertLess(self.time_table.get_departure_times()[time_index], seconds_in_day) @mock.patch('hour.hour.Hour') def test_should_raise_error_for_too_big_values(self, hour_mock): hour_mock.get_hour_in_seconds.return_value = 574567567567 with self.assertRaises(ValueError): TimeTable((hour_mock, )) def test_should_raise_error_for_too_big_values_no_mock(self): hour = HourStub('11:20') with self.assertRaises(ValueError): TimeTable((hour, )) def test_string_instead_hour(self): with self.assertRaises(ValueError): TimeTable(('bleble', ))
class TimeTableTest(unittest.TestCase): def setUp(self) -> None: self.time_table = TimeTable((2345, 4567, 8678)) def tearDown(self) -> None: del self.time_table def test_should_return_departure_times(self): self.assertNotEqual((), self.time_table.get_departure_times()) def test_time_should_be_in_correct_format(self): time_index = randint(0, len(self.time_table.get_departure_times()) - 1) seconds_in_day = 24 * 60 * 60 result_time = self.time_table.get_departure_times()[time_index] self.assertTrue(type(result_time) is int, msg='Should be from Clock') self.assertGreaterEqual( self.time_table.get_departure_times()[time_index], 0) self.assertLess(self.time_table.get_departure_times()[time_index], seconds_in_day) def test_should_raise_error_for_too_big_values(self): with self.assertRaises(ValueError): TimeTable((35, 4567, 574567567567))
import unittest import mock from faker import Faker from clock import Clock from station.station import Station from time_table.time_table import TimeTable class Route: def __init__(self, route_name: str, stations: tuple): self.stations = {station.get_name(): station for station in stations} self.route_name = route_name if __name__ == '__main__': directions = { 'Gdańsk': TimeTable((345, 6546, 75688, 6537)), 'Reda': TimeTable((45465, 8678, 9758)) } station = Station('Rumia', directions) route = Route('Reda - Gdańsk', (station, )) print('Następny pociąg ze stacji {} do stacji {} odjedzie o godzinie {}'. format('Rumia', 'Gdańsk', route.stations['Rumia'].get_next_departure_time('Gdańsk')))
def setUp(self) -> None: self.time_table = TimeTable((2345, 4567, 8678))
def test_should_raise_error_for_too_big_values(self): with self.assertRaises(ValueError): TimeTable((35, 4567, 574567567567))
def setUp(self) -> None: self.time_table = TimeTable( (Hour('11:15'), Hour('17:23'), Hour('23:10')))
def test_string_instead_hour(self): with self.assertRaises(ValueError): TimeTable(('bleble', ))
def test_should_raise_error_for_too_big_values_no_mock(self): hour = HourStub('11:20') with self.assertRaises(ValueError): TimeTable((hour, ))
def test_should_raise_error_for_too_big_values(self, hour_mock): hour_mock.get_hour_in_seconds.return_value = 574567567567 with self.assertRaises(ValueError): TimeTable((hour_mock, ))
def __init__(self, station_name, directions: dict): self.station_name = station_name self.directions = directions.keys() self.time_tables = directions def get_name(self): return self.station_name def get_current_time(self): return datetime.datetime.now().strftime('%H:%M') def get_directions(self): return self.directions def get_next_departure_time(self, direction) -> str: if direction not in self.directions: raise ValueError('Unknown direction') current_time = Clock.get_time() for item in self.time_tables[direction].get_departure_times(): if item > current_time: return Clock.get_formatted_time(item) return Clock.get_formatted_time( self.time_tables[direction].get_departure_times()[0]) if __name__ == '__main__': station = Station('Rumia', {'Gdańsk': TimeTable((345, 6546, 75688, 6537))}) print(station.get_next_departure_time('Gdańsk'))