def search_customer(customer_id): """ Searches the database of existing customers. Returns a dictionary with customer information. An empty dictionary is returned if the customer does not exist in the database. :param customer_id: primary key used to search the database :return: dict{customer_id, name, last_name, email_address, phone_number, email_address, status, credit_limit} """ customer = Customer.get_or_none(Customer.customer_id == customer_id) logging.debug('Searching for customer: %s in database.', customer_id) # make a full dictionary if the person exists if customer: logging.debug('Customer: %s found in the database.', customer_id) customer_dict = {'customer_id': customer.customer_id, 'name': customer.name, 'last_name': customer.last_name, 'home_address': customer.home_address, 'phone_number': customer.phone_number, 'email_address': customer.email_address, 'status': customer.status, 'credit_limit': customer.credit_limit} logging.debug('Customer dictionary created.') else: # create an empty dict logging.debug('Customer: %s not found in the database, returning empty dictionary.', customer_id) customer_dict = {} return customer_dict
def update_customer_credit(customer_id=None, credit_limit=None): update_customer = Customer.get_or_none(Customer.customer_id == customer_id) if update_customer: update_customer.credit_limit = credit_limit update_customer.save() else: raise ValueError
def test_integration(self): ''' This fucntion tests the model and basic_fuctions classes ''' add_customer(300, 'John', 'Doe', '100 Main St', '123-456-6789', '*****@*****.**', 'active', 10000) add_customer(400, 'Jane', 'Doe', '200 Main St', '123-456-6789', '*****@*****.**', 'inactive', 20000) searched_dict = search_customer(300) self.assertEqual(search_customer(500), {}) self.assertEqual( searched_dict, { 'name': 'John', 'lastname': 'Doe', 'email_address': '*****@*****.**', 'phone_number': '123-456-6789' }) self.assertEqual(list_active_customers(), 1) update_customer_credit(300, 40000) updated_customer = Customer.select().where( Customer.customer_id == 300).get() self.assertEqual(updated_customer.credit_limit, 40000) with self.assertRaises(ValueError): update_customer_credit(500, 20000) delete_customer(300) number = delete_customer(400) self.assertEqual(number, 1) number = delete_customer(500) self.assertEqual(number, 0) self.assertEqual(list_active_customers(), 0) add_customer('abc', 'John', 'Doe', '100 Main St', '123-456-6789', '*****@*****.**', 'active', 10000) customer = Customer.get_or_none() self.assertIsNone(customer)
def search_customer(customer_id=None): find_customer_model = Customer.get_or_none( Customer.customer_id == customer_id) if find_customer_model: find_customer_dict = model_to_dict(find_customer_model) else: find_customer_dict = {} return find_customer_dict
def search_customer(customer_id): '''Find customer by customer id. Return None if no matching customer id found.''' customer = Customer.get_or_none(customer_id=customer_id) if customer is None: logger.info('Customer with id %s could not be found.', customer_id) else: logger.info('Customer was %s was found.', customer_id) return customer
def delete_customer(_name): '''Delete a customer from the customer database.''' with db.transaction(): to_delete = Customer.get_or_none(Customer.name == _name) if to_delete is not None: to_delete.delete_instance() if to_delete is None: LOGGER.warning('Customer %s does not exist', _name)
def delete_customer(customer_id): """ This function will delete a customer from the customers.db database """ logger.info('In delete_customer().') the_customer = Customer.get_or_none(Customer.customer_id == customer_id) # make sure only the existing customer will be deleteed. if the_customer: the_customer.delete_instance() logger.info(f'Delete customer {customer_id} successfully.')
def update_customer(**kwargs): update_customer = Customer.get_or_none( Customer.customer_id == kwargs['customer_id']) if update_customer: insert_dict = { kwarg: kwargs[kwarg] for kwarg in kwargs if kwarg != 'customer_id' } update_query = Customer.update(insert_dict).where( Customer.customer_id == kwargs['customer_id']) update_query.execute() else: raise ValueError
def update_customer_credit(customer_id, credit_limit): """ Updates a customers credit limit in the database. A ValueError exception is thrown if the customer does not exist in the database. """ logging.debug('Attempting to update credit for customer: %s, to %s ', customer_id, credit_limit) customer = Customer.get_or_none(Customer.customer_id == customer_id) if customer: customer.credit_limit = credit_limit customer.save() logging.info('Updated credit limit for customer: %s to %s', customer_id, credit_limit) else: logging.error('Error: customer: %s does not exist.', customer_id) raise ValueError
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. """ the_customer = Customer.get_or_none(Customer.customer_id == customer_id) # make sure only the existing customer will be deleteed. #if the_customer: try: the_customer.credit_limit = credit_limit the_customer.save() logger.info(f'Customer {customer_id} credit limit is ' 'updated successfully .') except AttributeError: print(f'Customer {customer_id} does not exist.')
def test_integration(self): ''' This fucntion tests the model and basic_fuctions classes ''' add_customer(300, 'John', 'Doe', '100 Main St', '123-456-6789', '*****@*****.**', 'active', 10000) add_customer(400, 'Jane', 'Doe', '200 Main St', '123-456-6789', '*****@*****.**', 'inactive', 20000) displayed_customers = ['Customer: 300' + '\n' 'Name: John' + '\n' 'Last Name: Doe' + '\n' 'Home Address: 100 Main St' + '\n' 'Phone Number: 123-456-6789' + '\n' 'Email Address: [email protected]' + '\n' 'Status: active' + '\n' 'Credit Limit: 10000' + '\n' + '\n', 'Customer: 400' + '\n' 'Name: Jane' + '\n' 'Last Name: Doe' + '\n' 'Home Address: 200 Main St' + '\n' 'Phone Number: 123-456-6789' + '\n' 'Email Address: [email protected]' + '\n' 'Status: inactive' + '\n' 'Credit Limit: 20000' + '\n' + '\n'] self.assertEqual(display_customers(), displayed_customers) searched_dict = search_customer(300) self.assertEqual(search_customer(500), {}) self.assertEqual(searched_dict, {'name': 'John', 'lastname': 'Doe', 'email_address': '*****@*****.**', 'phone_number': '123-456-6789'}) self.assertEqual(list_active_customers(), 1) update_customer_credit(300, 40000) updated_customer = Customer.select().where(Customer.customer_id == 300).get() self.assertEqual(updated_customer.credit_limit, 40000) with self.assertRaises(ValueError): update_customer_credit(500, 20000) delete_customer(300) number = delete_customer(400) self.assertEqual(number, 1) number = delete_customer(500) self.assertEqual(number, 0) self.assertEqual(list_active_customers(), 0) add_customer('abc', 'John', 'Doe', '100 Main St', '123-456-6789', '*****@*****.**', 'active', 10000) customer = Customer.get_or_none() self.assertIsNone(customer)
def test_delete_customer(self): """Deletes a customer properly if input is given in the correct format""" # deleting a non existing customer should only cause a print statement and no run errors with warnings.catch_warnings(record=True) as w: delete_customer(100) self.assertNotEqual(w, []) self.assertIs(w[0].category, UserWarning) self.assertEqual( str(w[0].message), 'User with customer_id=100 does not exist in the ' 'database.') add_customer(100, 'Fran', 'K', '100 New York Ave, NYC, 98109', '248-331-6243', '*****@*****.**', 'Active', 5000) delete_customer(100) customer = Customer.get_or_none(Customer.customer_id == 100) self.assertIsNone(customer, 'Should evaluate to none.')
def test_add_customer(self): """Ensure that a customer is inserted if all the inputs have the correct format.""" add_customer(100, 'Fran', 'K', '100 New York Ave, NYC, 98109', '248-331-6243', '*****@*****.**', 'Active', 5000) query = Customer.get_or_none() # checks that something got added self.assertIsNotNone(query) # check that the right unique person got added self.assertEqual(Customer[100].customer_id, 100) self.assertEqual(Customer[100].name, 'Fran') self.assertEqual(Customer[100].last_name, 'K') self.assertEqual(Customer[100].home_address, '100 New York Ave, NYC, 98109') self.assertEqual(Customer[100].phone_number, '248-331-6243') self.assertEqual(Customer[100].email_address, '*****@*****.**') self.assertEqual(Customer[100].status, 'Active') self.assertEqual(Customer[100].credit_limit, 5000)
def test_add_customer(self): ''' Tests if customer is added, returns none if not able to add ''' add_customer(300, 'John', 'Doe', '100 Main St', '123-456-6789', '*****@*****.**', 'active', 10000) self.assertEqual(Customer[300].customer_id, 300) self.assertEqual(Customer[300].name, 'John') self.assertEqual(Customer[300].lastname, 'Doe') self.assertEqual(Customer[300].home_address, '100 Main St') self.assertEqual(Customer[300].phone_number, '123-456-6789') self.assertEqual(Customer[300].email_address, '*****@*****.**') self.assertEqual(Customer[300].status, 'active') self.assertEqual(Customer[300].credit_limit, 10000) delete_customer(300) #add logic here for not being able to add add_customer('abc', 'John', 'Doe', '100 Main St', '123-456-6789', '*****@*****.**', 'active', 10000) customer = Customer.get_or_none() self.assertIsNone(customer)
def search_customer(customer_id): """ This function will search for a customer using the id field then it will return a dictionary object. """ dict_customer = {} db_customer = Customer.get_or_none(Customer.customer_id == customer_id) if db_customer is not None: logger.info('Customer %s found with credit limit of %d.', db_customer.customer_name, db_customer.credit_limit) dict_customer = { 'customer_id': db_customer.customer_id, 'customer_name': db_customer.customer_name, 'customer_lastname': db_customer.customer_lastname, 'customer_address': db_customer.customer_address, 'customer_phone_number': db_customer.customer_phone_number, 'customer_email': db_customer.customer_email, 'credit_limit': db_customer.credit_limit, 'status': db_customer.status } return dict_customer
def search_customer(customer_id): """ This function will search a customer from the customers.db database 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. """ logger.info('In search_customer().') logger.info(f'Searching customer {customer_id}.') the_customer = Customer.get_or_none(Customer.customer_id == customer_id) # build the dict for the found customer if the_customer: customer = {} customer['name'] = the_customer.name customer['lastname'] = the_customer.lastname customer['phone_number'] = the_customer.phone_number customer['email_address'] = the_customer.email_address logger.info(f'Found customer {customer_id} successfully.') else: customer = {} # return empty dict object for non-existing customer. logger.info(f'customer {customer_id} is not found.') return customer
def search_customer(customer_id): '''Find customer by customer id. Return None if no matching customer id found.''' return Customer.get_or_none(customer_id=customer_id)
def delete_customer(customer_id=None): delete_customer = Customer.get_or_none(Customer.customer_id == customer_id) if delete_customer: delete_customer.delete_instance()