示例#1
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()
    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")
示例#5
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)
    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))