Demonstration of how to make a test suite compatible .sql file from a real world .xls file

ticdat makes it very easy to translate data between different file formats. This facilitates creating a unittest based test suite.

Here, we assume that we have a file named simple_data.xls. We want to convert this Excel file to a text file that can be stored in a source code control system like GitHub.

The first step is to import the dataFactory from the diet model. dataFactory describes the input schema, and thus is the right object for converting an Excel data input instance into an .sql file.


In [1]:
from diet import dataFactory

The next step is is to read the simple_data.xls file into a TicDat object.

To be safe, we first check for duplicate records in the .xls file. (The other data integrity checks are done on the TicDat object itself, when it is solved).


In [2]:
dataFactory.xls.get_duplicates("simple_data.xls")


Out[2]:
{}

As there are no duplicate records, we can go ahead and read the data from the .xls file without worry.


In [3]:
dat = dataFactory.xls.create_tic_dat("simple_data.xls")

I always like to assess the table lengths that were read just as a sanity check.


In [4]:
{t:len(getattr(dat, t)) for t in dataFactory.all_tables}


Out[4]:
{'categories': 4, 'foods': 9, 'nutritionQuantities': 36}

Now we can write out the simple_data.sql file.


In [5]:
dataFactory.sql.write_sql_file(dat, "simple_data.sql")

You can now copy this file into the appropriate location in the example-diet repository. Its easy enough to copy it right here from within iPython. First import a module that knows how to copy.


In [6]:
import shutil

Next, import the testing sub-component of the diet module. This would be inappropriate for production code, but its fine for exploratory programming.


In [7]:
import diet.testing.test_diet as diettest

diettest knows the path to the directory that is used for storing diet testing data.


In [8]:
shutil.copy("simple_data.sql", diettest.data_directory())

Pretty slick, no? Start with a Excel data file, covert it to a human readable .sql file, and copy it to the correct repository location, all with just 8 lines of an iPython session.