def test_get_meal_sessions(self): # Arrange mock_location_id = LocationRepo().new_location('Lagos', '+1').id MealSessionRepo().new_meal_session(name='breakfast', start_time=time(hour=8, minute=0, second=0), stop_time=time(hour=9, minute=0, second=0), date=date.today(), location_id=mock_location_id) MealSessionRepo().new_meal_session(name='lunch', start_time=time(hour=12, minute=30, second=0), stop_time=time(hour=14, minute=0, second=0), date=date.today(), location_id=mock_location_id) # Act result = MealSessionRepo().get_by_date_location( meal_date=date.today(), location_id=mock_location_id) # Assert self.assertIsNotNone(result) self.assertIsInstance(result, list)
def test_meal_session_cron_creates_meal_session_using_scheduler_date( self, mock_location_current_date, mock_scheduler_current_date, ): LocationFactory.create(id=1, name="Lagos") with self.app.app_context(): mock_scheduler_current_date.return_value = datetime( year=2019, month=4, day=10, hour=0, minute=0, tzinfo=pytz.timezone("Africa/Lagos")) mock_location_current_date.return_value = datetime( year=2019, month=3, day=10, hour=11, minute=0, tzinfo=pytz.timezone("Africa/Dakar")) Cron(self.app).run_meal_session_cron() meal_sessions = MealSessionRepo().fetch_all().items assert meal_sessions[0].name == "breakfast" assert meal_sessions[0].date.month == 4 assert meal_sessions[1].name == "lunch" assert meal_sessions[1].date.month == 4
class TestMealSessionRepo(BaseTestCase): def setUp(self): self.BaseSetUp() self.repo = MealSessionRepo() def tearDown(self): self.BaseTearDown() def test_new_meal_session_method_returns_new_meal_session_object(self): location = LocationFactory() meal_session = MealSessionFactory(location=location) new_meal_session = self.repo.new_meal_session( name=meal_session.name, start_time=meal_session.start_time, stop_time=meal_session.stop_time, date=meal_session.date, location_id=meal_session.location.id) self.assertIsInstance(new_meal_session, MealSession) self.assertEqual(new_meal_session.location_id, meal_session.location_id) self.assertEqual(new_meal_session.name, meal_session.name) self.assertEqual(new_meal_session.start_time, meal_session.start_time) self.assertEqual(new_meal_session.stop_time, meal_session.stop_time) def test_get_meal_sessions(self): # Arrange mock_location_id = LocationRepo().new_location('Lagos', '+1').id MealSessionRepo().new_meal_session(name='breakfast', start_time=time(hour=8, minute=0, second=0), stop_time=time(hour=9, minute=0, second=0), date=date.today(), location_id=mock_location_id) MealSessionRepo().new_meal_session(name='lunch', start_time=time(hour=12, minute=30, second=0), stop_time=time(hour=14, minute=0, second=0), date=date.today(), location_id=mock_location_id) # Act result = MealSessionRepo().get_by_date_location( meal_date=date.today(), location_id=mock_location_id) # Assert self.assertIsNotNone(result) self.assertIsInstance(result, list)
def test_job_to_schedule_method_creates_meal_sessions(self): LocationFactory.create(id=1, name="Lagos") with self.app.app_context(): Cron(self.app).run_meal_session_cron() meal_sessions = MealSessionRepo().fetch_all().items assert meal_sessions[0].name == "breakfast" assert meal_sessions[1].name == "lunch"
def handle_day_selection(self, payload): payload_action_value = payload['actions'][0]['value'] selected_date = payload_action_value.split('_')[0] location_id = payload_action_value.split('_')[1] day_meal_sessions = MealSessionRepo().get_by_date_location( meal_date=datetime.strptime(selected_date, '%Y-%m-%d').date(), location_id=location_id) page = SlackResponseFactory().create_response('day_selection')\ .build_page(meals=day_meal_sessions, payload=payload_action_value) return self.handle_response(slack_response=page)
def test_meal_session_cron_does_not_create_meal_session_if_session_already_exists( self, mock_location_current_date, mock_scheduler_current_date, mock_validate_meal_session_times): LocationFactory.create(id=1, name="Kampala") with self.app.app_context(): mock_scheduler_current_date.return_value = datetime( year=2019, month=4, day=10, tzinfo=pytz.timezone("Africa/Lagos")) mock_location_current_date.return_value = datetime( year=2019, month=3, day=10, tzinfo=pytz.timezone("Africa/Dakar")) mock_validate_meal_session_times.return_value = "A meal session already exists" Cron(self.app).run_meal_session_cron() meal_sessions = MealSessionRepo().fetch_all().items assert len(meal_sessions) == 0
class TestMealSessionRepo(BaseTestCase): def setUp(self): self.BaseSetUp() self.repo = MealSessionRepo() def test_new_meal_session_method_returns_new_meal_session_object(self): meal_session = MealSessionFactory.build() new_meal_session = self.repo.new_meal_session( name=meal_session.name, start_time=meal_session.start_time, stop_time=meal_session.stop_time, date=meal_session.date, location_id=meal_session.location_id) self.assertIsInstance(new_meal_session, MealSession) self.assertEqual(new_meal_session.location_id, meal_session.location_id) self.assertEqual(new_meal_session.name, meal_session.name) self.assertEqual(new_meal_session.start_time, meal_session.start_time) self.assertEqual(new_meal_session.stop_time, meal_session.stop_time)
def setUp(self): self.BaseSetUp() self.repo = MealSessionRepo()
def __init__(self, request): BaseController.__init__(self, request) self.meal_session_repo = MealSessionRepo() self.meal_service_repo = MealServiceRepo() self.business_logic = MealSessionLogic()
class MealSessionController(BaseController): def __init__(self, request): BaseController.__init__(self, request) self.meal_session_repo = MealSessionRepo() self.meal_service_repo = MealServiceRepo() self.business_logic = MealSessionLogic() def create_session(self): """ Creates a meal session if all data sent meets specified requirements :return: Json Response """ name, start_time, end_time, date, location_id = self.request_params( 'name', 'startTime', 'endTime', 'date', 'locationId') error_message, data = self.business_logic.validate_meal_session_details( **{ "name": name, "date": date, "location_id": location_id, "start_time": start_time, "end_time": end_time, }) if error_message: return make_response(jsonify({'msg': error_message}), 400) new_meal_session = self.meal_session_repo.new_meal_session( name=data['name'], start_time=data['start_time'], stop_time=data['end_time'], date=data['date_sent'], location_id=data['location_id']) new_meal_session.date = new_meal_session.date.strftime("%Y-%m-%d") return self.handle_response( 'OK', payload={'mealSession': new_meal_session.serialize()}, status_code=201) def update_session(self, meal_session_id): """ Updates a meal session if all data sent meets specified requirements :return: Json Response """ meal_session = self.meal_session_repo.get(meal_session_id) if not meal_session: return self.handle_response("Meal session Not Found", status_code=404) name, start_time, end_time, date, location_id = self.request_params( 'name', 'startTime', 'endTime', 'date', 'locationId') if not location_id: location_id = Auth.get_location() meal_session_data = { "name": name, "start_time": start_time, "end_time": end_time, "date": date, "location_id": location_id, "meal_session_id": meal_session.id, "meal_session": meal_session, } validated_data = self.business_logic.validate_update_of_meal_session( **meal_session_data) error_message = validated_data.get("error_message") if error_message: return make_response(jsonify({'msg': error_message}), 400) meal_session_updated = self.meal_session_repo.update_meal_session( meal_session, name=validated_data.get("name"), start_time=validated_data.get("start_time"), stop_time=validated_data.get("end_time"), date=validated_data.get("date"), location_id=validated_data.get("location_id")) meal_session_updated.name = meal_session_updated.name.value meal_session_updated.start_time = self.business_logic.get_time_as_string( meal_session_updated.start_time.hour, meal_session_updated.start_time.minute) meal_session_updated.stop_time = self.business_logic.get_time_as_string( meal_session_updated.stop_time.hour, meal_session_updated.stop_time.minute) meal_session_updated.date = meal_session_updated.date.strftime( "%Y-%m-%d") return self.handle_response( 'OK', payload={'mealSession': meal_session_updated.serialize()}, status_code=200) def list_meal_sessions(self): """ List all meal-sessions in the application, based on provided query """ options = self.get_params_dict() options['is_deleted'] = False sessions = self.meal_session_repo.filter_by(**options) if sessions.items: session_list = [session.serialize() for session in sessions.items] return self.handle_response('OK', payload={ 'MealSessions': session_list, 'meta': self.pagination_meta(sessions) }) return self.handle_response('No meal sessions found', status_code=404) def delete_session(self, meal_session_id): """ Deletes a meal session if correct meal_session_id is sent :return: Json Response """ meal_session = self.meal_session_repo.get(meal_session_id) if meal_session and not meal_session.is_deleted: meal_session = self.meal_session_repo.update( meal_session, **dict(is_deleted=True)) meal_session.name = meal_session.name.value meal_session.start_time = self.business_logic.get_time_as_string( meal_session.start_time.hour, meal_session.start_time.minute) meal_session.stop_time = self.business_logic.get_time_as_string( meal_session.stop_time.hour, meal_session.stop_time.minute) meal_session.date = meal_session.date.strftime("%Y-%m-%d") return self.handle_response( 'Meal session deleted successfully', payload={'mealSession': meal_session.serialize()}, status_code=200) return self.handle_response('Meal Session Not Found', status_code=404)
def test_handle_placing_order(self, func_get_unpaginated): # LocationFactory.create(name='Lagos') with self.app.app_context(): m_menu = Menu(id=1, main_meal=MealItem(name='Main meal 1')) func_get_unpaginated.return_value = [ m_menu, ] m_date = date.today().strftime('%Y-%m-%d') m_location_id = LocationRepo().get_unpaginated(name='Lagos')[0].id m_meal_period = MealSessionRepo().new_meal_session( name='lunch', start_time=time(hour=12, minute=20, second=0), stop_time=time(hour=14, minute=0, second=0), location_id=m_location_id, date=m_date).name m_value = f'{m_meal_period}_{m_date}_menu_{m_location_id}' m_payload = {'actions': [{'value': m_value}]} bot_controller = BotController(self.request_context) # Act result = bot_controller.handle_placing_order(m_payload) # Assert if result.status_code != 200: raise AssertionError() result_json = result.get_json() if 'text' not in result_json: raise AssertionError() if result_json['text'] != 'Select Main Meal': raise AssertionError() if 'attachments' not in result_json: raise AssertionError() if result_json['attachments'] is None: raise AssertionError() if 'text' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['text'] != '': raise AssertionError() if 'callback_id' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['callback_id'] != \ 'meal_action_selector': raise AssertionError() if 'color' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['color'] != '#3AA3E3': raise AssertionError() if 'attachment_type' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['attachment_type'] != 'default': raise AssertionError() if 'actions' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['actions'] is None: raise AssertionError() if 'name' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['name'] != \ 'main_meal': raise AssertionError() if 'type' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['type'] != 'button': raise AssertionError() if 'text' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['text'] != \ 'Main meal 1': raise AssertionError() if 'value' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['value'] != \ f'1_{m_value}': raise AssertionError()
def test_handle_action_selection_no_menus(self, func_get_unpaginated): with self.app.app_context(): # Arrange func_get_unpaginated.return_value = None m_date = date.today().strftime('%Y-%m-%d') m_location_id = LocationRepo().get_unpaginated(name='Lagos')[0].id m_meal_session = MealSessionRepo().new_meal_session( name='lunch', start_time=time(hour=12, minute=20, second=0), stop_time=time(hour=14, minute=0, second=0), location_id=m_location_id, date=m_date) m_value = f'{m_meal_session.name}_{m_date}_menu_{m_location_id}' m_payload = {'actions': [{'value': m_value}]} bot_controller = BotController(self.request_context) # Act result = bot_controller.handle_action_selection(m_payload) # Assert if result.status_code != 200: raise AssertionError() result_json = result.get_json() if 'text' not in result_json: raise AssertionError() if result_json['text'] != \ f'Sorry no menu found for date: {m_date}, ' \ f'meal period: {m_meal_session.name}': raise AssertionError() if 'attachments' not in result_json: raise AssertionError() if 'text' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['text'] != '': raise AssertionError() if 'callback_id' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['callback_id'] != \ 'center_selector': raise AssertionError() if 'color' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['color'] != '#3AA3E3': raise AssertionError() if 'attachment_type' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['attachment_type'] != 'default': raise AssertionError() if 'actions' not in result_json['attachments'][0]: raise AssertionError() if 'name' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['name'] != 'back': raise AssertionError() if 'text' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['text'] != 'Back': raise AssertionError() if 'type' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['type'] != 'button': raise AssertionError() if 'value' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['value'] != \ f'{m_location_id}': raise AssertionError()
def test_handle_action_selection(self, func_get_unpaginated, func_get, func_get_meal_items_by_ids): with self.app.app_context(): # Arrange m_menu = Menu(side_items='item 1', protein_items='item 1') func_get_unpaginated.return_value = [ m_menu, ] func_get.return_value.name = 'Main meal 1' func_get_meal_items_by_ids.return_value = [ 'item 1', ] m_date = date.today().strftime('%Y-%m-%d') m_location_id = LocationRepo().get_unpaginated(name='Lagos')[0].id m_meal_session = MealSessionRepo().new_meal_session( name='lunch', start_time=time(hour=12, minute=20, second=0), stop_time=time(hour=14, minute=0, second=0), location_id=m_location_id, date=m_date) m_value = f'{m_meal_session.name}_{m_date}_menu_{m_location_id}' m_payload = {'actions': [{'value': m_value}]} bot_controller = BotController(self.request_context) # Act result = bot_controller.handle_action_selection(m_payload) # Assert if result.status_code != 200: raise AssertionError() result_json = result.get_json() if 'text' not in result_json: raise AssertionError() if result_json['text'] != m_meal_session.name.upper(): raise AssertionError() if 'attachments' not in result_json: raise AssertionError() if result_json['attachments'] is None: raise AssertionError() if 'text' not in result_json['attachments'][0]: raise AssertionError() expected_text = 'Main meal: *Main meal 1*\n ' \ 'Side items: item 1\n' \ 'Protein items: item 1\n\n\n' if result_json['attachments'][0]['text'] != expected_text: raise AssertionError() if 'callback_id' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['callback_id'] != \ 'after_menu_list': raise AssertionError() if 'color' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['color'] != '#3AA3E3': raise AssertionError() if 'attachment_type' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['attachment_type'] != 'default': raise AssertionError() if 'actions' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['actions'] is None: raise AssertionError() if 'name' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['name'] != \ 'main meal': raise AssertionError() if 'text' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['text'] != \ 'Rate meal': raise AssertionError() if 'type' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['type'] != \ 'button': raise AssertionError() if 'value' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['value'] != \ f'{m_meal_session.name}_{m_date}_rate_{m_location_id}_{m_location_id}': raise AssertionError() if 'name' not in result_json['attachments'][0]['actions'][1]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['name'] != \ 'main meal': raise AssertionError() if 'text' not in result_json['attachments'][0]['actions'][1]: raise AssertionError() if result_json['attachments'][0]['actions'][1]['text'] != \ 'Place an order': raise AssertionError() if 'type' not in result_json['attachments'][0]['actions'][1]: raise AssertionError() if result_json['attachments'][0]['actions'][1]['type'] != \ 'button': raise AssertionError() if 'value' not in result_json['attachments'][0]['actions'][1]: raise AssertionError() if result_json['attachments'][0]['actions'][1]['value'] != \ f'{m_meal_session.name}_{m_date}_order_{m_location_id}_{m_location_id}': raise AssertionError()
def test_handle_period_selection(self): with self.app.app_context(): # Arrange m_date = date.today().strftime('%Y-%m-%d') m_location_id = LocationRepo().get_unpaginated(name='Lagos')[0].id m_meal_session = MealSessionRepo().new_meal_session( name='lunch', start_time=time(hour=12, minute=20, second=0), stop_time=time(hour=14, minute=0, second=0), location_id=m_location_id, date=m_date) m_value = f'{m_meal_session.name}_{m_date}_{m_location_id}' m_payload = {'actions': [{'value': m_value}]} bot_controller = BotController(self.request_context) # Act result = bot_controller.handle_period_selection(payload=m_payload) # Assert if result.status_code != 200: raise AssertionError() result_json = result.get_json() if 'attachments' not in result_json: raise AssertionError() if 'text' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['text'] \ != 'What do you want to do?': raise AssertionError() if 'callback_id' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['callback_id'] \ != 'action_selector': raise AssertionError() if 'color' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['color'] \ != '#3AA3E3': raise AssertionError() if 'attachment_type' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['attachment_type'] \ != 'default': raise AssertionError() if 'actions' not in result_json['attachments'][0]: raise AssertionError() if 'name' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['name'] \ != 'main meal': raise AssertionError() if 'text' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['text'] \ != 'View Menu List': raise AssertionError() if 'type' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['type'] \ != 'button': raise AssertionError() if 'value' not in result_json['attachments'][0]['actions'][0]: raise AssertionError() if result_json['attachments'][0]['actions'][0]['value'] \ != f'{m_meal_session.name}_{m_date}_menu_{m_location_id}': raise AssertionError() if 'actions' not in result_json['attachments'][0]: raise AssertionError() if 'name' not in result_json['attachments'][0]['actions'][1]: raise AssertionError() if result_json['attachments'][0]['actions'][1]['name'] \ != 'main meal': raise AssertionError() if 'text' not in result_json['attachments'][0]['actions'][1]: raise AssertionError() if result_json['attachments'][0]['actions'][1]['text'] \ != 'Place order': raise AssertionError() if 'type' not in result_json['attachments'][0]['actions'][1]: raise AssertionError() if result_json['attachments'][0]['actions'][1]['type'] \ != 'button': raise AssertionError() if 'value' not in result_json['attachments'][0]['actions'][1]: raise AssertionError() if result_json['attachments'][0]['actions'][1]['value'] \ != f'{m_meal_session.name}_{m_date}_order_{m_location_id}': raise AssertionError()
def test_day_selection(self): with self.app.app_context(): # Arrange mock_location_id = LocationRepo()\ .get_unpaginated(name='Lagos')[0].id MealSessionRepo().new_meal_session(name='breakfast', start_time=time(hour=8, minute=0, second=0), stop_time=time(hour=10, minute=0, second=0), date=date.today(), location_id=mock_location_id) MealSessionRepo().new_meal_session(name='lunch', start_time=time(hour=12, minute=30, second=0), stop_time=time(hour=14, minute=0, second=0), date=date.today(), location_id=mock_location_id) mock_date = date.today().strftime('%Y-%m-%d') mock_payload = { 'actions': [{ 'value': f"{mock_date}_{mock_location_id}" }] } bot_controller = BotController(self.request_context) # Act result = bot_controller.handle_day_selection(mock_payload) # Assert result_json = result.get_json() if result.status_code != 200: raise AssertionError() if 'text' not in result_json: raise AssertionError() if result_json['text'] != 'Select Meal Period': raise AssertionError() if 'attachments' not in result_json: raise AssertionError() if 'callback_id' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['callback_id'] \ != 'period_selector': raise AssertionError() if 'color' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['color'] != '#3AA3E3': raise AssertionError() if 'attachment_type' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['attachment_type'] != 'default': raise AssertionError() if 'actions' not in result_json['attachments'][0]: raise AssertionError() if len(result_json['attachments'][0]['actions']) != 2: raise AssertionError()