def summary(self): """Create a new dictionary with Total, number of donations, and average donation amount""" database.connect() logger.debug('Connected to database') database.execute_sql('PRAGMA foreign_keys = ON;') donor_summary = {} try: with database.transaction(): donor_list = Donor.select(Donor.donor_name) logger.debug(f'Printing a list of all the donors ' f'in the database') for donor in donor_list: logger.debug(f'{donor.donor_name}') donor_summary[donor.donor_name] = \ [Individual.sum_donations(donor.donor_name), Individual.number_donations(donor.donor_name), Individual.avg_donations(donor.donor_name)] except Exception as e: logger.debug(f'Error creating database summary.') logger.debug(e) logger.debug('Failed to iterate through database ' 'to create a summary.') finally: logger.debug('database closes') database.close() return donor_summary
def add_donation(person, contribution): database.connect(reuse_if_open=True) logger.info('Connected to database') database.execute_sql('PRAGMA foreign_keys = ON;') try: with database.transaction(): logger.info('Trying to add new donor.') Donor.get_or_create(donor_name=person) logger.info(f'Success adding donor {person}.') with database.transaction(): logger.info('Trying to add new donation.') new_donation = Donations.create(donor_name=person, donation=contribution) new_donation.save() logger.info(f'Database added a donation of ' f'{contribution} by {person}.') except Exception as e: logger.info(f'Error creating = {person}') logger.info(e) logger.info('Failed to add new donor.') finally: logger.info('database closes') database.close()
def test_delete_donor(self): """Delete a donor and check that the name is deleted in both the Donor table and the Donations table.""" #indiv_initalize = d.Individual('test2.db') database.connect(reuse_if_open=True) database.execute_sql('PRAGMA foreign_keys = ON;') #show that you can find 'Shane' aperson = Donor.get(Donor.donor_name == 'Shane') self.assertEqual(aperson.donor_name, 'Shane') # Test donor exists # Delete the donor 'Shane' d.Individual.delete_donor('Shane') # After deleting 'Shane', search donor table for shane query_donor = Donor.select().where(Donor.donor_name == 'Shane') donor_list = [] # Create a list of donations for 'name'. for result in query_donor: # logger.info(f'{result.donor_name}') donor_list.append(int(result.donor_name)) self.assertEqual(donor_list, []) # After deleting 'Shane', search donation table for shane query_donation = Donations.select().where(Donations.donor_name == 'Shane') donation_list = [] # Create a list of donations for 'name'. for result in query_donation: # logger.info(f'{result.donor_name}') donation_list.append(int(result.donation)) self.assertEqual(donation_list, []) # After deleting a donor, show that you can still find other donors. aperson = Donor.get(Donor.donor_name == 'Pete') self.assertEqual(aperson.donor_name, 'Pete') # Test donor was added
def letters(self): """Send letters to everyone base on thier last donation amount.""" database.connect() logger.debug('Connected to database') database.execute_sql('PRAGMA foreign_keys = ON;') try: with database.transaction(): donor_list = Donor.select(Donor.donor_name) logger.debug(f'Sending a thank you to every donor ' f'in database.') for donor in donor_list: logger.debug(f'{donor.donor_name}') letter = f'Dear {donor.donor_name}, ' \ f'thank you so much for your ' \ f'last contribution of ' \ f'${Individual.last_donation(donor.donor_name):.2f}! ' \ f'You have contributed a total of $' \ f'{Individual.sum_donations(donor.donor_name):.2f}, ' \ f'and we appreciate your support!' # Write the letter to a destination with open(donor.donor_name + '.txt', 'w') as to_file: to_file.write(letter) except Exception as e: logger.debug(f'Error writing letters to everyone.') logger.debug(e) finally: logger.debug('database closes') database.close()
def test_Individual_Add_Donation1(self): database.connect(reuse_if_open=True) database.execute_sql('PRAGMA foreign_keys = ON;') d.Individual.add_donation('Luke', 5) aperson = Donor.get(Donor.donor_name == 'Luke') adonation = Donations.get(Donations.donor_name == 'Luke') self.assertEqual(aperson.donor_name, 'Luke') # Test donor was added self.assertEqual(adonation.donation, 5) # Test donation was added for Luke
def sum_donations(name): database.connect(reuse_if_open=True) logger.info('In Individual.sum_donations') database.execute_sql('PRAGMA foreign_keys = ON;') try: with database.transaction(): logger.info(f'Summing all the donations by {name}.') query = Donations.select().where(Donations.donor_name == name) donation_list = [] # Create a list of donations for 'name'. for result in query: donation_list.append(int(result.donation)) except Exception as e: logger.info(f'Error counting # of donations.') logger.info(e) finally: logger.info(f'Returning the # of donations made by {name}') return sum(donation_list)
def number_donations(name): database.connect(reuse_if_open=True) logger.info('Connected to database') database.execute_sql('PRAGMA foreign_keys = ON;') try: with database.transaction(): logger.info('Trying to count number of donations.') query = Donations.select().where(Donations.donor_name == name) donation_list = [] # Create a list of donations for 'name'. for result in query: donation_list.append(int(result.donation)) except Exception as e: logger.info(f'Error counting # of donations.') logger.info(e) finally: logger.info(f'Returning the # of donations made by {name}') return int(len(donation_list))
def print_donors(self): # This prints the list of donors database.connect() logger.debug('Connected to database') database.execute_sql('PRAGMA foreign_keys = ON;') try: with database.transaction(): logger.debug('Trying print all the donors.') query = Donor.select() # Select all donors for donor in query: print(donor.donor_name) except Exception as e: logger.debug(f'Error printing donors.') logger.debug(e) logger.debug('Failed to print a list of donors.') finally: logger.debug('database closes') database.close()
def last_donation(name): database.connect(reuse_if_open=True) logger.info('Connected to database') database.execute_sql('PRAGMA foreign_keys = ON;') try: with database.transaction(): logger.info(f'Trying to find the last record of {name}.') query = Donations.select().where(Donations.donor_name == name)\ .order_by(Donations.id) donation_list = [] # Create a list of donations for 'name'. for result in query: donation_list.append(int(result.donation)) except Exception as e: logger.info(f'Error finding last donation.') logger.info(e) finally: logger.info(f'Returning the last donation made by {name}.') return donation_list[-1]
def search(self, search_for): """Return None if donor is not in database.""" database.connect() logger.info('Connected to database') database.execute_sql('PRAGMA foreign_keys = ON;') try: with database.transaction(): logger.info(f'Searching through donors for ' f'{search_for}.') query = Donor.get_or_none( Donor.donor_name == search_for) # Select all donors except Exception as e: logger.info(f'Error searching for donors.') logger.info(e) logger.info('Failed to execute donor search.') finally: logger.info('database closes') database.close() if query is None: return query else: return query.donor_name
def delete_donor(person): database.connect(reuse_if_open=True) logger.info('Connected to database') database.execute_sql('PRAGMA foreign_keys = OFF;') try: with database.transaction(): logger.info('Trying to delete donor.') Donations.delete().where(Donations.donor_name == person)\ .execute() Donor.delete().where(Donor.donor_name == person)\ .execute() logger.info(f'Success deleting donor {person}.') except Exception as e: logger.info(f'Error deleting {person}') logger.info(e) logger.info('Failed to delete donor.') finally: logger.info('database closes') database.close()
def test_Group_search2(self): """Returns 'name' when name does exist""" database.connect(reuse_if_open=True) group_instance = d.Group('test.db') search_result = group_instance.search('Zach') # 'Zach is in database self.assertEqual(search_result, 'Zach')
class TestDelete(unittest.TestCase): """Test deleting a donor. Need to recreate the database to ensure deletion doesn't happen before other test cases.""" cur_dir = os.getcwd() logger.debug(f'Current Directory is {cur_dir}') file_list = os.listdir(cur_dir) logger.debug(f'File list is {file_list}') db_file = [] if os.path.exists('test2.db'): logging.info('Trying to delete the database.') os.remove('test2.db') logger.info(f'Database test.db has been deleted.') # Create a new database named test.db cwd = os.getcwd() logger.info(f'Creating new database test.db.') database.init('test2.db') database.connect() logger.info('Creating Modules in database') database.create_tables([Donor, Donations]) database.close() logger.info('Database has been created and is closed.') # Loading tables in new database database.connect() logger.info('Connected to database') database.execute_sql('PRAGMA foreign_keys = ON;') donors = ['Shane', 'Pete', 'Zach', 'Joe', 'Fitz'] donations = [('Shane', 6), ('Shane', 5), ('Shane', 10), ('Joe', 5), ('Zach', 10), ('Pete', 7), ('Pete', 8), ('Fitz', 1)] def test_delete_donor(self): """Delete a donor and check that the name is deleted in both the Donor table and the Donations table.""" #indiv_initalize = d.Individual('test2.db') database.connect(reuse_if_open=True) database.execute_sql('PRAGMA foreign_keys = ON;') #show that you can find 'Shane' aperson = Donor.get(Donor.donor_name == 'Shane') self.assertEqual(aperson.donor_name, 'Shane') # Test donor exists # Delete the donor 'Shane' d.Individual.delete_donor('Shane') # After deleting 'Shane', search donor table for shane query_donor = Donor.select().where(Donor.donor_name == 'Shane') donor_list = [] # Create a list of donations for 'name'. for result in query_donor: # logger.info(f'{result.donor_name}') donor_list.append(int(result.donor_name)) self.assertEqual(donor_list, []) # After deleting 'Shane', search donation table for shane query_donation = Donations.select().where(Donations.donor_name == 'Shane') donation_list = [] # Create a list of donations for 'name'. for result in query_donation: # logger.info(f'{result.donor_name}') donation_list.append(int(result.donation)) self.assertEqual(donation_list, []) # After deleting a donor, show that you can still find other donors. aperson = Donor.get(Donor.donor_name == 'Pete') self.assertEqual(aperson.donor_name, 'Pete') # Test donor was added
class TestMailbox(unittest.TestCase): # Delete test.db first cur_dir = os.getcwd() logger.debug(f'Current Directory is {cur_dir}') file_list = os.listdir(cur_dir) logger.debug(f'File list is {file_list}') db_file = [] if os.path.exists('test.db'): logging.info('Trying to delete the database.') os.remove('test.db') logger.info(f'Database test.db has been deleted.') # Create a new database named test.db cwd = os.getcwd() logger.info(f'Creating new database test.db.') new_database.database.init('test.db') database.connect() logger.info('Creating Modules in database') database.create_tables([new_database.Donor, new_database.Donations]) database.close() logger.info('Database has been created and is closed.') # Loading tables in new database database.connect() logger.info('Connected to database') database.execute_sql('PRAGMA foreign_keys = ON;') donors = ['Shane', 'Pete', 'Zach', 'Joe', 'Fitz'] donations = [('Shane', 6), ('Shane', 5), ('Shane', 10), ('Joe', 5), ('Zach',10), ('Pete', 7), ('Pete', 8), ('Fitz', 1)] try: for donor in donors: with database.transaction(): logger.info(f'Trying to add new donor {donor}.') new_donor = Donor.get_or_create(donor_name=donor) #new_donor.save() logger.info(f'Success adding donor {donor}.') except Exception as e: logger.info(f'Error loading database') logger.info(e) logger.info('Failed to add new donor.') finally: logger.info('Completed loading donors') try: for donation in donations: with database.transaction(): logger.info('Trying to add new donation.') new_donation = Donations.create( donor_name=donation[0], donation=donation[1]) new_donation.save() logger.info(f'Database added a donation of ' f'{donation[0]} by {donation[1]}.') except Exception as e: logger.info(f'Error loading donations') logger.info(e) logger.info('Failed to add donations.') finally: logger.info('database closes') database.close() def test_Individual_Add_Donation1(self): database.connect(reuse_if_open=True) database.execute_sql('PRAGMA foreign_keys = ON;') d.Individual.add_donation('Luke', 5) aperson = Donor.get(Donor.donor_name == 'Luke') adonation = Donations.get(Donations.donor_name == 'Luke') self.assertEqual(aperson.donor_name, 'Luke') # Test donor was added self.assertEqual(adonation.donation, 5) # Test donation was added for Luke def test_Group_search1(self): """Returns None when name does not exist""" #database.connect(reuse_if_open=True) group_instance = d.Group('test.db') search_result = group_instance.search('Bob') # Bob is not in database self.assertEqual(search_result, None) def test_Group_search2(self): """Returns 'name' when name does exist""" database.connect(reuse_if_open=True) group_instance = d.Group('test.db') search_result = group_instance.search('Zach') # 'Zach is in database self.assertEqual(search_result, 'Zach') def test_thankyou(self): test_text = 'Thank you so much for the generous gift of $5.00, Shane!' # thank_you = d.Individual('Shane', [5]) self.assertEqual(d.Individual.thank_you('Shane', 5), test_text) def test_summary(self): """Test dictionary set with {Donor: Total, number of donations, and average donation amount}""" group_instance = d.Group('test.db') summary = group_instance.summary() # 'Zach is in database self.assertDictEqual(summary, {'Shane': [21, 3, 7.0], 'Joe': [5, 1, 5.0], 'Zach': [10, 1, 10.0], 'Pete':[15, 2, 7.5], 'Fitz':[1,1,1.0], 'Luke':[5, 1, 5.0]}) def test_sort_list(self): """sorts the dictionary by largest to smallest donation""" group_instance = d.Group('test.db') summary = group_instance.summary() sorted = group_instance.sort_list(summary) self.assertEqual(sorted, ['Shane', 'Pete', 'Zach', 'Joe', 'Luke', 'Fitz']) def test_number_donations(self): num_donations = d.Individual.number_donations('Shane') self.assertEqual(num_donations, 3) def test_sum_donations(self): sum_donation = d.Individual.sum_donations('Shane') self.assertEqual(sum_donation, 21) def test_avg_donations(self): avg_donations = d.Individual.avg_donations('Shane') self.assertEqual(avg_donations, 7)