Skip to content

Commit

Permalink
Try creating a separate session and working dir for each test module
Browse files Browse the repository at this point in the history
  • Loading branch information
lbianchi-lbl committed Feb 12, 2022
1 parent 0f184b3 commit 867ce73
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ jobs:
if: contains(matrix.os, 'linux')
run: |
echo "QT_QPA_PLATFORM=minimal" >> $GITHUB_ENV
- name: Copy files to FOQUS working dir
run: |
_ml_ai_user_dir="$PYTEST_FOQUS_WORKING_DIR"/user_ml_ai_models/
mkdir -p "$_ml_ai_user_dir"
cp examples/other_files/ML_AI_Plugin/{mea_column_model.py,TensorFlow_2-7_Models/mea_column_model.h5} "$_ml_ai_user_dir"
- name: Run pytest (default options)
if: "! matrix.use_coverage"
run: |
Expand Down
46 changes: 31 additions & 15 deletions foqus_lib/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import os
from pathlib import Path
import shutil
Expand Down Expand Up @@ -48,31 +49,46 @@ def psuade_path():
return Path(_psuade_path).resolve()


@pytest.fixture(scope="session", params=["UQ/Rosenbrock.foqus"])
@pytest.fixture(scope="module", params=["UQ/Rosenbrock.foqus"])
def flowsheet_session_file(examples_dir, request):
return str(examples_dir / "test_files" / request.param)


@pytest.fixture(
scope="session",
scope="module",
autouse=True,
)
def foqus_working_dir(qtbot_params):
# FIXME use CLI params
return Path("/tmp") / "foqus_working_dir"
def foqus_working_dir(request) -> Path:
# FIXME get base dir from env var/CLI config
base_dir = Path("/tmp") / "foqus_working_dir"
test_module_name = request.node.name
d = base_dir / test_module_name
d.mkdir(parents=True, exist_ok=True)
return d


@contextlib.contextmanager
def setting_working_dir(dest: Path) -> Path:
from foqus_lib.service.flowsheet import _set_working_dir

assert dest.is_dir()
initial_working_dir = Path(os.getcwd())
try:
_set_working_dir(dest)
yield dest
finally:
_set_working_dir(initial_working_dir)


@pytest.fixture(scope="module")
def foqus_session(foqus_working_dir, psuade_path):
from foqus_lib.service.flowsheet import _set_working_dir
from foqus_lib.framework.session import session

_set_working_dir(foqus_working_dir)
# foqus_working_dir.mkdir(exist_ok=True, parents=True)
# reproducing what happens in foqus_lib.focus.main()
# os.chdir(foqus_working_dir)
session.makeWorkingDirStruct()
session.makeWorkingDirFiles()
with setting_working_dir(foqus_working_dir) as wdir:
session.makeWorkingDirStruct()
session.makeWorkingDirFiles()

dat = session.session(useCurrentWorkingDir=True)
dat.foqusSettings.psuade_path = str(psuade_path)
return dat
dat = session.session(useCurrentWorkingDir=True)
dat.foqusSettings.psuade_path = str(psuade_path)
yield dat
# TODO is there any cleanup to be done, considering that the directory will be changed?
24 changes: 23 additions & 1 deletion foqus_lib/gui/tests/test_ml_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,36 @@ def flowsheet_session_file(examples_dir: Path, request) -> Path:
return examples_dir / request.param


@pytest.fixture(scope="module")
@pytest.fixture(scope="module", autouse=True)
def models_dir(
foqus_working_dir: Path,
) -> Path:

return foqus_working_dir / "user_ml_ai_models"


@pytest.fixture(
scope="module",
autouse=True,
)
def install_ml_ai_model_files(examples_dir: Path, models_dir: Path) -> Path:
"""
This is a module-level fixture with autouse b/c it needs to be created before the main window is instantiated.
"""

base_path = examples_dir / "other_files" / "ML_AI_Plugin"
ts_models_base_path = base_path / "TensorFlow_2-7_Models"

models_dir.mkdir(exist_ok=True, parents=False)

for path in [
base_path / "mea_column_model.py",
ts_models_base_path / "mea_column_model.h5",
]:
shutil.copy2(path, models_dir)
yield models_dir


@pytest.fixture(scope="module")
def model_files(
models_dir: Path,
Expand Down

0 comments on commit 867ce73

Please sign in to comment.