From 60888843dba4bac175082b130947b1eecf05c5bd Mon Sep 17 00:00:00 2001 From: Contramundum Date: Wed, 29 Nov 2023 17:22:14 +0900 Subject: [PATCH 1/4] Add test on upload_artifact --- python_tests/artifact/test_backend.py | 62 +++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/python_tests/artifact/test_backend.py b/python_tests/artifact/test_backend.py index 14a470e3..0bb662aa 100644 --- a/python_tests/artifact/test_backend.py +++ b/python_tests/artifact/test_backend.py @@ -1,3 +1,5 @@ +import base64 +import json import tempfile from unittest.mock import MagicMock @@ -177,3 +179,63 @@ def test_successful_trial_artifact_retrieval() -> None: ) assert status == 200 assert body == b"dummy_content" + + +DUMMY_DATA = f"data:text/plain; charset=utf-8,{base64.b64encode(b'dummy_content').decode('utf-8')}" + + +def test_upload_artifact_invalid() -> None: + storage = optuna.storages.InMemoryStorage() + + with tempfile.TemporaryDirectory() as tmpdir: + artifact_store = FileSystemArtifactStore(tmpdir) + + app = create_app(storage, artifact_store) + study = optuna.create_study(storage=storage) + + # Invalid: no trial + status, _, body = send_request( + app, + f"/api/artifacts/{study._study_id}/0", + "POST", + body=json.dumps({"file": DUMMY_DATA}), + content_type="application/json", + ) + assert status == 500 # TODO: This should return 400 + + # Invalid: complete trial + study.add_trial(optuna.create_trial(value=1.0, distributions={}, params={})) + trial = study.trials[-1] + status, _, body = send_request( + app, + f"/api/artifacts/{study._study_id}/{trial._trial_id}", + "POST", + body=json.dumps({"file": DUMMY_DATA}), + content_type="application/json", + ) + assert status == 400 + + +def test_upload_artifact() -> None: + storage = optuna.storages.InMemoryStorage() + + study = optuna.create_study(storage=storage) + with tempfile.TemporaryDirectory() as tmpdir: + artifact_store = FileSystemArtifactStore(tmpdir) + + app = create_app(storage, artifact_store) + + study.add_trial(optuna.create_trial(state=optuna.trial.TrialState.RUNNING)) + trial = study.trials[-1] + status, _, body = send_request( + app, + f"/api/artifacts/{study._study_id}/{trial._trial_id}", + "POST", + body=json.dumps({"file": DUMMY_DATA}), + content_type="application/json", + ) + assert status == 201 + res = json.loads(body) + with open(f"{tmpdir}/{res['artifact_id']}", "r") as f: + data = f.read() + assert data == "dummy_content" From 1f29783877da2feab59f24808ce0017a5701046b Mon Sep 17 00:00:00 2001 From: contramundum53 Date: Thu, 30 Nov 2023 16:46:08 +0900 Subject: [PATCH 2/4] Update python_tests/artifact/test_backend.py Co-authored-by: c-bata --- python_tests/artifact/test_backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_tests/artifact/test_backend.py b/python_tests/artifact/test_backend.py index 0bb662aa..35caad85 100644 --- a/python_tests/artifact/test_backend.py +++ b/python_tests/artifact/test_backend.py @@ -201,7 +201,7 @@ def test_upload_artifact_invalid() -> None: body=json.dumps({"file": DUMMY_DATA}), content_type="application/json", ) - assert status == 500 # TODO: This should return 400 + assert status == 500 # TODO(contramundum53): This should return 400 # Invalid: complete trial study.add_trial(optuna.create_trial(value=1.0, distributions={}, params={})) From 5bbb11cf4654116fef9d3669e046666d8a00ecf0 Mon Sep 17 00:00:00 2001 From: Contramundum Date: Mon, 4 Dec 2023 16:10:55 +0900 Subject: [PATCH 3/4] code fix --- python_tests/artifact/test_backend.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/python_tests/artifact/test_backend.py b/python_tests/artifact/test_backend.py index 0bb662aa..76627131 100644 --- a/python_tests/artifact/test_backend.py +++ b/python_tests/artifact/test_backend.py @@ -181,10 +181,10 @@ def test_successful_trial_artifact_retrieval() -> None: assert body == b"dummy_content" -DUMMY_DATA = f"data:text/plain; charset=utf-8,{base64.b64encode(b'dummy_content').decode('utf-8')}" +DUMMY_DATA_URL = f"data:text/plain; charset=utf-8,{base64.b64encode(b'dummy_content').decode('utf-8')}" -def test_upload_artifact_invalid() -> None: +def test_upload_artifact_invalid_no_trial() -> None: storage = optuna.storages.InMemoryStorage() with tempfile.TemporaryDirectory() as tmpdir: @@ -193,24 +193,31 @@ def test_upload_artifact_invalid() -> None: app = create_app(storage, artifact_store) study = optuna.create_study(storage=storage) - # Invalid: no trial status, _, body = send_request( app, f"/api/artifacts/{study._study_id}/0", "POST", - body=json.dumps({"file": DUMMY_DATA}), + body=json.dumps({"file": DUMMY_DATA_URL}), content_type="application/json", ) assert status == 500 # TODO: This should return 400 - # Invalid: complete trial +def test_upload_artifact_invalid_complete_trial() -> None: + storage = optuna.storages.InMemoryStorage() + + with tempfile.TemporaryDirectory() as tmpdir: + artifact_store = FileSystemArtifactStore(tmpdir) + + app = create_app(storage, artifact_store) + study = optuna.create_study(storage=storage) + study.add_trial(optuna.create_trial(value=1.0, distributions={}, params={})) trial = study.trials[-1] status, _, body = send_request( app, f"/api/artifacts/{study._study_id}/{trial._trial_id}", "POST", - body=json.dumps({"file": DUMMY_DATA}), + body=json.dumps({"file": DUMMY_DATA_URL}), content_type="application/json", ) assert status == 400 @@ -231,7 +238,7 @@ def test_upload_artifact() -> None: app, f"/api/artifacts/{study._study_id}/{trial._trial_id}", "POST", - body=json.dumps({"file": DUMMY_DATA}), + body=json.dumps({"file": DUMMY_DATA_URL}), content_type="application/json", ) assert status == 201 From 9e314a96ddb57a87cd3243fe987361a6a88402ed Mon Sep 17 00:00:00 2001 From: Contramundum Date: Mon, 4 Dec 2023 16:12:29 +0900 Subject: [PATCH 4/4] format --- python_tests/artifact/test_backend.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/python_tests/artifact/test_backend.py b/python_tests/artifact/test_backend.py index f2192f03..52fe6027 100644 --- a/python_tests/artifact/test_backend.py +++ b/python_tests/artifact/test_backend.py @@ -181,7 +181,9 @@ def test_successful_trial_artifact_retrieval() -> None: assert body == b"dummy_content" -DUMMY_DATA_URL = f"data:text/plain; charset=utf-8,{base64.b64encode(b'dummy_content').decode('utf-8')}" +DUMMY_DATA_URL = ( + f"data:text/plain; charset=utf-8,{base64.b64encode(b'dummy_content').decode('utf-8')}" +) def test_upload_artifact_invalid_no_trial() -> None: @@ -202,6 +204,7 @@ def test_upload_artifact_invalid_no_trial() -> None: ) assert status == 500 # TODO(contramundum53): This should return 400 + def test_upload_artifact_invalid_complete_trial() -> None: storage = optuna.storages.InMemoryStorage()