@@ -194,31 +194,71 @@ def test_insert_returning_returns_dataframe(self, conn_name: str, sql_magic):
194
194
assert all (isinstance (idval , int ) for idval in r ['id' ].tolist ())
195
195
196
196
197
- @pytest .mark .usefixtures ("populated_sqlite_database " )
197
+ @pytest .mark .usefixtures ("populated_cockroach_database " )
198
198
class TestJinjaTemplatesWithinSqlMagic :
199
199
"""Tests over jinjasql integration. See https://github.com/sripathikrishnan/jinjasql"""
200
200
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
+
201
207
@pytest .mark .parametrize ('a_value,expected_b_value' , [(1 , 2 ), (4 , 5 )])
202
208
def test_basic_query (self , sql_magic , ipython_shell , a_value , expected_b_value ):
203
209
"""Test simple template expansion"""
204
210
205
211
# Each a value corresponds with a different b value, see
206
- # populated_sqlite_database() .
212
+ # populate_database() in tests/conftest.py .
207
213
ipython_shell .user_ns ['a_value' ] = a_value
208
214
209
215
## jinjasql expansion!
210
216
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}}'
212
218
)
213
219
# Single row + column == scalar, as from populated_sqlite_database
214
220
assert isinstance (results , int )
215
221
assert results == expected_b_value
216
222
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
+ + '\n insert 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
+
217
256
def test_in_query_template (self , sql_magic , ipython_shell ):
218
257
"""Test an in clause expanded from a list. Requires '| inclause' formatter"""
219
258
ipython_shell .user_ns ['a_values' ] = [1 , 4 ] # both known a values.
220
259
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'
222
262
)
223
263
224
264
assert len (results ) == 2
@@ -229,7 +269,8 @@ def test_against_string(self, sql_magic, ipython_shell):
229
269
ipython_shell .user_ns ['str_id_val' ] = 'a'
230
270
231
271
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}}'
233
274
)
234
275
235
276
# Scalar result.
@@ -241,7 +282,7 @@ def test_sqlsafe(self, sql_magic, ipython_shell, ret_col, expected_value):
241
282
ipython_shell .user_ns ['ret_col' ] = ret_col
242
283
243
284
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'
245
286
)
246
287
247
288
# Scalar result.
@@ -253,7 +294,8 @@ def test_conditional_filter(self, sql_magic, ipython_shell, do_filter, expected_
253
294
ipython_shell .user_ns ['do_filter' ] = do_filter
254
295
255
296
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'
257
299
)
258
300
259
301
if isinstance (expected_values , int ):
0 commit comments