Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG use semicolon for databases for the line magic #8

Merged
merged 12 commits into from
Nov 16, 2017
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Fixed
- Fixed an issue where databases with spaces in their names could not be used
the line magic splits on spaces. It now splits on semicolons. (#8)

## [0.1.2] - 2017-09-20

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ To get a table preview, use the cell magic like this::

To return a DataFrame for further processing, use the line magic like this::

df = %civisquery my-database select * from dummy.table;
df = %civisquery my-database; select * from dummy.table;
9 changes: 7 additions & 2 deletions civis_jupyter_ext/magics/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ def magic(line, cell=None):

if cell is None:
# Not using maxsplit kwarg b/c it is not compatible w/ Python 2
database, sql = line.split(' ', 1)
items = [s for s in line.split(';', 1) if len(s) > 0]
# allow spaces
if len(items) == 1:
database, sql = items[0].split(' ', 1)
else:
database, sql = items
df = civis.io.read_civis_sql(
sql, database.strip(), use_pandas=True, client=client)
sql.strip(), database.strip(), use_pandas=True, client=client)
if len(df) == 0:
df = None
else:
Expand Down
10 changes: 7 additions & 3 deletions civis_jupyter_ext/magics/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ def test_cell_magic(civis_mock, rows):
cell, line, client=-1, preview_rows=100)


@pytest.mark.parametrize(
'sep,database', [
(' ', 'my-database'),
('; ', 'my database'),
('; ', 'my-database')])
@pytest.mark.parametrize(
'cols',
[(['a', 'b'], [1, 2]), ([], [])])
@mock.patch('civis_jupyter_ext.magics.query.civis')
def test_line_magic(civis_mock, cols):
def test_line_magic(civis_mock, cols, sep, database):
sql = 'select * from dummy.table'
database = 'my-database'
line = ' '.join([database, sql])
line = sep.join([database, sql])
test_df = pd.DataFrame({'c1': cols[0], 'c2': cols[1]})
civis_mock.io.read_civis_sql.return_value = test_df
civis_mock.APIClient.return_value = -1
Expand Down