From ff97967d99d9dde9396e3386a1cd415d4b00b776 Mon Sep 17 00:00:00 2001 From: Chris van Run Date: Fri, 15 Nov 2024 15:06:30 +0100 Subject: [PATCH] Fix duplication of tests --- tests/test_create_strategies.py | 8 +- tests/test_upload_strategies.py | 297 -------------------------------- 2 files changed, 4 insertions(+), 301 deletions(-) delete mode 100644 tests/test_upload_strategies.py diff --git a/tests/test_create_strategies.py b/tests/test_create_strategies.py index 326f8bf..b746981 100644 --- a/tests/test_create_strategies.py +++ b/tests/test_create_strategies.py @@ -69,7 +69,7 @@ ), ), ) -def test_clean_file_source(source, maximum_number, context): +def test_prep_file_source(source, maximum_number, context): with context: clean_file_source(source, maximum_number=maximum_number) @@ -149,7 +149,7 @@ def test_civ_strategy_specialization( ), ) @sync_generator_test -def test_file_civ_clean(source, context, interface_kind): +def test_file_civ_prep(source, context, interface_kind): strategy = FileCIVCreateStrategy( source=source, interface=ComponentInterfaceFactory( @@ -185,7 +185,7 @@ def test_file_civ_clean(source, context, interface_kind): ), ) @sync_generator_test -def test_image_civ_clean(source, context): +def test_image_civ_prep(source, context): strategy = ImageCIVCreateStrategy( source=source, interface=ComponentInterfaceFactory(super_kind="Image"), @@ -237,7 +237,7 @@ def test_image_civ_clean(source, context): ), ) @sync_generator_test -def test_value_civ_clean(source, context): +def test_value_civ_prep(source, context): strategy = ValueCIVCreateStrategy( source=source, interface=ComponentInterfaceFactory(super_kind="Value"), diff --git a/tests/test_upload_strategies.py b/tests/test_upload_strategies.py deleted file mode 100644 index 7402f7d..0000000 --- a/tests/test_upload_strategies.py +++ /dev/null @@ -1,297 +0,0 @@ -from contextlib import nullcontext -from pathlib import Path -from unittest.mock import MagicMock - -import pytest - -from gcapi.create_strategies import ( - CIVCreateStrategy, - FileCIVCreateStrategy, - ImageCIVCreateStrategy, - JobInputsCreateStrategy, - TooManyFiles, - ValueCIVCreateStrategy, - clean_file_source, -) -from tests.factories import ( - AlgorithmFactory, - ComponentInterfaceFactory, - HyperlinkedComponentInterfaceValueFactory, - HyperlinkedImageFactory, -) -from tests.utils import sync_generator_test - -TESTDATA = Path(__file__).parent / "testdata" - - -@pytest.mark.parametrize( - "source,maximum_number,context", - ( - ( - TESTDATA / "test.json", - None, - nullcontext(), - ), - ( - [TESTDATA / "test.json"], - None, - nullcontext(), - ), - ( - str(TESTDATA / "test.json"), - None, - nullcontext(), - ), - ( - [str(TESTDATA / "test.json")], - None, - nullcontext(), - ), - ( - [TESTDATA / "test.json", TESTDATA / "test.json"], - 1, - pytest.raises(TooManyFiles), - ), - ( - "I DO NOT EXIST", - None, - pytest.raises(FileNotFoundError), - ), - ( - ["I DO NOT EXIST"], - None, - pytest.raises(FileNotFoundError), - ), - ( - [], - None, - pytest.raises(FileNotFoundError), - ), - ), -) -def test_clean_file_source(source, maximum_number, context): - with context: - clean_file_source(source, maximum_number=maximum_number) - - -@pytest.mark.parametrize( - "init_cls,interface,expected_cls,context", - ( - ( - CIVCreateStrategy, - ComponentInterfaceFactory(super_kind="Image"), - ImageCIVCreateStrategy, - nullcontext(), - ), - ( - CIVCreateStrategy, - ComponentInterfaceFactory(super_kind="File"), - FileCIVCreateStrategy, - nullcontext(), - ), - ( - CIVCreateStrategy, - ComponentInterfaceFactory(super_kind="Value"), - ValueCIVCreateStrategy, - nullcontext(), - ), - ( - CIVCreateStrategy, - ComponentInterfaceFactory(super_kind="I do not exist"), - None, - pytest.raises(NotImplementedError), - ), - ( - ImageCIVCreateStrategy, - ComponentInterfaceFactory(super_kind="File"), - None, - pytest.raises(RuntimeError), - ), - ), -) -def test_proto_civ_typing(init_cls, interface, expected_cls, context): - with context: - proto_civ = init_cls( - source=[], - interface=interface, - client_api=MagicMock(), - ) - - if expected_cls: - assert type(proto_civ) is expected_cls - - -@pytest.mark.parametrize( - "source,context,interface_kind", - ( - ( - TESTDATA / "test.json", - nullcontext(), - "Anything", - ), - ( - [TESTDATA / "test.json", TESTDATA / "test.json"], - pytest.raises(TooManyFiles), - "Anything", - ), - ( # Direct JSON value on file super_kind - {"foo": "bar"}, - nullcontext(), - "Anything", - ), - ( # Dangerously easy to overlook, so we don't allow it - "A string which is not a file", - pytest.raises(FileNotFoundError), - "String", - ), - ), -) -@sync_generator_test -def test_file_civ_clean(source, context, interface_kind): - proto_civ = FileCIVCreateStrategy( - source=source, - interface=ComponentInterfaceFactory( - super_kind="File", kind=interface_kind - ), - client_api=MagicMock(), - ) - with context: - yield from proto_civ.prepare() - - -@pytest.mark.parametrize( - "source,context", - ( - (TESTDATA / "image10x10x101.mha", nullcontext()), - ( - [ - TESTDATA / "image10x10x10.mhd", - TESTDATA / "image10x10x10.zraw", - ], - nullcontext(), - ), - ( # Re-use an image - HyperlinkedImageFactory(), - nullcontext(), - ), - ( # Re-use a CIV - HyperlinkedComponentInterfaceValueFactory( - image=HyperlinkedImageFactory().api_url - ), - nullcontext(), - ), - ), -) -@sync_generator_test -def test_image_civ_clean(source, context): - proto_civ = ImageCIVCreateStrategy( - source=source, - interface=ComponentInterfaceFactory(super_kind="Image"), - client_api=MagicMock(), - ) - with context: - yield from proto_civ.prepare() - - -@pytest.mark.parametrize( - "source,context", - ( - (TESTDATA / "test.json", nullcontext()), - ( - [ - TESTDATA / "test.json", - TESTDATA / "test.json", - ], - pytest.raises(TooManyFiles), - ), - ( - TESTDATA / "invalid_test.json", - pytest.raises(ValueError), - ), - ( - {"foo": "bar"}, - nullcontext(), - ), - ( - ["foo", "bar"], - nullcontext(), - ), - ( - 1, - nullcontext(), - ), - ( - None, - nullcontext(), - ), - ( - [], - nullcontext(), - ), - ( - object(), # Can't dump this with JSON - pytest.raises(TypeError), - ), - ), -) -@sync_generator_test -def test_value_civ_clean(source, context): - proto_civ = ValueCIVCreateStrategy( - source=source, - interface=ComponentInterfaceFactory(super_kind="Value"), - client_api=MagicMock(), - ) - with context: - yield from proto_civ.prepare() - - -@pytest.mark.parametrize( - "algorithm,inputs,context", - ( - ( # algo ci < input ci - AlgorithmFactory(inputs=[]), - { - "foo": TESTDATA / "image10x10x101.mha", - }, - pytest.raises(ValueError), - ), - ( # algo ci > input ci - AlgorithmFactory( - inputs=[ - ComponentInterfaceFactory(), - ] - ), - {}, - pytest.raises(ValueError), - ), - ( # algo ci > input ci, but default - AlgorithmFactory( - inputs=[ - ComponentInterfaceFactory(default_value="bar"), - ] - ), - {}, - nullcontext(), - ), - ( # algo ci = input ci - AlgorithmFactory( - inputs=[ - ComponentInterfaceFactory(slug="a-slug"), - ] - ), - { - "a-slug": TESTDATA / "image10x10x101.mha", - }, - nullcontext(), - ), - ), -) -@sync_generator_test -def test_job_creation_clean(algorithm, inputs, context): - job = JobInputsCreateStrategy( - algorithm=algorithm, - inputs=inputs, - client_api=MagicMock(), - ) - with context: - yield from job.prepare()