class ApiManagerTest(unittest.TestCase): def setUp(self): self.coffee_file = Mock() self.coffee_file.get_machine_status = MagicMock( return_value=MachineStatus.stand_by) self.coffee_file.get_schedule_times = MagicMock( return_value=['09:00']) self.coffee_file.start_coffee_routine_async = MagicMock() self.target = ApiManager(self.coffee_file).api_flask.test_client(self) def test_api_should_given_information_about_app(self): result = self.target.get('/') self.assertEqual(result.data, 'Api da Cafeteira') def test_api_should_return_machine_status(self): result = self.target.get('/cafe') expected = json.dumps({'status': 'stand_by'}, indent=2) self.assertEqual(result.data, expected) def test_api_should_return_schedule_times(self): result = self.target.get('/calendario') expected = json.dumps( {'times': ['09:00']}, indent=2, separators=(',', ': ')) self.assertEqual(result.data, expected) def test_api_should_start_make_coffee(self): self.target.post('/cafe') self.assertTrue(self.coffee_file.start_coffee_routine_async.called) self.assertTrue(self.coffee_file.get_machine_status.called)
class Cafeteira: def __init__(self): self.twitter = TwitterManager() self.scheduled_times = self.setup_schedule() self.schedule = ScheduleManager(self.scheduled_times) self.machine = MachineManager() self.machine.listen_button(self.button_callback) self.start_schedule_process() self.start_api_process() def get_machine_status(self): return self.machine.machine_status def get_schedule_times(self): return self.scheduled_times def button_callback(self, pin): logger.debug("Button panic activaded!") if self.machine.machine_status is MachineStatus.stand_by: self.start_coffee_routine_async() else: self.twitter.tweet_turn_off_machine() self.machine.go_back_stand_by() def setup_schedule(self): FILE_PATH = "schedule_coffee.txt" reader = ScheduleReader(FILE_PATH) times = reader.read_scheduled_times() return times def check_schedule_are_ok(self): while True: if self.schedule.its_time() and self.machine.machine_status is MachineStatus.stand_by: logger.debug("Schedule time is Ok!") self.start_coffee_routine_async() time.sleep(30) def start_api_process(self): logger.debug("Create process for API") self.api = ApiManager(self) self.api.start() thread_api = Thread(target=self.api.start) thread_api.start() def start_schedule_process(self): logger.debug("Create process for Schedule Reader") thread_reading_schedule = Thread(target=self.check_schedule_are_ok) thread_reading_schedule.start() def start_coffee_routine_async(self): if self.machine.machine_status is MachineStatus.stand_by: logger.debug("Create thread for coffee") self.twitter.tweet_turn_on_machine() thread_making_coffee = Thread(target=self.machine.start_coffee_routine) thread_making_coffee.start()
def setUp(self): self.coffee_file = Mock() self.coffee_file.get_machine_status = MagicMock( return_value=MachineStatus.stand_by) self.coffee_file.get_schedule_times = MagicMock( return_value=['09:00']) self.coffee_file.start_coffee_routine_async = MagicMock() self.target = ApiManager(self.coffee_file).api_flask.test_client(self)
def start_api_process(self): logger.debug("Create process for API") self.api = ApiManager(self) self.api.start() thread_api = Thread(target=self.api.start) thread_api.start()