From cc209c459a9897d0fe5c4d26db82636311204e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Loipf=C3=BChrer?= Date: Sun, 8 Sep 2024 18:32:14 +0200 Subject: [PATCH] fix(core): properly pass group_id kwargs in tests --- abrechnung/http/routers/accounts.py | 14 ++++++-------- abrechnung/http/routers/transactions.py | 10 +++++----- frontend/libs/api/project.json | 3 ++- tests/http_tests/test_groups.py | 18 +++++++++--------- tests/test_transaction_logic.py | 12 +++++++++--- tests/test_transactions.py | 14 ++++++++++---- 6 files changed, 41 insertions(+), 30 deletions(-) diff --git a/abrechnung/http/routers/accounts.py b/abrechnung/http/routers/accounts.py index 5d198901..43d7dd6e 100644 --- a/abrechnung/http/routers/accounts.py +++ b/abrechnung/http/routers/accounts.py @@ -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 @@ -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( @@ -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( @@ -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( @@ -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) diff --git a/abrechnung/http/routers/transactions.py b/abrechnung/http/routers/transactions.py index 9baf3690..d0110909 100644 --- a/abrechnung/http/routers/transactions.py +++ b/abrechnung/http/routers/transactions.py @@ -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( @@ -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( @@ -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): @@ -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( @@ -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( diff --git a/frontend/libs/api/project.json b/frontend/libs/api/project.json index 8b5ddca3..5ea3ef80 100644 --- a/frontend/libs/api/project.json +++ b/frontend/libs/api/project.json @@ -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" diff --git a/tests/http_tests/test_groups.py b/tests/http_tests/test_groups.py index ef144a8d..941e4349 100644 --- a/tests/http_tests/test_groups.py +++ b/tests/http_tests/test_groups.py @@ -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 {} @@ -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", @@ -222,12 +222,12 @@ 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", @@ -235,7 +235,7 @@ 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_name2", a["name"]) self.assertEqual("description1", a["description"]) @@ -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): diff --git a/tests/test_transaction_logic.py b/tests/test_transaction_logic.py index a5d04a99..8f1ed3d8 100644 --- a/tests/test_transaction_logic.py +++ b/tests/test_transaction_logic.py @@ -1,4 +1,4 @@ -# pylint: disable=attribute-defined-outside-init,missing-kwoa +# pylint: disable=attribute-defined-outside-init,missing-kwoa,unexpected-keyword-arg import base64 from datetime import datetime from pathlib import Path @@ -76,13 +76,16 @@ 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", @@ -92,7 +95,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): @@ -121,6 +124,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", @@ -137,6 +141,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", @@ -186,6 +191,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, diff --git a/tests/test_transactions.py b/tests/test_transactions.py index 733bf642..48c69555 100644 --- a/tests/test_transactions.py +++ b/tests/test_transactions.py @@ -1,4 +1,4 @@ -# pylint: disable=missing-kwoa +# pylint: disable=missing-kwoa,unexpected-keyword-arg from datetime import date, datetime @@ -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, @@ -175,7 +176,8 @@ 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( @@ -187,6 +189,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, @@ -257,12 +260,13 @@ 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, @@ -283,6 +287,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, @@ -298,7 +303,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() @@ -350,6 +355,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,