Skip to content

Commit

Permalink
Fix typing issues in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jl-wynen committed Oct 30, 2024
1 parent 823c634 commit 789fe31
Show file tree
Hide file tree
Showing 24 changed files with 567 additions and 359 deletions.
5 changes: 0 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ strict = true
show_error_codes = true
warn_unreachable = true

[[tool.mypy.overrides]]
module = "tests.*"
disallow_untyped_defs = false
disallow_untyped_calls = false

[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = true
Expand Down
28 changes: 18 additions & 10 deletions src/scitacean/testing/backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
import json
from dataclasses import dataclass
from pathlib import Path
from typing import TypedDict


class ScicatCredentials(TypedDict):
"""A dict with testing credentials for SciCat."""

username: str
password: str


@dataclass
class SciCatUser:
class ScicatUser:
"""A SciCat user.
Warning
Expand All @@ -24,7 +32,7 @@ class SciCatUser:
group: str

@property
def credentials(self) -> dict[str, str]:
def credentials(self) -> ScicatCredentials:
"""Return login credentials for this user.
User as
Expand All @@ -51,43 +59,43 @@ def dump(self) -> dict[str, str | bool]:

# see https://github.com/SciCatProject/scicat-backend-next/blob/master/src/config/configuration.ts
USERS = {
"ingestor": SciCatUser(
"ingestor": ScicatUser(
username="ingestor",
password="aman", # noqa: S106
email="[email protected]",
group="ingestor",
),
"user1": SciCatUser(
"user1": ScicatUser(
username="user1",
password="a609316768619f154ef58db4d847b75e", # noqa: S106
email="[email protected]",
group="group1",
),
"user2": SciCatUser(
"user2": ScicatUser(
username="user2",
password="f522d1d715970073a6413474ca0e0f63", # noqa: S106
email="[email protected]",
group="group2",
),
"user3": SciCatUser(
"user3": ScicatUser(
username="user3",
password="70dc489e8ee823ae815e18d664424df2", # noqa: S106
email="[email protected]",
group="group3",
),
"user4": SciCatUser(
"user4": ScicatUser(
username="user4",
password="0014890e7020f515b92b767227ef2dfa", # noqa: S106
email="[email protected]",
group="group4",
),
"user5.1": SciCatUser(
"user5.1": ScicatUser(
username="user5.1",
password="359a5fda99bfe5dbc42ee9b3ede77fb7", # noqa: S106
email="[email protected]",
group="group5",
),
"user5.2": SciCatUser(
"user5.2": ScicatUser(
username="user5.2",
password="f3ebd2e4def95db59ef95ee32ef45242", # noqa: S106
email="[email protected]",
Expand All @@ -109,7 +117,7 @@ class SciCatAccess:
"""Access parameters for a local SciCat backend."""

url: str
user: SciCatUser
user: ScicatUser


def local_access(user: str) -> SciCatAccess:
Expand Down
6 changes: 3 additions & 3 deletions src/scitacean/testing/backend/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def fake_client(scicat_access: SciCatAccess) -> FakeClient:
"""
client = FakeClient.from_credentials(
url=scicat_access.url,
**scicat_access.user.credentials, # type: ignore[arg-type]
**scicat_access.user.credentials,
)
client.datasets.update(
{ds.pid: ds for ds in seed.INITIAL_DATASETS.values()} # type: ignore[misc]
Expand Down Expand Up @@ -87,7 +87,7 @@ def real_client(scicat_access: SciCatAccess, scicat_backend: bool) -> Client | N
return None
return Client.from_credentials(
url=scicat_access.url,
**scicat_access.user.credentials, # type: ignore[arg-type]
**scicat_access.user.credentials,
)


Expand Down Expand Up @@ -200,7 +200,7 @@ def _seed_database(
) -> None:
client = client_class.from_credentials(
url=scicat_access.url,
**scicat_access.user.credentials, # type: ignore[arg-type]
**scicat_access.user.credentials,
)
seed.seed_database(client=client, scicat_access=scicat_access)
seed.save_seed(target_dir)
Expand Down
6 changes: 3 additions & 3 deletions src/scitacean/testing/backend/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
)
from ...pid import PID
from ...thumbnail import Thumbnail
from .config import SITE, SciCatAccess, SciCatUser
from .config import SITE, SciCatAccess, ScicatUser

# Dataset models to upload to the database.
_DATASETS: dict[str, UploadRawDataset | UploadDerivedDataset] = {
Expand Down Expand Up @@ -215,7 +215,7 @@


def _apply_config_dataset(
dset: UploadRawDataset | UploadDerivedDataset, user: SciCatUser
dset: UploadRawDataset | UploadDerivedDataset, user: ScicatUser
) -> UploadRawDataset | UploadDerivedDataset:
dset = deepcopy(dset)
dset.owner = user.username
Expand All @@ -225,7 +225,7 @@ def _apply_config_dataset(


def _apply_config_attachment(
attachment: UploadAttachment, user: SciCatUser
attachment: UploadAttachment, user: ScicatUser
) -> UploadAttachment:
attachment = deepcopy(attachment)
attachment.ownerGroup = user.group
Expand Down
13 changes: 8 additions & 5 deletions src/scitacean/testing/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# Copyright (c) 2024 SciCat Project (https://github.com/SciCatProject/scitacean)
"""Fake file transfer."""

from collections.abc import Iterator
from collections.abc import Iterator, Mapping
from contextlib import contextmanager
from pathlib import Path
from typing import Any
from typing import Any, TypeVar

try:
from pyfakefs.fake_filesystem import FakeFilesystem
Expand All @@ -19,7 +19,10 @@
from ..filesystem import RemotePath
from ..transfer.util import source_folder_for

RemotePathOrStr = TypeVar("RemotePathOrStr", RemotePath, str, RemotePath | str)


# TODO add conditionally_disabled feature and remove custom transfers in tests
class FakeDownloadConnection:
"""'Download' files from a fake file transfer."""

Expand Down Expand Up @@ -110,8 +113,8 @@ def __init__(
self,
*,
fs: FakeFilesystem | None = None,
files: dict[str | RemotePath, bytes] | None = None,
reverted: dict[str | RemotePath, bytes] | None = None,
files: Mapping[RemotePathOrStr, bytes] | None = None,
reverted: Mapping[RemotePathOrStr, bytes] | None = None,
source_folder: str | RemotePath | None = None,
):
"""Initialize a file transfer.
Expand Down Expand Up @@ -157,7 +160,7 @@ def connect_for_upload(self, dataset: Dataset) -> Iterator[FakeUploadConnection]


def _remote_path_dict(
d: dict[str | RemotePath, bytes] | None,
d: Mapping[RemotePathOrStr, bytes] | None,
) -> dict[RemotePath, bytes]:
if d is None:
return {}
Expand Down
45 changes: 30 additions & 15 deletions tests/client/attachment_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
from scitacean.model import (
Attachment,
DatasetType,
DownloadAttachment,
UploadAttachment,
UploadDerivedDataset,
)
from scitacean.testing.backend import config as backend_config
from scitacean.testing.backend.seed import (
INITIAL_ATTACHMENTS,
INITIAL_DATASETS,
Expand All @@ -27,7 +29,7 @@ def scicat_client(client: Client) -> ScicatClient:


@pytest.fixture
def derived_dataset(scicat_access):
def derived_dataset(scicat_access: backend_config.SciCatAccess) -> UploadDerivedDataset:
return UploadDerivedDataset(
contactEmail="[email protected]",
creationTime=parse_date("1995-11-11T11:11:11.000Z"),
Expand All @@ -44,7 +46,7 @@ def derived_dataset(scicat_access):


@pytest.fixture
def attachment(scicat_access):
def attachment(scicat_access: backend_config.SciCatAccess) -> UploadAttachment:
return UploadAttachment(
caption="An attachment",
thumbnail=Thumbnail(mime="image/png", data=b"9278c78a904jh"),
Expand All @@ -53,15 +55,21 @@ def attachment(scicat_access):
)


def compare_attachment_after_upload(uploaded, downloaded):
def compare_attachment_after_upload(
uploaded: UploadAttachment, downloaded: DownloadAttachment
) -> None:
for key, expected in uploaded:
# The database populates a number of fields that are None in uploaded.
# But we don't want to test those here as we don't want to test the database.
if expected is not None:
assert expected == dict(downloaded)[key], f"key = {key}"


def test_create_attachment_for_dataset(scicat_client, attachment, derived_dataset):
def test_create_attachment_for_dataset(
scicat_client: ScicatClient,
attachment: UploadAttachment,
derived_dataset: UploadDerivedDataset,
) -> None:
attachment1 = deepcopy(attachment)
attachment2 = deepcopy(attachment)
attachment2.caption = "Another attachment"
Expand All @@ -82,8 +90,11 @@ def test_create_attachment_for_dataset(scicat_client, attachment, derived_datase


def test_create_attachment_for_dataset_with_existing_id(
real_client, attachment, derived_dataset, require_scicat_backend
):
real_client: Client,
attachment: UploadAttachment,
derived_dataset: UploadDerivedDataset,
require_scicat_backend: None,
) -> None:
scicat_client = real_client.scicat

dataset_id = scicat_client.create_dataset_model(derived_dataset).pid
Expand All @@ -95,17 +106,19 @@ def test_create_attachment_for_dataset_with_existing_id(


def test_cannot_create_attachment_for_dataset_for_nonexistent_dataset(
scicat_client, attachment
):
scicat_client: ScicatClient, attachment: UploadAttachment
) -> None:
with pytest.raises(ScicatCommError):
scicat_client.create_attachment_for_dataset(
attachment, dataset_id=PID(pid="nonexistent-id")
)


def test_create_attachment_for_dataset_for_dataset_populates_ids(
scicat_client, attachment, derived_dataset
):
scicat_client: ScicatClient,
attachment: UploadAttachment,
derived_dataset: UploadDerivedDataset,
) -> None:
assert attachment.id is None
assert attachment.datasetId is None
assert attachment.sampleId is None
Expand All @@ -123,28 +136,30 @@ def test_create_attachment_for_dataset_for_dataset_populates_ids(
assert finalized.proposalId is None


def test_get_attachments_for_dataset(scicat_client):
def test_get_attachments_for_dataset(scicat_client: ScicatClient) -> None:
dset = INITIAL_DATASETS["derived"]
attachments = scicat_client.get_attachments_for_dataset(dset.pid)
assert attachments == INITIAL_ATTACHMENTS["derived"]


def test_get_attachments_for_dataset_no_attachments(scicat_client):
def test_get_attachments_for_dataset_no_attachments(
scicat_client: ScicatClient,
) -> None:
assert INITIAL_ATTACHMENTS.get("raw") is None
dset = INITIAL_DATASETS["raw"]
attachments = scicat_client.get_attachments_for_dataset(dset.pid)
assert attachments == []


@pytest.mark.parametrize("key", ["raw", "derived"])
def test_get_dataset_does_not_initialise_attachments(client, key):
def test_get_dataset_does_not_initialise_attachments(client: Client, key: str) -> None:
dset = INITIAL_DATASETS["derived"]
downloaded = client.get_dataset(dset.pid)
assert downloaded.attachments is None


@pytest.mark.parametrize("key", ["raw", "derived"])
def test_download_attachments_for_dataset(client, key):
def test_download_attachments_for_dataset(client: Client, key: str) -> None:
dset = INITIAL_DATASETS[key]
downloaded = client.get_dataset(dset.pid)
with_attachments = client.download_attachments_for(downloaded)
Expand All @@ -156,7 +171,7 @@ def test_download_attachments_for_dataset(client, key):


@pytest.mark.parametrize("key", ["raw", "derived"])
def test_get_dataset_with_attachments(client, key):
def test_get_dataset_with_attachments(client: Client, key: str) -> None:
dset = INITIAL_DATASETS[key]
downloaded = client.get_dataset(dset.pid, attachments=True)
expected = [
Expand Down
Loading

0 comments on commit 789fe31

Please sign in to comment.