Skip to content

Commit

Permalink
fix(core): properly pass group_id kwargs in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikonse committed Sep 8, 2024
1 parent 4c9ebc3 commit 24fe9f3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
14 changes: 6 additions & 8 deletions abrechnung/http/routers/accounts.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from datetime import date
from typing import List, Optional
from typing import List

from fastapi import APIRouter, Depends, status
from pydantic import BaseModel

from abrechnung.application.accounts import AccountService
from abrechnung.domain.accounts import Account, ClearingShares, NewAccount
from abrechnung.domain.accounts import Account, NewAccount
from abrechnung.domain.users import User
from abrechnung.http.auth import get_current_user
from abrechnung.http.dependencies import get_account_service
Expand Down Expand Up @@ -53,7 +51,7 @@ async def create_account(
account=payload,
)

return await account_service.get_account(user=user, account_id=account_id)
return await account_service.get_account(user=user, group_id=group_id, account_id=account_id)


@router.get(
Expand All @@ -68,7 +66,7 @@ async def get_account(
user: User = Depends(get_current_user),
account_service: AccountService = Depends(get_account_service),
):
return await account_service.get_account(user=user, account_id=account_id)
return await account_service.get_account(user=user, group_id=group_id, account_id=account_id)


@router.post(
Expand All @@ -91,7 +89,7 @@ async def update_account(
account=payload,
)

return await account_service.get_account(user=user, account_id=account_id)
return await account_service.get_account(user=user, group_id=group_id, account_id=account_id)


@router.delete(
Expand All @@ -111,4 +109,4 @@ async def delete_account(
user=user,
account_id=account_id,
)
return await account_service.get_account(user=user, account_id=account_id)
return await account_service.get_account(user=user, group_id=group_id, account_id=account_id)
10 changes: 5 additions & 5 deletions abrechnung/http/routers/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def create_transaction(
transaction=payload,
)

return await transaction_service.get_transaction(user=user, transaction_id=transaction_id)
return await transaction_service.get_transaction(user=user, group_id=group_id, transaction_id=transaction_id)


@router.get(
Expand All @@ -90,7 +90,7 @@ async def get_transaction(
user: User = Depends(get_current_user),
transaction_service: TransactionService = Depends(get_transaction_service),
):
return await transaction_service.get_transaction(user=user, transaction_id=transaction_id)
return await transaction_service.get_transaction(user=user, group_id=group_id, transaction_id=transaction_id)


@router.post(
Expand All @@ -109,7 +109,7 @@ async def update_transaction(
await transaction_service.update_transaction(
user=user, transaction_id=transaction_id, transaction=payload, group_id=group_id
)
return await transaction_service.get_transaction(user=user, transaction_id=transaction_id)
return await transaction_service.get_transaction(user=user, group_id=group_id, transaction_id=transaction_id)


class UpdatePositionsPayload(BaseModel):
Expand All @@ -135,7 +135,7 @@ async def update_transaction_positions(
transaction_id=transaction_id,
positions=payload.positions,
)
return await transaction_service.get_transaction(user=user, transaction_id=transaction_id)
return await transaction_service.get_transaction(user=user, group_id=group_id, transaction_id=transaction_id)


@router.delete(
Expand All @@ -155,7 +155,7 @@ async def delete_transaction(
user=user,
transaction_id=transaction_id,
)
return await transaction_service.get_transaction(user=user, transaction_id=transaction_id)
return await transaction_service.get_transaction(user=user, group_id=group_id, transaction_id=transaction_id)


@router.get(
Expand Down
3 changes: 2 additions & 1 deletion frontend/libs/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"sed -i 's/type: any/type: \"clearing\"/' src/lib/generated/models/ClearingAccount.ts",
"echo \"/* eslint-disable */\\n$(cat src/lib/generated/schema.ts)\" > src/lib/generated/schema.ts",
"echo \"/* tslint:disable */\\n$(cat src/lib/generated/schema.ts)\" > src/lib/generated/schema.ts",
"echo \"/* istanbul ignore file */\\n$(cat src/lib/generated/schema.ts)\" > src/lib/generated/schema.ts"
"echo \"/* istanbul ignore file */\\n$(cat src/lib/generated/schema.ts)\" > src/lib/generated/schema.ts",
"npx prettier --write src/lib/generated"
],
"parallel": false,
"cwd": "libs/api"
Expand Down
18 changes: 9 additions & 9 deletions tests/http_tests/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ async def _fetch_group(self, group_id: int, expected_status: int = 200) -> dict:
self.assertEqual(group_id, ret_data["id"])
return ret_data

async def _fetch_account(self, account_id: int, expected_status: int = 200) -> dict:
resp = await self._get(f"/api/v1/accounts/{account_id}")
async def _fetch_account(self, group_id: int, account_id: int, expected_status: int = 200) -> dict:
resp = await self._get(f"/api/v1/groups/{group_id}/accounts/{account_id}")
self.assertEqual(expected_status, resp.status_code)
if expected_status >= 400:
return {}
Expand Down Expand Up @@ -213,7 +213,7 @@ async def test_update_account(self):
),
)
resp = await self._post(
f"/api/v1/accounts/{account_id}",
f"/api/v1/groups/{group_id}/accounts/{account_id}",
json={
"type": "personal",
"name": "new_name",
Expand All @@ -222,20 +222,20 @@ async def test_update_account(self):
)
self.assertEqual(200, resp.status_code)

a = await self._fetch_account(account_id)
a = await self._fetch_account(group_id, account_id)
self.assertEqual("new_name", a["name"])
self.assertEqual("description", a["description"])

resp = await self._post(
f"/api/v1/accounts/{account_id}",
f"/api/v1/groups/{group_id}/accounts/{account_id}",
json={
"type": "personal",
"name": "new_name2",
"description": "description1",
},
)
self.assertEqual(200, resp.status_code)
a = await self._fetch_account(account_id)
a = await self._fetch_account(group_id, account_id)
self.assertEqual("new_name2", a["name"])
self.assertEqual("description1", a["description"])

Expand Down Expand Up @@ -338,13 +338,13 @@ async def test_get_account(self):
description="description",
),
)
ret_data = await self._fetch_account(account_id)
ret_data = await self._fetch_account(group_id, account_id)
self.assertEqual("account1", ret_data["name"])

resp = await self._get(f"/api/v1/accounts/asdf1234")
resp = await self._get(f"/api/v1/groups/{group_id}/accounts/asdf1234")
self.assertEqual(422, resp.status_code)

resp = await self._get(f"/api/v1/accounts/13232")
resp = await self._get(f"/api/v1/groups/{group_id}/accounts/13232")
self.assertEqual(404, resp.status_code)

async def test_invites(self):
Expand Down
8 changes: 6 additions & 2 deletions tests/test_transaction_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ async def test_basic_clearing_account_workflow(self):
),
)

account: ClearingAccount = await self.account_service.get_account(user=self.user, account_id=account_id)
account: ClearingAccount = await self.account_service.get_account(user=self.user,group_id=self.group_id, account_id=account_id)
self.assertEqual(account_id, account.id)
self.assertEqual(2.0, account.clearing_shares[basic_account_id2])
self.assertEqual(1.0, account.clearing_shares[basic_account_id1])

await self.account_service.update_account(
user=self.user,
group_id=self.group_id,
account_id=account_id,
account=NewAccount(
name="Clearing",
Expand All @@ -92,7 +93,7 @@ async def test_basic_clearing_account_workflow(self):
clearing_shares={basic_account_id1: 1.0},
),
)
account = await self.account_service.get_account(user=self.user, account_id=account_id)
account = await self.account_service.get_account(user=self.user, group_id=self.group_id, account_id=account_id)
self.assertTrue(basic_account_id2 not in account.clearing_shares)

async def test_no_circular_clearing_accounts(self):
Expand Down Expand Up @@ -121,6 +122,7 @@ async def test_no_circular_clearing_accounts(self):
with self.assertRaises(Exception) as ctx:
await self.account_service.update_account(
user=self.user,
group_id=self.group_id,
account_id=account1_id,
account=NewAccount(
name="account1",
Expand All @@ -137,6 +139,7 @@ async def test_no_circular_clearing_accounts(self):
with self.assertRaises(Exception) as ctx:
await self.account_service.update_account(
user=self.user,
group_id=self.group_id,
account_id=account1_id,
account=NewAccount(
name="account1",
Expand Down Expand Up @@ -186,6 +189,7 @@ async def test_file_upload(self):

await self.transaction_service.update_transaction(
user=self.user,
group_id=self.group_id,
transaction_id=transaction_id,
transaction=UpdateTransaction(
type=TransactionType.purchase,
Expand Down
13 changes: 10 additions & 3 deletions tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ async def test_update_transaction(self):
)
await self.transaction_service.update_transaction(
user=self.test_user,
group_id=group_id,
transaction_id=transaction_id,
transaction=UpdateTransaction(
type=TransactionType.purchase,
Expand All @@ -175,7 +176,7 @@ async def test_update_transaction(self):
)

t: Transaction = await self.transaction_service.get_transaction(
user=self.test_user, transaction_id=transaction_id
user=self.test_user, transaction_id=transaction_id,
)
self.assertEqual(200.0, t.value)
self.assertEqual(
Expand All @@ -187,6 +188,7 @@ async def test_update_transaction(self):

await self.transaction_service.update_transaction(
user=self.test_user,
group_id=group_id,
transaction_id=transaction_id,
transaction=UpdateTransaction(
type=TransactionType.purchase,
Expand Down Expand Up @@ -257,12 +259,15 @@ async def test_account_deletion(self):
)

# we can delete the account when nothing depends on it
await self.account_service.delete_account(user=self.test_user, account_id=account1_id)
await self.account_service.delete_account(user=self.test_user,
group_id=group_id,
account_id=account1_id)

# the account has been deleted, we should not be able to add more shares to it
with self.assertRaises(Exception):
await self.transaction_service.update_transaction(
user=self.test_user,
group_id=group_id,
transaction_id=transaction_id,
transaction=UpdateTransaction(
type=TransactionType.purchase,
Expand All @@ -283,6 +288,7 @@ async def test_account_deletion(self):

await self.transaction_service.update_transaction(
user=self.test_user,
group_id=group_id,
transaction_id=transaction_id,
transaction=UpdateTransaction(
type=TransactionType.purchase,
Expand All @@ -298,7 +304,7 @@ async def test_account_deletion(self):
),
)
# we should not be able to delete this account as changes depend on it
await self.account_service.delete_account(user=self.test_user, account_id=account2_id)
await self.account_service.delete_account(user=self.test_user, group_id=group_id, account_id=account2_id)

async def test_purchase_items(self):
group_id = await self._create_group()
Expand Down Expand Up @@ -350,6 +356,7 @@ async def test_purchase_items(self):
position_id = t.positions[0].id
await self.transaction_service.update_transaction(
user=self.test_user,
group_id=group_id,
transaction_id=transaction_id,
transaction=UpdateTransaction(
type=TransactionType.purchase,
Expand Down

0 comments on commit 24fe9f3

Please sign in to comment.