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 get(self): """ Returns all of the Customers """ app.logger.info('Request for customers list...') customers = [] args = customer_args.parse_args() if args['fname']: app.logger.info('Filtering by first name: %s', args['fname']) customers = Customer.find_by_first_name(args['fname']) elif args['lname']: app.logger.info('Filtering by last name: %s', args['lname']) customers = Customer.find_by_last_name(args['lname']) elif args['city']: app.logger.info('Filtering by city: %s', args['city']) customers = Address.find_by_city(args['city']) elif args['state']: app.logger.info('Filtering by state: %s', args['state']) customers = Address.find_by_state(args['state']) elif args['zip_code']: app.logger.info('Filtering by zip code: %s', args['zip_code']) customers = Address.find_by_zip(args['zip_code']) else: app.logger.info('Getting all customers') customers = Customer.all() results = [cust.serialize() for cust in customers] return results, status.HTTP_200_OK