Example #1
0
    def test_basic_operations(self):
        """" Integration test for the basic operations """

        DATABASE.drop_tables([Customer])
        DATABASE.close()
        DATABASE.create_tables([Customer])
        DATABASE.close()

        # Add customers to the db
        add_customers(self.test_customer)

        cust_found = search_customer(2)
        self.assertEqual(self.test_customer[1][5], cust_found.get("email"))

        update_customer_credit(1, 500.00)
        self.assertEqual(500.00, Customer.get_by_id(1).credit_limit)

        # Find out how many customers are active
        active_cust = list_active_customers()
        self.assertEqual(2, active_cust)

        # Delete a customer then try to find it
        delete_customer(2)
        self.assertDictEqual({}, search_customer(2))

        # Find out how many customers are active and list their names
        self.assertEqual(1, list_active_customers())
        self.assertEqual(["Reed Richards"], list_active_customer_names())
Example #2
0
 def setUpClass(cls):
     """
     Setup the test case
     """
     # print('setup method is called')
     DATABASE.drop_tables([Customer])
     DATABASE.create_tables([Customer])
def init_database():
    """This function checks to see if the database exists, and if not, generates the tables."""
    database.init(DATABASE_NAME)
    database.connect()
    database.execute_sql('PRAGMA foreign_keys = ON')
    if not database.table_exists([Customer]):
        database.create_tables([Customer])
    database.close()
def set_up_db():
    """
    Set up routine needed for all tests to make sure the db is in a
    known/empty state
    """
    DATABASE.drop_tables([Customer])
    DATABASE.close()
    DATABASE.create_tables([Customer])
    DATABASE.close()
Example #5
0
    def setUp(self):
        """ Ensure we start with a clean table """

        # Configure logging for test activity
        bo.configure_logger()

        logging.info("Dropping Customer table")
        DATABASE.drop_tables([Customer])
        logging.info("Creating Customer table")
        DATABASE.create_tables([Customer])
 def setUp(self):
     """Defines starting test database used for function testing."""
     self.starting_db = [(1, 'Bob', 'Bobbo', '12 Green St', '1112223344',
                          '*****@*****.**', False, 85000),
                         (2, 'Jane', 'Janeo', '1550 Red Rd', '1118675309',
                          '*****@*****.**', True, 150000),
                         (5, 'Wilson', 'Volleyball', '1 Castaway Island', '0000000000',
                          '*****@*****.**', True, 0)
                         ]
     database.init(TEST_DATABASE)
     database.connect()
     database.execute_sql('PRAGMA foreign_keys = ON')
     database.create_tables([Customer])
     with database.transaction():
         Customer.delete().execute()
         Customer.insert_many(self.starting_db, fields=[Customer.customer_id, Customer.name,
                                                        Customer.lastname, Customer.home_address,
                                                        Customer.phone_number,
                                                        Customer.email_address,
                                                        Customer.active_status,
                                                        Customer.credit_limit
                                                        ]).execute()
     database.close()
Example #7
0
    def tearDown(self):
        """ Ensure we start with a clean table """

        DATABASE.drop_tables([Customer])
        DATABASE.create_tables([Customer])
Example #8
0
        msg = f'Customer {customer_id} is not found in the database'
        LOGGER.error(msg)
        raise ValueError


def list_active_customer():
    """ Return the number of customers with the active status."""
    LOGGER.debug("Searching the total active users")
    active_members = Customer.select().where(Customer.status).count()
    msg = f'Active Customers = {active_members}'
    LOGGER.info(msg)
    return active_members


if __name__ == "__main__":

    DATABASE.create_tables([Customer])
    # add_customer(1, 'Muhammad', 'Khan',
    #              '9x9 xyzxy Dr. Apt # 529D xxxxxx SC. 29403',
    #              '202-755-3xx1', '*****@*****.**', True, 244.20)
    # add_customer(2, 'Kristina', 'Lawry',
    #              '8x8 xyzxy Dr. Apt # 200D xxxxxx WA. 60089',
    #              '747-900-4950', '*****@*****.**', True, 399)
    # # update_customer(3, 500.00)
    # print(search_customer(2))
    # delete_customer(2)
    # print(search_customer(2))
    # update_customer(3, 500.00)
#     # for data in Customer.select():
#     #     print(data.credit_limit)
def reset_db():
    '''Resets the database'''
    DATABASE.drop_tables([Customer])
    DATABASE.create_tables([Customer])
Example #10
0
class OperationalTests(TestCase):
    """ Define a class for testing operations """

    def setUp(self):
        """ Ensure we start with a clean table """

    DATABASE.drop_tables([Customer])
    DATABASE.create_tables([Customer])

    def test_add_customer(self):
        """ Test adding customers """

        # Add a customer and ensure it is in the DB
        bo.add_customer(**TEST_USER1)
        test_customer = Customer.get(Customer.customer_id == TEST_USER1['customer_id'])
        self.assertEqual(test_customer.name, TEST_USER1['name'])

        # Ensure adding a non-unique customer_id fails
        with self.assertRaises(IntegrityError):
            bo.add_customer(**TEST_USER1)

        # Delete the test user
        bo.delete_customer(TEST_USER1['customer_id'])

    def test_search_customer(self):
        """ Test searching for a customer """

        # Add a customer and ensure it is in the DB
        bo.add_customer(**TEST_USER1)

        # Search for a valid user created in the first test
        test_customer = bo.search_customer(TEST_USER1['customer_id'])
        self.assertEqual(test_customer.name, TEST_USER1['name'])

        # Search for an invalid user
        with self.assertRaises(DoesNotExist):
            test_customer = bo.search_customer(50)

        # Delete the test user
        bo.delete_customer(TEST_USER1['customer_id'])

    def test_delete_customer(self):
        """ Test deleting customers """

        # Add a user to ensure we have something to delete
        bo.add_customer(**TEST_USER2)

        # Delete the test user
        bo.delete_customer(TEST_USER2['customer_id'])

        # Search for an invalid user
        with self.assertRaises(DoesNotExist):
            Customer.get(Customer.customer_id == TEST_USER2['customer_id'])

    def test_update_customer_credit(self):
        """ Test updating a customer's credit limit """

        # Add a user to ensure we have something to delete
        bo.add_customer(**TEST_USER1)

        # Update the limit for the first test user
        bo.update_customer_credit(TEST_USER1['customer_id'], 2500.00)
        test_customer = Customer.get(Customer.customer_id == TEST_USER1['customer_id'])
        self.assertEqual(test_customer.credit_limit, 2500.00)

        # Update the limit for a customer that doesn't exist
        with self.assertRaises(DoesNotExist):
            bo.update_customer_credit(50, 3000.00)

        # Delete the test user
        bo.delete_customer(TEST_USER1['customer_id'])

    def test_list_customers(self):
        """ Test listing all customers in the database """

        # Add an active customer
        bo.add_customer(**TEST_USER1)

        # Add an inactive customer
        bo.add_customer(**TEST_USER2)

        # Print the active customers
        active_customers = bo.list_active_customers()
        self.assertEqual(active_customers, 1)

    def tearDown(self):
        """ Ensure we start with a clean table """

        DATABASE.drop_tables([Customer])
        DATABASE.create_tables([Customer])