def from_csv(self, csv_file): """Get data from a csv file :param csv_file: csv file to get from :type csv_file, i.e. the filepath """ with open(csv_file, "r") as f: reader = csv.DictReader(f) for item in reader: expense = Expense.create_from_dict(item) self.add_expense(expense)
def test_create_from_dict(): """ Tests for the create_from_dict method """ expense = Expense(1, "school", 200) # Checks if method exists assert hasattr(expense, "create_from_dict") # Checks if method returns correct value type (object) test_dictionary = { 'ID': 1, 'Category': "school", 'Amount': 200.00, 'Date': "2021-05-05", } assert type(expense.create_from_dict(test_dictionary) == object) # Checks if returned object is instance of Expense class expense_object = expense.create_from_dict(test_dictionary) assert isinstance(expense_object, Expense)
def read_largest_id(self, csv_file): """Get largest id from csv_file :param csv_file: csv file to get from :type csv_file, i.e. the filepath """ id = [] with open(csv_file, "r") as f: reader = csv.DictReader(f) for item in reader: expense = Expense.create_from_dict(item) id.append(expense.ID) id.sort(reverse=True) # Check if the csv have no record at all if len(id) == 0: largest_ID = 0 else: largest_ID = id[0] return largest_ID