Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
kindly committed Feb 4, 2022
1 parent af1b62f commit 70b2830
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
21 changes: 12 additions & 9 deletions ocdskingfishercolab/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import textwrap
import warnings
from urllib.parse import urljoin

Expand Down Expand Up @@ -365,7 +366,7 @@ def render_json(json_string):
""")


def calculate_coverage(fields, scope=None, sql=True):
def calculate_coverage(fields, scope=None, sql=True, sql_only=False):
"""
Calculates the coverage of one or more fields using the summary tables produced by Kingfisher Summarize's
`--field-lists` option. Returns the coverage of each field and the co-occurrence coverage of all the fields.
Expand Down Expand Up @@ -517,18 +518,20 @@ def release_summary_join(scope_table, join_to_release):
coverage_wrapper(" AND \n ".join(conditions), "total")
)

select = ",\n ".join(query_parts)
select = f"""
SELECT
count(*) AS total_{scope},
{select.lstrip()}
select = ",\n ".join(query_parts)
select = textwrap.dedent(f"""
SELECT
count(*) AS total_{scope},
{select}
FROM
{scope_table}
{release_summary_join(scope_table, join_to_release)}
"""
{scope_table}
{release_summary_join(scope_table, join_to_release)}
""")

if sql:
print(select)
if sql_only:
return select
return get_ipython().run_cell_magic("sql", "", select)


Expand Down
20 changes: 19 additions & 1 deletion tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from ocdskingfishercolab import (UnknownPackageTypeError, download_dataframe_as_csv, download_package_from_ocid,
download_package_from_query, get_ipython_sql_resultset_from_query, list_collections,
list_source_ids, save_dataframe_to_spreadsheet, set_search_path)
list_source_ids, save_dataframe_to_spreadsheet, set_search_path, calculate_coverage)


def path(filename):
Expand Down Expand Up @@ -303,3 +303,21 @@ def test_save_dataframe_to_spreadsheet(save, capsys, tmpdir):
assert capsys.readouterr().out == "Uploaded file with ID 'test'\n"

save.assert_called_once_with({'title': 'yet_another_excel_file.xlsx'}, 'flattened.xlsx')


@patch('ocdskingfishercolab._notebook_id', _notebook_id)
def test_calculate_coverage(db, tmpdir):

sql = calculate_coverage(["ocid"], scope="release", sql_only=True)

assert sql.strip() == textwrap.dedent('''
SELECT
count(*) AS total_release,
ROUND(SUM(CASE WHEN release_summary.field_list ? 'ocid' THEN 1 ELSE 0 END) * 100.0 / count(*), 2) AS ocid_percentage,
ROUND(SUM(CASE WHEN release_summary.field_list ? 'ocid' THEN 1 ELSE 0 END) * 100.0 / count(*), 2) AS total_percentage
FROM
release_summary''').strip()




0 comments on commit 70b2830

Please sign in to comment.