-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use pytest temporary folders and fixtures to create test file hierarc…
…hy at test time (#516) * make test db a fixture that returns a session, use jp_data_dir fixture * Use job_id returned by the db * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * use fixture for root dir * create test dirs by copying test files from static dir into temp dirs instead of uploading test dirs for execution manager test * create test dirs by copying test files from static dir into temp dirs instead of uploading test dirs for scheduler tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * create test dirs by copying test files from static dir into temp dirs instead of uploading test dirs for job files manager tests * make static_test_files_dir fixture session-scoped * add return type annotations to fixtures where return type is not clear * explicitly close db session to ensure all db connections are released * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Assert tmp_path and similar as Path type --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
953aea6
commit 7cf42f9
Showing
12 changed files
with
209 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,60 @@ | ||
import shutil | ||
from pathlib import Path | ||
from typing import Tuple | ||
|
||
import pytest | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
from conftest import DB_URL | ||
from jupyter_scheduler.executors import DefaultExecutionManager | ||
from jupyter_scheduler.orm import Job | ||
|
||
JOB_ID = "69856f4e-ce94-45fd-8f60-3a587457fce7" | ||
NOTEBOOK_NAME = "side_effects.ipynb" | ||
SIDE_EFECT_FILE_NAME = "output_side_effect.txt" | ||
|
||
NOTEBOOK_DIR = Path(__file__).resolve().parent / "test_staging_dir" / "job-4" | ||
NOTEBOOK_PATH = NOTEBOOK_DIR / NOTEBOOK_NAME | ||
SIDE_EFFECT_FILE = NOTEBOOK_DIR / SIDE_EFECT_FILE_NAME | ||
@pytest.fixture | ||
def staging_dir_with_side_effects( | ||
static_test_files_dir, jp_scheduler_staging_dir | ||
) -> Tuple[Path, Path]: | ||
notebook_file_path = static_test_files_dir / "side_effects.ipynb" | ||
side_effect_file_path = static_test_files_dir / "output_side_effect.txt" | ||
job_staging_dir = jp_scheduler_staging_dir / "job-4" | ||
|
||
job_staging_dir.mkdir() | ||
shutil.copy2(notebook_file_path, job_staging_dir) | ||
shutil.copy2(side_effect_file_path, job_staging_dir) | ||
|
||
return (notebook_file_path, side_effect_file_path) | ||
|
||
|
||
@pytest.fixture | ||
def load_job(jp_scheduler_db): | ||
with jp_scheduler_db() as session: | ||
job = Job( | ||
runtime_environment_name="abc", | ||
input_filename=NOTEBOOK_NAME, | ||
job_id=JOB_ID, | ||
) | ||
session.add(job) | ||
session.commit() | ||
|
||
|
||
def test_add_side_effects_files(jp_scheduler_db, load_job): | ||
def side_effects_job_record(staging_dir_with_side_effects, jp_scheduler_db) -> str: | ||
notebook_name = staging_dir_with_side_effects[0].name | ||
job = Job( | ||
runtime_environment_name="abc", | ||
input_filename=notebook_name, | ||
) | ||
jp_scheduler_db.add(job) | ||
jp_scheduler_db.commit() | ||
|
||
return job.job_id | ||
|
||
|
||
def test_add_side_effects_files( | ||
side_effects_job_record, | ||
staging_dir_with_side_effects, | ||
jp_scheduler_root_dir, | ||
jp_scheduler_db_url, | ||
jp_scheduler_db, | ||
): | ||
job_id = side_effects_job_record | ||
staged_notebook_file_path = staging_dir_with_side_effects[0] | ||
staged_notebook_dir = staged_notebook_file_path.parent | ||
side_effect_file_name = staging_dir_with_side_effects[1].name | ||
|
||
manager = DefaultExecutionManager( | ||
job_id=JOB_ID, | ||
root_dir=str(NOTEBOOK_DIR), | ||
db_url=DB_URL, | ||
staging_paths={"input": str(NOTEBOOK_PATH)}, | ||
job_id=job_id, | ||
root_dir=jp_scheduler_root_dir, | ||
db_url=jp_scheduler_db_url, | ||
staging_paths={"input": staged_notebook_file_path}, | ||
) | ||
manager.add_side_effects_files(str(NOTEBOOK_DIR)) | ||
manager.add_side_effects_files(staged_notebook_dir) | ||
|
||
with jp_scheduler_db() as session: | ||
job = session.query(Job).filter(Job.job_id == JOB_ID).one() | ||
assert SIDE_EFECT_FILE_NAME in job.packaged_files | ||
job = jp_scheduler_db.query(Job).filter(Job.job_id == job_id).one() | ||
assert side_effect_file_name in job.packaged_files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.