Skip to content

Commit 67da4f8

Browse files
fix model_dumps
1 parent 93226cd commit 67da4f8

File tree

1 file changed

+26
-59
lines changed

1 file changed

+26
-59
lines changed

services/director-v2/tests/unit/with_dbs/test_api_route_clusters.py

Lines changed: 26 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# pylint:disable=unused-argument
33
# pylint:disable=redefined-outer-name
44

5-
import json
65
import random
76
from collections.abc import Callable, Iterator
87
from typing import Any
@@ -11,6 +10,7 @@
1110
import pytest
1211
import sqlalchemy as sa
1312
from _dask_helpers import DaskGatewayServer
13+
from common_library.serialization import model_dump_with_secrets
1414
from distributed.deploy.spec import SpecCluster
1515
from faker import Faker
1616
from httpx import URL
@@ -32,7 +32,6 @@
3232
)
3333
from pydantic import AnyHttpUrl, SecretStr, TypeAdapter
3434
from pytest_simcore.helpers.typing_env import EnvVarsDict
35-
from settings_library.utils_encoders import create_json_encoder_wo_secrets
3635
from simcore_postgres_database.models.clusters import ClusterType, clusters
3736
from starlette import status
3837

@@ -313,15 +312,15 @@ async def test_create_cluster(
313312
authentication=cluster_simple_authentication(),
314313
name=faker.name(),
315314
type=random.choice(list(ClusterType)),
315+
owner=faker.pyint(min_value=1),
316316
)
317317
response = await async_client.post(
318318
create_cluster_url,
319-
json=json.loads(
320-
cluster_data.model_dump_json(
321-
by_alias=True,
322-
exclude_unset=True,
323-
encoder=create_json_encoder_wo_secrets(ClusterCreate),
324-
)
319+
json=model_dump_with_secrets(
320+
cluster_data,
321+
show_secrets=True,
322+
by_alias=True,
323+
exclude_unset=True,
325324
),
326325
)
327326
assert response.status_code == status.HTTP_201_CREATED, f"received: {response.text}"
@@ -360,10 +359,8 @@ async def test_update_own_cluster(
360359
# try to modify one that does not exist
361360
response = await async_client.patch(
362361
f"/v2/clusters/15615165165165?user_id={user_1['id']}",
363-
json=json.loads(
364-
ClusterPatch().model_dump_json(
365-
**_PATCH_EXPORT, encoder=create_json_encoder_wo_secrets(ClusterPatch)
366-
)
362+
json=model_dump_with_secrets(
363+
ClusterPatch(), show_secrets=True, **_PATCH_EXPORT
367364
),
368365
)
369366
assert response.status_code == status.HTTP_404_NOT_FOUND
@@ -382,10 +379,8 @@ async def test_update_own_cluster(
382379
# now we modify nothing
383380
response = await async_client.patch(
384381
f"/v2/clusters/{the_cluster.id}?user_id={user_1['id']}",
385-
json=json.loads(
386-
ClusterPatch().model_dump_json(
387-
**_PATCH_EXPORT, encoder=create_json_encoder_wo_secrets(ClusterPatch)
388-
)
382+
json=model_dump_with_secrets(
383+
ClusterPatch(), show_secrets=True, **_PATCH_EXPORT
389384
),
390385
)
391386
assert response.status_code == status.HTTP_200_OK, f"received {response.text}"
@@ -402,10 +397,8 @@ async def test_update_own_cluster(
402397
ClusterPatch(endpoint=faker.uri()),
403398
ClusterPatch(authentication=cluster_simple_authentication()),
404399
]:
405-
jsonable_cluster_patch = json.loads(
406-
cluster_patch.json(
407-
**_PATCH_EXPORT, encoder=create_json_encoder_wo_secrets(ClusterPatch)
408-
)
400+
jsonable_cluster_patch = model_dump_with_secrets(
401+
cluster_patch, show_secrets=True, **_PATCH_EXPORT
409402
)
410403
print(f"--> patching cluster with {jsonable_cluster_patch}")
411404
response = await async_client.patch(
@@ -450,11 +443,7 @@ async def test_update_own_cluster(
450443
cluster_patch = ClusterPatch(owner=user_2["primary_gid"])
451444
response = await async_client.patch(
452445
f"/v2/clusters/{the_cluster.id}?user_id={user_1['id']}",
453-
json=json.loads(
454-
cluster_patch.model_dump_json(
455-
**_PATCH_EXPORT, encoder=create_json_encoder_wo_secrets(ClusterPatch)
456-
)
457-
),
446+
json=model_dump_with_secrets(cluster_patch, show_secrets=True, **_PATCH_EXPORT),
458447
)
459448
assert response.status_code == status.HTTP_200_OK, f"received {response.text}"
460449
returned_cluster = ClusterGet.model_validate(response.json())
@@ -472,11 +461,7 @@ async def test_update_own_cluster(
472461
)
473462
response = await async_client.patch(
474463
f"/v2/clusters/{the_cluster.id}?user_id={user_1['id']}",
475-
json=json.loads(
476-
cluster_patch.model_dump_json(
477-
**_PATCH_EXPORT, encoder=create_json_encoder_wo_secrets(ClusterPatch)
478-
)
479-
),
464+
json=model_dump_with_secrets(cluster_patch, show_secrets=True, **_PATCH_EXPORT),
480465
)
481466
assert (
482467
response.status_code == status.HTTP_403_FORBIDDEN
@@ -496,10 +481,8 @@ async def test_update_default_cluster_fails(
496481
# try to modify one that does not exist
497482
response = await async_client.patch(
498483
f"/v2/clusters/default?user_id={user_1['id']}",
499-
json=json.loads(
500-
ClusterPatch().model_dump_json(
501-
**_PATCH_EXPORT, encoder=create_json_encoder_wo_secrets(ClusterPatch)
502-
)
484+
json=model_dump_with_secrets(
485+
ClusterPatch(), show_secrets=True, **_PATCH_EXPORT
503486
),
504487
)
505488
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
@@ -568,11 +551,8 @@ async def test_update_another_cluster(
568551
]:
569552
response = await async_client.patch(
570553
f"/v2/clusters/{the_cluster.id}?user_id={user_2['id']}",
571-
json=json.loads(
572-
cluster_patch.json(
573-
**_PATCH_EXPORT,
574-
encoder=create_json_encoder_wo_secrets(ClusterPatch),
575-
)
554+
json=model_dump_with_secrets(
555+
cluster_patch, show_secrets=True, **_PATCH_EXPORT
576556
),
577557
)
578558
assert (
@@ -591,11 +571,8 @@ async def test_update_another_cluster(
591571
cluster_patch = ClusterPatch(accessRights={user_3["primary_gid"]: rights})
592572
response = await async_client.patch(
593573
f"/v2/clusters/{the_cluster.id}?user_id={user_2['id']}",
594-
json=json.loads(
595-
cluster_patch.model_dump_json(
596-
**_PATCH_EXPORT,
597-
encoder=create_json_encoder_wo_secrets(ClusterPatch),
598-
)
574+
json=model_dump_with_secrets(
575+
cluster_patch, show_secrets=True, **_PATCH_EXPORT
599576
),
600577
)
601578
assert (
@@ -612,11 +589,8 @@ async def test_update_another_cluster(
612589
cluster_patch = ClusterPatch(accessRights={user_3["primary_gid"]: rights})
613590
response = await async_client.patch(
614591
f"/v2/clusters/{the_cluster.id}?user_id={user_2['id']}",
615-
json=json.loads(
616-
cluster_patch.model_dump_json(
617-
**_PATCH_EXPORT,
618-
encoder=create_json_encoder_wo_secrets(ClusterPatch),
619-
)
592+
json=model_dump_with_secrets(
593+
cluster_patch, show_secrets=True, **_PATCH_EXPORT
620594
),
621595
)
622596
assert (
@@ -745,10 +719,8 @@ async def test_ping_invalid_cluster_raises_422(
745719
)
746720
response = await async_client.post(
747721
"/v2/clusters:ping",
748-
json=json.loads(
749-
some_fake_cluster.model_dump_json(
750-
by_alias=True, encoder=create_json_encoder_wo_secrets(ClusterPing)
751-
)
722+
json=model_dump_with_secrets(
723+
some_fake_cluster, show_secrets=True, by_alias=True
752724
),
753725
)
754726
with pytest.raises(httpx.HTTPStatusError):
@@ -773,12 +745,7 @@ async def test_ping_cluster(
773745
)
774746
response = await async_client.post(
775747
"/v2/clusters:ping",
776-
json=json.loads(
777-
valid_cluster.model_dump_json(
778-
by_alias=True,
779-
encoder=create_json_encoder_wo_secrets(SimpleAuthentication),
780-
)
781-
),
748+
json=model_dump_with_secrets(valid_cluster, show_secrets=True, alias=True),
782749
)
783750
response.raise_for_status()
784751
assert response.status_code == status.HTTP_204_NO_CONTENT

0 commit comments

Comments
 (0)