def test_display_custom_range(self): create_transaction('meh', self.revenue, self.account, 500, Transaction.DEPOSIT, date=datetime.date(2017, 1, 1)) create_transaction('meh', self.account, self.expense, 100, Transaction.WITHDRAW, date=datetime.date(2018, 1, 1)) context = self.client.get( reverse('account_detail', args=[ self.account.id, datetime.date(2017, 6, 1), datetime.date(2018, 2, 1) ])).context self.assertEqual(context['dstart'], datetime.date(2017, 6, 1)) self.assertEqual(context['dend'], datetime.date(2018, 2, 1)) self.assertEqual(context['out'], -100) self.assertEqual(context['in'], 0) self.assertEqual(context['difference'], -100) self.assertEqual(context['balance'], 400)
def test_balance_does_not_count_future_transactions(self): create_transaction('meh', self.foreign, self.account, 1000, Transaction.DEPOSIT, date(2100, 1, 1)) create_transaction('meh', self.personal, self.foreign, 500, Transaction.WITHDRAW, date(2100, 1, 1)) context = self.client.get(reverse('index')).context self.assertEqual(context['balance'], 0)
def test_correct_income(self): create_transaction('meh', self.revenue, self.account, 100, Transaction.DEPOSIT) context = self.client.get(reverse('account_view', args=[self.account.id])).context self.assertEquals(context['expenses'], 0) self.assertEquals(context['income'], 100) self.assertEquals(context['difference'], 100) self.assertEquals(context['balance'], 100)
def test_correct_expenses(self): create_transaction('meh', self.account, self.expense, 100, Transaction.WITHDRAW) context = self.client.get(reverse('account_view', args=[self.account.id])).context self.assertEquals(context['expenses'], -100) self.assertEquals(context['income'], 0) self.assertEquals(context['difference'], -100) self.assertEquals(context['balance'], -100)
def setUp(self): self.personal = Account.objects.create(name='personal') self.savings = Account.objects.create(name='savings') self.foreign = Account.objects.create( name='foreign', account_type=Account.AccountType.FOREIGN) self.transfer_transaction = create_transaction('transfer', self.personal, self.savings, 100, Transaction.TRANSFER, date(2017, 1, 1)) self.deposit_transaction = create_transaction('deposit', self.foreign, self.personal, 100, Transaction.DEPOSIT, date(2017, 6, 1)) self.withdraw_transaction = create_transaction('withdraw', self.personal, self.foreign, 100, Transaction.WITHDRAW, date(2017, 12, 1)) self.upcoming_transaction = create_transaction('upcoming', self.personal, self.foreign, 100, Transaction.WITHDRAW, date(2200, 1, 1))
def test_dataset_absence_for_expense_accounts(self): create_transaction('meh', self.revenue, self.account, 100, Transaction.DEPOSIT) create_transaction('meh', self.account, self.expense, 50, Transaction.WITHDRAW) context = self.client.get( reverse('account_view', args=[self.expense.id])).context self.assertFalse('dataset' in context)
def test_correct_difference(self): create_transaction('meh', self.revenue, self.account, 100, Transaction.DEPOSIT) create_transaction('meh', self.account, self.expense, 50, Transaction.WITHDRAW) context = self.client.get(reverse('account_view', args=[self.account.id])).context self.assertEqual(context['out'], -50) self.assertEqual(context['in'], 100) self.assertEqual(context['difference'], 50) self.assertEqual(context['balance'], 50)
def test_last_10_returns_something(self): create_transaction('meh', self.foreign, self.personal, 50, Transaction.DEPOSIT, date=date(2018, 1, 1)) self.assertEqual(Transaction.objects.last_10().count(), 1)
def test_context_with_transaction(self): create_transaction('meh', self.personal, self.expense, 100, Transaction.WITHDRAW) context = self.client.get(reverse('accounts')).context self.assertEquals(context['menu'], 'accounts') self.assertEquals(len(context['accounts']), 2) self.assertEquals(context['accounts'][0]['id'], self.account.id) self.assertEquals(context['accounts'][0]['name'], self.account.name) self.assertEquals(context['accounts'][0]['active'], self.account.active) self.assertEquals(context['accounts'][0]['balance'], self.account.balance)
def test_last_10_returns_at_most_10(self): for i in range(1, 32): create_transaction('meh', self.foreign, self.personal, 50, Transaction.DEPOSIT, date=date(2018, 1, i)) queryset = Transaction.objects.last_10() self.assertEqual(queryset.count(), 10) # they have to be the last ones for t in queryset: self.assertGreater(t.date.day, 20)
def test_is_transfer(self): transaction = create_transaction('meh', self.personal, self.savings, 100, Transaction.TRANSFER) self.assertTrue(transaction.is_transfer) self.assertFalse(transaction.is_withdraw) self.assertFalse(transaction.is_deposit) self.assertFalse(transaction.is_system)
def test_is_deposit(self): transaction = create_transaction('meh', self.foreign, self.personal, 100, Transaction.DEPOSIT) self.assertFalse(transaction.is_transfer) self.assertFalse(transaction.is_withdraw) self.assertTrue(transaction.is_deposit) self.assertFalse(transaction.is_system)
def test_average_amount_for_withdrawals(self): for i in range(1, 11): t = create_transaction('meh', self.personal, self.foreign, i * 10, Transaction.WITHDRAW) t.recurrence = self.recurrence t.save() self.assertEqual(self.recurrence.average_amount, -sum([i * 10 for i in range(1, 11)]) / 10)
def test_average_amount_for_deposits(self): for i in range(1, 11): t = create_transaction('meh', self.foreign, self.personal, i * 10, Transaction.DEPOSIT) t.recurrence = self.recurrence t.save() self.recurrence.transaction_type = Transaction.DEPOSIT self.recurrence.save() self.assertEquals(self.recurrence.average_amount, sum([i * 10 for i in range(1, 11)]) / 10)
def test_balance_with_past_transactions(self): create_transaction('meh', self.foreign, self.account, 1000, Transaction.DEPOSIT, date(2010, 1, 1)) create_transaction('meh', self.foreign, self.personal, 1000, Transaction.DEPOSIT, date(2015, 1, 1)) create_transaction('meh', self.account, self.foreign, 500, Transaction.WITHDRAW, date(2017, 1, 1)) create_transaction('meh', self.personal, self.foreign, 500, Transaction.WITHDRAW) context = self.client.get(reverse('index')).context self.assertEqual(context['balance'], 1000)
def test_context_CategoryIndex_with_category_with_transactions(self): category = Category.objects.create(name='expenses') create_transaction('Deposit', self.revenue, self.account, 1500, Transaction.DEPOSIT, category=category) create_transaction('Withdraw', self.account, self.expense, 500, Transaction.WITHDRAW, category=category) response = self.client.get(reverse('category_by_month')) self.assertEqual(response.status_code, 200) categories = response.context['categories'] self.assertEqual(len(categories), 1) self.assertEqual(categories[0]['name'], category.name) self.assertEqual(categories[0]['spent'], -500) self.assertEqual(categories[0]['income'], 1500)
def test_future_transaction_does_not_affect_calculations(self): create_transaction('meh', self.revenue, self.account, 500, Transaction.DEPOSIT, date=datetime.date(2017, 1, 1)) create_transaction('meh', self.account, self.expense, 100, Transaction.WITHDRAW, date=datetime.date(2018, 1, 1)) create_transaction('meh', self.account, self.expense, 50, Transaction.WITHDRAW, date=datetime.date(2200, 1, 1)) context = self.client.get( reverse('account_detail_all', args=[self.account.id])).context self.assertEqual(context['dstart'], datetime.date(2017, 1, 1)) self.assertEqual(context['dend'], datetime.date(2200, 1, 1)) self.assertEqual(context['out'], -100) self.assertEqual(context['in'], 500) self.assertEqual(context['difference'], 400) self.assertEqual(context['balance'], 400)
def setUp(self): self.first = create_account(name='first', account_type=Account.FOREIGN) self.second = create_account(name='second', account_type=Account.FOREIGN) self.third = create_account(name='third', account_type=Account.FOREIGN) self.personal = create_account(name='personal') create_transaction('first', self.personal, self.first, 50, Transaction.WITHDRAW) create_transaction('second', self.personal, self.second, 100, Transaction.WITHDRAW) create_transaction('second', self.personal, self.third, 25, Transaction.WITHDRAW) self.site = AdminSite() self.modeladmin = MockedAdmin(Account, self.site)
def setUp(self): User.objects.create_superuser(username='******', email='*****@*****.**', password='******') self.client.login(username='******', password='******') self.account = Account.objects.create(name='first account') self.foreign = Account.objects.create( name="other account", account_type=Account.FOREIGN) self.recurrence = RecurringTransaction.objects.create( title='A recurrence', amount=500, date=date(2019, 1, 1), src=self.account, dst=self.foreign, interval=RecurringTransaction.MONTHLY, transaction_type=Transaction.WITHDRAW, usual_month_day=1 ) self.transaction = create_transaction('title', self.account, self.foreign, 500, Transaction.WITHDRAW, date(2019, 1, 1)) self.transaction.recurrence = self.recurrence self.transaction.save()
def test_transaction_str_method(self): transaction = create_transaction('transaction', self.personal, self.foreign, 10, Transaction.WITHDRAW) self.assertEqual(str(transaction), transaction.title)
def test_transaction_type_string_transfer(self): transaction = create_transaction('meh', self.personal, self.savings, 100, Transaction.TRANSFER) self.assertEqual(transaction.get_transaction_type_str(), 'Transfer')
def test_transaction_type_string_deposit(self): transaction = create_transaction('meh', self.foreign, self.personal, 100, Transaction.DEPOSIT) self.assertEqual(transaction.get_transaction_type_str(), 'Deposit')
def test_transaction_type_string_withdraw(self): transaction = create_transaction('meh', self.personal, self.foreign, 100, Transaction.WITHDRAW) self.assertEqual(transaction.get_transaction_type_str(), 'Withdrawal')
def test_deposit_amount(self): transaction = create_transaction('meh', self.foreign, self.personal, 100, Transaction.DEPOSIT) self.assertEqual(transaction.amount, 100)
def test_transfer_amount(self): transaction = create_transaction('meh', self.personal, self.savings, 100, Transaction.TRANSFER) self.assertEqual(transaction.amount, 100)
def test_withdraw_amount(self): transaction = create_transaction('meh', self.personal, self.foreign, 100, Transaction.WITHDRAW) self.assertEqual(transaction.amount, 100)
def test_absolute_url(self): transaction = create_transaction('transaction', self.personal, self.foreign, 10, Transaction.WITHDRAW) self.assertEqual(transaction.get_absolute_url(), reverse('transaction_detail', args=[transaction.pk]))
def test_split_str_method(self): transaction = create_transaction('meh', self.foreign, self.personal, 100, Transaction.DEPOSIT) split = transaction.splits.first() self.assertEqual(str(split), split.title)