Skip to content

Commit 7314215

Browse files
committed
vdk-ipython: infer correctly job name and add more tests
If job name is not explcitly provided we should infer it correctly from the path name. Also I extended ipython test coverage to cover most of the other untested job input methods
1 parent e1bb698 commit 7314215

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

projects/vdk-plugins/vdk-ipython/src/vdk/plugin/ipython.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ def __init__(
4141
):
4242
path = pathlib.Path(path) if path else pathlib.Path(os.getcwd())
4343
job_args = json.loads(arguments) if arguments else None
44-
self._name = name
44+
self._name = path.name if name is None else name
4545
self._path = path
4646
self._arguments = job_args
4747
self._template = template
4848
self.job = None
4949
self.job_input = None
50+
log.debug(
51+
f"Job Control for job with name {self._name} from path {self._path} "
52+
f"with arguments {self._arguments} and template {self._template}"
53+
)
5054

5155
def get_initialized_job_input(self):
5256
"""

projects/vdk-plugins/vdk-ipython/tests/test_plugin.py

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
# SPDX-License-Identifier: Apache-2.0
33
import logging
44
import time
5+
from contextlib import contextmanager
56

67
import pytest
8+
from _pytest._py.path import LocalPath
9+
from _pytest.monkeypatch import MonkeyPatch
710
from IPython.core.error import UsageError
811
from IPython.testing.globalipapp import start_ipython
912
from vdk.api.job_input import IJobInput
@@ -27,6 +30,14 @@ def ip(session_ip):
2730
session_ip.run_line_magic(magic_name="reset", line="-f")
2831

2932

33+
@contextmanager
34+
def get_vdk_ipython(session_ip, vdk_load_arguments_line=""):
35+
session_ip.run_line_magic(magic_name="load_ext", line="vdk.plugin.ipython")
36+
session_ip.run_line_magic(magic_name="reload_VDK", line=vdk_load_arguments_line)
37+
yield session_ip
38+
session_ip.run_line_magic(magic_name="reset", line="-f")
39+
40+
3041
def test_load_vdk_with_no_arguments(ip):
3142
assert ip.user_global_ns["VDK"] is not None
3243
assert isinstance(ip.user_global_ns["VDK"], JobControl)
@@ -53,13 +64,13 @@ def test_get_initialized_job_input(ip):
5364
assert isinstance(ip.user_global_ns["job_input"], IJobInput)
5465

5566

56-
def test_calling_get_initialise_job_input_multiple_times(ip, tmpdir):
67+
def test_calling_get_initialise_job_input_multiple_times(ip):
5768
assert ip.user_global_ns["VDK"] is not None
5869
assert isinstance(ip.user_global_ns["VDK"], JobControl)
5970

6071
# first call
6172
ip.get_ipython().run_cell("job_input = VDK.get_initialized_job_input()")
62-
result_job_input = ip.get_ipython().getoutput("job_input")
73+
result_job_input = ip.user_global_ns["job_input"]
6374

6475
# test first called object
6576
assert ip.user_global_ns["job_input"] is not None
@@ -73,11 +84,11 @@ def test_calling_get_initialise_job_input_multiple_times(ip, tmpdir):
7384
assert isinstance(ip.user_global_ns["job_input"], IJobInput)
7485

7586
# check whether first job_input is the same as the second one
76-
assert result_job_input == ip.get_ipython().getoutput("job_input")
87+
assert result_job_input == ip.user_global_ns["job_input"]
7788

7889

7990
# uses the pytest tmpdir fixture - https://docs.pytest.org/en/6.2.x/tmpdir.html#the-tmpdir-fixture
80-
async def test_extension_with_ingestion_job(ip, tmpdir):
91+
def test_extension_with_ingestion_job(ip, tmpdir):
8192
# set environmental variables via Jupyter notebook
8293
job_dir = str(tmpdir) + "vdk-sqlite.db"
8394
ip.get_ipython().run_cell("%env VDK_INGEST_METHOD_DEFAULT=sqlite")
@@ -111,11 +122,11 @@ async def test_extension_with_ingestion_job(ip, tmpdir):
111122
)
112123

113124
# get the data that is going to be ingested
114-
ip.get_ipython().run_cell("import requests")
125+
ip.get_ipython().run_cell("import json")
115126
ip.get_ipython().run_cell(
116-
'response = requests.get("https://jsonplaceholder.typicode.com/todos/1")'
127+
"""raw = '{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }' """
117128
)
118-
ip.get_ipython().run_cell("payload = response.json()")
129+
ip.get_ipython().run_cell("payload = json.loads(raw)")
119130

120131
# send data for ingestion
121132
ip.get_ipython().run_cell(
@@ -177,6 +188,55 @@ def test_extension_with_pure_sql_job(ip, tmpdir):
177188
)
178189

179190

191+
def test_extension_with_job_input_get_job_dir(
192+
session_ip, tmpdir: LocalPath, monkeypatch: MonkeyPatch
193+
):
194+
monkeypatch.chdir(str(tmpdir))
195+
with get_vdk_ipython(session_ip) as ip:
196+
ip.get_ipython().run_cell("job_input = VDK.get_initialized_job_input()")
197+
assert ip.get_ipython().run_cell(
198+
"str(job_input.get_job_directory())"
199+
).result == str(tmpdir)
200+
201+
202+
def test_extension_with_job_input_get_name(
203+
session_ip, tmpdir: LocalPath, monkeypatch: MonkeyPatch
204+
):
205+
monkeypatch.chdir(str(tmpdir))
206+
with get_vdk_ipython(session_ip) as ip:
207+
ip.get_ipython().run_cell("job_input = VDK.get_initialized_job_input()")
208+
209+
assert (
210+
ip.get_ipython().run_cell("job_input.get_name()").result == tmpdir.basename
211+
)
212+
assert ip.user_global_ns["job_input"].get_name() == tmpdir.basename
213+
214+
215+
def test_extension_with_job_input_execution_properties(
216+
ip, tmpdir: LocalPath, monkeypatch: MonkeyPatch
217+
):
218+
ip.get_ipython().run_cell("job_input = VDK.get_initialized_job_input()")
219+
220+
execution_properties = ip.user_global_ns["job_input"].get_execution_properties()
221+
assert "pa__execution_id" in execution_properties
222+
assert "pa__op_id" in execution_properties
223+
224+
225+
def test_extension_with_job_input_get_arguments(session_ip):
226+
with get_vdk_ipython(session_ip, '--arguments {"a":2}') as ip:
227+
ip.get_ipython().run_cell("job_input = VDK.get_initialized_job_input()")
228+
229+
assert ip.get_ipython().run_cell("job_input.get_arguments()").result == {"a": 2}
230+
231+
232+
def test_extension_with_job_input_properties(ip):
233+
ip.get_ipython().run_cell("job_input = VDK.get_initialized_job_input()")
234+
assert ip.get_ipython().run_cell("job_input.set_all_properties({'test':'my-test'})")
235+
assert (
236+
ip.get_ipython().run_cell("job_input.get_property('test')").result == "my-test"
237+
)
238+
239+
180240
def test_finalise(ip):
181241
ip.get_ipython().run_cell("job_input = VDK.get_initialized_job_input()")
182242

@@ -236,7 +296,7 @@ def test_call_finalize_before_get_initialized_job_input(ip):
236296
# verifying that calling finalize before get_initialized_job_input won't produce errors(the method will not fail)
237297

238298

239-
def test_calling_get_initialise_job_input_multiple_times_after_finalize(ip, tmpdir):
299+
def test_calling_get_initialise_job_input_multiple_times_after_finalize(ip):
240300
# first call
241301
ip.get_ipython().run_cell("job_input = VDK.get_initialized_job_input()")
242302
result_job_input = ip.get_ipython().run_cell("job_input").result

0 commit comments

Comments
 (0)