def test_when_init_then_make_parsed_lines_into_a_dictionary(self): test_aggregated = AggregatedMoneyTracker('money_tracker.txt') expected_result = { '22-03-2019': { 'Expenses': [ Expense(5.5, 'Eating Out', '22-03-2019'), Expense(34.0, 'Clothes', '22-03-2019'), Expense(41.79, 'Food', '22-03-2019'), Expense(12.0, 'Eating Out', '22-03-2019'), Expense(7.0, 'House', '22-03-2019'), Expense(14.0, 'Pets', '22-03-2019'), Expense(112.4, 'Bills', '22-03-2019'), Expense(21.5, 'Transport', '22-03-2019') ], 'Incomes': [Income(760.0, 'Salary', '22-03-2019')] }, '23-03-2019': { 'Expenses': [ Expense(15.0, 'Food', '23-03-2019'), Expense(5.0, 'Sports', '23-03-2019') ], 'Incomes': [ Income(50.0, 'Savings', '23-03-2019'), Income(200.0, 'Deposit', '23-03-2019') ] } } self.assertEqual(test_aggregated.records, expected_result)
def setUp(self): self.categories = [ Income('760', 'Salary'), Expense('5.5', 'Eating Out'), Expense('34', 'Clothes'), Income('50', 'Savings') ] self.strings = [ '760, Salary, New Income', '5.5, Eating Out, New Expense', '34, Clothes, New Expense', '50, Savings, New Income' ] self.strings1 = [ '50, Savings, New Income', '15, Food, New Expense', '200, Deposit, New Income', '5, Sports, New Expense' ] self.categories1 = [ Income('50', 'Savings'), Expense('15', 'Food'), Income('200', 'Deposit'), Expense('5', 'Sports') ] self.aggregator1 = AggregateData({ '=== 22-03-2018 ===': self.strings, '=== 23-03-2018 ===': self.strings1 }) self.tracker1 = MoneyTracker(self.aggregator1)
def test_add_expense(self): with self.subTest("with new date"): data = self.tracker1.list_user_data() self.tracker1.add_expense("Kniga", 17, '22-03-9999') self.assertTrue(Expense(17, "Kniga") in data['=== 22-03-9999 ===']) with self.subTest("with old date"): data = self.tracker1.list_user_data() self.tracker1.add_expense("Kola", 155.5, '22-03-2018') self.assertTrue( Expense(155.5, "Kola") in data['=== 22-03-2018 ==='])
def setUp(self): self.dates_dict = { '2018,11,12': { 'income': [Income('salary', 50)], 'expence': [Expense('food', 5)] }, '2018,11,15': { 'income': [], 'expence': [Expense('shoes', 100)] }, '2018,11,17': { 'income': [], 'expence': [Expense('clothes', 20)] } }
def test_get_string_with_data(self): self.assertEqual(MoneyTracker.get_string([1, 2, 3]), '123') self.assertEqual( MoneyTracker.get_string( [Income('salary', 50), Expense('shoes', 100)]), ('50, salary, New Income\n' '100, shoes, New Expense\n'))
def setUp(self): self.agr_money_tracker = AgregatedMoneyTracker() self.agr_money_tracker._data = { '2018,11,12': { 'income': [], 'expence': [Expense('food', 5)] }, '2018,11,15': { 'income': [Income('salary', 50)], 'expence': [Expense('shoes', 100)] }, '2018,11,17': { 'income': [Income('clothes', 20)], 'expence': [] } }
def add_expense(self, expense_category, money, date): new_expense = Expense(money, expense_category) conv_date = self.convert_date(date) if conv_date in self.data.keys(): self.data[conv_date].append(new_expense) else: self.data[conv_date] = [new_expense]
def test_set_record_for_date_with_old_date(self): expected_result = { '2018,11,12': { 'income': [], 'expence': [Expense('food', 5)] }, '2018,11,15': { 'income': [Income('salary', 50)], 'expence': [Expense('shoes', 100)] }, '2018,11,17': { 'income': [Income('clothes', 20)], 'expence': [Expense('water', 5)] } } self.agr_money_tracker.set_record_for_date('2018,11,17', '5, water, New Expense\n') self.assertEqual(self.agr_money_tracker._data, expected_result)
def create_database(self): for line in self.raw_content: try: date = datetime.strptime(line, '=== %d-%m-%Y ===') except ValueError: amount, label, category = line.split(', ') cat_obj = Expense(amount, label) if category == 'New Expense' \ else Income(amount, label) self.database.append([date, cat_obj, cat_obj.CATEGORY_STR])
def __add_transaction(self, amount, transactionType, category): if category == "New Income": return Income(category, amount) elif category == "New Expense": return Expense(category, amount) else: raise ValueError("Transaction category must be:\n" "New Income\n" "New Expense\n")
def generate_categories_from_parser(self): for item in self.data: if '===' in item: date = item.split('===')[1].replace(' ', '') else: temp = item.split(', ') if temp[2] == 'New Income': if '.' in temp[0]: income = Income(float(temp[0]), temp[1], date) else: income = Income(int(temp[0]), temp[1], date) self.incomes.append(income) else: if '.' in temp[0]: expense = Expense(float(temp[0]), temp[1], date) else: expense = Expense(int(temp[0]), temp[1], date) self.expenses.append(expense)
def test_add_record_with_date_in_the_dates_dict(self): dates_dict_result = { '2018,11,12': { 'income': [Income('salary', 50)], 'expence': [Expense('food', 5), Expense('computer', 2500)] }, '2018,11,15': { 'income': [], 'expence': [Expense('shoes', 100)] }, '2018,11,17': { 'income': [], 'expence': [Expense('clothes', 20)] } } self.assertEqual( ParseMTD.add_record(self.dates_dict, '2018,11,12', Expense('computer', 2500)), (dates_dict_result, '2018,11,12'))
def test_add_expense(self): expected = [ '5.5, Eating Out, New Expense', '34, Clothes, New Expense', '41.79, Food, New Expense', '12, Eating Out, New Expense', '7, House, New Expense', '14, Pets, New Expense', '112.4, Bills, New Expense', '21.5, Transport, New Expense', '15, Food, New Expense', '5, Sports, New Expense', '50, Food, New Expense' ] self.agg.add_expense(Expense(50, 'Food', '22-03-2018')) actual = [str(income) for income in self.agg.expenses] self.assertEqual(actual, expected)
def __init__(self, content): self.user_data = {} for line in content: if '=' in line: curr_date = line.strip('=\n ') self.user_data[curr_date] = {'income': [], 'expense': []} else: amount, label, category = line.split(', ') amount = MoneyTracker.int_or_float(float(amount)) if 'income' in category.lower(): self.user_data[curr_date]['income'].append(Income(amount, label)) elif 'expense' in category.lower(): self.user_data[curr_date]['expense'].append(Expense(amount, label))
def test_add_record_with_date_not_in_dates_dict(self): dates_dict_result = { '2018,11,12': { 'income': [Income('salary', 50)], 'expence': [Expense('food', 5)] }, '2018,11,15': { 'income': [], 'expence': [Expense('shoes', 100)] }, '2018,11,17': { 'income': [], 'expence': [Expense('clothes', 20)] }, '2018,5,11': { 'income': [Income('paycheck', 10)], 'expence': [] } } self.assertEqual( ParseMTD.add_record(self.dates_dict, '2018,5,11', Income('paycheck', 10)), (dates_dict_result, '2018,5,11'))
def __init__(self, file_name): self.records = {} data = Parser.parse_money_tracker_data(file_name) for line in data: if line.startswith('==='): current_date = line[4:-5] self.records[current_date] = {'Expenses': [], 'Incomes': []} else: record = line.split(', ') if 'Income' in record[2]: self.records[current_date]['Incomes'].append( Income(float(record[0]), record[1], current_date)) if 'Expense' in record[2]: self.records[current_date]['Expenses'].append( Expense(float(record[0]), record[1], current_date))
def add_record(self, date_object, amount, label, category): if category.title() in Income.CATEGORY_STR: self.database.append([date_object, Income(amount, label), Income.CATEGORY_STR]) elif category.title() in Expense.CATEGORY_STR: self.database.append([date_object, Expense(amount, label), Expense.CATEGORY_STR])
def test_type_init_income(self): with self.assertRaises(TypeError): Expense([], "", '22-03-18') Expense(12, {}, '22-03-18')
def setUp(self): self.one = Expense(35, "Food", '22-03-18') self.two = Expense(50, "Eating Out", '22-03-18')
def test_str_dunder(self): test_expense = Expense(40.6, 'Clothes', '23-12-2018') expected_result = '40.6, Clothes, New Expense' self.assertEqual(str(test_expense), expected_result)
def test_eq_dunder_when_two_expenses_are_not_equal(self): test_expense1 = Expense(40.6, 'Clothes', '23-12-2018') test_expense2 = Expense(40.6, 'Clothes', '23-11-2018') self.assertFalse(test_expense1 == test_expense2)
def test_amount_init_income(self): with self.assertRaises(ValueError): Expense(-15, "", '22-03-18')
def test_when_category_then_return_category_cls_attribute(self): test_expense = Expense(40.6, 'Clothes', '23-12-2018') expected_result = 'Expense' self.assertEqual(test_expense.category, expected_result)
def parse_expence(cls, row): checked_row = cls.expence_re.match(row) if checked_row is None: return None parts = checked_row.group().split(', ') return Expense(parts[1], float(parts[0]))
def add_expence(self, day, month, year, category, amount): date = f'{year:04d},{month:02d},{day:02d}' self._data_base.set_record_for_date(date, Expense(category, amount))
def test_eq_expence(self): self.assertNotEqual(self.expence, Expense('clotes', 5)) self.assertNotEqual(self.expence, Expense('food', 10)) self.assertEqual(self.expence, Expense('food', 5))
def setUp(self): self.expence = Expense('food', 5) self.income = Income('salary', 10.55)
def test_get_expences_for_date_date_with_records(self): self.assertEqual( self.agr_money_tracker.get_expences_for_date('2018,' '11,15'), [Expense('shoes', 100)])
def test_get_user_expences(self): self.assertEqual( self.money_tracker.get_user_expences(), [Expense('food', 5), Expense('shoes', 100)])
def add_expense(self, amount, record_type, date): if date not in self.aggregated_data.records.keys(): self.aggregated_data.records[date] = {'Expenses': [], 'Incomes': []} self.aggregated_data.records[date]['Expenses'].append(Expense(float(amount), record_type, date))