Skip to content

Commit 2422643

Browse files
committed
feat: Add get transactions viewmodel
1 parent 0e452dd commit 2422643

2 files changed

Lines changed: 104 additions & 17 deletions

File tree

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1-
from typing import List
1+
from typing import List, Dict
2+
23
from src.shared.domain.entities.transactions import Transaction
34

45

56
class TransactionViewmodel:
6-
transaction: Transaction
7-
7+
"""
8+
Converte uma única entidade Transaction em dicionário.
9+
"""
810
def __init__(self, transaction: Transaction):
9-
self.transaction = transaction
11+
self.id = transaction.id
12+
self.user_id = transaction.user_id
13+
self.plan = transaction.plan.name
14+
self.value = transaction.value
15+
self.create_date = transaction.create_date
1016

11-
def to_dict(self) -> dict:
17+
def to_dict(self) -> Dict:
1218
return {
13-
"id": self.transaction.id,
14-
"user_id": self.transaction.user_id,
15-
"plan": self.transaction.plan.value,
16-
"value": self.transaction.value,
17-
"create_date": self.transaction.create_date
19+
"id": self.id,
20+
"user_id": self.user_id,
21+
"plan": self.plan,
22+
"value": self.value,
23+
"create_date": self.create_date
1824
}
1925

2026

2127
class GetTransactionsByUserViewmodel:
22-
transactions: List[Transaction]
23-
2428
def __init__(self, transactions: List[Transaction]):
25-
self.transactions = transactions
29+
self.transactions = [TransactionViewmodel(t).to_dict() for t in transactions]
2630

27-
def to_dict(self) -> dict:
31+
def to_dict(self) -> Dict:
2832
return {
29-
"transactions": [TransactionViewmodel(t).to_dict() for t in self.transactions] if self.transactions else [],
30-
"last_transaction_id": self.transactions[-1].id if self.transactions else None,
31-
"message": "the transactions were retrieved successfully"
33+
"transactions": self.transactions,
34+
"message": "transactions retrieved successfully"
3235
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import pytest
2+
3+
from src.modules.get_transactions_by_user.app.get_transactions_by_user_viewmodel import (
4+
GetTransactionsByUserViewmodel
5+
)
6+
from src.shared.domain.entities.transactions import Transaction
7+
from src.shared.domain.enums.plan_enum import PLAN
8+
9+
10+
class Test_GetTransactionsByUserViewmodel:
11+
12+
def test_single_transaction(self):
13+
tx = Transaction(
14+
id="tx-1",
15+
user_id="user-1",
16+
plan=PLAN.BRONZE,
17+
value=50.0,
18+
create_date=1700000000
19+
)
20+
viewmodel = GetTransactionsByUserViewmodel([tx])
21+
result = viewmodel.to_dict()
22+
23+
expected = {
24+
"transactions": [
25+
{
26+
"id": "tx-1",
27+
"user_id": "user-1",
28+
"plan": "BRONZE",
29+
"value": 50.0,
30+
"create_date": 1700000000
31+
}
32+
],
33+
"message": "transactions retrieved successfully"
34+
}
35+
assert result == expected
36+
37+
def test_multiple_transactions(self):
38+
tx1 = Transaction(
39+
id="tx-1",
40+
user_id="user-1",
41+
plan=PLAN.SILVER,
42+
value=75.5,
43+
create_date=1700001000
44+
)
45+
tx2 = Transaction(
46+
id="tx-2",
47+
user_id="user-1",
48+
plan=PLAN.GOLD,
49+
value=120.0,
50+
create_date=1700002000
51+
)
52+
viewmodel = GetTransactionsByUserViewmodel([tx1, tx2])
53+
result = viewmodel.to_dict()
54+
55+
expected = {
56+
"transactions": [
57+
{
58+
"id": "tx-1",
59+
"user_id": "user-1",
60+
"plan": "SILVER",
61+
"value": 75.5,
62+
"create_date": 1700001000
63+
},
64+
{
65+
"id": "tx-2",
66+
"user_id": "user-1",
67+
"plan": "GOLD",
68+
"value": 120.0,
69+
"create_date": 1700002000
70+
}
71+
],
72+
"message": "transactions retrieved successfully"
73+
}
74+
assert result == expected
75+
76+
def test_empty_list(self):
77+
viewmodel = GetTransactionsByUserViewmodel([])
78+
result = viewmodel.to_dict()
79+
80+
expected = {
81+
"transactions": [],
82+
"message": "transactions retrieved successfully"
83+
}
84+
assert result == expected

0 commit comments

Comments
 (0)