def setUp(self):
     self.BaseSetUp()
     self.mock_meal_item = MealItem(
         is_deleted=False,
         created_at=datetime.now(),
         updated_at=datetime.now(),
         meal_type='Mock meal type',
         name='Mock meal item',
         image='Mock image',
         location_id=1
     )
     self.mock_deleted_meal_item = MealItem(
         is_deleted=True,
         created_at=datetime.now(),
         updated_at=datetime.now(),
         meal_type='Mock meal type',
         name='Mock meal item',
         image='Mock image',
         location_id=1
     )
    def test_list_menus_range_ok_response(
            self, mock_menu_controller_pagination_meta,
            mock_meal_item_repo_get,
            mock_menu_repo_get_range_paginated_options,
            mock_meal_periods_has_value, mock_auth_get_location):
        '''Test list_menus_range OK response.
        '''
        # Arrange
        with self.app.app_context():
            mock_auth_get_location.return_value = 1
            mock_meal_periods_has_value.return_value = True
            mock_menu = Menu(date=datetime.now(),
                             meal_period='',
                             location_id=1,
                             main_meal_id=1,
                             allowed_side=1,
                             allowed_protein=1,
                             side_items='1,2',
                             protein_items='1,2',
                             vendor_engagement_id=1,
                             created_at=datetime.now(),
                             updated_at=datetime.now())
            mock_meal_item = MealItem(id=1,
                                      meal_type=1,
                                      name='',
                                      image='',
                                      location_id=1,
                                      created_at=datetime.now(),
                                      updated_at=datetime.now())
            mock_menu_repo_get_range_paginated_options.return_value.items = [
                mock_menu,
            ]

            mock_meal_item_repo_get.return_value = mock_meal_item
            mock_period_start_date = '2019-01-01'
            mock_period_end_date = '2019-02-28'
            mock_menu_controller_pagination_meta.return_value = {
                'total_rows': 1,
                'total_pages': 1,
                'current_page': 1,
                'next_page': False,
                'prev_page': False
            }
            menu_controller = MenuController(self.request_context)

            # Act
            result = menu_controller.list_menus_range('lunch',
                                                      mock_period_start_date,
                                                      mock_period_end_date)

            # Assert
            assert result.status_code == 200
            assert result.get_json()['msg'] == 'OK'
    def test_create_menu_successful(self, mock_get_location,
                                    mock_request_params, mock_get_unpaginated,
                                    mock_new_menu, mock_get,
                                    mock_get_meal_items):
        '''Test response returned when the menu is created successfully.
        '''
        # Arrange
        with self.app.app_context():
            mock_get_location.return_value = 1
            mock_request_params.return_value = (None, None, None, None, None,
                                                None, None, None)
            mock_get_unpaginated.return_value = None
            mock_new_menu.return_value = MealItem(meal_type='',
                                                  name='',
                                                  image='',
                                                  location_id=0,
                                                  created_at=datetime.now(),
                                                  updated_at=datetime.now(),
                                                  is_deleted=False,
                                                  id=1,
                                                  location={})
            mock_get.return_value = MealItem(meal_type='',
                                             name='',
                                             image='',
                                             location_id=0,
                                             created_at=datetime.now(),
                                             updated_at=datetime.now(),
                                             is_deleted=False,
                                             id=1)
            mock_get_meal_items.return_value = []
            menu_controller = MenuController(self.request_context)

            # Act
            result = menu_controller.create_menu()

            # Assert
            assert result.status_code == 201
            assert result.get_json()['msg'] == "OK"
    def test_list_menus_valid_meal_period_date(self,
                                               mock_menu_repo_get_meal_items,
                                               mock_meal_repo_get,
                                               mock_menu_repo_get_unpaginated,
                                               mock_auth_get_location,
                                               mock_meal_periods_has_value):
        '''Test list_menus response when the meal period or date is valid.
        '''
        # Arrange
        with self.app.app_context():
            mock_menu = Menu(date=datetime.now(),
                             meal_period='',
                             location_id=1,
                             main_meal_id=1,
                             allowed_side=1,
                             allowed_protein=1,
                             side_items='',
                             protein_items='',
                             vendor_engagement_id=1,
                             created_at=datetime.now(),
                             updated_at=datetime.now())
            mock_meal_item = MealItem(id=1,
                                      meal_type=1,
                                      name='',
                                      image='',
                                      location_id=1,
                                      created_at=datetime.now(),
                                      updated_at=datetime.now())
            mock_meal_periods_has_value.return_value = True
            mock_auth_get_location.return_value = 1
            mock_menu_repo_get_unpaginated.return_value = [
                mock_menu,
            ]
            mock_meal_repo_get.return_value = mock_meal_item
            mock_menu_repo_get_meal_items.return_value = []
            menu_controller = MenuController(self.request_context)

            # Act
            result = menu_controller.list_menus('lunch', '2019-02-01')

            # Assert
            assert result.status_code == 200
            assert result.get_json()['msg'] == 'OK'
    def test_update_menu_ok_response(self, mock_meal_item_repo_get,
                                     mock_menu_repo_update, mock_menu_repo_get,
                                     mock_menu_controller_request_params):
        '''Test update_menu when the response is OK.
        '''
        # Arrange
        with self.app.app_context():
            mock_menu_controller_request_params.return_value = ('2019-01-01',
                                                                'lunch', 1, 1,
                                                                1, '1,1',
                                                                '1,2', 1)
            mock_menu = Menu(date=datetime.now(),
                             meal_period='',
                             location_id=1,
                             main_meal_id=1,
                             allowed_side=1,
                             allowed_protein=1,
                             side_items='1,2',
                             protein_items='1,2',
                             vendor_engagement_id=1,
                             created_at=datetime.now(),
                             updated_at=datetime.now())
            mock_meal_item = MealItem(id=1,
                                      meal_type=1,
                                      name='',
                                      image='',
                                      location_id=1,
                                      created_at=datetime.now(),
                                      updated_at=datetime.now())
            mock_menu_repo_get.return_value = mock_menu
            mock_menu_repo_update.return_value = mock_menu
            mock_meal_item_repo_get.return_value = mock_meal_item
            mock_menu_id = 1
            menu_controller = MenuController(self.request_context)

            # Act
            result = menu_controller.update_menu(mock_menu_id)

            # Assert
            assert result.status_code == 200
            assert result.get_json()['msg'] == 'OK'
예제 #6
0
    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()