def test_remove_all_addresses(self): """ Remove all addresses """ custs = Customer.all() self.assertEqual(custs, []) cust = Customer(first_name="Marry", last_name="Wang", user_id="marrywang", password="******", active=True) self.assertTrue(cust != None) self.assertEqual(cust.customer_id, None) self.assertEqual(cust.address_id, None) cust.save() addr = Address( street="100 W 100th St.", apartment="100", city="New York", state="New York", zip_code="10035", ) addr.customer_id = cust.customer_id addr.save() all_addresses = Address.all() self.assertEquals(len(all_addresses), 1) addr.remove_all() all_addresses = Address.all() self.assertEquals(len(all_addresses), 0)
def test_add_a_customer(self): """ Create a address and add it to the database, then create a customer with the address.id and add it to the database """ custs = Customer.all() self.assertEqual(custs, []) cust = Customer(first_name="Marry", last_name="Wang", user_id="marrywang", password="******", active=True) self.assertTrue(cust != None) self.assertEqual(cust.customer_id, None) self.assertEqual(cust.address_id, None) cust.save() addr = Address( street="100 W 100th St.", apartment="100", city="New York", state="New York", zip_code="10035", ) addr.customer_id = cust.customer_id addr.save() cust.address_id = addr.id # Asert that it was assigned an id and shows up in the database self.assertEqual(addr.id, 1) custs = Customer.all() self.assertEqual(len(custs), 1) self.assertEqual(cust.customer_id, 1) custs = Customer.all() self.assertEqual(len(custs), 1)
def test_delete_a_customer(self): """ Delete a customer """ customer = Customer(first_name="Marry", last_name="Wang", user_id="marrywang", password="******", active=True) customer.save() address = Address(street="100 W 100 St.", apartment="100", city="New York", state="New York", zip_code="100") address.customer_id = customer.customer_id address.save() customer.address_id = address.id customer.save() self.assertEqual(len(Customer.all()), 1) self.assertEqual(len(Address.all()), 1) # delete the customer and make sure it isn't in the database customer.delete() self.assertEqual(len(Customer.all()), 0) self.assertEqual(len(Address.all()), 0)
def post(self): """ Creates a Customer This endpoint will create a Customer based the data in the body that is posted """ app.logger.info('Request to create a customer') check_content_type('application/json') cust = Customer() cust.deserialize(api.payload) cust.save() customer_id = cust.customer_id addr = Address() addr.deserialize(api.payload['address']) addr.customer_id = customer_id addr.save() cust.address_id = addr.id cust.save() message = cust.serialize() location_url = api.url_for(CustomerResource, user_id=cust.user_id, _external=True) return message, status.HTTP_201_CREATED, {'Location': location_url}