示例#1
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()
示例#2
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)
示例#3
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)
示例#4
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))
示例#5
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)
示例#6
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)
示例#7
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()
示例#8
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)
示例#9
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()
示例#10
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 {}
示例#11
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 {}