def test_fails_on_unbalanced_movements_and_single_account(self): self.data_update(movements_specs=[ MovementSpec(self.accs[0], Money(100, self.cur)), MovementSpec(self.accs[1], Money(-99, self.cur)) ]) errmsg = TransactionMovementSpecListValidator\ .ERR_MSGS['UNBALANCED_SINGLE_CURRENCY'] self.assertRaisesMessage(ValidationError, errmsg, self.call)
def test_from_data_base(self): trans = self.create() assert trans.get_description() == self.data['description'] assert trans.get_date() == self.data['date'] movements = trans.get_movements_specs() assert movements == [ MovementSpec(self.accs[0], Money(10, self.curs[0])), MovementSpec(self.accs[1], Money(-8, self.curs[1])), ]
def test_fails_if_movements_have_a_single_acc(self): self.data_update(movements_specs=[ MovementSpec(self.accs[0], Money(100, self.cur)), MovementSpec(self.accs[0], Money(-100, self.cur)) ]) errmsg = TransactionMovementSpecListValidator.ERR_MSGS[ 'SINGLE_ACCOUNT'] with self.assertRaisesMessage(ValidationError, errmsg): self.call()
def test_fails_if_duplicated_currency_account_pair(self): self.data_update(movements_specs=[ MovementSpec(self.accs[0], Money(1, self.cur)), MovementSpec(self.accs[0], Money(1, self.cur)), MovementSpec(self.accs[1], Money(-2, self.cur)) ]) errmsg = TransactionMovementSpecListValidator.ERR_MSGS[ "REPEATED_CURRENCY_ACCOUNT_PAIR"] with self.assertRaisesMessage(ValidationError, errmsg): self.call()
def test_base(self): currency = CurrencyTestFactory() account = AccountTestFactory() other_acc = AccountTestFactory() transaction_with = TransactionTestFactory(movements_specs=[ MovementSpec(account, Money('10', currency)), MovementSpec(other_acc, Money('-10', currency)), ]) transaction_without = TransactionTestFactory.create() assert list(Transaction.objects.filter_by_account(account)) ==\ [transaction_with]
def test_post_single_transaction(self): resp = self.client.post('/transactions/', self.post_data) assert resp.status_code == 201, resp.data assert resp.json()['date'] == '2018-12-21' assert resp.json()['description'] == self.post_data['description'] obj = Transaction.objects.get(pk=resp.json()['pk']) assert obj.get_description() == 'A' assert obj.date == date(2018, 12, 21) assert obj.get_movements_specs() == [ MovementSpec(self.accs[0], Money(200, self.cur)), MovementSpec(self.accs[1], Money(-200, self.cur)) ]
def setUp(self): super().setUp() self.accs = AccountTestFactory.create_batch(3, acc_type=AccTypeEnum.LEAF) self.curs = CurrencyTestFactory.create_batch(2) self.moneys = [Money(10, self.curs[0]), Money(-8, self.curs[1])] self.movements_specs = [ MovementSpec(self.accs[0], self.moneys[0]), MovementSpec(self.accs[1], self.moneys[1]) ] self.data = { 'description': 'hola', 'date': date(1993, 11, 23), 'movements_specs': [ MovementSpecSerializer(self.movements_specs[0]).data, MovementSpecSerializer(self.movements_specs[1]).data ] }
def setUp(self): super().setUp() # Some default data for the post request self.populate_accounts() self.accs = AccountTestFactory.create_batch( 2, acc_type=AccTypeEnum.LEAF ) self.cur = CurrencyTestFactory() self.moneys = [Money(200, self.cur), Money(-200, self.cur)] self.movements_specs = [ MovementSpec(self.accs[0], self.moneys[0]), MovementSpec(self.accs[1], self.moneys[1]) ] self.post_data = { 'description': 'A', 'date': date(2018, 12, 21), 'movements_specs': [ MovementSpecSerializer(self.movements_specs[0]).data, MovementSpecSerializer(self.movements_specs[1]).data ] }
def test_set_movements_base(self): cur = CurrencyTestFactory() values = ((Decimal(1) / Decimal(3)), (Decimal(2) / Decimal(3)), Decimal(-1)) moneys = [Money(val, cur) for val in values] accs = AccountTestFactory.create_batch(3) mov_specs = [ MovementSpec(acc, money) for acc, money in zip(accs, moneys) ] trans = TransactionTestFactory() assert trans.get_movements_specs() != mov_specs trans.set_movements(mov_specs) assert trans.get_movements_specs() == mov_specs
def test_patch_transaction(self): accs = AccountTestFactory.create_batch( 3, acc_type=AccTypeEnum.LEAF ) cur = CurrencyTestFactory() trans = TransactionTestFactory() new_movements = [MovementSpecSerializer(x).data for x in ( MovementSpec(accs[0], Money(100, cur)), MovementSpec(accs[1], Money(50, cur)), MovementSpec(accs[2], Money(-150, cur)) )] resp = self.client.patch( f'/transactions/{trans.pk}/', {'movements_specs': new_movements} ) assert resp.status_code == 200, resp.data trans.refresh_from_db() movements = trans.get_movements_specs() assert len(movements) == 3 assert [x.money for x in movements] == \ [Money(100, cur), Money(50, cur), Money(-150, cur)]
def setUp(self): super().setUp() self.date_ = date(2017, 12, 24) self.accs = [ AccountFactory()("A", AccTypeEnum.LEAF, get_root_acc()), AccountFactory()("B", AccTypeEnum.LEAF, get_root_acc()) ] # Force second money to exactly offset the first. self.cur = CurrencyTestFactory() self.moneys = [Money(100, self.cur), Money(-100, self.cur)] self.data = { "description": "Hola", "date_": self.date_, "movements_specs": [MovementSpec(a, m) for a, m in zip(self.accs, self.moneys)] }
def test_check_balance_and_add_transaction(self): # The user has two accounts he uses, with two transactions between them cur = CurrencyTestFactory() accs = AccountTestFactory.create_batch(2, acc_type=AccTypeEnum.LEAF) transactions = [ TransactionTestFactory(date_=date(2018, 1, 2), movements_specs=[ MovementSpec(accs[0], Money(100, cur)), MovementSpec(accs[1], Money(-100, cur)) ]), TransactionTestFactory(date_=date(2018, 1, 1), movements_specs=[ MovementSpec(accs[0], Money(22, cur)), MovementSpec(accs[1], Money(-22, cur)) ]) ] transactions.sort(key=lambda x: x.get_date(), reverse=True) serialized_transactions = \ TransactionSerializer(transactions, many=True).data # He also has another two accounts with an unrelated transaction other_accs = AccountTestFactory.create_batch(2, acc_type=AccTypeEnum.LEAF) TransactionTestFactory(date_=date(2017, 1, 2), movements_specs=[ MovementSpec(other_accs[0], Money(100, cur)), MovementSpec(other_accs[1], Money(-100, cur)) ]) # He queries ony for transactions involving acc1, and see the # same ones listed, in chronological order assert self.get_json(f"{URLS.transaction}?account_id={accs[0].pk}") == \ serialized_transactions # He adds a new transaction of 10 cur to acc2 new_transaction = self.post_json( URLS.transaction, { "description": "New Transaction", "date": "2018-01-03", "movements_specs": [{ "account": accs[0].pk, "money": { "quantity": 10, "currency": cur.pk } }, { "account": accs[1].pk, "money": { "quantity": -10, "currency": cur.pk } }] }) serialized_transactions.insert(0, new_transaction) # He queries again for transactions involving acc1, and see all # of them listed assert self.get_json(f"{URLS.transaction}?account_id={accs[0].pk}") == \ serialized_transactions
def test_create_base(self): assert self.create() == MovementSpec(self.acc, self.money)
def test_from_movement(self): transactions = TransactionTestFactory() mov = transactions.movement_set.all()[0] assert MovementSpec.from_movement(mov) == \ MovementSpec(mov.get_account(), mov.get_money())