Package: py-data-grid-text-reader
Data Grid Text Readerpy-data-grid-text-readeris a library that reads a string representing a
database record set (that resembles the output of a call to the Apache Spark
DataFrame.show() command) and converts it to a different representation, such
as a list of dictionaries or a set of SQL CREATE TABLE and INSERT statements
that can be used to persist the data to a database.This library is based on thepy-dataframe-show-readerlibrary
(seehttps://github.com/internetsystemsgroup/py-dataframe-show-reader/) and
reuses much code from that project. It is intended that this library can read
and parse the same data record set strings that thepy-dataframe-show-readerlibrary can.The primary intended use of the functions in this library is to be used to
enable writing more concise and easy-to-read tests of data transformations than
would otherwise be possible.Imagine we are working on a database query, function or stored procedure that
performs a data transformation that is complex enough that we would like to
quickly verify that it performs as intended, and also have tests to help ensure
that a developer modifying the query in the future does not inadvertently break
the query for an overlooked edge case.SELECTCAST(STRFTIME('%W',sale_date)ASINT)+1ASweek_of_year,AVG(units_sold)ASavg_units_sold,SUM(gross_sales)ASgross_salesFROMsales_by_dayGROUPBY1ORDERBY1This is not a complex query, but perhaps we would like to verify that:Dates are in fact grouped into different weeks.Units sold are truly averaged and not summed.Gross sales are truly summed and not averaged.A unit testing purist might argue that each of these assertions should be
covered by a separate test method, but there are at least two reasons why one
might choose not to do that.Practical experience tells us that detectable overhead is incurred for
each separate database transformation test, so we may want to limit the number
of separate tests in order to keep our full test suite running in a
reasonable duration.When working with data sets in SQL, particularly when
using aggregate, grouping and window functions,
interactions between different rows can be easy to overlook. Tweaking a
query to fix an aggregate function like a summation might inadvertently break
the intended behavior of a windowing function in the query.
A change to the query might allow a summation-only unit test to pass while
leaving broken window function behavior undetected because we have neglected to
update the window-function-only unit test.If we accept that we'd like to use a single test to verify the three
requirements of our query, we need three rows in our input data set.Using unadorned pytest, our test might look like this:deftest_without_data_grid_text_reader():withcreate_engine('sqlite://').connect()asconnection:connection.execute("""CREATE TABLE sales_by_day (sale_date DATE,units_sold INT,gross_sales DOUBLE);""")connection.execute("""INSERT INTO sales_by_day(sale_date, units_sold, gross_sales) VALUES('2019-01-01', 10, 100),('2019-01-02', 20, 200),('2019-01-08', 80, 800);""")actual_rows=[dict(row)forrowinconnection.execute("""SELECT  CAST(STRFTIME('%W', sale_date) AS INT) + 1 AS week_of_year,AVG(units_sold) AS avg_units_sold,SUM(gross_sales) AS gross_salesFROM    sales_by_dayGROUP BY 1ORDER BY 1""")]assert2==len(actual_rows)# Number of rowsassert3==len(actual_rows[0])# Number of columnsassert1==actual_rows[0]['week_of_year']assert15==actual_rows[0]['avg_units_sold']assert300==actual_rows[0]['gross_sales']assert2==actual_rows[1]['week_of_year']assert80==actual_rows[1]['avg_units_sold']assert800==actual_rows[1]['gross_sales']Using the Data Grid Text Reader, our test could instead look like this:deftest_using_data_grid_text_reader():withcreate_engine('sqlite://').connect()asconnection:reader=DataGridTextReader()reader.save_as_table('sales_by_day',"""+----------+----------+-----------+|sale_date |units_sold|gross_sales|[date      |int       |double     |+----------+----------+-----------+|2019-01-01|10        |100        ||2019-01-02|20        |200        ||2019-01-08|80        |800        |+----------+----------+-----------+""",connection)actual_rows=[dict(row)forrowinconnection.execute("""SELECT  CAST(STRFTIME('%W', sale_date) AS INT) + 1 AS week_of_year,AVG(units_sold) AS avg_units_sold,SUM(gross_sales) AS gross_salesFROM    sales_by_dayGROUP BY 1ORDER BY 1""")]expected_rows=reader.data_grid_text_to_list("""+------------+--------------+-----------+|week_of_year|avg_units_sold|gross_sales|[int         |int           |double     |+------------+--------------+-----------+|1           |15            |300        ||2           |80            |800        |+------------+--------------+-----------+""")assert_equal(expected_rows,actual_rows)In the second test example, thesave_as_tablefunction accepts some data grid
text and persists it to a table calledsales_by_day. Then thedata_grid_text_to_listfunction accepts some different data grid text and
creates a list of dict that can be compared to theactual_rowsvalue
returned by executing the data transformation query.In the first version, the setup portion of the test contains twelve lines,
and it may take a few moments to digest the contents of the input rows.
In the second version, the setup portion contains eleven lines and displays the
input data in a more concise tabular form that may be easier for other
programmers (and our future selves) to digest when we need to maintain this
code down the road.If the method under test was more complicated and required more rows and/or
columns in order to adequately test, the length of the first test format would
grow much more quickly than that of the test using the DataFrame Show Reader.Thedata_grid_text_to_listfunction gives us a convenient way to create anexpected_rowslist to pass to theassert_equalfunction (to check
list equality) that is included in the package.Running the TestsClone the git repo.cdinto the root level of the repo.At a terminal command line, runpython3 -m venv venvRunsource venv/bin/activateRunpip install -r requirements_dev.txtRunpytestWhen finished with your session, deactivate your virtual environment withdeactivate.How to Build the Python Package for Distribution on PyPIpython setup.py sdist bdist_wheelHow to Publish the Package to PyPIPublish to the PyPI test site:twine upload --repository-url https://test.pypi.org/legacy/ dist/*Confirm that the test is successfully uploaded:View the package athttps://test.pypi.org/project/py-data-grid-text-reader/Try installing the project like so:pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple py-data-grid-text-readerAfter confirming that the package has successfully been published to test.pypi,
publish to the official PyPI:twine upload dist/*Confirm that the distribution package is successfully uploaded:View the package athttps://pypi.org/project/py-data-grid-text-reader/Install the project like so:pip install py-data-grid-text-readerInstallationTo install the package for use in your own package, run:pip install py-data-grid-text-readerWho Maintains Data Grid Text Reader?Data Grid Text Reader is the work of the community. The core committers and
maintainers are responsible for reviewing and merging PRs, as well as steering
conversation around new feature requests. If you would like to become a
maintainer, please contact us.