This module contains classes and functions for defining preconditions and postconditions for database state. The conditions can be used in unit tests to efficiently evaluate the expected database state.
- class pygrametl.drawntabletesting.Table(name, table, nullsubst='NULL', variableprefix='$', loadFrom=None, testconnection=None)¶
A class representing a concrete database table.
Note that the asserts assume that the Table instance and the database table do not have duplicate rows, if they do the asserts raise an error.
name: the name of the table in the database.
table: the contents of the table as an ASCII drawing.
nullsubst: a string that represents NULL in the drawn table.
variableprefix: a string all variables must have as a prefix.
loadFrom: additional rows to be loaded as a path to a file that continues the table argument or as an iterable producing dicts.
testconnection: The connection wrapper to use for the unit tests. If None pygrametl’s current default connection wrapper is used.
Return all rows added or updated since the original drawn table.
withKey: if True the primary keys are included in the rows.
Return a Boolean indicating if none of the rows in this object occur in the database table. If verbose=True an AssertionError is also raised if the row sets are not disjoint.
verbose: if True an ASCII representation of the rows violating the assertion is printed as part of the AssertionError.
Return a Boolean indicating if the rows in this object and the rows in the database table match. If verbose=True an AssertionError is also raised if the rows don’t match.
Return a Boolean stating if the rows in this object are a subset of those in the database table. If verbose=True an AssertionError is also raised if the rows are not a subset of those in the database table.
- classmethod clear()¶
Drop all tables and variables without checking their contents.
Create the table if it does not exist without adding any rows.
Drop the table in the database without checking the contents.
Create the table if it does not exist, otherwise verify the rows.
If the table does exist but does not contain the expected set of rows an exception is raised to prevent overriding existing data.
Return a string of SQL that creates the table.
Return a string of SQL that inserts all rows into the table.
Return the primary key.
For a simple primary key, the name is returned. For a composite primary key, all names are returned in a tuple.
Forcefully create a new table and add the provided rows.
- update(index, line)¶
Create a new instance with the row specified by the index updated with the values included in the provided line.
index: the index of the row to be updated.
line: an ASCII representation of the new values.
Create a new connection wrapper for use with unit tests.
connection: A PEP249 connection to use. If None (the default), a connection to a temporary SQLite in-memory database is created.