コード例 #1
0
    def patch(self, business_id):
        """
        Update a single business
        """

        # To do check if user is admin
        schema = BusinessSchema(partial=True)

        update_data = request.get_json()

        validated_update_data, errors = schema.load(update_data)

        if errors:
            return dict(status="fail", message=errors), 400

        business = Business.get_by_id(business_id)

        if not business:
            return dict(
                status="fail",
                message=f"Business with id {business_id} not found"), 404

        updated_business = Business.update(business, **validated_update_data)

        if not updated_business:
            return dict(status='fail', message='Internal Server Error'), 500

        return dict(status="success",
                    message="Business updated successfully"), 200
コード例 #2
0
    def setUp(self):
        """Instantiate the Business class so that it can be reused by other test cases."""

        self.business = Business()
        self.business.create_business(1, 'Cosmas', 'Cosma Tech', 'Nairobi',
                                      'Technology', 'Masters of ecommerce')
        self.business.create_business(2, 'John', 'John Corporate', 'Kitale',
                                      'Fishing', 'Process fish')
コード例 #3
0
 def add_business(self, business, app):
     try:
         BusinessModel.insert({
             'name': business.name,
             'ruc': business.ruc,
             'address': business.address
         })
         message = f'''Se agregó la empresa : {business.name} {business.ruc}'''
         print(message)
         return helper.handler_response(app, 201, message)
     except Exception as e:
         return helper.handler_response(app, 500, f'{str(e)}')
コード例 #4
0
class UpdateBusinessTest(unittest.TestCase):
    """Illustrate test cases to test expected behavior of update business functionality. """
    def setUp(self):
        """Instantiate the Business class so that it can be reused by other test cases."""

        self.business = Business()
        self.business.create_business(1, 'Cosmas', 'Cosma Tech', 'Nairobi',
                                      'Technology', 'Masters of ecommerce')
        self.business.create_business(2, 'John', 'John Corporate', 'Kitale',
                                      'Fishing', 'Process fish')

    def test_business_id_existence(self):
        """Test if a KeyError will be raised when the business id does not exist."""

        with self.assertRaises(KeyError):
            self.business.update_business(3, 'Cosmas', 'Cosma Tech', 'Nairobi',
                                          'Technology',
                                          'Masters of ecommerce and statics')

    def test_empty_business_id(self):
        """Test whether no business id is provided."""

        self.assertEqual(
            self.business.update_business('', 'Cosmas', 'Cosma Tech',
                                          'Nairobi', 'Technology',
                                          'Masters of ecommerce and statics'),
            'Business ID is required!')

    def test_negative_integer_business_id_raises_ValueError(self):
        """Test if ValueError is raised when business id a negative number."""

        response = self.business.update_business(
            -1, 'Cosmas', 'Cosma Tech', 'Nairobi', 'Technology',
            'Masters of ecommerce and statics')
        self.assertEqual(response, 'The business ID must be a positive number')

    def test_non_integer_business_id_raises_TypeError(self):
        """Test if TypeError is raised when business id not a number."""

        response = self.business.update_business(
            '1', 'Cosmas', 'Cosma Tech', 'Nairobi', 'Technology',
            'Masters of ecommerce and statics')
        self.assertEqual(response, 'The business ID must be an number!')

    def test_business_updated_successfully(self):
        """Test whether business was updated successfully."""

        response = self.business.update_business(
            1, 'Cosmas', 'Cosma Tech', 'Nairobi', 'Technology',
            'Masters of ecommerce and statics')
        self.assertEqual(response, "Business was successfully updated!")
コード例 #5
0
def create_business(
        *,
        db: Session = Depends(get_db),
        full_name: str = Body(None),
        main_administrator: EmailStr = Body(None),
        licenses_to_purchase: int = Body(None),
        current_user: DBUser = Depends(get_current_active_user),
):
    business = crud.business.get_by_business_name(db, full_name=full_name)
    if business:
        raise HTTPException(
            status_code=400,
            detail="A business with this name already exists in the system.",
        )

    try:
        business_in = Business(full_name=full_name,
                               licenses_to_purchase=licenses_to_purchase,
                               created_by=current_user.id,
                               modified_by=current_user.id)

        business = crud.business.create(db,
                                        main_administrator,
                                        business_in=business_in)
        return business
    except Exception as e:
        print(e)
        raise HTTPException(
            status_code=400,
            detail=str(e),
        )
コード例 #6
0
 def delete_business(self, business_id, app):
     try:
         delete = BusinessModel.where('id', business_id).delete()
         message = f'''No se encontró el business_id : {business_id}'''
         if delete:
             message = f'''Se eliminó el business_id : {business_id}'''
         return helper.handler_response(app, 201, f'Buscar business_id: {business_id}', message)
     except Exception as e:
         return helper.handler_response(app, 500, f'{str(e)}')
コード例 #7
0
 def all_business(self, app):
     try:
         business = BusinessModel.get()
         result = {}
         if business:
             result = business.serialize()
         return helper.handler_response(app, 201, 'Lista de Empresas', result)
     except Exception as e:
         return helper.handler_response(app, 500, f'{str(e)}')
コード例 #8
0
 def register_business(self, user_id, business):
     """docstring for register business"""
     fields = ["name", "category", "location"]
     result = self.check_req_fields(business, fields)
     if result["success"]:
         Business(user_id=user_id, name=business["name"], category=business["category"],
                  location=business["location"]).register_business()
         return jsonify({"success":True, "message":"Business Created"}), 201
     return jsonify(result), 422
コード例 #9
0
 def find_business(self, business_id, app):
     try:
         business = BusinessModel.where('id', business_id).first()
         result = {}
         if business:
             result = business.serialize()
         return helper.handler_response(app, 201, f'Buscar user_id: {business_id}', result)
     except Exception as e:
         return helper.handler_response(app, 500, f'{str(e)}')
コード例 #10
0
class CreateBusinessTest(unittest.TestCase):
    """Illustrate test cases to test expected behavior of business registration functionality. """
    def setUp(self):
        """Instantiate the Business class so that it can be reused by other test cases."""

        self.business = Business()

    def test_empty_business_id(self):
        """Test whether no business id is provided."""

        response = self.business.create_business('', 'cosmas', 'Cosma Tech',
                                                 'Nairobi', 'Category_one',
                                                 'Womens wear')
        self.assertEqual(response, 'Business ID is required!')

    def test_non_integer_business_id_raises_TypeError(self):
        """Test if TypeError is raised when business id not a number."""

        response = self.business.create_business('1', 'cosmas', 'Cosma Tech',
                                                 'Nairobi', 'Category_one',
                                                 'Womens wear')
        self.assertEqual(response, 'The business ID must be an number!')

    def test_negative_integer_business_id_raises_ValueError(self):
        """Test if ValueError is raised when business id a negative number."""

        response = self.business.create_business(-1, 'cosmas', 'Cosma Tech',
                                                 'Nairobi', 'Category_one',
                                                 'Womens wear')
        self.assertEqual(response, 'The business ID must be a positive number')

    def test_duplicate_business_id_raises_KeyError(self):
        """Test if KeyError is raised when business id already exist."""

        with self.assertRaises(KeyError):
            self.business.create_business(1, 'cosmas', 'Cosma Tech', 'Nairobi',
                                          'Technology', 'Masters of ecommerce')
            self.business.create_business(1, 'Allan', 'Allan Tech', 'Kitale',
                                          'Technology', 'Cryptocurrency')

    def test_successful_registered_business(self):
        """Test whether a success message is returned when a business is registered."""

        test_result = self.business.create_business(2, 'Bruce', 'Bruce Tech',
                                                    'Nairobi', 'Masoko',
                                                    'Womens wear')
        self.assertEqual(
            ['Bruce', 'Bruce Tech', 'Nairobi', 'Masoko', 'Womens wear'],
            test_result["value_list"])
        self.assertEqual('The business is successfully registered!!!',
                         test_result['message'],
                         msg='Fail to register')
コード例 #11
0
 def get_businesses(page, limit, search_string, location, category):
     """docstring for paginating through the business"""
     filters = {}
     # generate filters
     if location is not None:
         filters["location"] = location
     if category is not None:
         filters["category"] = category
     res = Business.get_businesses(page, limit, search_string, filters)
     if res["success"]:
         return jsonify(res), 200
     return jsonify(res), 404
コード例 #12
0
 def update_business(self, user_id, business_id, business):
     """docstring for updating a business"""
     fields = ["name", "category", "location"]
     result = self.check_req_fields(business, fields)
     if result["success"]:
         business_object = {
             "name" : business["name"],
             "category" : business["category"],
             "location" : business["location"]
         }
         return Business.update_business(user_id, business_id, business_object)
     return jsonify(result), 422
コード例 #13
0
class DeleteBusinessTest(unittest.TestCase):
    """Illustrate test cases to test expected behavior of delete business functionality. """
    def setUp(self):
        """Instantiate the Business class so that it can be reused by other test cases."""

        self.business = Business()

    def test_business_id_existence(self):
        """Test if a KeyError will be raised when the business id does not exist."""

        self.business.create_business(1, 'Cosmas', 'Cosma Tech', 'Nairobi',
                                      'Technology', 'Masters of ecommerce')
        self.assertEqual(self.business.delete_business(4),
                         'Business does not exist')

    def test_empty_business_id(self):
        """Test whether no business id is provided."""

        self.assertEqual(self.business.delete_business(''),
                         'Business ID is required!')

    def test_negative_integer_business_id_raises_ValueError(self):
        """Test if ValueError is raised when business id a negative number."""

        self.assertEqual(self.business.delete_business(-1),
                         'The business ID must be a positive number')

    def test_non_integer_business_id_raises_TypeError(self):
        """Test if TypeError is raised when business id not a number."""

        self.assertEqual(self.business.delete_business('1'),
                         'The business ID must be an number!')
コード例 #14
0
    def post(self):
        """
        Creating an Business ad
        """
        business_schema = BusinessSchema()

        business_data = request.get_json()

        validated_business_data, errors = business_schema.load(business_data)

        if errors:
            return dict(status='fail', message=errors), 400

        business = Business(**validated_business_data)

        saved_business = business.save()

        if not saved_business:
            return dict(status='fail', message='Internal Server Error'), 500

        new_business_data, errors = business_schema.dumps(business)

        return dict(status='success',
                    data=dict(business=json.loads(new_business_data))), 201
コード例 #15
0
 def get(self, current_user):
     """Get user profile data"""
     user = User.query.filter_by(user_id=current_user.user_id).first()
     businesses = Business.query.filter_by(business_owner_id=current_user.user_id).all()
     business_object = Business.to_json(businesses)
     return {'Message': 'User information retrieved successfully',
             'Status': 'Success',
             'User': {'username': user.username,
                      'email': user.email,
                      'first_name': user.first_name,
                      'last_name': user.last_name,
                      'gender': user.gender
                      },
             'Businesses': business_object["Businesses"]
             }, 200
コード例 #16
0
    def get(self):
        """
        Getting All businesses
        """

        business_schema = BusinessSchema(many=True)

        businesses = Business.find_all()

        businesses_data, errors = business_schema.dumps(businesses)

        if errors:
            return dict(status="fail", message="Internal Server Error"), 500

        return dict(status="success",
                    data=dict(businesses=json.loads(businesses_data))), 200
コード例 #17
0
    def delete(self, business_id):
        """
        Delete a single business
        """

        business = Business.get_by_id(business_id)

        if not business:
            return dict(
                status="fail",
                message=f"Business with id {business_id} not found"), 404

        deleted_business = business.delete()

        if not deleted_business:
            return dict(status='fail', message='Internal Server Error'), 500

        return dict(status='success', message="Successfully deleted"), 200
コード例 #18
0
    def get(self, business_id):
        """
        Getting individual business
        """
        schema = BusinessSchema()

        business = Business.get_by_id(business_id)

        if not business:
            return dict(
                status="fail",
                message=f"Business with id {business_id} not found"), 404

        business_data, errors = schema.dumps(business)

        if errors:
            return dict(status="fail", message=errors), 500

        return dict(status='success',
                    data=dict(business=json.loads(business_data))), 200
コード例 #19
0
from app.models.business import Business

business = Business()
コード例 #20
0
 def delete_business(business_id, user_id):
     """docstring for delete business"""
     return Business.delete_business(business_id, user_id)
コード例 #21
0
 def check_business(business_id):
     """docstring for get business"""
     return Business.get_business(business_id)
コード例 #22
0
 def get_business(business_id):
     """docstring for get business"""
     res = Business.get_business(business_id)
     if res["success"]:
         return jsonify(res), 200
     return jsonify(res), 404
コード例 #23
0
class ViewBusinessTest(unittest.TestCase):
    """Illustrate test cases to test expected behavior of view registered functionality. """
    def setUp(self):
        """Instantiate the Business class so that it can be reused by other test cases."""

        self.business = Business()

    def test_view_all_businesses_records(self):
        """Test whether a dictionary of all the registered businesses will be returned."""

        self.business.create_business(1, 'Cosmas', 'Cosma Tech', 'Nairobi',
                                      'Technology', 'Masters of ecommerce')
        self.business.create_business(2, 'Allan', 'Allan Tech', 'Kitale',
                                      'Technology', 'Cryptocurrency')
        self.assertEqual(
            self.business.view_all_businesses(), {
                1: {
                    'owner': 'Cosmas',
                    'name': 'Cosma Tech',
                    'location': 'Nairobi',
                    'category': 'Technology',
                    'summary': 'Masters of ecommerce'
                },
                2: {
                    'owner': 'Allan',
                    'name': 'Allan Tech',
                    'location': 'Kitale',
                    'category': 'Technology',
                    'summary': 'Cryptocurrency'
                }
            })

    def test_view_business_by_id(self):
        """Test whether a dictionary of the registered information will be returned."""

        self.business.create_business(1, 'Cosmas', 'Cosma Tech', 'Nairobi',
                                      'Technology', 'Masters of ecommerce')
        self.assertEqual(
            self.business.view_business_by_id(1), {
                'owner': 'Cosmas',
                'name': 'Cosma Tech',
                'location': 'Nairobi',
                'category': 'Technology',
                'summary': 'Masters of ecommerce'
            })

    def test_non_existed_business_id_raises_KeyError(self):
        """Test if a KeyError will be raised when the business id does not exist."""

        self.business.create_business(1, 'Cosmas', 'Cosma Tech', 'Nairobi',
                                      'Technology', 'Masters of ecommerce')
        self.business.create_business(2, 'John', 'John Corporate', 'Kitale',
                                      'Fishing', 'Process fish')
        self.business.create_business(3, 'Allan', 'Allan Tech', 'Kitale',
                                      'Technology', 'Cryptocurrency')
        self.assertEqual(self.business.view_business_by_id(4),
                         'Business does not exist')
コード例 #24
0
    def setUp(self):
        """Instantiate the Business class so that it can be reused by other test cases."""

        self.business = Business()