2
2
# SPDX-License-Identifier: Apache-2.0
3
3
import logging
4
4
import time
5
+ from contextlib import contextmanager
5
6
6
7
import pytest
8
+ from _pytest ._py .path import LocalPath
9
+ from _pytest .monkeypatch import MonkeyPatch
7
10
from IPython .core .error import UsageError
8
11
from IPython .testing .globalipapp import start_ipython
9
12
from vdk .api .job_input import IJobInput
@@ -27,6 +30,14 @@ def ip(session_ip):
27
30
session_ip .run_line_magic (magic_name = "reset" , line = "-f" )
28
31
29
32
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
+
30
41
def test_load_vdk_with_no_arguments (ip ):
31
42
assert ip .user_global_ns ["VDK" ] is not None
32
43
assert isinstance (ip .user_global_ns ["VDK" ], JobControl )
@@ -53,13 +64,13 @@ def test_get_initialized_job_input(ip):
53
64
assert isinstance (ip .user_global_ns ["job_input" ], IJobInput )
54
65
55
66
56
- def test_calling_get_initialise_job_input_multiple_times (ip , tmpdir ):
67
+ def test_calling_get_initialise_job_input_multiple_times (ip ):
57
68
assert ip .user_global_ns ["VDK" ] is not None
58
69
assert isinstance (ip .user_global_ns ["VDK" ], JobControl )
59
70
60
71
# first call
61
72
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" ]
63
74
64
75
# test first called object
65
76
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):
73
84
assert isinstance (ip .user_global_ns ["job_input" ], IJobInput )
74
85
75
86
# 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" ]
77
88
78
89
79
90
# 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 ):
81
92
# set environmental variables via Jupyter notebook
82
93
job_dir = str (tmpdir ) + "vdk-sqlite.db"
83
94
ip .get_ipython ().run_cell ("%env VDK_INGEST_METHOD_DEFAULT=sqlite" )
84
95
ip .get_ipython ().run_cell (f"%env VDK_SQLITE_FILE={ job_dir } " )
85
96
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" )
86
98
87
99
# get the job_input
88
100
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):
111
123
)
112
124
113
125
# 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 " )
115
127
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 }' """
117
129
)
118
- ip .get_ipython ().run_cell ("payload = response. json( )" )
130
+ ip .get_ipython ().run_cell ("payload = json.loads(raw )" )
119
131
120
132
# send data for ingestion
121
133
ip .get_ipython ().run_cell (
@@ -177,6 +189,55 @@ def test_extension_with_pure_sql_job(ip, tmpdir):
177
189
)
178
190
179
191
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
+
180
241
def test_finalise (ip ):
181
242
ip .get_ipython ().run_cell ("job_input = VDK.get_initialized_job_input()" )
182
243
@@ -236,7 +297,7 @@ def test_call_finalize_before_get_initialized_job_input(ip):
236
297
# verifying that calling finalize before get_initialized_job_input won't produce errors(the method will not fail)
237
298
238
299
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 ):
240
301
# first call
241
302
ip .get_ipython ().run_cell ("job_input = VDK.get_initialized_job_input()" )
242
303
result_job_input = ip .get_ipython ().run_cell ("job_input" ).result
0 commit comments