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")
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))
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)