Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
 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")
Exemplo n.º 3
0
 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)
Exemplo n.º 4
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)
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    def test_modified_save_function_pobox_vs_house(self):

        first_address = Address()
        first_address.house_number = '3509'
        first_address.road = 'pelican brief ln'
        first_address.postcode = '89084'
        first_address.city = "north las vegas"
        first_address.state = 'nevada'

        first_address.save()

        second_address = Address()
        second_address.po_box = '17'
        second_address.city = 'henderson'
        second_address.state = 'nv'
        second_address.postcode = '89119'

        second_address.save()

        saved_addresses = Address.objects.all()
        self.assertEqual(saved_addresses.count(), 2)

        first_saved_address = saved_addresses[0]
        second_saved_address = saved_addresses[1]

        self.assertEqual(str(first_saved_address),
                         '3509 Pelican Brief Ln, North Las Vegas, NV 89084')
        self.assertEqual(str(second_saved_address),
                         'PO Box 17, Henderson, NV 89119')
Exemplo n.º 7
0
    def test_modified_save_function_super_save(self):

        first_address = Address()
        first_address.house_number = '3509'
        first_address.road = 'Pelican Brief Ln'
        first_address.postcode = '89084'
        first_address.city = "North Las Vegas"
        first_address.state = 'NV'

        first_address.save()

        saved_addresses = Address.objects.all()
        self.assertEqual(saved_addresses.count(), 1)

        first_saved_address = saved_addresses[0]
        first_saved_address.house_number = '3520'
        first_saved_address.save(update_fields=['house_number'])

        self.assertEqual(str(first_saved_address),
                         '3520 Pelican Brief Ln, North Las Vegas, NV 89084')

        second_address = Address()
        second_address.po_box = '17'
        second_address.city = 'henderson'
        second_address.state = 'nv'
        second_address.postcode = '89119'
        second_address.county = 'Clark'
        second_address.save()

        saved_addresses = Address.objects.all()
        self.assertEqual(saved_addresses.count(), 2)

        second_saved_address = saved_addresses[1]
        self.assertEqual(second_saved_address.county, 'Clark')
Exemplo n.º 8
0
    def test_save_unit_address(self):

        first_address = Address()
        first_address.house_number = '3509'
        first_address.road = 'pelican brief ln'
        first_address.unit = '2B'
        first_address.postcode = '89084'
        first_address.city = "north las vegas"
        first_address.state = 'nevada'

        first_address.save()

        saved_addresses = Address.objects.all()
        first_saved_address = saved_addresses[0]
        self.assertEqual(
            str(first_saved_address),
            '3509 Pelican Brief Ln, 2B, North Las Vegas, NV 89084')
Exemplo n.º 9
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}
Exemplo n.º 10
0
    def test_creating_containers_with_addresses(self):

        first_address = Address()
        first_address.house_number = '3509'
        first_address.road = 'Pelican Brief Ln'
        first_address.postcode = '89084'
        first_address.city = 'North Las Vegas'
        first_address.state = 'Nevada'
        first_address.country = 'USA'
        first_address.save()

        first_container = Container()
        first_container.name = 'Tardis'
        first_container.save()
        first_container.address.add(first_address)

        saved_containers = Container.objects.all()
        self.assertEqual(saved_containers.count(), 1)

        first_saved_container = saved_containers[0]
        self.assertEqual(first_saved_container.address.all()[0].city,
                         'North Las Vegas')