From ba23617127b12fe228144aa86666c2bb723a7a37 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Mon, 27 Nov 2023 14:57:01 +0900 Subject: [PATCH 1/5] Update test_backend.py --- python_tests/artifact/test_backend.py | 54 +++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/python_tests/artifact/test_backend.py b/python_tests/artifact/test_backend.py index 22544e6f..bdd6c00d 100644 --- a/python_tests/artifact/test_backend.py +++ b/python_tests/artifact/test_backend.py @@ -1,9 +1,17 @@ +import tempfile +from unittest import TestCase from unittest.mock import MagicMock +import optuna +from optuna.artifacts import FileSystemArtifactStore +from optuna.artifacts import upload_artifact from optuna.storages import BaseStorage +from optuna_dashboard._app import create_app from optuna_dashboard.artifact import _backend import pytest +from ..wsgi_client import send_request + def test_get_artifact_path() -> None: study = MagicMock(_study_id=0) @@ -80,3 +88,49 @@ def test_list_trial_artifacts(init_storage_with_artifact_meta: MagicMock) -> Non {"artifact_id": "id1", "filename": "bar.txt"}, {"artifact_id": "id2", "filename": "baz.txt"}, ] + + +class TestProxyStudyArtifact(TestCase): + def setUp(self) -> None: + self.storage = optuna.storages.InMemoryStorage() + self.study = optuna.create_study(storage=self.storage) + + def test_artifact_store_none(self) -> None: + app = create_app(self.storage) + status, _, body = send_request( + app, + "/artifacts/0/0", + "GET", + content_type="application/json", + ) + self.assertEqual(status, 400) + self.assertEqual(body, b"Cannot access to the artifacts.") + + def test_artifact_not_found(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + artifact_store = FileSystemArtifactStore(tmpdir) + app = create_app(self.storage, artifact_store) + status, _, body = send_request( + app, + f"/artifacts/{self.study._study_id}/abc123", + "GET", + content_type="application/json", + ) + self.assertEqual(status, 404) + self.assertEqual(body, b"Not Found") + + def test_successful_artifact_retrieval(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + artifact_store = FileSystemArtifactStore(tmpdir) + with tempfile.NamedTemporaryFile() as f: + f.write(b"dummy_content") + f.flush() + artifact_id = upload_artifact(self.study, f.name, artifact_store=artifact_store) + app = create_app(self.storage, artifact_store) + status, _, _ = send_request( + app, + f"/artifacts/{self.study._study_id}/{artifact_id}", + "GET", + content_type="application/json", + ) + self.assertEqual(status, 200) From 4b439a69970c770809ef0e320ac5ea79f3c99685 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Mon, 27 Nov 2023 16:29:00 +0900 Subject: [PATCH 2/5] Update python_tests/artifact/test_backend.py Co-authored-by: c-bata --- python_tests/artifact/test_backend.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python_tests/artifact/test_backend.py b/python_tests/artifact/test_backend.py index bdd6c00d..d7df189a 100644 --- a/python_tests/artifact/test_backend.py +++ b/python_tests/artifact/test_backend.py @@ -104,7 +104,6 @@ class TestProxyStudyArtifact(TestCase): content_type="application/json", ) self.assertEqual(status, 400) - self.assertEqual(body, b"Cannot access to the artifacts.") def test_artifact_not_found(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: From c89aac71d63f00aca798e25eb605b7e834ec5fc6 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Mon, 27 Nov 2023 16:29:08 +0900 Subject: [PATCH 3/5] Update python_tests/artifact/test_backend.py Co-authored-by: c-bata --- python_tests/artifact/test_backend.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python_tests/artifact/test_backend.py b/python_tests/artifact/test_backend.py index d7df189a..07ea34d7 100644 --- a/python_tests/artifact/test_backend.py +++ b/python_tests/artifact/test_backend.py @@ -116,7 +116,6 @@ class TestProxyStudyArtifact(TestCase): content_type="application/json", ) self.assertEqual(status, 404) - self.assertEqual(body, b"Not Found") def test_successful_artifact_retrieval(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: From e3cad622f7dc7a96dc983cfef6f03b480a999e41 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Mon, 27 Nov 2023 16:29:17 +0900 Subject: [PATCH 4/5] Update python_tests/artifact/test_backend.py Co-authored-by: c-bata --- python_tests/artifact/test_backend.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python_tests/artifact/test_backend.py b/python_tests/artifact/test_backend.py index 07ea34d7..143e86b4 100644 --- a/python_tests/artifact/test_backend.py +++ b/python_tests/artifact/test_backend.py @@ -125,10 +125,11 @@ class TestProxyStudyArtifact(TestCase): f.flush() artifact_id = upload_artifact(self.study, f.name, artifact_store=artifact_store) app = create_app(self.storage, artifact_store) - status, _, _ = send_request( + status, _, body = send_request( app, f"/artifacts/{self.study._study_id}/{artifact_id}", "GET", content_type="application/json", ) self.assertEqual(status, 200) + self.assertEqual(body, b"dummy_content") From ff210f50d64ab864bca0cf31f6fad854cd1be1f0 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Mon, 27 Nov 2023 16:30:28 +0900 Subject: [PATCH 5/5] Update test_backend.py --- python_tests/artifact/test_backend.py | 73 ++++++++++++++------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/python_tests/artifact/test_backend.py b/python_tests/artifact/test_backend.py index 143e86b4..00e55d51 100644 --- a/python_tests/artifact/test_backend.py +++ b/python_tests/artifact/test_backend.py @@ -1,5 +1,4 @@ import tempfile -from unittest import TestCase from unittest.mock import MagicMock import optuna @@ -90,46 +89,48 @@ def test_list_trial_artifacts(init_storage_with_artifact_meta: MagicMock) -> Non ] -class TestProxyStudyArtifact(TestCase): - def setUp(self) -> None: - self.storage = optuna.storages.InMemoryStorage() - self.study = optuna.create_study(storage=self.storage) +def test_artifact_store_none() -> None: + storage = optuna.storages.InMemoryStorage() + app = create_app(storage) + status, _, body = send_request( + app, + "/artifacts/0/0", + "GET", + content_type="application/json", + ) + assert status == 400 - def test_artifact_store_none(self) -> None: - app = create_app(self.storage) + +def test_artifact_not_found() -> 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) status, _, body = send_request( app, - "/artifacts/0/0", + f"/artifacts/{study._study_id}/abc123", "GET", content_type="application/json", ) - self.assertEqual(status, 400) + assert status == 404 - def test_artifact_not_found(self) -> None: - with tempfile.TemporaryDirectory() as tmpdir: - artifact_store = FileSystemArtifactStore(tmpdir) - app = create_app(self.storage, artifact_store) - status, _, body = send_request( - app, - f"/artifacts/{self.study._study_id}/abc123", - "GET", - content_type="application/json", - ) - self.assertEqual(status, 404) - def test_successful_artifact_retrieval(self) -> None: - with tempfile.TemporaryDirectory() as tmpdir: - artifact_store = FileSystemArtifactStore(tmpdir) - with tempfile.NamedTemporaryFile() as f: - f.write(b"dummy_content") - f.flush() - artifact_id = upload_artifact(self.study, f.name, artifact_store=artifact_store) - app = create_app(self.storage, artifact_store) - status, _, body = send_request( - app, - f"/artifacts/{self.study._study_id}/{artifact_id}", - "GET", - content_type="application/json", - ) - self.assertEqual(status, 200) - self.assertEqual(body, b"dummy_content") +def test_successful_artifact_retrieval() -> None: + storage = optuna.storages.InMemoryStorage() + study = optuna.create_study(storage=storage) + with tempfile.TemporaryDirectory() as tmpdir: + artifact_store = FileSystemArtifactStore(tmpdir) + with tempfile.NamedTemporaryFile() as f: + f.write(b"dummy_content") + f.flush() + artifact_id = upload_artifact(study, f.name, artifact_store=artifact_store) + app = create_app(storage, artifact_store) + status, _, body = send_request( + app, + f"/artifacts/{study._study_id}/{artifact_id}", + "GET", + content_type="application/json", + ) + assert status == 200 + assert body == b"dummy_content"