def clear_database(): ''' Resets database before tests are run ''' # Set up empty table for testing database.drop_tables([Customer]) database.create_tables([Customer])
def test_add_customer(self): """ create an empty database for testing """ database.drop_tables([Customer]) database.create_tables([Customer]) logger.info('-- Testing adding new customer --') bo.add_customer(1, 'Emily', 'Yang', '121 Main street NewYork', '2062847320', '*****@*****.**', True, 10000) customer = Customer.get(Customer.customer_id == 1) self.assertEqual(customer.name, 'Emily') self.assertEqual(customer.lastname, 'Yang') self.assertEqual(customer.home_address, '121 Main street NewYork') self.assertEqual(customer.phone_number, '2062847320') self.assertEqual(customer.email_address, '*****@*****.**') self.assertEqual(customer.status, True) self.assertEqual(customer.credit_limit, 10000) bo.add_customer(2, 'Adam', 'Chilly', '233 Jone street LA', '4342941868', '*****@*****.**', True, 5000) bo.add_customer(3, 'Steve', 'Wagon', '4508 Lord Ave Seattle', '6879337640', '*****@*****.**', False, 0) bo.add_customer(4, 'Jone', 'Comba', '1129 Brand street Boise', '3745689770', '*****@*****.**', False, 100) bo.add_customer(5, 'Zaler', 'Danny', '29 Colb street Portland', '2323456787', '*****@*****.**', True, 1000) self.assertEqual(len(Customer), 5) for customer in Customer: logger.info(f'{customer.name} {customer.home_address} ' f'{customer.phone_number} {customer.email_address} ' f'{customer.status} {customer.credit_limit}') logger.info('-- End of Testing adding new customer --\n')
def add_customer(**kwargs): try: kwargs['credit_limit'] = float(kwargs['credit_limit']) except ValueError as err: raise if not database.table_exists('Customer'): database.create_tables([Customer]) new_customer = Customer.insert(kwargs) new_customer.execute()
def setUp(self): database.create_tables([Customer]) for customer in customers: Customer.create(customer_id=customer[0], name=customer[1], lastname=customer[2], home_address=customer[3], phone_number=customer[4], email_address=customer[5], status=customer[6], credit_limit=customer[7])
def setUp(self): database.drop_tables([Customer]) database.create_tables([Customer]) for customer in customers: Customer.create(customer_id=customer[CUSTOMER_ID], name=customer[CUSTOMER_NAME], lastname=customer[CUSTOMER_LASTNAME], home_address=customer[CUSTOMER_HOME_ADDRESS], phone_number=customer[CUSTOMER_PHONE_NUMNER], email_address=customer[CUSTOMER_EMAIL_ADDRESS], status=customer[CUSTOMER_STATUS], credit_limit=customer[CUSTOMER_CREDIT_LIMIT])
def add_customer(customer_id, name, last_name, home_address, phone_number, email_address, status, credit_limit): try: credit_limit = float(credit_limit) except ValueError as err: raise if not database.table_exists('Customer'): database.create_tables([Customer]) new_customer = Customer.create(customer_id=customer_id, first_name=name, last_name=last_name, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) new_customer.save()
def test_module(self): ''' Test overall basic_operations functionality ''' # Set up empty table for testing database.drop_tables([Customer]) database.create_tables([Customer]) # Create new customers new_customer = { 'id': '00001', 'firstname': 'Ron', 'lastname': 'Swanson', 'address': '123 Fake Street', 'phone': '555-867-5309', 'email': '*****@*****.**', 'status': 0, 'credit_limit': 10000 } second_customer = { 'id': '00002', 'firstname': 'Leslie', 'lastname': 'Knope', 'address': '345 Any Drive', 'phone': '555-867-5310', 'email': '*****@*****.**', 'status': 1, 'credit_limit': 20000 } # Add customers for customer in [new_customer, second_customer]: basic_operations.add_customer( customer['id'], customer['firstname'], customer['lastname'], customer['address'], customer['phone'], customer['email'], customer['status'], customer['credit_limit']) # Delete a customer basic_operations.delete_customer(new_customer['id']) # Query number of active customers, should be 1 num_active = basic_operations.list_active_customers() self.assertEqual(num_active, 1)
}, { 'customer_id': 2, 'name': 'Peter', 'lastname': 'Young', 'home_address': "This is Peter's home address", 'phone_number': '425-222-2222', 'email_address': '*****@*****.**', 'status': True, 'credit_limit': 5000 }, ] # Create Customer table in the database database.drop_tables(MODELS) database.create_tables(MODELS) logging.info(f"database = {database.get_tables()}") # All all customers in the CLIENTS dictionary into the database for person in CLIENTS: add_customer(**person) # Search for an existing customer in the database a_client = search_customer(1) logging.info(f"a_client = {a_client}") # Update an existing customer's credit limit in the database update_customer_credit(2, 15000) logging.info(f"new limit = {Customer.get_by_id(2).credit_limit}") # Return the total number of customers in the database whose status is currently active
def setUp(self): database.create_tables([Customer])
'''Basic database operations for Rental application''' import logging from customer_model import Customer, database logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Create the Customer database if it doesn't exists already logger.info( 'One off program to build the classes from the model in the database') database.create_tables([Customer]) def add_customer(customer_id, customer_name, customer_lastname, customer_home_address, customer_phone_number, customer_email_address, customer_status, customer_credit_limit): '''Adds a customer to the database''' with database.transaction(): new_customer = Customer.create(customer_id=customer_id, name=customer_name, lastname=customer_lastname, home_address=customer_home_address, phone_number=customer_phone_number, email_address=customer_email_address, status=customer_status, credit_limit=customer_credit_limit) new_customer.save() logger.info('Database add successful') return new_customer
def open_database(): """Open the database to create tables""" #BaseModel.Meta.database = database logging.info('create table') database.create_tables([Customer])