def test_list_active_customers(self): """ Test that list_active_customers() returns correct amount of active customers """ active_count = b_o.list_active_customers() self.assertEqual(active_count, 0) Customer.create(customer_id='1564', name='First', lastname='Last', home_address='12 1st st, Seattle, WA 98101', phone_number='5551251255', email_address='*****@*****.**', status=True, credit_limit=150.00) active_count = b_o.list_active_customers() self.assertEqual(active_count, 1) Customer.create(customer_id='2', name='First2', lastname='Last2', home_address='12 2nd st, Seattle, WA 98101', phone_number='5551251252', email_address='*****@*****.**', status=False, credit_limit=152.00) active_count = b_o.list_active_customers() self.assertEqual(active_count, 1)
def test_delete_customer(self): """ Test that delete_customer function works """ Customer.create(customer_id='1564', name='First', lastname='Last', home_address='12 1st st, Seattle, WA 98101', phone_number='5551251255', email_address='*****@*****.**', status=True, credit_limit=150.00) Customer.create(customer_id='2', name='First2', lastname='Last2', home_address='12 2nd st, Seattle, WA 98101', phone_number='5551251252', email_address='*****@*****.**', status=True, credit_limit=152.00) db_query = Customer.get_by_id('1564') self.assertEqual(db_query.customer_id, '1564') self.assertEqual(Customer.select().count(), 2) try: b_o.delete_customer('doesnt exist') except Exception as exception: # pylint: disable=W0703 self.fail( "delete_customer() raised {} unexpectedly".format(exception)) b_o.delete_customer('1564') self.assertEqual(Customer.select().count(), 1) with self.assertRaises(peewee.DoesNotExist): Customer.get_by_id('1564')
def list_active_customers(): """ This function will return an integer with the number of customers whose status is currently active. """ db_query = Customer.select().where(Customer.status == True).count() # pylint: disable=C0121 return db_query
def list_customer_names(): """Returns a list of customers in the database""" customer_list = Customer.select() return [ person.customer_id + ": " + person.last_name + ", " + person.first_name for person in customer_list ]
def test_basic_operations_integration(self): """ Test that list_active_customers() returns correct amount of active customers """ test_user = { 'customer_id': '1255', 'name': 'Tim', 'lastname': 'Allen', 'home_address': "15402 W 8 Mile Rd, Detroit, MI 48219", 'phone_number': '5558468665', 'email_address': '*****@*****.**', 'status': True, 'credit_limit': 10000.00 } b_o.add_customer(**test_user) test_search = b_o.search_customer('1255') self.assertEqual(test_search['name'], 'Tim') self.assertEqual(test_search['lastname'], 'Allen') self.assertEqual(test_search['phone_number'], '5558468665') self.assertEqual(test_search['email_address'], '*****@*****.**') b_o.update_customer_credit('1255', 520) db_query = Customer.get_by_id('1255') self.assertEqual(db_query.credit_limit, 520) customer_count = b_o.list_active_customers() self.assertEqual(customer_count, 1) b_o.delete_customer('1255') customer_count = b_o.list_active_customers() self.assertEqual(customer_count, 0)
def add_customer( customer_id, name, lastname, home_address, phone_number, # pylint: disable=R0913 email_address, status, credit_limit): """ This function will add a new customer to the sqlite3 database. """ try: new_customer = Customer.create(customer_id=customer_id, name=name, lastname=lastname, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) new_customer.save() except peewee.IntegrityError as exception: if 'unique' in str(exception).lower(): LOGGER.warning( 'Tried to add a customer_id that already exists: %s', customer_id) raise peewee.IntegrityError( 'Tried to add a customer_id that already exists: {}'.format( customer_id)) LOGGER.debug("Integrity Error in add_customer: %s", str(exception)) except Exception as exception: LOGGER.debug('add_customer exception: %s', str(exception)) raise Exception('add_customer raised this error: {}'.format( str(exception)))
def delete_customer(customer_id): """Deletes customer from database""" try: the_customer = Customer.get(Customer.customer_id == customer_id) the_customer.delete_instance() except peewee.DoesNotExist: LOGGER.warning(f'Customer {customer_id} is not in the database!') raise
def test_search_customer(self): """ Test that add_customer function works and returns empty dictionary if doesn't exist """ Customer.create(customer_id='1564', name='First', lastname='Last', home_address='12 1st st, Seattle, WA 98101', phone_number='5551251255', email_address='*****@*****.**', status=True, credit_limit=150.00) self.assertEqual(len(b_o.search_customer("doesn't exist")), 0) db_query = b_o.search_customer('1564') self.assertEqual(db_query['name'], 'First') self.assertEqual(db_query['lastname'], 'Last') self.assertEqual(db_query['phone_number'], '5551251255') self.assertEqual(db_query['email_address'], '*****@*****.**')
def test_add_customer(self): """ Test that add_customer function works and returns correct errors """ test_user = { 'customer_id': '1255', 'name': 'Tim', 'lastname': 'Allen', 'home_address': "15402 W 8 Mile Rd, Detroit, MI 48219", 'phone_number': '5558468665', 'email_address': '*****@*****.**', 'status': True, 'credit_limit': 10000.00 } b_o.add_customer(**test_user) db_query = Customer.get_by_id('1255') self.assertEqual(db_query.customer_id, test_user['customer_id']) self.assertEqual(db_query.name, test_user['name']) self.assertEqual(db_query.lastname, test_user['lastname']) self.assertEqual(db_query.home_address, test_user['home_address']) self.assertEqual(db_query.phone_number, test_user['phone_number']) self.assertEqual(db_query.email_address, test_user['email_address']) self.assertEqual(db_query.status, test_user['status']) self.assertEqual(db_query.credit_limit, test_user['credit_limit']) with self.assertRaises(peewee.IntegrityError) as context: b_o.add_customer(**test_user) self.assertTrue('Tried to add a customer_id that already exists:' in str(context.exception)) second_user = { 'customer_id': '1245', 'name': 'Tim', 'lastname': 'Allen', 'home_address': "15402 W 8 Mile Rd, Detroit, MI 48219", 'phone_number': '55584686654433443434344343434334343', 'email_address': '*****@*****.**', 'status': True, 'credit_limit': '100' } b_o.add_customer(**second_user) self.assertEqual(Customer.select().count(), 2)
def test_update_credit_limit(self): """ Test that update_credit_limit() function works """ Customer.create(customer_id='1564', name='First', lastname='Last', home_address='12 1st st, Seattle, WA 98101', phone_number='5551251255', email_address='*****@*****.**', status=True, credit_limit=150.00) db_query = Customer.get_by_id('1564') self.assertEqual(db_query.credit_limit, 150.00) b_o.update_customer_credit('1564', 200.20) db_query = Customer.get_by_id('1564') self.assertEqual(float(db_query.credit_limit), 200.20) with self.assertRaises(ValueError): b_o.update_customer_credit("Value Fail", 500)
def delete_customer(customer_id): """ This function will delete a customer from the sqlite3 database. """ try: db_query = Customer.get_by_id(customer_id) db_query.delete_instance() except peewee.DoesNotExist: LOGGER.warning( "'%s' doesn't exist in database using delete_customer()", customer_id)
def update_customer_credit(customer_id, credit_limit): """Searches for existing customer, updates the credit limit, or raises ValueError if customer not found""" try: update_credit = Customer.get(Customer.customer_id == customer_id) update_credit.credit_limit = credit_limit update_credit.save() LOGGER.info('Credit limit successfully updated and saved') except peewee.DoesNotExist: LOGGER.info('Error with updating credit limit') raise
def list_active_customers(): """ This function will return an integer with the number of customers whose status is currently active. """ # db_query = Customer.select().where(Customer.status == True).count() # pylint: disable=C0121 # Above line changed to below to show iteration customer_count = 0 for customer in Customer.select(): if customer.status is True: customer_count += 1 return customer_count
def update_customer_credit(customer_id, credit_limit): """ This function will search an existing customer by customer_id and update their credit limit or raise a ValueError exception if the customer does not exist. """ try: db_query = Customer.get_by_id(customer_id) except peewee.DoesNotExist: LOGGER.warning( "'%s' doesn't exist in database using update_customer_credit()", customer_id) raise ValueError db_query.credit_limit = float(credit_limit) db_query.save()
def search_customer(customer_id): """Searches for customer by ID, returns a dictionary object with name, last name, email address, and phone number OR an empty dict if no customer found""" try: the_customer = Customer.get(Customer.customer_id == customer_id) return { 'Name': the_customer.first_name, 'Last Name': the_customer.last_name, 'Email': the_customer.email, 'Phone Number': the_customer.phone } except peewee.DoesNotExist: LOGGER.warning(f'Customer {customer_id} is not in the database!') # Return an empty dictionary return {}
def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): """Add new customer to database""" try: new_customer = Customer.create(customer_id=customer_id, first_name=name, last_name=lastname, address=home_address, phone=phone_number, email=email_address, status=status, credit_limit=credit_limit) new_customer.save() LOGGER.info('New customer saved!') except peewee.IntegrityError: LOGGER.info( f'Customer ID {customer_id} had issues, may already exist in the database' ) raise
def search_customer(customer_id): """ This function will return a dictionary object with name, lastname, email address and phone number of a customer or an empty dictionary object if no customer was found. """ customer_dict = {} try: db_query = Customer.get_by_id(customer_id) except peewee.DoesNotExist: LOGGER.warning( "'%s' doesn't exist in database using search_customer()", customer_id) return customer_dict # raise peewee.DoesNotExist("{} doesn't exist in database".format(customer_id)) keys = ['name', 'lastname', 'email_address', 'phone_number'] values = [ db_query.name, db_query.lastname, db_query.email_address, db_query.phone_number ] customer_dict = dict(zip(keys, values)) return customer_dict
def list_active_customers(): """Returns number of active customers""" active_count = Customer.select().where(Customer.status).count() LOGGER.info(f'I counted {active_count} active customers') return active_count