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)
class LocationController(BaseController): def __init__(self, request): BaseController.__init__(self, request) self.location_repo = LocationRepo() def list_locations(self): locations = self.location_repo.fetch_all() location_list = [location.serialize() for location in locations.items] return self.handle_response( "OK", payload={ "locations": location_list, "meta": self.pagination_meta(locations), }, ) def get_location(self, location_id): location = self.location_repo.get(location_id) if location: return self.handle_response( "OK", payload={"location": location.serialize()}) return self.handle_response("Invalid or Missing location_id", status_code=400) def create_location(self): name, zone = self.request_params("name", "zone") location = self.location_repo.new_location(name=name, zone=zone) return self.handle_response("OK", payload={"location": location.serialize()}, status_code=201) def update_location(self, location_id): name, zone = self.request_params("name", "zone") location = self.location_repo.get(location_id) if location: location = self.location_repo.update(location, **dict(name=name, zone=zone)) return self.handle_response( "OK", payload={"location": location.serialize()}, status_code=201) return self.handle_response("Location Not Found", status_code=404) def delete_location(self, location_id): location = self.location_repo.get(location_id) if location and not location.is_deleted: location = self.location_repo.update(location, **dict(is_deleted=True)) return self.handle_response( "Location deleted successfully", payload={"location": location.serialize()}, status_code=200, ) return self.handle_response("Location Not Found", status_code=404)
def __init__(self, request): """ Constructor. Parameters: ----------- request """ BaseController.__init__(self, request) self.user_role_repo = UserRoleRepo() self.role_repo = RoleRepo() self.user_repo = UserRepo() self.location_repo = LocationRepo() self.perm_repo = PermissionRepo()
class TestMenuRepo(BaseTestCase): def setUp(self): self.BaseSetUp() self.repo = LocationRepo() def test_new_location_method_returns_new_location_object(self): location = LocationFactory.build() new_location = self.repo.new_location( location.name, location.zone) self.assertIsInstance(new_location, Location) self.assertEqual(new_location.name, location.name) self.assertEqual(new_location.zone, location.zone)
def test_center_selection(self): # Arrange with self.app.app_context(): mock_location = LocationRepo().get_unpaginated(name='Kampala')[0] mock_payload = {'actions': [{'value': mock_location.id}]} bot_controller = BotController(self.request_context) # Act result = bot_controller.handle_center_selection(mock_payload) # Assert result_json = result.get_json() if result.status_code != 200: raise AssertionError() if result_json['text'] != 'Select Date': 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'] != 'day_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()
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()
def __init__(self, request): BaseController.__init__(self, request) self.location_repo = LocationRepo()
class LocationController(BaseController): def __init__(self, request): BaseController.__init__(self, request) self.location_repo = LocationRepo() def get_locations(self): locations = self.location_repo.filter_by(**{'is_deleted': 'false'}) location_list = [location.serialize() for location in locations.items] return self.handle_response('OK', payload={ 'locations': location_list, 'meta': self.pagination_meta(locations) }) def get_location(self, location_id): location = self.location_repo.get(location_id) if location: location = location.serialize() return self.handle_response('OK', payload={'location': location}) else: return self.handle_response( 'Bad Request - Invalid or missing location_id', status_code=400) def create_location(self): location_code, location_name = self.request_params( 'locationCode', 'location') location = self.location_repo.create_location(location_code, location_name) return self.handle_response('OK', payload={'location': location.serialize()}, status_code=201) def update_location(self, location_id): location_code, location_name = self.request_params( 'locationCode', 'location') location = self.location_repo.get(location_id) if location: updates = {} if location_code: updates['location_code'] = location_code if location_name: updates['location'] = location_name self.location_repo.update(location, **updates) return self.handle_response( 'OK', payload={'location': location.serialize()}) return self.handle_response( 'Invalid or incorrect location_id provided', status_code=400) def delete_location(self, location_id): location = self.location_repo.get(location_id) updates = {} if location: updates['is_deleted'] = True self.location_repo.update(location, **updates) return self.handle_response('OK', payload={"status": "success"}) return self.handle_response( 'Invalid or incorrect location_id provided', status_code=400)
def setUp(self): self.BaseSetUp() self.repo = LocationRepo()