Skip to content

Commit 51a5ee4

Browse files
Add tests for Scheduler job and job definition creation with input folder, 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>
1 parent 4d7de94 commit 51a5ee4

File tree

7 files changed

+113
-33
lines changed

7 files changed

+113
-33
lines changed

conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
DB_FILE_PATH = f"{HERE}/jupyter_scheduler/tests/testdb.sqlite"
1414
DB_URL = f"sqlite:///{DB_FILE_PATH}"
1515

16+
TEST_ROOT_DIR = f"{HERE}/jupyter_scheduler/tests/test_root_dir"
17+
1618

1719
@pytest.fixture
1820
def jp_server_config(jp_server_config):
@@ -44,7 +46,7 @@ def jp_scheduler_db():
4446

4547

4648
@pytest.fixture
47-
def jp_scheduler(jp_data_dir):
49+
def jp_scheduler():
4850
return Scheduler(
49-
db_url=DB_URL, root_dir=str(jp_data_dir), environments_manager=MockEnvironmentManager()
51+
db_url=DB_URL, root_dir=str(TEST_ROOT_DIR), environments_manager=MockEnvironmentManager()
5052
)
Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,43 @@
1-
import os
2-
from contextlib import contextmanager
31
from pathlib import Path
4-
from unittest.mock import PropertyMock, patch
52

63
import pytest
74
from sqlalchemy import create_engine
85
from sqlalchemy.orm import sessionmaker
96

7+
from conftest import DB_URL
108
from jupyter_scheduler.executors import DefaultExecutionManager
11-
from jupyter_scheduler.orm import Base, Job
9+
from jupyter_scheduler.orm import Job
1210

13-
NOTEBOOK_DIR = Path(__file__).resolve().parent / "test_staging_dir" / "job-3"
11+
JOB_ID = "69856f4e-ce94-45fd-8f60-3a587457fce7"
1412
NOTEBOOK_NAME = "side_effects.ipynb"
13+
SIDE_EFECT_FILE_NAME = "output_side_effect.txt"
14+
15+
NOTEBOOK_DIR = Path(__file__).resolve().parent / "test_staging_dir" / "job-4"
1516
NOTEBOOK_PATH = NOTEBOOK_DIR / NOTEBOOK_NAME
16-
SIDE_EFFECT_FILE = NOTEBOOK_DIR / "output_side_effect.txt"
17+
SIDE_EFFECT_FILE = NOTEBOOK_DIR / SIDE_EFECT_FILE_NAME
1718

1819

19-
def test_execution_manager_with_side_effects():
20-
db_url = "sqlite://"
21-
engine = create_engine(db_url, echo=False)
22-
Base.metadata.create_all(engine)
23-
db_session = sessionmaker(bind=engine)
24-
with db_session() as session:
20+
@pytest.fixture
21+
def load_job(jp_scheduler_db):
22+
with jp_scheduler_db() as session:
2523
job = Job(
2624
runtime_environment_name="abc",
2725
input_filename=NOTEBOOK_NAME,
28-
job_id="123",
26+
job_id=JOB_ID,
2927
)
3028
session.add(job)
3129
session.commit()
3230

33-
manager = DefaultExecutionManager(
34-
job_id="123",
35-
root_dir=str(NOTEBOOK_DIR),
36-
db_url=db_url,
37-
staging_paths={"input": str(NOTEBOOK_PATH)},
38-
)
3931

40-
with patch.object(
41-
DefaultExecutionManager,
42-
"db_session",
43-
new_callable=PropertyMock,
44-
) as mock_db_session:
45-
mock_db_session.return_value = db_session
46-
manager.add_side_effects_files(str(NOTEBOOK_DIR))
47-
48-
assert (
49-
"output_side_effect.txt" in job.packaged_files
50-
), "Side effect file was not added to packaged_files"
32+
def test_add_side_effects_files(jp_scheduler_db, load_job):
33+
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)},
38+
)
39+
manager.add_side_effects_files(str(NOTEBOOK_DIR))
40+
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
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world!
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "dbbca65f-4b6a-4490-94b6-f7cdd87e7023",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"file_path = 'a/b/helloworld.txt'\n",
11+
"\n",
12+
"with open(file_path, 'r') as file:\n",
13+
" content = file.read()\n",
14+
"\n",
15+
"print(content)"
16+
]
17+
}
18+
],
19+
"metadata": {
20+
"kernelspec": {
21+
"display_name": "Python 3 (ipykernel)",
22+
"language": "python",
23+
"name": "python3"
24+
},
25+
"language_info": {
26+
"codemirror_mode": {
27+
"name": "ipython",
28+
"version": 3
29+
},
30+
"file_extension": ".py",
31+
"mimetype": "text/x-python",
32+
"name": "python",
33+
"nbconvert_exporter": "python",
34+
"pygments_lexer": "ipython3",
35+
"version": "3.11.9"
36+
}
37+
},
38+
"nbformat": 4,
39+
"nbformat_minor": 5
40+
}

jupyter_scheduler/tests/test_scheduler.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
import pytest
77

88
from jupyter_scheduler.models import (
9+
CreateJob,
910
CreateJobDefinition,
1011
ListJobDefinitionsQuery,
1112
SortDirection,
1213
SortField,
1314
UpdateJobDefinition,
1415
)
15-
from jupyter_scheduler.orm import JobDefinition
16+
from jupyter_scheduler.orm import Job, JobDefinition
1617

1718

1819
def test_create_job_definition(jp_scheduler):
@@ -39,6 +40,49 @@ def test_create_job_definition(jp_scheduler):
3940
assert "hello world" == definition.name
4041

4142

43+
def test_create_job_definition_with_input_folder(jp_scheduler):
44+
job_definition_id = jp_scheduler.create_job_definition(
45+
CreateJobDefinition(
46+
input_uri="job-5/import-helloworld.ipynb",
47+
runtime_environment_name="default",
48+
name="import hello world",
49+
output_formats=["ipynb"],
50+
package_input_folder=True,
51+
)
52+
)
53+
54+
with jp_scheduler.db_session() as session:
55+
definitions = session.query(JobDefinition).all()
56+
assert 1 == len(definitions)
57+
definition = definitions[0]
58+
assert job_definition_id
59+
assert job_definition_id == definition.job_definition_id
60+
assert "import hello world" == definition.name
61+
assert "a/b/helloworld.txt" in definition.packaged_files
62+
63+
64+
def test_create_job_with_input_folder(jp_scheduler):
65+
job_id = jp_scheduler.create_job(
66+
CreateJob(
67+
input_uri="job-5/import-helloworld.ipynb",
68+
runtime_environment_name="default",
69+
name="import hello world",
70+
output_formats=["ipynb"],
71+
package_input_folder=True,
72+
)
73+
)
74+
75+
with jp_scheduler.db_session() as session:
76+
jobs = session.query(Job).all()
77+
assert 1 == len(jobs)
78+
job = jobs[0]
79+
assert job_id
80+
assert job_id == job.job_id
81+
assert "import hello world" == job.name
82+
assert "default" == job.runtime_environment_name
83+
assert "a/b/helloworld.txt" in job.packaged_files
84+
85+
4286
job_definition_1 = {
4387
"job_definition_id": "f4f8c8a9-f539-429a-b69e-b567f578646e",
4488
"name": "hello world 1",

0 commit comments

Comments
 (0)