Skip to content

Commit

Permalink
Update type syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jl-wynen committed May 27, 2024
1 parent 56b30eb commit f26be94
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/scitacean/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ def query_datasets(
)
params = {"fields": params_model(**fields).model_dump_json()}

limits: dict[str, Union[str, int]] = {}
limits: dict[str, str | int] = {}
if order is not None:
limits["order"] = order
if limit is not None:
Expand Down
41 changes: 24 additions & 17 deletions tests/client/query_client_test.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2024 SciCat Project (https://github.com/SciCatProject/scitacean)

from typing import Union

import pytest
from dateutil.parser import parse as parse_datetime

from scitacean import Client, DatasetType, RemotePath, model
from scitacean.testing.backend import skip_if_not_backend
from scitacean.testing.backend.config import SciCatAccess

UPLOAD_DATASETS: dict[
str, Union[model.UploadDerivedDataset, model.UploadRawDataset]
] = {
UPLOAD_DATASETS: dict[str, model.UploadDerivedDataset | model.UploadRawDataset] = {
"raw1": model.UploadRawDataset(
ownerGroup="PLACEHOLDER",
accessGroups=["uu", "faculty"],
Expand Down Expand Up @@ -107,7 +103,7 @@


@pytest.fixture(scope="module", autouse=True)
def seed_database(request: pytest.FixtureRequest, scicat_access: SciCatAccess) -> None:
def _seed_database(request: pytest.FixtureRequest, scicat_access: SciCatAccess) -> None:
skip_if_not_backend(request)

client = Client.from_credentials(
Expand All @@ -120,19 +116,22 @@ def seed_database(request: pytest.FixtureRequest, scicat_access: SciCatAccess) -
SEED[key] = client.scicat.create_dataset_model(dset)


def test_query_dataset_multiple_by_single_field(real_client, seed_database):
@pytest.mark.usefixtures("_seed_database")
def test_query_dataset_multiple_by_single_field(real_client):
datasets = real_client.scicat.query_datasets({"proposalId": "p0124"})
actual = {ds.pid: ds for ds in datasets}
expected = {SEED[key].pid: SEED[key] for key in ("raw1", "raw2", "raw3")}
assert actual == expected


def test_query_dataset_no_match(real_client, seed_database):
@pytest.mark.usefixtures("_seed_database")
def test_query_dataset_no_match(real_client):
datasets = real_client.scicat.query_datasets({"owner": "librarian"})
assert not datasets


def test_query_dataset_multiple_by_multiple_fields(real_client, seed_database):
@pytest.mark.usefixtures("_seed_database")
def test_query_dataset_multiple_by_multiple_fields(real_client):
datasets = real_client.scicat.query_datasets(
{"proposalId": "p0124", "principalInvestigator": "investigator 1"},
)
Expand All @@ -141,7 +140,8 @@ def test_query_dataset_multiple_by_multiple_fields(real_client, seed_database):
assert actual == expected


def test_query_dataset_multiple_by_derived_field(real_client, seed_database):
@pytest.mark.usefixtures("_seed_database")
def test_query_dataset_multiple_by_derived_field(real_client):
datasets = real_client.scicat.query_datasets(
{"investigator": "investigator 1"},
)
Expand All @@ -150,22 +150,25 @@ def test_query_dataset_multiple_by_derived_field(real_client, seed_database):
assert actual == expected


def test_query_dataset_uses_conjunction_of_fields(real_client, seed_database):
@pytest.mark.usefixtures("_seed_database")
def test_query_dataset_uses_conjunction_of_fields(real_client):
datasets = real_client.scicat.query_datasets(
{"proposalId": "p0124", "investigator": "investigator X"},
)
assert not datasets


def test_query_dataset_can_use_custom_type(real_client, seed_database):
@pytest.mark.usefixtures("_seed_database")
def test_query_dataset_can_use_custom_type(real_client):
datasets = real_client.scicat.query_datasets(
{"sourceFolder": RemotePath("/hex/raw4")},
)
expected = [SEED["raw4"]]
assert datasets == expected


def test_query_dataset_set_order(real_client, seed_database):
@pytest.mark.usefixtures("_seed_database")
def test_query_dataset_set_order(real_client):
datasets = real_client.scicat.query_datasets(
{"proposalId": "p0124"},
order="creationTime:desc",
Expand All @@ -175,7 +178,8 @@ def test_query_dataset_set_order(real_client, seed_database):
assert datasets == expected


def test_query_dataset_limit_ascending_creation_time(real_client, seed_database):
@pytest.mark.usefixtures("_seed_database")
def test_query_dataset_limit_ascending_creation_time(real_client):
datasets = real_client.scicat.query_datasets(
{"proposalId": "p0124"},
limit=2,
Expand All @@ -186,7 +190,8 @@ def test_query_dataset_limit_ascending_creation_time(real_client, seed_database)
assert actual == expected


def test_query_dataset_limit_descending_creation_time(real_client, seed_database):
@pytest.mark.usefixtures("_seed_database")
def test_query_dataset_limit_descending_creation_time(real_client):
datasets = real_client.scicat.query_datasets(
{"proposalId": "p0124"},
limit=2,
Expand All @@ -197,15 +202,17 @@ def test_query_dataset_limit_descending_creation_time(real_client, seed_database
assert actual == expected


def test_query_dataset_limit_needs_order(real_client, seed_database):
@pytest.mark.usefixtures("_seed_database")
def test_query_dataset_limit_needs_order(real_client):
with pytest.raises(ValueError, match="limit"):
real_client.scicat.query_datasets(
{"proposalId": "p0124"},
limit=2,
)


def test_query_dataset_all(real_client, seed_database):
@pytest.mark.usefixtures("_seed_database")
def test_query_dataset_all(real_client):
datasets = real_client.scicat.query_datasets({})
actual = {ds.pid: ds for ds in datasets}
# We cannot test `datasets` directly because there are other datasets
Expand Down

0 comments on commit f26be94

Please sign in to comment.