def test_queries(self):
     """ Find active/inactive customers with filter """
     cust = Customer(first_name="Peter",
                     last_name="Parker",
                     user_id="pparker",
                     password="******",
                     active=True)
     cust.save()
     addr = Address(apartment="1R",
                    city="Chicago",
                    state="Illinois",
                    street="6721 5th Ave",
                    zip_code="10030",
                    customer_id=cust.customer_id)
     addr.save()
     cust.address_id = addr.id
     cust.save()
     """ Find by first name """
     cust_fname = Customer.find_by_first_name(cust.first_name)
     self.assertEqual(cust_fname[0].customer_id, cust.customer_id)
     """ Find by last name """
     cust_lname = Customer.find_by_last_name(cust.last_name)
     self.assertEqual(cust_lname[0].customer_id, cust.customer_id)
     """ Find by status """
     cust_active = Customer.find_by_status(False)
     self.assertEqual(len(cust_active), 0)
     """ Find by city """
     cust_city = Address.find_by_city(cust.city)
     self.assertEqual(cust_city[0].customer_id, cust.customer_id)
     """ Find by state """
     cust_state = Address.find_by_state(cust.state)
     self.assertEqual(cust_state[0].customer_id, cust.customer_id)
     """ Find by zip code """
     cust_zip = Address.find_zip(cust.zip_code)
     self.assertEqual(cust_zip[0].customer_id, cust.customer_id)
 def test_serialize_a_customer(self):
     """ Test serialization of a customer """
     cust = Customer(
         first_name="Marry",
         last_name="Wang",
         user_id="marrywang",
         password="******",
         active=True,
     )
     cust.save()
     addr = Address(
         street="48 John St",
         apartment="1B",
         city="New York",
         state="New York",
         zip_code="22890",
         customer_id=cust.customer_id,
     )
     addr.save()
     cust.address_id = addr.id
     cust.save()
     data = cust.serialize()
     self.assertNotEqual(data, None)
     self.assertIn('first_name', data)
     self.assertEqual(data['first_name'], "Marry")
     self.assertIn('last_name', data)
     self.assertEqual(data['last_name'], "Wang")
     self.assertIn('user_id', data)
     self.assertEqual(data['user_id'], "marrywang")
    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)
Exemple #5
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}