-
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.
Add tests for Scheduler job and job definition creation with input fo…
…lder, refactor execution manager test (#513) * use fixtures, rename job used to job-4 * test scheduler job creation and job def creation with input folder * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove duplciate fixture --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4d7de94
commit 51a5ee4
Showing
7 changed files
with
113 additions
and
33 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
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,50 +1,43 @@ | ||
import os | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
from unittest.mock import PropertyMock, patch | ||
|
||
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 Base, Job | ||
from jupyter_scheduler.orm import Job | ||
|
||
NOTEBOOK_DIR = Path(__file__).resolve().parent / "test_staging_dir" / "job-3" | ||
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 / "output_side_effect.txt" | ||
SIDE_EFFECT_FILE = NOTEBOOK_DIR / SIDE_EFECT_FILE_NAME | ||
|
||
|
||
def test_execution_manager_with_side_effects(): | ||
db_url = "sqlite://" | ||
engine = create_engine(db_url, echo=False) | ||
Base.metadata.create_all(engine) | ||
db_session = sessionmaker(bind=engine) | ||
with db_session() as session: | ||
@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="123", | ||
job_id=JOB_ID, | ||
) | ||
session.add(job) | ||
session.commit() | ||
|
||
manager = DefaultExecutionManager( | ||
job_id="123", | ||
root_dir=str(NOTEBOOK_DIR), | ||
db_url=db_url, | ||
staging_paths={"input": str(NOTEBOOK_PATH)}, | ||
) | ||
|
||
with patch.object( | ||
DefaultExecutionManager, | ||
"db_session", | ||
new_callable=PropertyMock, | ||
) as mock_db_session: | ||
mock_db_session.return_value = db_session | ||
manager.add_side_effects_files(str(NOTEBOOK_DIR)) | ||
|
||
assert ( | ||
"output_side_effect.txt" in job.packaged_files | ||
), "Side effect file was not added to packaged_files" | ||
def test_add_side_effects_files(jp_scheduler_db, load_job): | ||
manager = DefaultExecutionManager( | ||
job_id=JOB_ID, | ||
root_dir=str(NOTEBOOK_DIR), | ||
db_url=DB_URL, | ||
staging_paths={"input": str(NOTEBOOK_PATH)}, | ||
) | ||
manager.add_side_effects_files(str(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 |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello world! |
40 changes: 40 additions & 0 deletions
40
jupyter_scheduler/tests/test_root_dir/job-5/import-helloworld.ipynb
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "dbbca65f-4b6a-4490-94b6-f7cdd87e7023", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"file_path = 'a/b/helloworld.txt'\n", | ||
"\n", | ||
"with open(file_path, 'r') as file:\n", | ||
" content = file.read()\n", | ||
"\n", | ||
"print(content)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.9" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
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.