drawntabletesting¶
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)¶
Bases:
object
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.
Arguments:
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.
- additions(withKey=False)¶
Return all rows added or updated since the original drawn table.
Arguments:
withKey: if True the primary keys are included in the rows.
- assertDisjoint(verbose=True)¶
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.
Arguments:
verbose: if True an ASCII representation of the rows violating the assertion is printed as part of the AssertionError.
- assertEqual(verbose=True)¶
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.
Arguments:
verbose: if True an ASCII representation of the rows violating the assertion is printed as part of the AssertionError.
- assertSubset(verbose=True)¶
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.
Arguments:
verbose: if True an ASCII representation of the rows violating the assertion is printed as part of the AssertionError.
- classmethod clear()¶
Drop all tables and variables without checking their contents.
- create()¶
Create the table if it does not exist without adding any rows.
- drop()¶
Drop the table in the database without checking the contents.
- ensure()¶
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.
- getSQLToCreate()¶
Return a string of SQL that creates the table.
- getSQLToInsert()¶
Return a string of SQL that inserts all rows into the table.
- key()¶
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.
- reset()¶
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.
Arguments:
index: the index of the row to be updated.
line: an ASCII representation of the new values.
- pygrametl.drawntabletesting.connectionwrapper(connection=None)¶
Create a new connection wrapper for use with unit tests.
Arguments:
connection: A PEP249 connection to use. If None (the default), a connection to a temporary SQLite in-memory database is created.