scitacean.testing.client.FakeClient#

class scitacean.testing.client.FakeClient(*, file_transfer=None, disable=None)[source]#

Mimics a client without accessing the internet.

This class is a stand in for scitacean.Client to emulate downloading and uploading of datasets without actually accessing a SciCat server.

Since this client fakes communication with SciCat, it stores raw pydantic models, namely

Individual functions can be disabled (that is, made to raise an exception) using the disabled argument of the initializer.

Important

FakeClient does not behave exactly like a Client connected to a real server as that would require reimplementing a large part of the SciCat backend. You should thus always also test your code against a (potentially locally deployed) real server.

In particular, do not rely on specific error messages or the detailed settings in the datasets returned by FakeClient.upload_new_dataset_now!

Examples

Set up a fake client for download:

client = FakeClient()
pid = PID(prefix="sample.prefix", pid="1234-5678-abcd")
client.datasets[pid] = model.DownloadDataset(pid=pid, ...)
client.orig_datablocks[pid] = [model.OrigDatablock(
    datasetId=str(pid),
    ...
)]
client.get_dataset(pid)

Upload a dataset:

client = FakeClient(file_transfer=FakeFileTransfer())
finalized = client.upload_new_dataset_now(dset)

# contains new dataset and datablock:
client.datasets[finalized.pid]
client.orig_datablocks[finalized.pid]

Disable a method:

client = FakeClient(
    disable={"create_dataset_model": RuntimeError("Upload failed")})
# raises RuntimeError("Upload failed"):
client.upload_new_dataset_now(dset)

See also

scitacean.testing.transfer.FakeFileTransfer

Fake file up-/downloads without a real server.

Constructors

__init__(*[, file_transfer, disable])

Initialize a fake client with empty dataset storage.

Methods

download_attachments_for(target)

Download all attachments for a given object.

download_files(dataset, *, target[, select, ...])

Download files of a dataset.

from_credentials(*, url, username, password)

Create a new fake client.

from_token(*, url, token[, file_transfer])

Create a new fake client.

get_dataset(pid[, strict_validation, ...])

Download a dataset from SciCat.

get_sample(sample_id[, strict_validation])

Download a sample from SciCat.

upload_new_dataset_now(dataset)

Upload a dataset as a new entry to SciCat immediately.

upload_new_sample_now(sample)

Upload a sample as a new entry to SciCat immediately.

without_login(*, url[, file_transfer])

Create a new fake client.

Attributes

file_transfer

Stored handler for file down-/uploads.

scicat

Low-level client for SciCat.

__init__(*, file_transfer=None, disable=None)[source]#

Initialize a fake client with empty dataset storage.

Parameters:
classmethod from_credentials(*, url, username, password, file_transfer=None)[source]#

Create a new fake client.

All arguments except file_Transfer are ignored.

Return type:

FakeClient

classmethod from_token(*, url, token, file_transfer=None)[source]#

Create a new fake client.

All arguments except file_Transfer are ignored.

Return type:

FakeClient

classmethod without_login(*, url, file_transfer=None)[source]#

Create a new fake client.

All arguments except file_Transfer are ignored.

Return type:

FakeClient