|
| 1 | +import shutil |
1 | 2 | from pathlib import Path
|
| 3 | +from typing import Tuple |
2 | 4 |
|
3 | 5 | import pytest
|
4 |
| -from sqlalchemy import create_engine |
5 |
| -from sqlalchemy.orm import sessionmaker |
6 | 6 |
|
7 |
| -from conftest import DB_URL |
8 | 7 | from jupyter_scheduler.executors import DefaultExecutionManager
|
9 | 8 | from jupyter_scheduler.orm import Job
|
10 | 9 |
|
11 |
| -JOB_ID = "69856f4e-ce94-45fd-8f60-3a587457fce7" |
12 |
| -NOTEBOOK_NAME = "side_effects.ipynb" |
13 |
| -SIDE_EFECT_FILE_NAME = "output_side_effect.txt" |
14 | 10 |
|
15 |
| -NOTEBOOK_DIR = Path(__file__).resolve().parent / "test_staging_dir" / "job-4" |
16 |
| -NOTEBOOK_PATH = NOTEBOOK_DIR / NOTEBOOK_NAME |
17 |
| -SIDE_EFFECT_FILE = NOTEBOOK_DIR / SIDE_EFECT_FILE_NAME |
| 11 | +@pytest.fixture |
| 12 | +def staging_dir_with_side_effects( |
| 13 | + static_test_files_dir, jp_scheduler_staging_dir |
| 14 | +) -> Tuple[Path, Path]: |
| 15 | + notebook_file_path = static_test_files_dir / "side_effects.ipynb" |
| 16 | + side_effect_file_path = static_test_files_dir / "output_side_effect.txt" |
| 17 | + job_staging_dir = jp_scheduler_staging_dir / "job-4" |
| 18 | + |
| 19 | + job_staging_dir.mkdir() |
| 20 | + shutil.copy2(notebook_file_path, job_staging_dir) |
| 21 | + shutil.copy2(side_effect_file_path, job_staging_dir) |
| 22 | + |
| 23 | + return (notebook_file_path, side_effect_file_path) |
18 | 24 |
|
19 | 25 |
|
20 | 26 | @pytest.fixture
|
21 |
| -def load_job(jp_scheduler_db): |
22 |
| - with jp_scheduler_db() as session: |
23 |
| - job = Job( |
24 |
| - runtime_environment_name="abc", |
25 |
| - input_filename=NOTEBOOK_NAME, |
26 |
| - job_id=JOB_ID, |
27 |
| - ) |
28 |
| - session.add(job) |
29 |
| - session.commit() |
30 |
| - |
31 |
| - |
32 |
| -def test_add_side_effects_files(jp_scheduler_db, load_job): |
| 27 | +def side_effects_job_record(staging_dir_with_side_effects, jp_scheduler_db) -> str: |
| 28 | + notebook_name = staging_dir_with_side_effects[0].name |
| 29 | + job = Job( |
| 30 | + runtime_environment_name="abc", |
| 31 | + input_filename=notebook_name, |
| 32 | + ) |
| 33 | + jp_scheduler_db.add(job) |
| 34 | + jp_scheduler_db.commit() |
| 35 | + |
| 36 | + return job.job_id |
| 37 | + |
| 38 | + |
| 39 | +def test_add_side_effects_files( |
| 40 | + side_effects_job_record, |
| 41 | + staging_dir_with_side_effects, |
| 42 | + jp_scheduler_root_dir, |
| 43 | + jp_scheduler_db_url, |
| 44 | + jp_scheduler_db, |
| 45 | +): |
| 46 | + job_id = side_effects_job_record |
| 47 | + staged_notebook_file_path = staging_dir_with_side_effects[0] |
| 48 | + staged_notebook_dir = staged_notebook_file_path.parent |
| 49 | + side_effect_file_name = staging_dir_with_side_effects[1].name |
| 50 | + |
33 | 51 | manager = DefaultExecutionManager(
|
34 |
| - job_id=JOB_ID, |
35 |
| - root_dir=str(NOTEBOOK_DIR), |
36 |
| - db_url=DB_URL, |
37 |
| - staging_paths={"input": str(NOTEBOOK_PATH)}, |
| 52 | + job_id=job_id, |
| 53 | + root_dir=jp_scheduler_root_dir, |
| 54 | + db_url=jp_scheduler_db_url, |
| 55 | + staging_paths={"input": staged_notebook_file_path}, |
38 | 56 | )
|
39 |
| - manager.add_side_effects_files(str(NOTEBOOK_DIR)) |
| 57 | + manager.add_side_effects_files(staged_notebook_dir) |
40 | 58 |
|
41 |
| - with jp_scheduler_db() as session: |
42 |
| - job = session.query(Job).filter(Job.job_id == JOB_ID).one() |
43 |
| - assert SIDE_EFECT_FILE_NAME in job.packaged_files |
| 59 | + job = jp_scheduler_db.query(Job).filter(Job.job_id == job_id).one() |
| 60 | + assert side_effect_file_name in job.packaged_files |
0 commit comments