def test_update_customer_credit_good(): ''' Verify valid credit limit values are udpated ''' bo.update_customer_credit("update_credit", 2000.0) result = Customer.get(Customer.customer_id == "update_credit") assert result.credit_limit == float(2000.0) bo.update_customer_credit("update_credit", "25") result = Customer.get(Customer.customer_id == "update_credit") assert result.credit_limit == float("25")
def _insert_customer_list(customer_list, database): ''' Internal function to add customer list with error checking :param customer_list: sequence of customer dictionaries to insert :param database: database object for the atomic transaction ''' with database.atomic(): try: Customer.insert_many(customer_list).execute() except pw.IntegrityError as error: logging.error(f"Customer already exists: {str(error)}") raise else: LOGGER.info(f"Inserted {len(customer_list)} customer(s)")
def test_add_csv_bulk(self): ''' Adds the contents of the test data csv to the database This tests the bulk_add_customers and convert_csv methods ''' parallel.bulk_add_customers(parallel.convert_csv('customers.csv')) assert Customer.select().count() == 10000
def delete_customer(customer_id): ''' Deletes a customer if it exists, otherwise throws DoesNotExist error :param customer_id: customer_id to search and delete ''' customer = Customer.get(Customer.customer_id == customer_id) customer.delete_instance()
def test_add_csv_bulk(): ''' Adds the contents of the test data csv to the database This tests the bulk_add_customers and convert_csv methods ''' bo.bulk_add_customers( bo.convert_csv('lesson04_assignment_data_customer.csv')) assert Customer.select().count() == 10000 + len(customer_id_list)
def test_list_active_customers(): ''' test list_active_customers count ''' result = bo.list_active_customers() assert result == len(customer_id_list) customer = Customer.get(Customer.customer_id == "active_customers") customer.status = False customer.save() result = bo.list_active_customers() assert result == len(customer_id_list) - 1
def test_add_customer_ok(): ''' Add a new valid customer ''' customer_id = "add_ok" ok_customer = dict(customer_data) ok_customer['customer_id'] = customer_id bo.add_customer(**ok_customer) result = Customer.get(Customer.customer_id == customer_id) assert result.first_name == ok_customer["name"] assert result.last_name == ok_customer["lastname"] assert result.home_address == ok_customer["home_address"] assert result.phone_number == ok_customer["phone_number"] assert result.email_address == ok_customer["email_address"] assert result.status == ok_customer["status"] assert result.credit_limit == float(ok_customer["credit_limit"])
def search_customer(customer_id): ''' Search for a customer :param customer_id: customer id to search for ''' try: customer = Customer.get(Customer.customer_id == customer_id) return { "name": customer.first_name, "lastname": customer.last_name, "email_address": customer.email_address, "phone_number": customer.phone_number } except DoesNotExist: return {}
def search_customer(customer_id=None): ''' Search for a customer :param customer_id: customer id to search for ''' try: customer = Customer.get(Customer.customer_id == customer_id) keys = ("name", "lastname", "email_address", "phone_number") values = ("first_name", "last_name", "email_address", "phone_number") return { key: getattr(customer, value) for key, value in zip(keys, values) } except pw.DoesNotExist: logging.info(f"Customer not found: {customer_id}") return {}
def update_customer_credit(customer_id, credit_limit): ''' Updates the customer credit amount :param customer_id: customer id to search and update :param credit_limit: credit limit value to change to ''' try: credit_limit = float(credit_limit) except ValueError as err: logging.error(err) raise try: customer = Customer.get(Customer.customer_id == customer_id) except DoesNotExist: logging.info(f"Customer does not exist: {customer_id}") raise ValueError(f"Customer_id does not exist: {customer_id}") customer.credit_limit = credit_limit customer.save()
def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): ''' Add a customer ''' try: credit_limit = float(credit_limit) except ValueError as err: logging.error(err) raise if not isinstance(status, bool): logging.error(f"Invalid value: status is not a bool: {status}") raise ValueError(f"Invalid value: status is not a bool: {status}") new_customer = Customer.create(customer_id=customer_id, first_name=name, last_name=lastname, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) new_customer.save()
def list_active_customers(): ''' Returns the number of active customers ''' return Customer.select().where(Customer.status).count()
def get_customers_record_count(): ''' Get the Customers count ''' return Customer.select().count()