diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 73f9253..d54225e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,17 +9,13 @@ jobs: - uses: actions/setup-python@v3 with: python-version: 3.7 # Google Colaboratory uses Python 3.7 - - uses: actions/cache@v3 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements*.txt', 'setup.py') }} - restore-keys: | - ${{ runner.os }}-pip- + cache: pip + cache-dependency-path: setup.py - run: pip install .[test] - # The unit tests are run with ipython, as the code requires an iPython instance, like in a notebook. - env: TEST_DATABASE_URL: postgresql://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres - run: ipython -m pytest -- --cov ocdskingfishercolab + # The unit tests are run with ipython, as the code requires an iPython instance, like in a notebook. + run: ipython -m pytest -- -W error --cov ocdskingfishercolab - env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: coveralls --service=github diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 96fcf83..9b40f7b 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -11,12 +11,8 @@ jobs: - uses: actions/setup-python@v3 with: python-version: 3.8 - - uses: actions/cache@v3 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements*.txt', 'setup.py') }} - restore-keys: | - ${{ runner.os }}-pip- + cache: pip + cache-dependency-path: setup.py - run: curl -s -S --retry 3 $BASEDIR/tests/install.sh | bash - - run: curl -s -S --retry 3 $BASEDIR/tests/script.sh | bash - - run: pip install .[test] diff --git a/ocdskingfishercolab/__init__.py b/ocdskingfishercolab/__init__.py index 47436af..5a4b566 100644 --- a/ocdskingfishercolab/__init__.py +++ b/ocdskingfishercolab/__init__.py @@ -114,8 +114,8 @@ def list_source_ids(pattern=''): pattern = f'%{pattern}%' - # This inspects locals to find pattern - return get_ipython().magic(f'sql {sql}') + # This inspects locals to find `pattern`. + return get_ipython().run_line_magic('sql', sql) def list_collections(source_id=None): @@ -133,8 +133,8 @@ def list_collections(source_id=None): sql += " WHERE source_id = :source_id" sql += " ORDER BY id DESC" - # This inspects locals to find source_id - return get_ipython().magic(f'sql {sql}') + # This inspects locals to find `source_id`. + return get_ipython().run_line_magic('sql', sql) def set_search_path(schema_name): @@ -145,7 +145,7 @@ def set_search_path(schema_name): :param str schema_name: a schema name """ try: - get_ipython().magic(f'sql SET search_path = {schema_name}, public') + get_ipython().run_line_magic('sql', f'SET search_path = {schema_name}, public') # https://github.com/catherinedevlin/ipython-sql/issues/191 except ResourceClosedError: pass @@ -240,18 +240,18 @@ def get_ipython_sql_resultset_from_query(sql): :rtype: sql.run.ResultSet """ ipython = get_ipython() - autopandas = ipython.magic('config SqlMagic.autopandas') + autopandas = ipython.run_line_magic('config', 'SqlMagic.autopandas') # Disable autopandas, so we know that the sql magic call will always return # a ResultSet (rather than a pandas DataFrame). Since the DataFrame would # be created from the ResultSet, it would be less efficient. if autopandas: - ipython.magic('config SqlMagic.autopandas=False') + ipython.run_line_magic('config', 'SqlMagic.autopandas = False') # Use ipython.run_line_magic instead of ipython.magic here # to get variables from the scope of the ipython cell, # instead of the scope in this function. results = ipython.run_line_magic('sql', sql) if autopandas: - ipython.magic('config SqlMagic.autopandas=True') + ipython.run_line_magic('config', 'SqlMagic.autopandas = True') return results @@ -300,16 +300,16 @@ def download_package_from_ocid(collection_id, ocid, package_type): """ ipython = get_ipython() - autopandas = ipython.magic('config SqlMagic.autopandas') + autopandas = ipython.run_line_magic('config', 'SqlMagic.autopandas') # Disable autopandas, so we know that the sql magic call will always return # a ResultSet (rather than a pandas DataFrame). Since the DataFrame would # be created from the ResultSet, it would be less efficient. if autopandas: - ipython.magic('config SqlMagic.autopandas=False') - # This inspects locals to find ocid and collection_id - results = ipython.magic(f'sql {sql}') + ipython.run_line_magic('config', 'SqlMagic.autopandas = False') + # This inspects locals to find `ocid` and `collection_id`. + results = ipython.run_line_magic('sql', sql) if autopandas: - ipython.magic('config SqlMagic.autopandas=True') + ipython.run_line_magic('config', 'SqlMagic.autopandas = True') data = [row[0] for row in results] diff --git a/tests/conftest.py b/tests/conftest.py index 750f185..9fe518c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -61,10 +61,10 @@ def db(): conn.commit() - get_ipython().magic('load_ext sql') - get_ipython().magic(f'sql {created_database_url}') - # Set autopandas, because we think most users will - get_ipython().magic('config SqlMagic.autopandas=True') + get_ipython().run_line_magic('load_ext', 'sql') + get_ipython().run_line_magic('sql', created_database_url) + # Set autopandas, because we think most users will want it. + get_ipython().run_line_magic('config', 'SqlMagic.autopandas = True') yield cur finally: diff --git a/tests/test_module.py b/tests/test_module.py index f490491..d221a82 100644 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -45,7 +45,7 @@ def chdir(path): def test_set_search_path(db): set_search_path('test') - get_ipython().magic('sql show search_path')['search_path'][0] == 'test, public' + get_ipython().run_line_magic('sql', 'show search_path')['search_path'][0] == 'test, public' @patch('ocdskingfishercolab.files.download') @@ -229,15 +229,14 @@ def test_get_ipython_sql_resultset_from_query(db): @patch('ocdskingfishercolab._notebook_id', _notebook_id) def test_get_ipython_sql_resultset_from_query_error(db, capsys): - get_ipython().magic('sql invalid') + get_ipython().run_line_magic('sql', 'invalid') captured = capsys.readouterr() assert '(psycopg2.errors.SyntaxError) syntax error at or near "invalid"\n' \ 'LINE 1: ...google.com/drive/1lpWoGnOb6KcjHDEhSBjWZgA8aBLCfDp0 */invalid\n' \ ' ^\n\n' \ - '[SQL: /* https://colab.research.google.com/drive/1lpWoGnOb6KcjHDEhSBjWZgA8aBLCfDp0' \ - ' */invalid]\n' \ - '(Background on this error at: http://sqlalche.me/e/13/f405)\n' in captured.out + '[SQL: /* https://colab.research.google.com/drive/1lpWoGnOb6KcjHDEhSBjWZgA8aBLCfDp0 */invalid]\n' \ + '(Background on this error at: https://sqlalche.me/e/14/f405)\n' in captured.out @patch('ocdskingfishercolab._notebook_id', _notebook_id)