Skip to content

Commit 5e4e413

Browse files
author
James Robinson
authored
Switch JinjaSQL tests against CockroachDB for more realistic test sce… (#78)
Switch JinjaSQL tests against CockroachDB for more realistic test scenario. New test for inserting into a table via JinjaSQL interpolation.
1 parent 98f453e commit 5e4e413

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

tests/test_sql_magic.py

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,31 +194,71 @@ def test_insert_returning_returns_dataframe(self, conn_name: str, sql_magic):
194194
assert all(isinstance(idval, int) for idval in r['id'].tolist())
195195

196196

197-
@pytest.mark.usefixtures("populated_sqlite_database")
197+
@pytest.mark.usefixtures("populated_cockroach_database")
198198
class TestJinjaTemplatesWithinSqlMagic:
199199
"""Tests over jinjasql integration. See https://github.com/sripathikrishnan/jinjasql"""
200200

201+
# 1) We test against cockroachdb and not sqlite because the underlying psycopg2 driver
202+
# will be more finicky about types coming out of jinja-ized queries.
203+
204+
# 2) We don't use f-strings to interpolate COCKROACH_HANDLE because the subsequent
205+
# jinja syntax is incompatible with f-strings (both use '{}' incompatibly)
206+
201207
@pytest.mark.parametrize('a_value,expected_b_value', [(1, 2), (4, 5)])
202208
def test_basic_query(self, sql_magic, ipython_shell, a_value, expected_b_value):
203209
"""Test simple template expansion"""
204210

205211
# Each a value corresponds with a different b value, see
206-
# populated_sqlite_database().
212+
# populate_database() in tests/conftest.py.
207213
ipython_shell.user_ns['a_value'] = a_value
208214

209215
## jinjasql expansion!
210216
results = sql_magic.execute(
211-
'@sqlite\n#scalar select b from int_table where a = {{a_value}}'
217+
COCKROACH_HANDLE + '\n#scalar select b from int_table where a = {{a_value}}'
212218
)
213219
# Single row + column == scalar, as from populated_sqlite_database
214220
assert isinstance(results, int)
215221
assert results == expected_b_value
216222

223+
def test_insert_into(self, sql_magic, ipython_shell, capsys):
224+
"""Create a one-off table, then test inserting into from python variables interpolated by jinja"""
225+
226+
sql_magic.execute(
227+
COCKROACH_HANDLE + '\n create table scratch(id int not null primary key, name text)'
228+
)
229+
230+
name_value = 'Joe'
231+
232+
ipython_shell.user_ns['id_value'] = 12
233+
ipython_shell.user_ns['name_value'] = name_value
234+
235+
# Insert into table via Jinja expansion.
236+
sql_magic.execute(
237+
COCKROACH_HANDLE
238+
+ '\ninsert into scratch(id, name) values ({{id_value}}, {{name_value}})'
239+
)
240+
241+
captured = capsys.readouterr()
242+
assert '1 row affected' in captured.out
243+
244+
# Now expect the row to be present and we can grab the inserted name
245+
# back by interpolated id query.
246+
from_sql = sql_magic.execute(
247+
COCKROACH_HANDLE + '\n #scalar select name from scratch where id = {{id_value}}'
248+
)
249+
250+
assert isinstance(from_sql, str)
251+
assert from_sql == name_value
252+
253+
# Explicit cleanup -- fixture-level cleanup seems to block indefinitely
254+
sql_magic.execute(COCKROACH_HANDLE + '\n drop table scratch')
255+
217256
def test_in_query_template(self, sql_magic, ipython_shell):
218257
"""Test an in clause expanded from a list. Requires '| inclause' formatter"""
219258
ipython_shell.user_ns['a_values'] = [1, 4] # both known a values.
220259
results = sql_magic.execute(
221-
'@sqlite select b from int_table where a in {{a_values | inclause}} order by b'
260+
COCKROACH_HANDLE
261+
+ ' select b from int_table where a in {{a_values | inclause}} order by b'
222262
)
223263

224264
assert len(results) == 2
@@ -229,7 +269,8 @@ def test_against_string(self, sql_magic, ipython_shell):
229269
ipython_shell.user_ns['str_id_val'] = 'a'
230270

231271
results = sql_magic.execute(
232-
'@sqlite\n#scalar select int_col from str_table where str_id = {{str_id_val}}'
272+
COCKROACH_HANDLE
273+
+ '\n#scalar select int_col from str_table where str_id = {{str_id_val}}'
233274
)
234275

235276
# Scalar result.
@@ -241,7 +282,7 @@ def test_sqlsafe(self, sql_magic, ipython_shell, ret_col, expected_value):
241282
ipython_shell.user_ns['ret_col'] = ret_col
242283

243284
results = sql_magic.execute(
244-
'@sqlite\n#scalar select {{ret_col | sqlsafe}} from int_table where a=1'
285+
COCKROACH_HANDLE + '\n#scalar select {{ret_col | sqlsafe}} from int_table where a=1'
245286
)
246287

247288
# Scalar result.
@@ -253,7 +294,8 @@ def test_conditional_filter(self, sql_magic, ipython_shell, do_filter, expected_
253294
ipython_shell.user_ns['do_filter'] = do_filter
254295

255296
results = sql_magic.execute(
256-
'@sqlite\n#scalar select b from int_table where true {%if do_filter%} and a=1 {% endif %} order by a'
297+
COCKROACH_HANDLE
298+
+ '\n#scalar select b from int_table where true {%if do_filter%} and a=1 {% endif %} order by a'
257299
)
258300

259301
if isinstance(expected_values, int):

0 commit comments

Comments
 (0)