Skip to content

Commit 104786e

Browse files
committed
Add better categories
1 parent 12ab23d commit 104786e

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

src/pyfirefly/pyfirefly.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,35 @@ async def get_transactions(
231231

232232
return [Transaction.from_dict(tx) for tx in transactions]
233233

234-
async def get_categories(self, category_id: int, start: str | None = None, end: str | None = None) -> Category:
234+
async def get_categories(self) -> list[Category]:
235+
"""Get all categories from the Firefly server.
236+
237+
Returns
238+
-------
239+
A list of Category objects containing category information.
240+
241+
"""
242+
categories: list[dict[str, Any]] = []
243+
next_page: int | None = 1
244+
245+
while next_page:
246+
response = await self._request(
247+
uri="categories",
248+
method="GET",
249+
params={"page": next_page},
250+
)
251+
252+
categories.extend(response["data"])
253+
254+
pagination = response.get("meta", {}).get("pagination", {})
255+
current_page = int(pagination.get("current_page", 1) or 1)
256+
total_pages = int(pagination.get("total_pages", 1) or 1)
257+
258+
next_page = current_page + 1 if current_page < total_pages else None
259+
260+
return [Category.from_dict(cat) for cat in categories]
261+
262+
async def get_category(self, category_id: int, start: str | None = None, end: str | None = None) -> Category:
235263
"""Get a specific category by its ID.
236264
237265
Args:

tests/__snapshots__/test_models.ambr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
Budget(type='budgets', id='2', attributes=BudgetAttributes(created_at='2018-09-17T12:46:47+01:00', updated_at='2018-09-17T12:46:47+01:00', name='Bills', active=False, notes='Some notes', order=5, auto_budget_type='reset', currency_id='12', currency_code='EUR', currency_symbol='$', currency_decimal_places=2, native_currency_id='5', native_currency_code='EUR', native_currency_symbol='$', native_currency_decimal_places=2, auto_budget_amount='-1012.12', native_auto_budget_amount='-1012.12', auto_budget_period='monthly', spent=[BudgetSpent(sum='123.45', currency_id='5', currency_code='USD', currency_symbol='$', currency_decimal_places=2)])),
4040
])
4141
# ---
42+
# name: test_categories_model
43+
list([
44+
Category(type='categories', id='2', attributes=CategoryAttributes(created_at='2018-09-17T12:46:47+01:00', updated_at='2018-09-17T12:46:47+01:00', name='Lunch', notes='Some example notes', native_currency_id='5', native_currency_code='EUR', native_currency_symbol='$', native_currency_decimal_places=2, spent=[CategoryAmount(currency_id='5', currency_code='USD', currency_symbol='$', currency_decimal_places=2, sum='-12423.45')], earned=[CategoryAmount(currency_id='5', currency_code='USD', currency_symbol='$', currency_decimal_places=2, sum='123.45')])),
45+
])
46+
# ---
4247
# name: test_category_model
4348
Category(type='categories', id='2', attributes=CategoryAttributes(created_at='2018-09-17T12:46:47+01:00', updated_at='2018-09-17T12:46:47+01:00', name='Lunch', notes='Some example notes', native_currency_id='5', native_currency_code='EUR', native_currency_symbol='$', native_currency_decimal_places=2, spent=[CategoryAmount(currency_id='5', currency_code='USD', currency_symbol='$', currency_decimal_places=2, sum='-12423.45')], earned=[CategoryAmount(currency_id='5', currency_code='USD', currency_symbol='$', currency_decimal_places=2, sum='123.45')]))
4449
# ---

tests/fixtures/categories.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"data": [
3+
{
4+
"type": "categories",
5+
"id": "2",
6+
"attributes": {
7+
"created_at": "2018-09-17T12:46:47+01:00",
8+
"updated_at": "2018-09-17T12:46:47+01:00",
9+
"name": "Lunch",
10+
"notes": "Some example notes",
11+
"native_currency_id": "5",
12+
"native_currency_code": "EUR",
13+
"native_currency_symbol": "$",
14+
"native_currency_decimal_places": 2,
15+
"spent": [
16+
{
17+
"currency_id": "5",
18+
"currency_code": "USD",
19+
"currency_symbol": "$",
20+
"currency_decimal_places": 2,
21+
"sum": "-12423.45"
22+
}
23+
],
24+
"earned": [
25+
{
26+
"currency_id": "5",
27+
"currency_code": "USD",
28+
"currency_symbol": "$",
29+
"currency_decimal_places": 2,
30+
"sum": "123.45"
31+
}
32+
]
33+
}
34+
}
35+
],
36+
"meta": {
37+
"pagination": {
38+
"total": 3,
39+
"count": 20,
40+
"per_page": 100,
41+
"current_page": 1,
42+
"total_pages": 1
43+
}
44+
}
45+
}

tests/test_models.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ async def test_category_model(
109109
),
110110
)
111111

112-
categories = await firefly_client.get_categories(category_id=1, start="2025-01-01", end="2025-12-31")
113-
assert categories == snapshot
112+
category = await firefly_client.get_category(category_id=1, start="2025-01-01", end="2025-12-31")
113+
assert category == snapshot
114114

115115
# Now without a date range
116116
aresponses.add(
@@ -124,7 +124,28 @@ async def test_category_model(
124124
),
125125
)
126126

127-
categories = await firefly_client.get_categories(category_id=1)
127+
category = await firefly_client.get_category(category_id=1)
128+
assert category == snapshot
129+
130+
131+
async def test_categories_model(
132+
aresponses: ResponsesMockServer,
133+
firefly_client: Firefly,
134+
snapshot: SnapshotAssertion,
135+
) -> None:
136+
"""Test the Categories model."""
137+
aresponses.add(
138+
"localhost:9000",
139+
"/api/v1/categories",
140+
"GET",
141+
aresponses.Response(
142+
status=200,
143+
headers={"Content-Type": "application/vnd.api+json"},
144+
text=load_fixtures("categories.json"),
145+
),
146+
)
147+
148+
categories = await firefly_client.get_categories()
128149
assert categories == snapshot
129150

130151

0 commit comments

Comments
 (0)