def delete_customer(customer_id): """Delete a customer from the customers database.""" try: connect_customer_db() Customer.get_by_id(customer_id).delete_instance() logger.info(f"{customer_id} deleted from the database.") except DoesNotExist: logger.info(f"{customer_id} does not exist in the database.") raise DoesNotExist finally: db.close()
def test_update_customer_credit(self): """Test updating customer credit.""" reset_db() add_customer("test_id1", "Rhianna", "doesntneedalastname", "543 Sample Address, Winnebago, AK 99302", "3241232455", "*****@*****.**", True, 333.33) # Make sure the current credit limit is 333.33 self.assertEqual( Customer.get_by_id("test_id1").credit_limit, Decimal('333.33')) # Then update the record and make sure it changed update_customer_credit("test_id1", Decimal('1000.00')) self.assertEqual( Customer.get_by_id("test_id1").credit_limit, Decimal('1000.00'))
def test_delete_customer(self): """Test deleting a record from db.""" reset_db() add_customer("test_id1", "Rhianna", "doesntneedalastname", "543 Sample Address, Winnebago, AK 99302", "3241232455", "*****@*****.**", True, 333.33) # Make sure the record exists self.assertEqual(Customer.get_by_id("test_id1").first_name, "Rhianna") # Then delete the record and make sure it's gone delete_customer("test_id1") self.assertEqual(search_customer("test_id1"), {})
def test_add_customer(self): """Test adding new customer to the database.""" reset_db() add_customer("test_id1", "Rhianna", "doesntneedalastname", "543 Sample Address, Winnebago, AK 99302", "3241232455", "*****@*****.**", True, 333.33) test_customer = Customer.get_by_id("test_id1") self.assertEqual(test_customer.first_name, "Rhianna") self.assertEqual(test_customer.phone_number, "3241232455") test_customer.delete_instance()
def test_list_credit_limits(self): """Test listing customers and their credit limits.""" reset_db() add_customer("test_id1", "Rhianna", "doesntneedalastname", "543 Sample Address, Winnebago, AK 99302", "3241232455", "*****@*****.**", True, 333.33) # Make sure the current credit limit is 333.33 self.assertEqual( Customer.get_by_id("test_id1").credit_limit, Decimal('333.33')) expected_result = "doesntneedalastname, Rhianna: 333.33" self.assertEqual(list_credit_limits()[0], expected_result)
def update_customer_credit(customer_id, credit_limit): """Update a customer's credit limit.""" try: connect_customer_db() customer = Customer.get_by_id(customer_id) customer.credit_limit = credit_limit customer.save() logger.info( f"Credit limit for {customer_id} updated to {credit_limit}.") except DoesNotExist as err: logger.info(f"{customer_id} does not exist in the database.") raise DoesNotExist finally: db.close()
def search_customer(customer_id): """ Return dictionary object with a customer's name and contact info if found, otherwise return an empty dictionary object. """ try: connect_customer_db() customer = Customer.get_by_id(customer_id) logger.info(f"{customer_id} found. Returning name and contact info.") return { 'first_name': customer.first_name, 'last_name': customer.last_name, 'email_address': customer.email_address, 'phone_number': customer.phone_number } except DoesNotExist: logger.info(f"{customer_id} does not exist in the database.") return {} finally: db.close()