22# SPDX-License-Identifier: Apache-2.0
33import logging
44import time
5+ from contextlib import contextmanager
56
67import pytest
8+ from _pytest ._py .path import LocalPath
9+ from _pytest .monkeypatch import MonkeyPatch
710from IPython .core .error import UsageError
811from IPython .testing .globalipapp import start_ipython
912from 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+
3041def 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,16 +84,17 @@ 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" )
8495 ip .get_ipython ().run_cell (f"%env VDK_SQLITE_FILE={ job_dir } " )
8596 ip .get_ipython ().run_cell ("%env VDK_DB_DEFAULT_TYPE=SQLITE" )
97+ ip .get_ipython ().run_cell ("%env INGESTER_WAIT_TO_FINISH_AFTER_EVERY_SEND=true" )
8698
8799 # get the job_input
88100 ip .get_ipython ().run_cell ("job_input = VDK.get_initialized_job_input()" )
@@ -111,11 +123,11 @@ async def test_extension_with_ingestion_job(ip, tmpdir):
111123 )
112124
113125 # get the data that is going to be ingested
114- ip .get_ipython ().run_cell ("import requests " )
126+ ip .get_ipython ().run_cell ("import json " )
115127 ip .get_ipython ().run_cell (
116- 'response = requests.get("https://jsonplaceholder.typicode.com/todos/1")'
128+ """raw = '{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }' """
117129 )
118- ip .get_ipython ().run_cell ("payload = response. json( )" )
130+ ip .get_ipython ().run_cell ("payload = json.loads(raw )" )
119131
120132 # send data for ingestion
121133 ip .get_ipython ().run_cell (
@@ -177,6 +189,55 @@ def test_extension_with_pure_sql_job(ip, tmpdir):
177189 )
178190
179191
192+ def test_extension_with_job_input_get_job_dir (
193+ session_ip , tmpdir : LocalPath , monkeypatch : MonkeyPatch
194+ ):
195+ monkeypatch .chdir (str (tmpdir ))
196+ with get_vdk_ipython (session_ip ) as ip :
197+ ip .get_ipython ().run_cell ("job_input = VDK.get_initialized_job_input()" )
198+ assert ip .get_ipython ().run_cell (
199+ "str(job_input.get_job_directory())"
200+ ).result == str (tmpdir )
201+
202+
203+ def test_extension_with_job_input_get_name (
204+ session_ip , tmpdir : LocalPath , monkeypatch : MonkeyPatch
205+ ):
206+ monkeypatch .chdir (str (tmpdir ))
207+ with get_vdk_ipython (session_ip ) as ip :
208+ ip .get_ipython ().run_cell ("job_input = VDK.get_initialized_job_input()" )
209+
210+ assert (
211+ ip .get_ipython ().run_cell ("job_input.get_name()" ).result == tmpdir .basename
212+ )
213+ assert ip .user_global_ns ["job_input" ].get_name () == tmpdir .basename
214+
215+
216+ def test_extension_with_job_input_execution_properties (
217+ ip , tmpdir : LocalPath , monkeypatch : MonkeyPatch
218+ ):
219+ ip .get_ipython ().run_cell ("job_input = VDK.get_initialized_job_input()" )
220+
221+ execution_properties = ip .user_global_ns ["job_input" ].get_execution_properties ()
222+ assert "pa__execution_id" in execution_properties
223+ assert "pa__op_id" in execution_properties
224+
225+
226+ def test_extension_with_job_input_get_arguments (session_ip ):
227+ with get_vdk_ipython (session_ip , '--arguments {"a":2}' ) as ip :
228+ ip .get_ipython ().run_cell ("job_input = VDK.get_initialized_job_input()" )
229+
230+ assert ip .get_ipython ().run_cell ("job_input.get_arguments()" ).result == {"a" : 2 }
231+
232+
233+ def test_extension_with_job_input_properties (ip ):
234+ ip .get_ipython ().run_cell ("job_input = VDK.get_initialized_job_input()" )
235+ assert ip .get_ipython ().run_cell ("job_input.set_all_properties({'test':'my-test'})" )
236+ assert (
237+ ip .get_ipython ().run_cell ("job_input.get_property('test')" ).result == "my-test"
238+ )
239+
240+
180241def test_finalise (ip ):
181242 ip .get_ipython ().run_cell ("job_input = VDK.get_initialized_job_input()" )
182243
@@ -236,7 +297,7 @@ def test_call_finalize_before_get_initialized_job_input(ip):
236297 # verifying that calling finalize before get_initialized_job_input won't produce errors(the method will not fail)
237298
238299
239- def test_calling_get_initialise_job_input_multiple_times_after_finalize (ip , tmpdir ):
300+ def test_calling_get_initialise_job_input_multiple_times_after_finalize (ip ):
240301 # first call
241302 ip .get_ipython ().run_cell ("job_input = VDK.get_initialized_job_input()" )
242303 result_job_input = ip .get_ipython ().run_cell ("job_input" ).result
0 commit comments