Skip to content

Commit

Permalink
The get_info API returns run_statuses and end_statuses.
Browse files Browse the repository at this point in the history
  • Loading branch information
wei committed Aug 23, 2024
1 parent 89e8491 commit 8a547dd
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 16 deletions.
77 changes: 74 additions & 3 deletions tests/test_web/test_webapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
upload,
)
from tidy3d.web.core.environment import Env
from tidy3d.web.core.system_config import SystemConfig
from tidy3d.web.core.types import TaskType

TASK_NAME = "task_name_test"
Expand Down Expand Up @@ -174,6 +175,42 @@ def mock_get_info(monkeypatch, set_api_key):
)


@pytest.fixture
def mock_get_system_config(monkeypatch, set_api_key):
"""Mocks webapi.get_system_config."""

responses.add(
responses.GET,
f"{Env.current.web_api_endpoint}/tidy3d/system/py/config",
json={
"data": {
"runStatuses": [
"draft",
"queued",
"preprocess",
"queued_solver",
"running",
"postprocess",
"visualize",
"success",
],
"endStatuses": [
"success",
"error",
"errored",
"diverged",
"diverge",
"deleted",
"draft",
"abort",
"aborted",
],
}
},
status=200,
)


@pytest.fixture
def mock_start(monkeypatch, set_api_key, mock_get_info):
"""Mocks webapi.start."""
Expand Down Expand Up @@ -225,10 +262,38 @@ def mock_get_run_info(task_id):
run_count[0] += 1
return perc_done, 1

def mock_get_system_info():
return SystemConfig(
**{
"runStatuses": [
"draft",
"queued",
"preprocess",
"queued_solver",
"running",
"postprocess",
"visualize",
"success",
],
"endStatuses": [
"success",
"error",
"errored",
"diverged",
"diverge",
"deleted",
"draft",
"abort",
"aborted",
],
}
)

monkeypatch.setattr("tidy3d.web.api.connect_util.REFRESH_TIME", 0.00001)
monkeypatch.setattr(f"{api_path}.RUN_REFRESH_TIME", 0.00001)
monkeypatch.setattr(f"{api_path}.get_status", mock_get_status)
monkeypatch.setattr(f"{api_path}.get_run_info", mock_get_run_info)
monkeypatch.setattr(f"{api_path}.get_system_config", mock_get_system_info)


@pytest.fixture
Expand Down Expand Up @@ -290,7 +355,13 @@ def mock_get_run_info(monkeypatch, set_api_key):

@pytest.fixture
def mock_webapi(
mock_upload, mock_metadata, mock_get_info, mock_start, mock_monitor, mock_download, mock_load
mock_upload,
mock_metadata,
mock_get_info,
mock_start,
mock_monitor,
mock_download,
mock_load,
):
"""Mocks all webapi operation."""

Expand Down Expand Up @@ -545,7 +616,7 @@ def mock_job_status(monkeypatch):


@responses.activate
def test_batch(mock_webapi, mock_job_status, mock_load, tmp_path):
def test_batch(mock_webapi, mock_job_status, mock_load, mock_get_system_config, tmp_path):
# monkeypatch.setattr("tidy3d.web.api.container.Batch.monitor", lambda self: time.sleep(0.1))
# monkeypatch.setattr("tidy3d.web.api.container.Job.status", property(lambda self: "success"))

Expand All @@ -569,7 +640,7 @@ def test_batch(mock_webapi, mock_job_status, mock_load, tmp_path):


@responses.activate
def test_async(mock_webapi, mock_job_status):
def test_async(mock_webapi, mock_get_system_config, mock_job_status):
# monkeypatch.setattr("tidy3d.web.api.container.Job.status", property(lambda self: "success"))
sims = {TASK_NAME: make_sim()}
_ = run_async(sims, folder_name=PROJECT_NAME)
Expand Down
71 changes: 68 additions & 3 deletions tests/test_web/test_webapi_eme.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
upload,
)
from tidy3d.web.core.environment import Env
from tidy3d.web.core.system_config import SystemConfig
from tidy3d.web.core.types import TaskType

from ..test_components.test_eme import make_eme_sim
Expand Down Expand Up @@ -163,10 +164,38 @@ def mock_get_run_info(task_id):
run_count[0] += 1
return perc_done, 1

def mock_get_system_info():
return SystemConfig(
**{
"runStatuses": [
"draft",
"queued",
"preprocess",
"queued_solver",
"running",
"postprocess",
"visualize",
"success",
],
"endStatuses": [
"success",
"error",
"errored",
"diverged",
"diverge",
"deleted",
"draft",
"abort",
"aborted",
],
}
)

monkeypatch.setattr("tidy3d.web.api.connect_util.REFRESH_TIME", 0.00001)
monkeypatch.setattr(f"{api_path}.RUN_REFRESH_TIME", 0.00001)
monkeypatch.setattr(f"{api_path}.get_status", mock_get_status)
monkeypatch.setattr(f"{api_path}.get_run_info", mock_get_run_info)
monkeypatch.setattr(f"{api_path}.get_system_config", mock_get_system_info)


@pytest.fixture
Expand Down Expand Up @@ -289,7 +318,7 @@ def mock_download(*args, **kwargs):


@responses.activate
def test_run(mock_webapi, monkeypatch, tmp_path):
def test_run(mock_webapi, mock_get_system_config, monkeypatch, tmp_path):
sim = make_eme_sim()
monkeypatch.setattr(f"{api_path}.load", lambda *args, **kwargs: True)
assert run(
Expand Down Expand Up @@ -319,6 +348,42 @@ def test_abort_task(set_api_key, mock_get_info):
abort(TASK_ID)


@pytest.fixture
def mock_get_system_config(monkeypatch, set_api_key):
"""Mocks webapi.get_system_config."""

responses.add(
responses.GET,
f"{Env.current.web_api_endpoint}/tidy3d/system/py/config",
json={
"data": {
"runStatuses": [
"draft",
"queued",
"preprocess",
"queued_solver",
"running",
"postprocess",
"visualize",
"success",
],
"endStatuses": [
"success",
"error",
"errored",
"diverged",
"diverge",
"deleted",
"draft",
"abort",
"aborted",
],
}
},
status=200,
)


""" Containers """


Expand All @@ -343,7 +408,7 @@ def mock_job_status(monkeypatch):


@responses.activate
def test_batch(mock_webapi, mock_job_status, tmp_path):
def test_batch(mock_webapi, mock_job_status, mock_get_system_config, tmp_path):
# monkeypatch.setattr("tidy3d.web.api.container.Batch.monitor", lambda self: time.sleep(0.1))
# monkeypatch.setattr("tidy3d.web.api.container.Job.status", property(lambda self: "success"))

Expand All @@ -358,7 +423,7 @@ def test_batch(mock_webapi, mock_job_status, tmp_path):


@responses.activate
def test_async(mock_webapi, mock_job_status):
def test_async(mock_webapi, mock_job_status, mock_get_system_config):
# monkeypatch.setattr("tidy3d.web.api.container.Job.status", property(lambda self: "success"))
sims = {TASK_NAME: make_eme_sim()}
_ = run_async(sims, folder_name=PROJECT_NAME)
69 changes: 67 additions & 2 deletions tests/test_web/test_webapi_heat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
upload,
)
from tidy3d.web.core.environment import Env
from tidy3d.web.core.system_config import SystemConfig
from tidy3d.web.core.types import TaskType

from ..test_components.test_heat import make_heat_sim
Expand Down Expand Up @@ -163,10 +164,38 @@ def mock_get_run_info(task_id):
run_count[0] += 1
return perc_done, 1

def mock_get_system_info():
return SystemConfig(
**{
"runStatuses": [
"draft",
"queued",
"preprocess",
"queued_solver",
"running",
"postprocess",
"visualize",
"success",
],
"endStatuses": [
"success",
"error",
"errored",
"diverged",
"diverge",
"deleted",
"draft",
"abort",
"aborted",
],
}
)

monkeypatch.setattr("tidy3d.web.api.connect_util.REFRESH_TIME", 0.00001)
monkeypatch.setattr(f"{api_path}.RUN_REFRESH_TIME", 0.00001)
monkeypatch.setattr(f"{api_path}.get_status", mock_get_status)
monkeypatch.setattr(f"{api_path}.get_run_info", mock_get_run_info)
monkeypatch.setattr(f"{api_path}.get_system_config", mock_get_system_info)


@pytest.fixture
Expand Down Expand Up @@ -319,6 +348,42 @@ def test_abort_task(set_api_key, mock_get_info):
abort(TASK_ID)


@pytest.fixture
def mock_get_system_config(monkeypatch, set_api_key):
"""Mocks webapi.get_system_config."""

responses.add(
responses.GET,
f"{Env.current.web_api_endpoint}/tidy3d/system/py/config",
json={
"data": {
"runStatuses": [
"draft",
"queued",
"preprocess",
"queued_solver",
"running",
"postprocess",
"visualize",
"success",
],
"endStatuses": [
"success",
"error",
"errored",
"diverged",
"diverge",
"deleted",
"draft",
"abort",
"aborted",
],
}
},
status=200,
)


""" Containers """


Expand All @@ -343,7 +408,7 @@ def mock_job_status(monkeypatch):


@responses.activate
def test_batch(mock_webapi, mock_job_status, tmp_path):
def test_batch(mock_webapi, mock_job_status, mock_get_system_config, tmp_path):
# monkeypatch.setattr("tidy3d.web.api.container.Batch.monitor", lambda self: time.sleep(0.1))
# monkeypatch.setattr("tidy3d.web.api.container.Job.status", property(lambda self: "success"))

Expand All @@ -358,7 +423,7 @@ def test_batch(mock_webapi, mock_job_status, tmp_path):


@responses.activate
def test_async(mock_webapi, mock_job_status):
def test_async(mock_webapi, mock_job_status, mock_get_system_config):
# monkeypatch.setattr("tidy3d.web.api.container.Job.status", property(lambda self: "success"))
sims = {TASK_NAME: make_heat_sim()}
_ = run_async(sims, folder_name=PROJECT_NAME)
46 changes: 46 additions & 0 deletions tests/test_web/test_webapi_system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Tests webapi and things that depend on it

import pytest
import responses
import tidy3d as td
from tidy3d.web.api.webapi import (
get_system_config,
)
from tidy3d.web.core.environment import Env

task_core_path = "tidy3d.web.core.task_core"
api_path = "tidy3d.web.api.webapi"

Env.dev.active()


@pytest.fixture
def set_api_key(monkeypatch):
"""Set the api key."""
import tidy3d.web.core.http_util as http_module

monkeypatch.setattr(http_module, "api_key", lambda: "apikey")
monkeypatch.setattr(http_module, "get_version", lambda: td.version.__version__)


@pytest.fixture
def mock_get_system_config(monkeypatch, set_api_key):
"""Mocks webapi.get_info."""

responses.add(
responses.GET,
f"{Env.current.web_api_endpoint}/tidy3d/system/py/config",
json={
"data": {
"runStatuses": [],
"endStatuses": [],
}
},
status=200,
)


@responses.activate
def test_system_config(mock_get_system_config):
info = get_system_config()
assert info is not None
Loading

0 comments on commit 8a547dd

Please sign in to comment.