def test_load_donor_data_from_file(self): donor1 = Donor("jeff bezos", [200000]) donor2 = Donor("Bill Gates", [150000]) dc = DonorCollection([donor1, donor2]) msg = "File donors.txt does not exist. The donors record history is currently empty!" with mock.patch("builtins.open", mock.mock_open()) as mock_open: mock_open.side_effect = FileNotFoundError() result = dc.load_donor_data_from_file('donors.txt') assert result == msg assert dc.donor_list == [] mock_open.assert_called_with('donors.txt', 'r') dc = DonorCollection() msg = "Donors records successfully loaded from donors.txt !" donor_text = "jeff bezos;30000.0,50000.0\nbill gates;20000.0,80000.0\n" mock_open = mock.mock_open(read_data=donor_text) with mock.patch("builtins.open", mock_open): result = dc.load_donor_data_from_file('donors.txt') assert result == msg assert dc.donor_list == [ Donor('bill gates', [20000.0, 80000.0]), Donor('jeff bezos', [30000.0, 50000.0]) ] assert dc.donor_list[0] > dc.donor_list[1] mock_open.assert_called_with('donors.txt', 'r')
def main(): # Loading donors data if it exists filename = 'donors.txt' donor_collection = DonorCollection() msg = donor_collection.load_donor_data_from_file(filename) IO.print_data_to_user(msg) # Display the menu while True: IO.print_main_menu() choice = IO.input_choice("Which action would you like to perform? ") if choice == '1': IO.send_thank_you_to_single_donor(donor_collection) elif choice == '2': IO.print_data_to_user(donor_collection.generate_donor_report()) elif choice == '3': IO.print_data_to_user(donor_collection.generate_letter_to_all_donors()) elif choice == '4': if len(donor_collection.donor_list): donor_collection.save_donor_data_to_file(filename) IO.print_data_to_user("GoodBye!") break else: IO.print_data_to_user("Your selection is invalid. Please select a menu option from 1 to 4")