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 test_add_a_customer(self): """ Create a customer and add it to the database """ customers = Customer.all() self.assertEqual(customers, []) customer = Customer(firstname="John", lastname="Doe", email="*****@*****.**", subscribed=False, address1="123 Main St", address2="1B", city="New York", country="USA", province="NY", zip="12310") self.assertTrue(customer != None) self.assertEqual(customer.id, None) customer.save() # Asert that it was assigned an id and shows up in the database self.assertNotEqual(customer.id, None) customers = Customer.all() self.assertEqual(len(customers), 1) self.assertNotEqual(customer.id, None) customers = Customer.all() self.assertEqual(len(customers), 1) self.assertEqual(customers[0].firstname, "John") self.assertEqual(customers[0].lastname, "Doe") self.assertEqual(customers[0].email, "*****@*****.**") self.assertEqual(customers[0].subscribed, False) self.assertEqual(customers[0].address1, "123 Main St") self.assertEqual(customers[0].address2, "1B") self.assertEqual(customers[0].city, "New York") self.assertEqual(customers[0].province, "NY") self.assertEqual(customers[0].country, "USA") self.assertEqual(customers[0].zip, "12310")
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("Arturo", "Frank", "USA", "*****@*****.**", "IAmUser", "password", "1231231234", True, 1) customer.save() time.sleep(WAIT_SECONDS) self.assertEqual(len(Customer.all()), 1) # delete the customer and make sure it isn't in the database customer.delete() self.assertEqual(len(Customer.all()), 0)
def test_delete_a_customer(self): """ Delete a Customer """ customer = Customer( first_name="Harry", last_name="Potter", email="*****@*****.**", address="9 3/4 Gryffindor Lane", active=True, ) customer.create() self.assertEqual(len(Customer.all()), 1) customer.delete() self.assertEqual(len(Customer.all()), 0)
def test_add_a_customer(self): """ Create a customer and add it to the database """ customers = Customer.all() time.sleep(WAIT_SECONDS) self.assertEqual(customers, []) customer = Customer("Arturo", "Frank", "USA", "*****@*****.**", "IAmUser", "password", "1231231234", True, 1) self.assertTrue(customer != None) self.assertEqual(customer._id, None) customer.save() time.sleep(WAIT_SECONDS) self.assertNotEqual(customer._id, None) customers = Customer.all() time.sleep(WAIT_SECONDS) self.assertEqual(len(customers), 1)
def get(self): """ Returns all of the Customers unless a query parameter is specified """ app.logger.info('Request to list Customers...') customers = [] app.logger.info("First time") # args = customer_args.parse_args() # change to request args to by pass the odd bug for reqparse args = request.args app.logger.info("Second time %s", args) if args.get('last_name'): app.logger.info('Filtering by last name: %s', args['last_name']) customers = Customer.find_by_last_name(args['last_name']) elif args.get('first_name'): app.logger.info('Filtering by first name: %s', args['first_name']) customers = Customer.find_by_first_name(args['first_name']) elif args.get('email'): app.logger.info('Filtering by email: %s', args['email']) customers = Customer.find_by_email(args['email']) elif args.get('address'): app.logger.info('Filtering by address: %s', args['address']) customers = Customer.find_by_address(args['address']) elif args.get('active'): app.logger.info('Filtering by active: %s', args['active']) customers = Customer.find_by_active(args['active']) else: customers = Customer.all() results = [customer.serialize() for customer in customers] app.logger.info('[%s] Customers returned', len(results)) return results, status.HTTP_200_OK
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_update_cust_email_and_name(self): """ Update a Customer email and name""" customer = Customer(firstname="John", lastname="Doe", email="*****@*****.**", subscribed=False, address1="123 Main St", address2="1B", city="New York", country="USA", province="NY", zip="12310") customer.save() self.assertNotEqual(customer.id, None) # Change it an save it customer.email = "*****@*****.**" customer.firstname = "Isabel" customer.save() self.assertNotEqual(customer.id, None) # Fetch it back and make sure the id hasn't changed # but the data did change customers = customer.all() self.assertEqual(len(customers), 1) self.assertEqual(customers[0].email, "*****@*****.**") self.assertEqual(customers[0].firstname, "Isabel")
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
def test_delete_a_customer(self): """ Delete a Customer """ customer = Customer(firstname="John", lastname="Doe", email="*****@*****.**", subscribed=False, address1="123 Main St", address2="1B", city="New York", country="USA", province="NY", zip="12310") customer.save() self.assertEqual(len(customer.all()), 1) # delete the customer and make sure it isn't in the database customer.delete() self.assertEqual(len(customer.all()), 0)
def test_add_a_customer(self): """Create a customer and add it to the database""" customers = Customer.all() self.assertEqual(customers, []) customer = Customer( first_name="John", last_name="Doe", email="*****@*****.**", address="456 Bronx Ave", active=True, ) self.assertTrue(customer is not None) self.assertEqual(customer.id, None) customer.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(customer.id, 1) customers = Customer.all() self.assertEqual(len(customers), 1)
def test_remove_all_customers(self): """ Remove all customers """ customer = Customer(first_name="Marry", last_name="Wang", user_id="marrywang", password="******", active=True, address_id=100) customer.save() customer = Customer(first_name="Marry", last_name="Jane", user_id="marryjane", password="******", active=True, address_id=200) customer.save() all_customers = Customer.all() self.assertEquals(len(all_customers), 2) customer.remove_all() all_customers = Customer.all() self.assertEquals(len(all_customers), 0)
def get(self): """query and get the intersection of the queries. if there is no given query return all the list Args: **par: parameter of query empty equry return: 1. the intersection of the parameters 2. empty equry will return all the data """ app.logger.info('Query a Customer with query') app.logger.info('Queries are: {}'.format( request.args.items().__str__())) username = request.args.get('username') address = request.args.get('address') if username: # Query customers by name app.logger.info('Filtering by username:%s', username) results = Customer.find_by_name(username) message = [customer.serialize() for customer in results] return_code = status.HTTP_200_OK elif address: # Query customers by name app.logger.info('Filtering by username:%s', username) results = Customer.find_by_address(address) message = [customer.serialize() for customer in results] return_code = status.HTTP_200_OK elif request.args: # Query customers by query #key = request.args.keys() app.logger.info('Filtering by query:%s', request.args.keys()) results = Customer.find_by(kwargs=request.args) #results = Customer.find_by_query({key:request.args.get(key)}) message = [customer.serialize() for customer in results] return_code = status.HTTP_200_OK else: # List all customers. results = Customer.all() message = [customer.serialize() for customer in results] return_code = status.HTTP_200_OK return message, return_code
def test_update_a_customer(self): """ Update a Customer""" customer = Customer("Arturo", "Frank", "USA", "*****@*****.**", "IAmUser", "password", "1231231234", True, 1) customer.save() time.sleep(WAIT_SECONDS) self.assertNotEqual(customer._id, None) # Change it an save it customer.first_name = "k9" customer.save() time.sleep(WAIT_SECONDS) # Fetch it back and make sure the id hasn't changed # but the data did change customers = Customer.all() time.sleep(WAIT_SECONDS) self.assertEqual(len(customers), 1) self.assertEqual(customers[0].first_name, "k9")
def test_update_a_customer(self): """ Update a customer """ customer = Customer(first_name="Marry", last_name="Wang", user_id="marrywang", password="******", active=True, address_id=100) customer.save() self.assertEqual(customer.customer_id, 1) # Change it an save it customer.password = "******" customer.save() self.assertEqual(customer.customer_id, 1) # Fetch it back and make sure the id hasn't changed # but the data did change customer = Customer.all() self.assertEqual(len(customer), 1) self.assertEqual(customer[0].password, "DevOps is awesome")
def test_update_a_customer(self): """ Update a Customer """ customer = Customer( first_name="John", last_name="Smith", email="*****@*****.**", address="123 Brooklyn Ave", active=True, ) customer.create() self.assertEqual(customer.id, 1) # Change it an update it customer.address = "Times Sq 42nd St" customer.update() self.assertEqual(customer.id, 1) # Fetch it back and make sure the id hasn't changed # but the data did change customers = Customer.all() self.assertEqual(len(customers), 1) self.assertEqual(customers[0].address, "Times Sq 42nd St")
def test_list_all_customers(self): """ Create two customers and add them to the database, then obtain list of all customers and ensure it is = 2 """ cust1 = Customer(first_name="Jane", last_name="Doe", user_id="janedoe", password="******", active=True) cust1.save() cust2 = Customer( first_name="John", last_name="Doe", user_id="johndoe", password="******", active=True, ) cust2.save() all_customers = Customer.all() self.assertEquals(len(all_customers), 2)