def test_delete_sale(self): with self.app() as c: with self.app_context(): sale = SaleModel('01-01-2019', 100, 'test_payment_type', 'test_status', '1') sale.save_to_db() r = c.delete('/sale/1') self.assertEqual(r.status_code, 200) self.assertDictEqual(d1={'message': 'Sale deleted'}, d2=json.loads(r.data))
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 post(self): """ Creates a new sale using the provided name, price and sale_id. :return: success or failure message. :rtype: application/json response. """ request_data = Sale.parser.parse_args() sale = SaleModel(**request_data) try: sale.save_to_db() except: return ({'message': 'An error occurred inserting the sale .'}, 500) return (sale.json(), 201)
def put(self, _id): """ Creates or updates an sale using the provided name, price and sale_id. :param id: the id of the sale . :type int :return: success or failure message. :rtype: application/json response. """ request_data = Sale.parser.parse_args() sale = SaleModel.find_by_id(_id) if sale is None: sale = sale(**request_data) else: sale.date = request_data['date'] sale.total_price = request_data['total_price'] sale.payment_type = request_data['payment_type'] sale.status = request_data['status'] sale.customer_id = request_data['customer_id'] try: sale.save_to_db() except: return ({ 'message': 'An error occurred updating the sale .' }, 500) else: return sale.json()
def setUp(self): super(SaleTest, self).setUp() with self.app() as c: with self.app_context(): sale = SaleModel('01-01-2019', 100, 'test_payment_type', 'test_status', '1') sale.save_to_db() auth_request = c.post( '/auth', data=json.dumps({ 'username': '******', 'password': '******' }), headers={'Content-Type': 'application/json'}) self.auth_header = "JWT {}".format( json.loads(auth_request.data)['access_token'])
def get(cls): """ Returns a list of all sales. :return: all sales' data. :rtype: application/json. """ return {'sale': [sale.json() for sale in SaleModel.find_all()]}
def test_create_duplicate_item(self): with self.app() as c: with self.app_context(): sale = SaleModel('01-01-2019', 100, 'test_payment_type', 'test_status', '1') sale.save_to_db() sale_data = { 'date': '01-01-2019', 'total price': 100, 'payment_type': 'test_payment_type', 'status': 'test_status', 'customer_id': '1', } c.post('/sale', data=sale_data) r = c.post('/sale', data=sale_data) self.assertEqual(r.status_code, 400)
def test_sale_found(self): with self.app() as c: with self.app_context(): sale = SaleModel('01-01-2019', 100, 'test_payment_type', 'test_status', '1') sale.save_to_db() r = c.get('/sale/1', headers={'Authorization': self.auth_header}) expected = { 'date': '01-01-2019', 'total price': 100, 'payment_type': 'test_payment_type', 'status': 'test_status', 'customer_id': '1', } self.assertEqual(r.status_code, 200) self.assertDictEqual(d1=expected, d2=json.loads(r.data))
def test_item_list(self): with self.app() as c: with self.app_context(): sale = SaleModel('01-01-2019', 100, 'test_payment_type', 'test_status', '1') sale.save_to_db() r = c.get('/sales') sale_data = { 'date': '01-01-2019', 'total price': 100, 'payment_type': 'test_payment_type', 'status': 'test_status', 'customer_id': '1', } self.assertDictEqual(d1={'sales': sale_data}, d2=json.loads(r.data))
def test_put_item(self): with self.app() as c: with self.app_context(): sale = SaleModel('01-01-2019', 100, 'test_payment_type', 'test_status', '1') sale.save_to_db() sale_data = { 'date': '01-01-2019', 'total price': 100, 'payment_type': 'test_payment_type', 'status': 'test_status', 'customer_id': '1', } r = c.put('/sale', data=sale_data) self.assertEqual(r.status_code, 200) self.assertEqual( SaleModel.find_by_date('01-01-2019', '1').total_price, 100) self.assertDictEqual(d1=sale_data, 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))
def get(self, _id): """ Finds an sale by its name and returns it. :param id: the id of the sale. :type str :return: sale data. :rtype: application/json. """ sale = SaleModel.find_by_id(_id) if sale: return sale.json() else: return ({'message': 'sale not found'}, 404)
def delete(self, _id): """ Finds an sale by its name and deletes it. :param name: the name of the sale. :type name: str :return: success or failure message. :rtype: application/json response. """ sale = SaleModel.find_by_id(_id) if sale: try: sale.delete_from_db() return {'message': 'sale deleted'} except: return ({ 'message': 'An error occurred deleting the sale .' }, 500) else: return {'message': 'Sale Not Found'}