def test_list_vendors_ok_response(self, mock_pagination_meta,
                                      mock_filter_by, mock_get_location):
        '''Test list_vendors OK response.
        '''
        # Arrange
        with self.app.app_context():
            mock_pagination_meta.return_value = {
                'total_rows': 1,
                'total_pages': 1,
                'current_page': 1,
                'next_page': 1,
                'prev_page': 1
            }
            mock_filter_by.return_value.items = [
                self.mock_vendor,
            ]
            mock_get_location.return_value = 1
            vendor_controller = VendorController(self.request_context)

            # Act
            result = vendor_controller.list_vendors()

            # Assert
            assert result.status_code == 200
            assert result.get_json()['msg'] == 'OK'
    def test_update_vendor_ok_response(
        self,
        mock_vendor_repo_get,
        mock_request_params
    ):
        '''Test update_vendor OK response.
        '''
        # Arrange
        with self.app.app_context():
            mock_request_params.return_value = (
                self.fake.name(),
                self.fake.phone_number(),
                self.fake.address(),
                True,
                self.fake.name()
            )
            mock_vendor_repo_get.return_value = self.mock_vendor
            vendor_controller = VendorController(self.request_context)

            # Act
            result = vendor_controller.update_vendor(1)

            # Assert
            assert result.status_code == 200
            assert result.get_json()['msg'] == 'OK'
    def test_update_vendor_when_vendor_doesnot_exist(
        self,
        mock_vendor_repo_get,
        mock_request_params
    ):
        '''Test update_vendor when vendor doesnot exist.
        '''
        # Arrange
        with self.app.app_context():
            mock_request_params.return_value = (
                self.fake.name(),
                self.fake.phone_number(),
                self.fake.address(),
                True,
                self.fake.name()
            )
            mock_vendor_repo_get.return_value = None
            vendor_controller = VendorController(self.request_context)

            # Act
            result = vendor_controller.update_vendor(1)

            # Assert
            assert result.status_code == 400
            assert result.get_json()['msg'] == 'Invalid or incorrect ' \
                'vendor_id provided'
    def test_creat_vendor_ok_response(
        self,
        mock_new_vendor,
        mock_request_params,
        mock_get_location
    ):
        '''Test create_vendor OK response.
        '''
        mock_get_location.return_value = 1
        mock_request_params.return_value = (
            self.fake.name(),
            self.fake.phone_number(),
            self.fake.address(),
            True,
            self.fake.name()
        )
        mock_new_vendor.return_value = self.mock_vendor
        vendor_controller = VendorController(self.request_context)

        # Act
        result = vendor_controller.create_vendor()

        # Assert
        assert result.status_code == 201
        assert result.get_json()['msg'] == 'OK'
    def test_get_vendor_ok_response(self, mock_vendor_repo_get):
        '''Test get_vendor OK response.
        '''
        # Arrange
        with self.app.app_context():
            mock_vendor_repo_get.return_value = self.mock_vendor
            vendor_controller = VendorController(self.request_context)

            # Act
            result = vendor_controller.get_vendor(1)

            # Assert
            assert result.status_code == 200
            assert result.get_json()['msg'] == 'OK'
    def test_get_vendor_when_vendor_doesnot_exist(self, mock_vendor_repo_get):
        '''Test get_vendor when the vendor doesnot exist.
        '''
        # Arrange
        with self.app.app_context():
            mock_vendor_repo_get.return_value = None
            vendor_controller = VendorController(self.request_context)

            # Act
            result = vendor_controller.get_vendor(1)

            # Assert
            assert result.status_code == 400
            assert result.get_json()['msg'] == 'Bad Request - Invalid or ' \
                'Missing vendor_id'
    def test_delete_vendor_when_vendor_is_already_deleted(
            self, mock_vendor_repo_get):
        '''Test delete_vendor when vendor is already deleted.
        '''
        # Arrange
        with self.app.app_context():
            mock_vendor_repo_get.return_value = self.mock_deleted_vendor
            vendor_controller = VendorController(self.request_context)

            # Act
            result = vendor_controller.delete_vendor(1)

            # Assert
            assert result.status_code == 400
            assert result.get_json()['msg'] == 'Vendor has already ' \
                'been deleted'
    def test_delete_vendor_when_vendor_doesnot_exist(self,
                                                     mock_vendor_repo_get):
        '''Test delete_vendor when vendor doesnot exist.
        '''
        # Arrange
        with self.app.app_context():
            mock_vendor_repo_get.return_value = None
            vendor_controller = VendorController(self.request_context)

            # Act
            result = vendor_controller.delete_vendor(1)

            # Assert
            assert result.status_code == 400
            assert result.get_json()['msg'] == 'Invalid or incorrect ' \
                'vendor_id provided'
    def test_delete_vendor_when_vendor_has_dependants(self,
                                                      mock_vendor_repo_get):
        '''Test delete_vendor when vendor has dependants.
        '''
        # Arrange
        with self.app.app_context():
            mock_vendor_repo_get.return_value = \
                self.mock_vendor_with_dependants
            vendor_controller = VendorController(self.request_context)

            # Act
            result = vendor_controller.delete_vendor(1)

            # Assert
            assert result.status_code == 400
            assert result.get_json()['msg'] == 'Vendor cannot be deleted ' \
                'because it has a child object'
    def test_delete_vendor_ok_response(self, mock_filter_by,
                                       mock_vendor_repo_update,
                                       mock_vendor_repo_get):
        '''Test delete_vendor OK response.
        '''
        # Arrange
        with self.app.app_context():
            mock_vendor_repo_get.return_value = self.mock_vendor
            mock_vendor_repo_update.return_value = self.mock_vendor
            mock_filter_by.return_value.items = [
                self.mock_vendor_engagement,
            ]
            vendor_controller = VendorController(self.request_context)

            # Act
            result = vendor_controller.delete_vendor(1)

            # Assert
            assert result.status_code == 200
            assert result.get_json()['msg'] == 'Vendor deleted'
Ejemplo n.º 11
0
def index():
    if request.method == 'GET':
        text_query = request.args.get('q', None)
        vendors = None
        if text_query != None:
            vendors = VendorController.find_by_name(text_query)
        else:
            vendors = VendorController.get_all_vendors()
        return {"vendors": vendors}

    if request.method == 'POST':
        req_body = request.json
        res = VendorController.create_vendor(req_body)
        return {
            'vendor': res.get('value'),
            'success': res.get('success', False),
            'message': res.get('message')
        }

    if request.method == 'DELETE':
        success = VendorController.delete_all_vendors()
        return {"success": success}
Ejemplo n.º 12
0
def vendor_by_id(vendor_id=None):
    if request.method == 'GET':
        message = 'vendor not found'
        vendor = VendorController.find_by_id(vendor_id)
        return {
            'vendor': vendor,
            'success': True if vendor else False,
            'message': None if vendor else message
        }
    if request.method == 'PUT':
        req_body = request.json
        res = VendorController.update_vendor(vendor_id, req_body)
        return {
            'vendor': res.get('value'),
            'success': res.get('success', False),
            'message': res.get('message')
        }
    if request.method == 'DELETE':
        res = VendorController.delete_vendor(vendor_id)
        return {
            'success': res.get('success', False),
            'message': res.get('message')
        }
from app.blueprints.base_blueprint import Blueprint, BaseBlueprint, request, Security, Auth
from app.controllers.vendor_controller import VendorController
from app.utils.auth import Auth
from flasgger import swag_from

vendor_blueprint = Blueprint('vendor', __name__, url_prefix='{}/vendors'.format(BaseBlueprint.base_url_prefix))


vendor_controller = VendorController(request)


@vendor_blueprint.route('/', methods=['GET'])
@swag_from('documentation/get_all_vendors_per_page.yml')
def list_vendors():
	return vendor_controller.list_vendors()


@vendor_blueprint.route('/deleted/', methods=['GET'])
@swag_from('documentation/get_deleted_vendors_per_page.yml')
def list_deleted_vendors():
	return vendor_controller.list_deleted_vendors()


@vendor_blueprint.route('/suspended/', methods=['GET'])
@swag_from('documentation/get_suspended_vendors_per_page.yml')
def list_suspended_vendors():
	return vendor_controller.list_suspended_vendors()


@vendor_blueprint.route('/<int:vendor_id>', methods=['GET'])
@swag_from('documentation/get_vendor_by_id.yml')