def test_delete_customer(_customers): """Test delete customer """ for customer in _customers: assert l.delete_customer(customer[0]) is False for customer in _customers: l.add_customer( customer[0], customer[1], customer[2], customer[3], customer[4], customer[5], customer[6], customer[7], ) for customer in _customers: assert l.delete_customer(customer[0]) is True # Verify that the database is again empty. for customer in _customers: with pytest.raises(NoResultFound): query = l.SESSION.query(l.db.Customer).filter( l.db.Customer.customer_id == customer[0] ) query.one()
def test_search_customer(_search_customers): """Tests search """ for customer in _search_customers[0]: l.add_customer( customer[0], customer[1], customer[2], customer[3], customer[4], customer[5], customer[6], customer[7], ) result = l.search_customer(_search_customers[1][1]) assert result == {} result = l.search_customer(_search_customers[1][0]) assert result["name"] == _search_customers[0][1][1] assert result["lastname"] == _search_customers[0][1][2] assert result["email"] == _search_customers[0][1][5] assert result["phone_number"] == _search_customers[0][1][4] for customer in _search_customers[0]: print(customer[0]) l.delete_customer(customer[0])
def test_update_customer_credit(_update_customer_credit): """Tests update credit """ for customer in _update_customer_credit: l.add_customer( customer[0], customer[1], customer[2], customer[3], customer[4], customer[5], customer[6], customer[7], ) l.update_customer_credit("798", 0) l.update_customer_credit("797", 1000) l.update_customer_credit("797", -42) l.update_customer_credit("796", 500) with pytest.raises(ValueError) as excinfo: l.update_customer_credit("00100", 1000) # error assert "NoCustomer" in str(excinfo.value) for customer in _update_customer_credit: l.delete_customer(customer[0])
def test_list_active_customer_when_there_are_none(self): """Test the function list_active_customers when no active customers exist""" LOGGER.info("*** TEST ***: list_active_customers when none") # Given basic_operations.delete_customer("FY2020-001") # When self.assertEqual(0, basic_operations.list_active_customers())
def test_list_active_customers(_list_active_customers): """ actives """ for customer in _list_active_customers: l.add_customer(customer[0], customer[1], customer[2], customer[3], customer[4], customer[5], customer[6], customer[7]) actives = l.list_active_customers() assert actives == 4 for customer in _list_active_customers: l.delete_customer(customer[0])
def test_add_customer(_add_customers): """ additions """ for customer in _add_customers: l.add_customer(*customer) added = l.search_customer(customer[0]) assert added["name"] == customer[1] assert added["last_name"] == customer[2] assert added["email_address"] == customer[5] assert added["phone_number"] == customer[4] for customer in _add_customers: l.delete_customer(customer[0])
def test_delete_customer_does_not_exist(self): """Test if trying to delete non-existing customer""" # Given # When # Then try: # Just log warning, no exception and no failure basic_operations.delete_customer("Scooby") except Exception as e_val: self.fail(e_val)
def test_list_active_customers(_list_active_customers): """ actives """ for customer in _list_active_customers: l.add_customer(*customer) actives = l.list_active_customers() assert actives == 4 for customer in _list_active_customers: l.delete_customer(customer[0]) assert l.list_active_customers() == 0
def test_add_customer(_add_customers): """ additions """ for customer in _add_customers: l.add_customer(customer[0], customer[1], customer[2], customer[3], customer[4], customer[5], customer[6], customer[7]) added = l.search_customer(customer[0]) assert added["name"] == customer[1] assert added["lastname"] == customer[2] assert added["email"] == customer[5] assert added["phone_number"] == customer[4] for customer in _add_customers: l.delete_customer(customer[0])
def test_list_active_customers(self): """Test that the function lists all active customers""" # Given basic_operations.add_customer(*CUSTOMER2) basic_operations.add_customer(*CUSTOMER3) # When # Then self.assertEqual(2, basic_operations.list_active_customers()) basic_operations.delete_customer(CUSTOMER2[0]) basic_operations.delete_customer(CUSTOMER3[0])
def test_delete_customer_does_not_exist(self): """Test if trying to delete non-existing customer""" LOGGER.info("*** TEST ***:delete_customer for non-existing customer") # Given # When # Then try: # Just log warning, no exception and no failure basic_operations.delete_customer("Scooby") except DoesNotExist as e_val: self.fail(e_val)
def test_add_customer(_add_customers): """ additions """ while True: try: customer = next(_add_customers) except StopIteration: break l.add_customer(customer[0], customer[1], customer[2], customer[3], customer[4], customer[5], customer[6], customer[7]) added = l.search_customer(customer[0]) assert added["name"] == customer[1] assert added["lastname"] == customer[2] assert added["email"] == customer[5] assert added["phone_number"] == customer[4] l.delete_customer(customer[0])
def test_list_active_customers(_list_active_customers): """ actives """ active_count = 0 for customer in _list_active_customers: if customer[-2] == 'active': active_count += 1 l.add_customer(customer[0], customer[1], customer[2], customer[3], customer[4], customer[5], customer[6], customer[7]) actives = l.list_active_customers() assert actives == active_count for customer in _list_active_customers: l.delete_customer(customer[0])
def test_delete_customer(self): """Test that you can delete a customer""" add_test_customer() added_customer = Customers.get(Customers.customer_id == '001') actual_customer = (added_customer.customer_id, added_customer.name, added_customer.lastname, added_customer.home_address, added_customer.phone_number, added_customer.email_address, added_customer.status, added_customer.credit_limit) expected_customer = ('001', 'Joey', 'Smith', '123 Jackson Street', '1234567890', '*****@*****.**', 'active', 1000.00) basic_operations.delete_customer('0001') self.assertEqual(actual_customer, expected_customer) LOGGER.info('Closing database') DATABASE.close() basic_operations.delete_customer('001') try: DATABASE.connect() DATABASE.execute_sql('PRAGMA foreign_keys = ON;') LOGGER.info('Successfully connected to the database') searched_customer = Customers.get(Customers.customer_id == '001') LOGGER.info('Customer Found!') deleted_customer = { 'customer_id': searched_customer.customer_id, 'name': searched_customer.name, 'lastname': searched_customer.lastname, 'home_address': searched_customer.home_address, 'phone_number': searched_customer.phone_number, 'email_address': searched_customer.email_address, 'status': searched_customer.status, 'credit_limit': searched_customer.credit_limit } except Exception as ex: LOGGER.info('Error finding customer 001') LOGGER.info(ex) deleted_customer = {} self.assertEqual(deleted_customer, {}) LOGGER.info('Closing database') DATABASE.close()
def test_full_integration(self): """Integrates all methods into one test""" basic_operations.add_customer('004', 'Joey', 'Smith', '123 Jackson Street', 1234567890, '*****@*****.**', 'active', 1000.00) basic_operations.update_customer_credit('004', 2000) basic_operations.update_status('004', 'inactive') expected_customer = { 'customer_id': '004', 'name': 'Joey', 'lastname': 'Smith', 'home_address': '123 Jackson Street', 'phone_number': '1234567890', 'email_address': '*****@*****.**', 'status': 'inactive', 'credit_limit': 2000.00 } actual_searched_customer = basic_operations.search_customer('004') self.assertEqual(actual_searched_customer, expected_customer) actual_customer_count = basic_operations.list_active_customers() self.assertEqual(actual_customer_count, 0) basic_operations.delete_customer('004') try: DATABASE.connect() DATABASE.execute_sql('PRAGMA foreign_keys = ON;') LOGGER.info('Successfully connected to the database') searched_customer = Customers.get(Customers.customer_id == '004') LOGGER.info('Customer Found!') deleted_customer = { 'customer_id': searched_customer.customer_id, 'name': searched_customer.name, 'lastname': searched_customer.lastname, 'home_address': searched_customer.home_address, 'phone_number': searched_customer.phone_number, 'email_address': searched_customer.email_address, 'status': searched_customer.status, 'credit_limit': searched_customer.credit_limit } except Exception as ex: LOGGER.info('Error finding customer 004') LOGGER.info(ex) deleted_customer = {} self.assertEqual(deleted_customer, {}) LOGGER.info('Closing database') DATABASE.close()
def test_main(_init_data): """ Integration tests combining add, delete, update, and list """ # Adding some data to start for customer in _init_data: l.add_customer(customer[0], customer[1], customer[2], customer[3], customer[4], customer[5], customer[6], customer[7]) assert _init_data[0][1] == l.search_customer(_init_data[0][0])['name'] assert _init_data[1][2] == l.search_customer(_init_data[1][0])['last_name'] # Delete a customer deleted = l.delete_customer(594) assert deleted is True # Search for deleted customer, verify empty dict is returned gone = l.search_customer(594) assert gone == {} # Test updating customer credit limit updated = l.update_customer_credit(593, 1500) assert updated == 1 new_credit = l.search_customer(593)['credit_limit'] assert new_credit == 1500 # Test active customer count active = l.list_active_customers() assert active == 3
def test_search_customer(_search_customers): """ search """ for customer in _search_customers[0]: l.add_customer(*customer) result = l.search_customer(_search_customers[1][1]) assert result == {} result = l.search_customer(_search_customers[1][0]) assert result["name"] == _search_customers[0][1][1] assert result["last_name"] == _search_customers[0][1][2] assert result["email_address"] == _search_customers[0][1][5] assert result["phone_number"] == _search_customers[0][1][4] for customer in _search_customers[0]: l.delete_customer(customer[0])
def test_list_active_emails(self): """Test the function list_active_emails""" LOGGER.info("*** TEST ***: list_active_emails") # Given basic_operations.add_customer(*CUSTOMER2) basic_operations.add_customer(*CUSTOMER3) # When expected_emails_list = ["*****@*****.**", "*****@*****.**"] # Then actual_email_list = basic_operations.list_active_emails() LOGGER.info(actual_email_list) self.assertEqual(expected_emails_list, actual_email_list) basic_operations.delete_customer(CUSTOMER2[0]) basic_operations.delete_customer(CUSTOMER3[0])
def test_delete_customer_exists(self): """Test if the delete_customer function works for existing customers""" # Given expected_customer = { "name": CUSTOMER1[1], "last_name": CUSTOMER1[2], "phone_number": CUSTOMER1[4], "email_address": CUSTOMER1[5], } # When actual_customer = basic_operations.search_customer("FY2020-001") self.assertEqual(expected_customer, actual_customer) basic_operations.delete_customer("FY2020-001") actual_customer = basic_operations.search_customer("FY2020-001") self.assertEqual({}, actual_customer)
def test_search_customer(_search_customers): """ search """ for customer in _search_customers: l.add_customer(customer[0], customer[1], customer[2], customer[3], customer[4], customer[5], customer[6], customer[7]) result = l.search_customer(1000) # check nonexistent customer assert result == {} result = l.search_customer(_search_customers[1][0]) assert result["name"] == _search_customers[1][1] assert result["last_name"] == _search_customers[1][2] assert result["email_address"] == _search_customers[1][5] assert result["phone_number"] == _search_customers[1][4] for customer in _search_customers: l.delete_customer(customer[0])
def test_delete_customer(_delete_customers): """ delete """ for customer in _delete_customers: l.add_customer(*customer) response = l.delete_customer(customer[0]) assert response is True deleted = l.search_customer(customer[0]) assert deleted == {}
def test_delete_customer(_delete_customers): """ delete """ for customer in _delete_customers: l.add_customer(customer[0], customer[1], customer[2], customer[3], customer[4], customer[5], customer[6], customer[7]) response = l.delete_customer(customer[0]) assert response is True deleted = l.search_customer(customer[0]) assert deleted == {}
def test_list_active_customers(_list_active_customers): """ actives """ for customer in _list_active_customers: try: l.add_customer(customer[0], customer[1], customer[2], customer[3], customer[4], customer[5], customer[6], customer[7]) except IndexError as e: print(e) print( 'Index out of range. Cannot add customer that doesn\'t exist') print(l.list_active_customers) actives = l.list_active_customers() assert len(actives) == 4 try: for customer in _list_active_customers: l.delete_customer(customer[0]) except IndexError: pass
def test_delete_customer(_customers): """test delete customer """ for customer in _customers: assert l.delete_customer(customer[0]) is False for customer in _customers: l.add_customer( customer[0], customer[1], customer[2], customer[3], customer[4], customer[5], customer[6], customer[7], ) for customer in _customers: assert l.delete_customer(customer[0]) is True
def test_search_customer(_search_customers): """ search """ while True: try: customer = next(_search_customers) # if customer == _search_customers[-1]: # break except StopIteration: break l.add_customer(customer[0], customer[1], customer[2], customer[3], customer[4], customer[5], customer[6], customer[7]) result = l.search_customer(customer[1][1]) assert result == {} result = l.search_customer(customer[0]) assert result["name"] == customer[1] assert result["lastname"] == customer[2] assert result["email"] == customer[5] assert result["phone_number"] == customer[4] l.delete_customer(customer[0])
def test_update_customer_credit(_update_customer_credit): """ update """ for customer in _update_customer_credit: l.add_customer(*customer) l.update_customer_credit("798", 0) assert l.search_customer("798")['credit_limit'] == 0 l.update_customer_credit("797", 1000) assert l.search_customer("797")['credit_limit'] == 1000 l.update_customer_credit("797", -42) assert l.search_customer("797")['credit_limit'] == -42 l.update_customer_credit("796", 500) assert l.search_customer("796")['credit_limit'] == 500 with pytest.raises(ValueError) as excinfo: l.update_customer_credit("00100", 1000) # error assert 'NoCustomer' in str(excinfo.value) for customer in _update_customer_credit: assert l.delete_customer(customer[0]) == True
def test_integration_basic_operation(_customer): for customer in _customer: l.add_customer(*customer) for customer in _customer: assert l.search_customer(customer[0])['name'] == customer[1] assert l.search_customer(customer[0])['last_name'] == customer[2] with pytest.raises(IntegrityError) as e: l.add_customer(*_customer[0]) assert "DoubleEntry" in str(e.value) l.update_customer_credit("C000000", 10000) assert l.search_customer('C000000')['credit_limit'] == 10000 with pytest.raises(ValueError) as e: l.update_customer_credit("C001011", 10) assert 'NoCustomer' in str(e.value) assert l.list_active_customers() == 9 for customer in _customer: assert l.delete_customer(customer[0]) is True assert l.search_customer(customer[0]) == {}
def test_basic_operations(self): """Test all basic operations in one integration test""" # Add customer basic_operations.add_customer( "TEST001", "Matt", "Casari", "Washington", "999-999-9999", "*****@*****.**", True, 500000.00, ) # Add a second customer basic_operations.add_customer( "TEST002", "Ringo", "Bingo", "California", "999-999-9990", "*****@*****.**", True, 20000.00, ) # Try to add another customer TEST002 basic_operations.add_customer( "TEST002", "Ringo", "Bingo", "California", "999-999-9990", "*****@*****.**", True, 20000.00, ) # Update customer 1 credit basic_operations.update_customer_credit("TEST001", 999999.99) # Delete customer 2 basic_operations.delete_customer("TEST002") # Update customer 2 credit (who no longer exists) basic_operations.update_customer_credit("TEST002", 55.99) # Delete a non-existing customer basic_operations.delete_customer("Happy1") # Search customer 1 customer1 = basic_operations.search_customer("TEST001") # Search customer 2 (who no longer exists) customer2 = basic_operations.search_customer("TEST002") # list number customers customer_count = basic_operations.list_active_customers() # Get the email list email_list = basic_operations.list_active_emails() # Assert all self.assertEqual({}, customer2) self.assertEqual("Matt", customer1["name"]) self.assertEqual("999-999-9999", customer1["phone_number"]) self.assertEqual(1, customer_count) self.assertEqual(["*****@*****.**"], email_list) # Delete the remaining customer basic_operations.delete_customer("TEST001") # Mark end of test LOGGER.info("END: test_integration")
def test_delete_customer(self): bo.add_customer(1, 'Lady', 'Gaga', '453 Hollywood Blvd', 555334290, '*****@*****.**', True, 20000) self.assertEqual(True, bo.delete_customer(1)) self.assertEqual(False, bo.delete_customer(5))
def test_delete_customer(self): bo.add_customer('C00002', 'Lady', 'Gaga', '453 Hollywood Blvd', '555334290', '*****@*****.**', 'Active', '20000') self.assertEqual(True, bo.delete_customer('C00002')) self.assertEqual(False, bo.delete_customer('C00003'))