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())
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()
def update_customer_credit(customer_id, credit_limit): """Updates a customer's credit limit in the database.""" init_database() try: customer = Customer.get_by_id(customer_id) customer.credit_limit = credit_limit customer.save() return True except peewee.DoesNotExist: logging.error("Customer ID %s doesn't exist in database.", customer_id) raise ValueError('Customer ID does not exist in database.') finally: database.close()
def delete_customer(customer_id): """Deletes a customer from the database.""" init_database() try: with database.transaction(): customer = Customer.get_by_id(customer_id) customer.delete_instance() logging.info('Customer with ID %s successfully deleted.', customer_id) return True except peewee.DoesNotExist: logging.error('Customer delete with ID %s failed, not in database..', customer_id) return False finally: database.close()
def search_customer(customer_id): """Searches for a customer in the database, returning a dictionary of contact info if found.""" init_database() try: customer = Customer.get_by_id(customer_id) customer_dict = { 'name': customer.name, 'lastname': customer.lastname, 'email_address': customer.email_address, 'phone_number': customer.phone_number } return customer_dict except peewee.DoesNotExist: logging.warning("Customer ID %s doesn't exist in database.", customer_id) return {} finally: database.close()
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()
def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): """Adds a new customer to the database.""" init_database() try: with database.transaction(): new_customer = Customer.create(customer_id=customer_id, name=name, lastname=lastname, home_address=home_address, phone_number=phone_number, email_address=email_address, active_status=status, credit_limit=credit_limit) new_customer.save() logging.info('New customer, ID %s, added successfully.', customer_id) return True except peewee.IntegrityError as exc: logging.error('Error creating new customer with ID %s: %s.', customer_id, exc) return False finally: database.close()
def tearDownClass(cls): """ close the database connection at the end of each test. """ # print('teardown method is called') DATABASE.close()
FILE_HANDLER = logging.FileHandler(LOG_FILE) FILE_HANDLER.setFormatter(FORMATTER) FILE_HANDLER.setLevel(logging.INFO) CONSOLE_HANDLER = logging.StreamHandler() CONSOLE_HANDLER.setLevel(logging.DEBUG) CONSOLE_HANDLER.setFormatter(FORMATTER) LOGGER = logging.getLogger() LOGGER.addHandler(FILE_HANDLER) LOGGER.info('One off program to build the classes from the model in \ the database') DATABASE.create_tables([Customer]) DATABASE.close() CUSTOMERS = [ (1, 'Allen', 'Iverson', 'Philadelphia', '555-123-4567', '*****@*****.**', True, 2000), (2, 'Shaw', 'Kemp', 'Seattle', '123-512-3568', '*****@*****.**', True, 1000), (3, 'Vince', 'Carter', 'Torento', '236-123-4569', '*****@*****.**', True, 5000), (4, 'Rose', 'Derrick', 'Chicago', '854-123-4570', '*****@*****.**', False, 2000), (5, 'Steve', 'Nash', 'Phoenix', '654-568-4961', '*****@*****.**', False, 100), ]