Skip to content

Commit b0c9de7

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent c6edd46 commit b0c9de7

2 files changed

Lines changed: 39 additions & 14 deletions

File tree

docs/development/code.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ The following (simplified) code for tagging a dataset (from `src/routers/dataset
4545
Click the (+) icon for information.
4646

4747
```python title="src/routers/datasets.py"
48-
4948
router = APIRouter(prefix="/datasets", tags=["datasets"]) # (1)!
5049

50+
5151
@router.post(path="/tag") # (2)!
5252
async def tag_dataset(
5353
data_id: Annotated[Identifier, Body()], # (3)!
@@ -65,7 +65,7 @@ async def tag_dataset(
6565
msg = f"Dataset {data_id} already tagged with {tag!r}."
6666
raise TagAlreadyExistsError(msg) from None
6767

68-
logger.info("Dataset {data_id} tagged '{tag}'.", data_id=data_id, tag=tag) # (11)!
68+
logger.info("Dataset {data_id} tagged '{tag}'.", data_id=data_id, tag=tag) # (11)!
6969

7070
tags = await database.datasets.get_tags_for(data_id, expdb_db)
7171

docs/development/tests.md

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,36 @@ There are a number of fixtures in `conftest.py`, here is a quick rundown of the
8383
The pseudocode below shows how you might combine these for a test of the new REST API, either as standalone or when compared to the PHP API:
8484

8585
```python
86-
8786
async def test_python(py_api: httpx.AsyncClient, expdb_session: AsyncSession) -> None:
88-
await expdb_session.execute(text("INSERT INTO dataset ..."), params=...) # Insert dataset with id 42
87+
await expdb_session.execute(
88+
text("INSERT INTO dataset ..."), params=...
89+
) # Insert dataset with id 42
8990

90-
response = await py_api.get("/datasets/42") # Since this call shares the session, it should retrieve this data
91+
response = await py_api.get(
92+
"/datasets/42"
93+
) # Since this call shares the session, it should retrieve this data
9194

9295
assert ...
9396
# after the test is done, the fixture clean up will ensure the change is not committed to the database, no extra code needed
9497

95-
async def test_python_and_php(py_api: httpx.AsyncClient, php_api: httpx.AsyncClient, expdb_connection: AsyncConnection) -> None:
96-
await expdb_connection.execute(text("INSERT INTO dataset ..."), parameters=...) # Insert dataset with id 42
98+
99+
async def test_python_and_php(
100+
py_api: httpx.AsyncClient, php_api: httpx.AsyncClient, expdb_connection: AsyncConnection
101+
) -> None:
102+
await expdb_connection.execute(
103+
text("INSERT INTO dataset ..."), parameters=...
104+
) # Insert dataset with id 42
97105
await expdb_connection.commit() # We need to persist the data in the database, because the PHP REST API cannot see our transaction
98106

99-
response = await php_api.get("/datasets/42") # The PHP REST API can see the dataset, because it exists in the database
107+
response = await php_api.get(
108+
"/datasets/42"
109+
) # The PHP REST API can see the dataset, because it exists in the database
100110
response = await py_api.get("/datasets/42") # The Python REST API can see the dataset also
101111

102112
# We need to clean up after ourselves, otherwise the test has side effects.
103113
# This isn't a great pattern, prefer instead the use of context managers which will execute the delete statements even if unexpected exceptions occur.
104114
await expdb_connection.execute(text("DELETE FROM dataset ..."), parameters=...)
105115
await expdb_connection.commit()
106-
107116
```
108117

109118
???- "Why not always use the `*_connection`?"
@@ -142,17 +151,32 @@ def test_get_dataset_success(py_api: httpx.AsyncClient) -> None:
142151
For all other tests, do not use `py_api` but call the implementing function directly. For example, do not call `client.get("/datasets/1")` but instead `get_dataset`:
143152

144153
```python
145-
async def test_get_dataset_private_success(expdb_session: AsyncSession, userdb_session: AsyncSession) -> None:
154+
async def test_get_dataset_private_success(
155+
expdb_session: AsyncSession, userdb_session: AsyncSession
156+
) -> None:
146157
private_dataset = 42
147158
owner_of_that_dataset = OWNER_USER
148-
dataset = await get_dataset(dataset_id=42, user=owner_of_that_dataset, userdb_session=userdb_session, expdb_session=expdb_session)
159+
dataset = await get_dataset(
160+
dataset_id=42,
161+
user=owner_of_that_dataset,
162+
userdb_session=userdb_session,
163+
expdb_session=expdb_session,
164+
)
149165
assert dataset.id == private_dataset
150166

151-
async def test_get_dataset_private_access_denied(expdb_session: AsyncSession, userdb_session: AsyncSession) -> None:
167+
168+
async def test_get_dataset_private_access_denied(
169+
expdb_session: AsyncSession, userdb_session: AsyncSession
170+
) -> None:
152171
private_dataset = 42
153172
owner_of_that_dataset = SOME_USER # Test User defined in a common file
154173
with pytest.raises(DatasetNoAccessError) as e:
155-
await get_dataset(dataset_id=42, user=owner_of_that_dataset, userdb_session=userdb_session, expdb_session=expdb_session)
174+
await get_dataset(
175+
dataset_id=42,
176+
user=owner_of_that_dataset,
177+
userdb_session=userdb_session,
178+
expdb_session=expdb_session,
179+
)
156180
assert e.value.status_code == HTTPStatus.FORBIDDEN
157181
```
158182

@@ -177,6 +201,7 @@ async def test_get_dataset(py_api: httpx.AsyncClient, php_api: httpx.AsyncClient
177201
else:
178202
_assert_error_response_equal(py_response, php_response)
179203

204+
180205
def _assert_success_response_equal(py_json, php_json) -> None:
181206
# PHP API returns numbers as strings
182207
py_json = nested_num_to_str(py_json)
@@ -186,6 +211,7 @@ def _assert_success_response_equal(py_json, php_json) -> None:
186211
# and then finally we compare the results to ensure the remaining data is identical
187212
assert py_json == php_json
188213

214+
189215
def _assert_error_response_equal(py_response, php_response) -> None:
190216
# There might be some translation of error codes
191217
if py_response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY:
@@ -197,7 +223,6 @@ def _assert_error_response_equal(py_response, php_response) -> None:
197223

198224
# Python follows RFC9457 while PHP has a custom system:
199225
assert py_response.json()["code"] == php_response.json()["error"]["code"]
200-
201226
```
202227

203228
### Usage of the Database

0 commit comments

Comments
 (0)