def test_get_current_accumulated_expenses_first_day(self): group = ExpenseGroup.objects.first() day1 = datetime_util.first_date_current_month() day1_expense = Expense.get_expense_value_in(day1, day1, group=group) rs = admin._get_current_accumulated_expenses(group) self.assertCountEqual([day1_expense], rs)
def test_get_current_accumulated_expenses(self): group = ExpenseGroup.objects.first() day1 = datetime_util.first_date_current_month() day1_expense = Expense.get_expense_value_in(day1, day1, group=group) day2 = day1 + timedelta(days=1) Expense.objects.create( wallet=Wallet.objects.first(), category=ExpenseCategory.objects.filter(group=group).first(), value=50, pay_date=day2, ) day2_expense = Expense.get_expense_value_in(day2, day2, group=group) rs = admin._get_current_accumulated_expenses(group) self.assertEqual(day1_expense, rs[0]) self.assertEqual(day1_expense + day2_expense, rs[1])
def test_get_expenses_in(self): Expense.objects.all().delete() self.assertEqual( 0, Expense.get_expenses_in( start_date=datetime_util.today(), end_date=datetime_util.today(), ).count()) e = Expense.objects.create( wallet=Wallet.objects.first(), category=ExpenseCategory.objects.first(), value=199, ) self.assertEqual( e, Expense.get_expenses_in( start_date=datetime_util.today(), end_date=datetime_util.today(), ).get())
def _get_group_expense_info(from_date: datetime.date, to_date: datetime.date) -> dict: return { 'groups': [{ 'name': group.name, 'expense_value': Expense.get_expense_value_in(from_date, to_date, group=group), 'categories': [{ 'name': cat.name, 'expense_value': Expense.get_expense_value_in(from_date, to_date, category=cat), 'expenses_url': ("/admin/money/expense/" f"?category__id__exact={cat.id}" f"&pay_date__gte={from_date.strftime(DATE_FORMAT)}" f"&pay_date__lte={to_date.strftime(DATE_FORMAT)}") } for cat in ExpenseCategory.objects.filter(group=group)] } for group in ExpenseGroup.objects.all()] }
def test_expense_report_overview_context(self): expense_this_month = Expense.get_expense_value_in( self.month_start, self.month_end) days = (self.month_end - self.month_start).days + 1 avg_expense = int(expense_this_month / days) one_month_ago = self.month_start - relativedelta(months=1) two_month_ago = self.month_start - relativedelta(months=2) res = self.client.get(self.this_month_report_url) self.assertContains(res, expense_this_month) self.assertContains(res, avg_expense) self.assertContains(res, one_month_ago.strftime('%b')) self.assertContains(res, two_month_ago.strftime('%b'))
def _get_current_accumulated_expenses(group: ExpenseGroup) -> List[int]: """ Returns a list of accumulated expense. The i-th element is the expense from day 1 of this month until day i-th. """ expense_til_today = [] for date in datetime_util.get_date_iterator( datetime_util.first_date_current_month(), datetime_util.today()): expense = sum([ ex.value for ex in Expense.get_expenses_in( start_date=date, end_date=date).filter(category__group=group) ]) expense_til_today.append(expense if len(expense_til_today) == 0 else expense_til_today[-1] + expense) return expense_til_today
def app_index(self, request, app_label, extra_context=None): from_date = datetime_util.first_date_current_month() to_date = datetime_util.today() income = Income.get_income_value_in(from_date, to_date) expense = Expense.get_expense_value_in(from_date, to_date) return super().app_index(request, app_label, extra_context={ 'net_worth': finance_util.get_net_worth(), 'income_this_month': income, 'expense_this_month': expense, 'balance_this_month': income - expense, 'pinned_models': [ model_cls._meta.object_name for model_cls in (Budget, Expense, Income) ] })
def _get_expense_info_in( time_ranges: List[Tuple[datetime.date, datetime.date]], ) -> List[PreviousExpenseInfo]: """ Get expense info in each time range. """ first_value = 0 first_avg = 0 rs = [] for i, (from_date, to_date) in enumerate(time_ranges): value = Expense.get_expense_value_in(from_date, to_date) value_change_percent = (int((first_value - value) / value * 100) if value != 0 and i > 0 else 0) average = (value / (to_date - from_date).days if to_date != from_date else value) avg_change_percent = (int((first_avg - average) / average * 100) if average != 0 and i > 0 else 0) info = PreviousExpenseInfo( from_date=from_date.strftime(DATE_FORMAT), to_date=to_date.strftime(DATE_FORMAT), name=from_date.strftime("%b"), value=value, value_change_percent=value_change_percent, daily_avg=average, daily_avg_change_percent=avg_change_percent, ) rs.append(info) if i == 0: first_value = value first_avg = average return rs