def test_customer_relationship(self):
        with self.app_context():
            customer = CustomerModel('test_first_name','test_last_name','050-0000000','*****@*****.**','test_city',
                                 'test_address','01-01-2019')
            sale = SaleModel('01-01-2019', 100, 'test_payment_type','test_status',customer.id)

            customer.save_to_db()
            sale.save_to_db()

            self.assertEqual(customer.sales.count(), 1)
            self.assertEqual(customer.sales.first().date, sale.date)
 def test_delete_customer(self):
     with self.app() as c:
         with self.app_context():
             customer = CustomerModel('test_first_name', 'test_last_name',
                                      '050-0000000', '*****@*****.**',
                                      'test_city', 'test_address',
                                      '01-01-2019')
             customer.save_to_db()
             r = c.delete('/customer/1')
             self.assertEqual(r.status_code, 200)
             self.assertDictEqual(d1={'message': 'customer deleted'},
                                  d2=json.loads(r.data))
    def test_crud(self):
        with self.app_context():
            customer = CustomerModel('test_first_name','test_last_name','050-0000000','*****@*****.**','test_city',
                                 'test_address','01-01-2019')

            self.assertIsNone(CustomerModel.find_by_name('test_first_name','test_last_name'), "Found an customer with name 'test_first_name' before save_to_db")

            customer.save_to_db()

            self.assertIsNotNone(CustomerModel.find_by_name('test_first_name','test_last_name'),
                                 "Did not find an customer with name 'test_first_nametest_last_name' after save_to_db")

            customer.delete_from_db()

            self.assertIsNone(CustomerModel.find_by_name('test_first_name','test_last_name'), "Found an customer with name 'test_first_nametest_last_name' after delete_from_db")
示例#4
0
    def test_customer_json(self):

        customer = CustomerModel('test_first_name', 'test_last_name',
                                 '050-0000000', '*****@*****.**', 'test_city',
                                 'test_address', '01-01-2019')
        expected = {
            'first_name': 'test_first_name',
            'last_name': 'test_last_name',
            'phone': '050-0000000',
            'email': '*****@*****.**',
            'city': 'test_city',
            'address': 'test_address',
            'birth_date': '01-01-2019'
        }

        self.assertEqual(
            customer.json(), expected,
            "The JSON export of the customer is incorrect. Received {}, expected {}."
            .format(customer.json(), expected))
示例#5
0
    def get(cls):
        """
        Returns a list of all customers.

        :return: all customers' data.
        :rtype: application/json.
        """
        return {
            'customers':
            [customer.json() for customer in CustomerModel.find_all()]
        }
    def test_customer_list(self):
        with self.app() as c:
            with self.app_context():
                customer = CustomerModel('test_first_name', 'test_last_name',
                                         '050-0000000', '*****@*****.**',
                                         'test_city', 'test_address',
                                         '01-01-2019')
                customer.save_to_db()
                r = c.get('/customers')
                expected = {
                    'first_name': 'test_first_name',
                    'last_name': 'test_last_name',
                    'phone': '050-0000000',
                    'email': '*****@*****.**',
                    'city': 'test_city',
                    'address': 'test_address',
                    'birth_date': '01-01-2019'
                }

                self.assertDictEqual(d1=expected, d2=json.loads(r.data))
    def test_customer_with_sales_found(self):
        with self.app() as c:
            with self.app_context():
                customer = CustomerModel('test_first_name', 'test_last_name',
                                         '050-0000000', '*****@*****.**',
                                         'test_city', 'test_address',
                                         '01-01-2019')
                customer.save_to_db()
                sale = SaleModel('01-01-2019', 100, 'test_payment_type',
                                 'test_status', '1')
                sale.save_to_db()
                r = c.get('/customer/1/sales')

                self.assertEqual(r.status_code, 200)
                expected = {
                    'date': '01-01-2019',
                    'total price': 100,
                    'payment_type': 'test_payment_type',
                    'status': 'test_status',
                    'customer_id': '1',
                }
                self.assertDictEqual(d1=expected, d2=json.loads(r.data))
示例#8
0
    def test_create_customer(self):
        customer = CustomerModel('test_first_name', 'test_last_name',
                                 '050-0000000', '*****@*****.**', 'test_city',
                                 'test_address', '01-01-2019')

        self.assertEqual(
            customer.first_name, 'test_first_name',
            "The test_first_name of the customer after creation does not equal the constructor argument."
        )
        self.assertListEqual(
            customer.sales.all(), [],
            "The customer's sales length was not 0 even though no sales were added."
        )
示例#9
0
    def get(self, _id):
        """
        Finds an customer by its full name and returns it.

        :param id
        :type id: int
        :return: customer data.
        :rtype: application/json.
        """
        customer = CustomerModel.find_by_id(_id)
        if customer:
            return customer.json()
        else:
            return ({'message': 'Customer not found'}, 404)
示例#10
0
    def test_create_customer(self):
        with self.app() as c:
            with self.app_context():
                json_customer = {
                    'first_name': 'test_first_name',
                    'last_name': 'test_last_name',
                    'phone': '050-0000000',
                    'email': '*****@*****.**',
                    'city': 'test_city',
                    'address': 'test_address',
                    'birth_date': '01-01-2019'
                }
                r = c.post('/customer', json=json_customer)

                self.assertEqual(r.status_code, 201)
                self.assertIsNotNone(
                    CustomerModel.find_by_name('test_first_name',
                                               'test_last_name'))
                self.assertDictEqual(d1=json_customer, d2=json.loads(r.data))
示例#11
0
    def delete(self, _id):
        """
        Finds an customer by its name and deletes it.

        :param id: the id of the customer.
        :type int
        :return: success or failure message.
        :rtype: application/json response.
        """
        # claims = get_jwt_claims()
        #
        # if not claims['is_admin']:
        #     return {'message': 'Admin privilege required.'}, 401
        customer = CustomerModel.find_by_id(_id)
        if customer:
            try:
                customer.delete_from_db()
                return {'message': 'Customer deleted'}
            except:
                return ({
                    'message': 'An error occurred deleting the customer.'
                }, 500)
        else:
            return {'message': 'Customer Not Found'}
示例#12
0
 def post(self):
     """
     Creates a new customer using the provided first_name, last_name .
     """
     request_data = Customer.parser.parse_args()
     if CustomerModel.find_by_name(request_data['first_name'],
                                   request_data['last_name']):
         return ({
             'message':
             ("An customer with name '{}' '{}' already exists.").format(
                 request_data['first_name'], request_data['last_name'])
         }, 400)
     customer = CustomerModel(**request_data)
     try:
         customer.save_to_db()
     except:
         return ({
             'message': 'An error occurred inserting the customer.'
         }, 500)
     return (customer.json(), 201)
示例#13
0
    def put(self, _id):
        """
        Creates or updates an customer using the provided name, price and customer_id.

        :param id: the id of the customer.
        :type int:
        :param first_name: the first_name of the customer.
        :type str
        :param last_name: the last_name of the customer.
        :type str

        :return: success or failure message.
        :rtype: application/json response.
        """
        request_data = Customer.parser.parse_args()
        customer = CustomerModel.find_by_id(_id)
        if customer is None:
            customer = CustomerModel(**request_data)
        else:
            customer.first_name = request_data['first_name']
            customer.last_name = request_data['last_name']
            customer.phone = request_data['phone']
            customer.email = request_data['email']
            customer.city = request_data['city']
            customer.address = request_data['address']
            customer.birth_date = request_data['birth_date']
        try:
            customer.save_to_db()
        except:
            return ({
                'message': 'An error occurred updating the customer.'
            }, 500)
        return customer.json()