def test_basic_operations(self): """" Integration test for the basic operations """ DATABASE.drop_tables([Customer]) DATABASE.close() DATABASE.create_tables([Customer]) DATABASE.close() # Add customers to the db add_customers(self.test_customer) cust_found = search_customer(2) self.assertEqual(self.test_customer[1][5], cust_found.get("email")) update_customer_credit(1, 500.00) self.assertEqual(500.00, Customer.get_by_id(1).credit_limit) # Find out how many customers are active active_cust = list_active_customers() self.assertEqual(2, active_cust) # Delete a customer then try to find it delete_customer(2) self.assertDictEqual({}, search_customer(2)) # Find out how many customers are active and list their names self.assertEqual(1, list_active_customers()) self.assertEqual(["Reed Richards"], list_active_customer_names())
def test_list_active_customers(self): '''Tests the list_active_customers whose status is currently active.''' reset_db() # Testing no active customers self.assertEqual(basic_operations.list_active_customers(), 0) basic_operations.add_customers(TEST_LIST) # Testing some active customers self.assertEqual(basic_operations.list_active_customers(), 2)
def add_customers_to_database(self): ''' Method called by multiple tests, adds customers to database ''' # Clear database for tests self.clear_database() # Add customers to database basic_operations.add_customers(self.customer_dict)
def test_search_customers(self): '''Tests search_customers function.''' reset_db() basic_operations.add_customers(TEST_LIST) # Testing the search_customers function customers_search = basic_operations.search_customers([101, 53]) self.assertEqual(customers_search[0]['f_name'], TEST_LIST[0][1]) self.assertEqual(customers_search[1]['f_name'], TEST_LIST[2][1])
def test_search_customer(self): '''Tests search_customer function.''' reset_db() basic_operations.add_customers(TEST_LIST) # Testing the search_customer function self.assertEqual( basic_operations.search_customer(101)['f_name'], TEST_LIST[0][1]) # Test searching for a non-existing customer self.assertEqual(basic_operations.search_customer(3), {})
def test_update_customer_credit(self): '''Tests the update_customer_credit limit function.''' reset_db() basic_operations.add_customers(TEST_LIST) # Testing the update_customer_credit function basic_operations.update_customer_credit(123, 100000) self.assertEqual(Customer.get(Customer.cust_id == 123).credit, 100000) # Testing the update_customer_credit for customers not in the database with self.assertRaises(ValueError): basic_operations.update_customer_credit(42, 1000)
def test_delete_customers(self): '''Tests delete_customers function.''' reset_db() basic_operations.add_customers(TEST_LIST) # Testing the delete_customers function basic_operations.delete_customers([101, 53]) self.assertEqual(basic_operations.search_customer(101), {}) self.assertEqual(basic_operations.search_customer(53), {}) self.assertEqual( basic_operations.search_customer(123)['f_name'], TEST_LIST[1][1])
def test_add_customers(self): """ Test adding a valid customer records to the DB """ set_up_db() add_customers(self.test_customers) test_customer = Customer.get_by_id(1) self.assertEqual(self.test_customers[0][1], test_customer.name) test_customer = Customer.get_by_id(2) self.assertEqual(self.test_customers[1][1], test_customer.name) test_customer = Customer.get_by_id(3) self.assertEqual(self.test_customers[2][1], test_customer.name)
def test_integration(self): '''Integrated test set''' cust_list = [(101, 'Bugs', 'Bunny', '123 NE 160th Ave, Kirkland, WA 98034', '425-123-4567', '*****@*****.**', 'Active', 100.00), (123, 'Donald', 'Duck', '456 SE 45th St, Bellevue, WA 98004', '425-234-5678', '*****@*****.**', 'Active', 500.00), (53, 'Elmer', 'Fudd', '789 W 52nd Pl, Bothell, WA 98077', '425-345-6789', '*****@*****.**', 'Inactive', 12000.00)] test_list = [{'f_name': 'Elmer', 'l_name': 'Fudd', 'email': '*****@*****.**', 'phone': '425-345-6789'}, {'f_name': 'Bugs', 'l_name': 'Bunny', 'email': '*****@*****.**', 'phone': '425-123-4567'}] # Test adding customers basic_operations.add_customers(cust_list) # Verify customers were added self.assertEqual(Customer.get(Customer.cust_id == 101).f_name, cust_list[0][1]) self.assertEqual(Customer.get(Customer.cust_id == 123).f_name, cust_list[1][1]) self.assertEqual(Customer.get(Customer.cust_id == 53).f_name, cust_list[2][1]) # Verify duplicate customers are not added with self.assertRaises(ValueError): basic_operations.add_customer(*cust_list[2]) # Verify able to search for customers customers = basic_operations.search_customers([53, 101, 33]) self.assertEqual(customers[0], test_list[0]) self.assertEqual(customers[1], test_list[1]) # Verify searching for a non-existant customer is empty self.assertEqual(customers[2], {}) # Verify able to update customer credit limit basic_operations.update_customer_credit(123, 100000) cust_1 = Customer.get(Customer.cust_id == 123) self.assertEqual(cust_1.credit, 100000) # Verify customer credit limit cannot be updated for non-customer with self.assertRaises(ValueError): basic_operations.update_customer_credit(42, 1000) # Verify active customers are counted self.assertEqual(basic_operations.list_active_customers(), 2) # Verify a customer can be deleted basic_operations.delete_customers([53, 123]) self.assertEqual(Customer.get(Customer.cust_id == 101).f_name, cust_list[0][1]) self.assertEqual(basic_operations.search_customer(123), {}) self.assertEqual(basic_operations.search_customer(53), {}) # Verify handling of deleting non-customers with self.assertRaises(ValueError): basic_operations.delete_customer(53)
def test_delete_customer(self): '''Tests the delete_customer function.''' reset_db() basic_operations.add_customers(TEST_LIST) # Testing the delete_customer function basic_operations.delete_customer(123) self.assertEqual( basic_operations.search_customer(101)['f_name'], TEST_LIST[0][1]) self.assertEqual( basic_operations.search_customer(53)['f_name'], TEST_LIST[2][1]) self.assertEqual(basic_operations.search_customer(123), {}) # Testing the delete_customer not in the database with self.assertRaises(ValueError): basic_operations.delete_customer(55)
def test_add_customers(self): """ Tests function to add multiple customers """ setup_db() info1 = [ 4, "Marc", "ter Stegen", "400 Las Ramblas", 0000000000, "*****@*****.**", True, 1000000.00 ] info2 = [ 5, "Sergi", "Roberto", "567 Las Ramblas", 5678901234, "*****@*****.**", True, 90000.00 ] add_customers([info1, info2]) customer1 = Customer.get(Customer.customer_id == 4) self.assertEqual("Marc", customer1.name) customer2 = Customer.get(Customer.customer_id == 5) self.assertEqual("Sergi", customer2.name)
def test_add_customers(self): '''Tests add a new customer to the database.''' reset_db() # Test Adding a list of customers customers = basic_operations.add_customers(TEST_LIST) # Test instances of added customers self.assertIsInstance(customers[0], Customer) self.assertIsInstance(customers[1], Customer) self.assertIsInstance(customers[2], Customer) # Verify added customers data self.assertEqual( Customer.get(Customer.cust_id == 101).f_name, TEST_LIST[0][1]) self.assertEqual( Customer.get(Customer.cust_id == 123).f_name, TEST_LIST[1][1]) self.assertEqual( Customer.get(Customer.cust_id == 53).f_name, TEST_LIST[2][1])