示例#1
0
def add_customer(
        customer_id,
        name,
        last_name,
        home_address,
        phone_number,
        email_address,
        status,
        credit_limit,
):
    """
    This function adds a new customer to the sqlite3 database.
    """
    logging.debug("Adding new customer, %s %s to database", name, last_name)
    try:
        Customers.create(
            customer_id=customer_id,
            name=name,
            last_name=last_name,
            home_address=home_address,
            phone_number=phone_number,
            email_address=email_address,
            status=status,
            credit_limit=credit_limit,
        )
    except Exception as e_val:
        logging.warning("Customer %s already exists", customer_id)
        logging.warning(e_val)
示例#2
0
    def test_add_customer(self):
        """Testing ability to add customer to database"""
        # Given

        # When
        basic_operations.add_customer(*CUSTOMER1)
        basic_operations.add_customer(*CUSTOMER2)
        basic_operations.add_customer(*CUSTOMER3)

        # Then
        db_customer1 = Customers.get(Customers.customer_id == "FY2020-001")
        db_customer2 = Customers.get(Customers.customer_id == "FY2011-003")
        db_customer3 = Customers.get(Customers.customer_id == "FY1997-123")

        self.assertEqual(CUSTOMER1[1], db_customer1.name)
        self.assertEqual(CUSTOMER1[2], db_customer1.last_name)
        self.assertEqual(CUSTOMER1[3], db_customer1.home_address)
        self.assertEqual(CUSTOMER1[4], db_customer1.phone_number)
        self.assertEqual(CUSTOMER1[5], db_customer1.email_address)
        self.assertEqual(CUSTOMER1[6], db_customer1.status)
        self.assertEqual(CUSTOMER1[7], db_customer1.credit_limit)

        self.assertEqual(CUSTOMER2[6], db_customer2.status)

        self.assertEqual(CUSTOMER3[3], db_customer3.home_address)

        db_customer1.delete_instance()
        db_customer2.delete_instance()
        db_customer3.delete_instance()
示例#3
0
def add_customer(
        customer_id,
        name,
        last_name,
        home_address,
        phone_number,
        email_address,
        status,
        credit_limit,
):
    """
    This function adds a new customer to the sqlite3 database.
    """
    LOGGER.info("Adding new customer, %s %s to database", name, last_name)
    try:
        Customers.create(
            customer_id=customer_id,
            name=name,
            last_name=last_name,
            home_address=home_address,
            phone_number=phone_number,
            email_address=email_address,
            status=status,
            credit_limit=credit_limit,
        )
        LOGGER.info("Added new customer %s %s to database", name, last_name)
    except IntegrityError as e_val:
        LOGGER.warning("Customer %s already exists", customer_id)
        LOGGER.warning(e_val)
示例#4
0
def list_active_customers():
    """
    This function returns an integer with the number of customers whose status is currently active.
    """
    db_customers = Customers.select()

    return sum([int(x.status) for x in db_customers])
示例#5
0
def list_active_emails():
    """
    This function returns a list of emails of only active customers in the database
    """
    db_customers = Customers.select().where(Customers.status)
    LOGGER.debug("Returning list of active customer emails")
    email_list = [x.email_address for x in db_customers]
    LOGGER.info("Email list: %s", email_list)
    return email_list
示例#6
0
 def tearDown(self):
     try:
         db_customer = Customers.get(Customers.customer_id == "FY2020-001")
         db_customer.delete_instance()
     except Exception as e_val:
         logging.debug(e_val)
     try:
         DATABASE.close()
     except Exception as e_val:
         logging.debug(e_val)
示例#7
0
 def tearDown(self):
     try:
         db_customer = Customers.get(Customers.customer_id == "FY2020-001")
         db_customer.delete_instance()
     except NotImplementedError as e_val:
         LOGGER.debug(e_val)
     try:
         DATABASE.close()
     except NotImplementedError as e_val:
         LOGGER.debug(e_val)
示例#8
0
def list_active_customers():
    """
    This function returns an integer with the number of customers whose status is currently active.
    """
    db_customers = Customers.select()
    LOGGER.debug("Calculating number of active customers")
    # Technically used this in Lesson 03, but it is a comprehension.  Another method added below.
    number_active = sum([int(x.status) for x in db_customers])
    LOGGER.info("There are %d active customers", number_active)

    return number_active
示例#9
0
    def test_update_customer_credit(self):
        """Test the ability to change customer credit"""
        # Given
        new_credit = 333.33

        # When
        # basic_operations.add_customer(*CUSTOMER1)
        basic_operations.update_customer_credit("FY2020-001", new_credit)

        # Then
        actual_customer = Customers.get(Customers.customer_id == "FY2020-001")
        self.assertEqual(new_credit, float(actual_customer.credit_limit))
示例#10
0
def update_customer_credit(customer_id, credit_limit):
    """
    This function searches an existing customer by customer_id and update their credit limit
    or raise a ValueError exception if the customer does not exist.
    """
    logging.info("Changing %s credit limit to %.2f", customer_id, credit_limit)
    try:
        db_customer = Customers.get(Customers.customer_id == customer_id)
        db_customer.credit_limit = credit_limit
        db_customer.save()
    except Exception as e_val:
        logging.warning("Error updating %s credit limit", customer_id)
        logging.warning(e_val)
示例#11
0
def delete_customer(customer_id):
    """
    This function deletes a customer from the sqlite3 database.
    """
    logging.info("Deleting customer %s", customer_id)
    try:
        logging.info("Customer %s deleted", customer_id)
        db_customer = Customers.get(Customers.customer_id == customer_id)
        db_customer.delete_instance()
    except Exception as e_val:
        logging.warning(
            "Customer %s does not exist: Delete operation ignored", customer_id
        )
        logging.warning(e_val)
示例#12
0
    def test_add_customer_exisiting(self):
        """Testing adding existing customer to database"""
        # Given

        # When
        basic_operations.add_customer(*CUSTOMER1)

        # Then
        try:
            basic_operations.add_customer(*CUSTOMER1)
        except Exception as e_val:
            self.fail(e_val)

        db_customer1 = Customers.get(Customers.customer_id == "FY2020-001")
        db_customer1.delete_instance()
示例#13
0
def update_customer_credit(customer_id, credit_limit):
    """
    This function searches an existing customer by customer_id and update their credit limit
    or raise a ValueError exception if the customer does not exist.
    """
    LOGGER.info("Changing %s credit limit to %.2f", customer_id, credit_limit)
    try:
        db_customer = Customers.get(Customers.customer_id == customer_id)
        db_customer.credit_limit = credit_limit
        db_customer.save()
        LOGGER.info(
            "Successfully changed %s credit limit to %.2f", customer_id, credit_limit
        )
    except DoesNotExist as e_val:
        LOGGER.warning("Error updating %s credit limit", customer_id)
        LOGGER.warning(e_val)
示例#14
0
    def test_add_customer_exisiting(self):
        """Testing adding existing customer to database"""
        LOGGER.info("*** TEST ***: add_customer with existing customer_id")
        # Given

        # When
        basic_operations.add_customer(*CUSTOMER1)

        # Then
        try:
            basic_operations.add_customer(*CUSTOMER1)
        except IntegrityError as e_val:
            self.fail(e_val)

        db_customer1 = Customers.get(Customers.customer_id == "FY2020-001")
        db_customer1.delete_instance()
示例#15
0
def search_customer(customer_id):
    """
    This function returns a dictionary object with name, last_name, email address
    and phone number of a customer or an empty dictionary object if no customer was found.
    """
    logging.info("Searching for customer ID# %s", customer_id)
    try:
        db_customer = Customers.get(Customers.customer_id == customer_id)
        return {
            "name": db_customer.name,
            "last_name": db_customer.last_name,
            "email_address": db_customer.email_address,
            "phone_number": db_customer.phone_number,
        }
    except Exception as e_val:
        logging.warning("No customer: %s", customer_id)
        logging.warning(e_val)
        return {}
示例#16
0
def search_customer(customer_id):
    """
    This function returns a dictionary object with name, last_name, email address
    and phone number of a customer or an empty dictionary object if no customer was found.
    """
    LOGGER.info("Searching for customer ID# %s", customer_id)
    try:
        db_customer = Customers.get(Customers.customer_id == customer_id)
        data = {
            "name": db_customer.name,
            "last_name": db_customer.last_name,
            "email_address": db_customer.email_address,
            "phone_number": db_customer.phone_number,
        }
        LOGGER.info("Returning: {data}")
        return data
    except (IntegrityError, DoesNotExist) as e_val:
        LOGGER.warning("No customer: %s", customer_id)
        LOGGER.warning(e_val)
        return {}