Package: lathorp
Lathorp - PostgreSQL Testing FixturesLathorp provides handypytest fixturesfor test code that involves access to aPostgreSQLdatabase.For example, if the code under test writes data to a database, or queries it for data, you want to write tests to see
that it writes and reads the data as you expect.
You need an isolated and controlled environment for testing, so you need to set up a temporary database just for
testing and fill it up with known data.
Setting up a new database is a relatively time consuming operation that slows your tests down. You want to do this
just once per testing session and let multiple tests share the same temporary database instance.
However, many tests should be isolated from each other - you don't want side effects from a test (say, data written to
the database) leaking to another test. The solution is to write known data before each test and delete it all after
the test has finished.The FixturesThepgfixture is a session-scoped fixture that creates a new temporary database once per test session and deletes
it after the session is done.Thepg_connectfixture is a function-scoped fixture that you use to establish a connection to the temporary database.
You can optionally load data into the database before the test begins, by pointing to data files that PostgreSQL can
read. The data is automatically deleted after the test function returns, so that it doesn't leak to other tests that
may expect different data.Utility FunctionsA utility function calledload_schema_definitionscan be used to create the database structure by reading SQL DDL
files that you provide. Embed it in your own session-scoped fixture and call that together withpg_connect.Another utility,copy_data, copies data from files in CSV or text format (as supported by PostgreSQL).
It can be used directly, or automatically by passing a path to the files to thepg_connectfixture.A third utility,delete_data, deletes all data from a set of tables.
It is used automatically bypg_connectto remove data from tables that it loaded withcopy_data.
You can also use it directly to clean up any changes made by your test.Using Lathorp in Your ProjectInclude thelathorppackage with the development packages of your projects.
Withpipenv:pipenvinstall--devlathorpImport fixtures fromlathorpin your tests. You can also import them inconftest.pyto make them available to all
your tests. Seetests/conftest.pyin this project for an example.Add eitherpgorpg_connectas arguments to your test functions, as shown below. There's no point in adding
both, sincepg_connectitself usespg.defmy_test(pg_connect):"""A test that connects to the temporary test database."""withpg_connect()asconn:withconn.cursor()ascursor:cursor.execute("SELECT 'Hello, world!' AS hello;")For more examples, seetests/test_fixtures.pyin this project.Lathorp truly shines when you use it together with schema definitions (SQL DDL) files and test data files (CSV or
PostgreSQL-readable text).
Create a session-scoped fixture that callsload_schema_definitionsand give it the path to your schema definitions.
Then use this fixture along withpg_connectand give it the path to your data files.# In conftest.pyimportpathlibfromlathorpimportload_schema_definitionsfromlathorp.fixturesimportpgfromlathorp.fixturesimportpg_connectdefinit_schema(pg):load_schema_definitions(pathlib.Path('path/to/my/ddl/file_or_directory'))# In your test moduledeftest_my_fun(init_schema,pg_connect):conn=pg_connect(pathlib.Path('path/to/my_table.csv'))# loads data into my_tablewithconn.cursor()ascursor:cursor.execute('SELECT * FROM my_table;')Now every test can have access to an initialized database with test-specific data.Why "Lathorp?The name is a reference toDr. Emmett Lathorp "Doc" Brown,
the crazy scientist and inventor of the DeLorean time machine fromBack to the Futuretrilogy.The Lathorp library too lets you go back in time to a fresh database after every test.