def test_get_transaction_returns_in_chronological_order(self): transactions = TransactionTestFactory.create_batch(3) transactions[0].set_date(date(2000, 1, 3)) transactions[1].set_date(date(2000, 1, 1)) transactions[2].set_date(date(2000, 1, 2)) transactions.sort(key=lambda x: x.get_date(), reverse=True) assert self.client.get('/transactions/').json() == \ TransactionSerializer(transactions, many=True).data
def test_get_transactions(self): self.populate_accounts() Transaction.objects.all().delete() transactions = TransactionTestFactory.create_batch(5) transactions.sort(key=lambda x: x.pk, reverse=True) transactions.sort(key=lambda x: x.date, reverse=True) assert self.client.get('/transactions/').json() == \ [TransactionSerializer(x).data for x in transactions]
def test_get_transaction_filtered_by_account(self): accs = AccountTestFactory.create_batch(2) other_accs = AccountTestFactory.create_batch(2) transaction = TransactionTestFactory(movements_specs=[ MovementSpecTestFactory(account=accs[0]), MovementSpecTestFactory(account=accs[1]) ]) other_transaction = TransactionTestFactory(movements_specs=[ MovementSpecTestFactory(account=other_accs[0]), MovementSpecTestFactory(account=other_accs[1]) ]) assert self.client.get(f'/transactions/?account_id={accs[0].pk}').json() == \ TransactionSerializer([transaction], many=True).data
class JournalSerializer(Serializer): """ Serializes a Journal. """ account = PrimaryKeyRelatedField(read_only=True) initial_balance = BalanceSerializer() transactions = TransactionSerializer(many=True) balances = BalanceSerializer(many=True, source="get_balances")
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_get_single_transaction(self): transactions = TransactionTestFactory.create_batch(2) assert self.client.get(f'/transactions/{transactions[0].pk}/').json() == \ TransactionSerializer(transactions[0]).data
def update(self, obj): """ Uses the serializer to update with self.data """ ser = TransactionSerializer(obj, data=self.data) ser.is_valid(True) return ser.save()
def create(self): """ Uses the serializer to create with self.data """ ser = TransactionSerializer(data=self.data) ser.is_valid(True) return ser.save()