testing

Testing is described with examples in Tests.

class pemi.testing.Scenario(name, pipe, factories, sources, targets, target_case_collectors, flow='flow', selector=None, usefixtures=None)[source]

A Scenario describes the transformation that is being tested (a Pemi pipe), and the data sources and targets that are the subject of the test. Scenarios are composed of one more Cases.

Parameters
  • name (str) – The name of a scenario. Multiple scenarios may be present in a file, but the names of each scenario must be unique.

  • pipe (pemi.Pipe) – The Pemi pipe that is the main subject of the test. Test data will be provided to the sources of the pipe (defined below), and the pipe will be executed. Note that the pipe is only executed once per scenario.

  • flow (str) – The name of the method used to execute the pipe (default: flow).

  • factories (dict) – A dictionary where the keys are the names of factories and the values are FactoryBoy factories that will be used to generate unique keys.

  • sources (dict) – A dictionary where the keys are the names of sources that will be the subjects of testing. The values are methods that accept the pipe referenced in the pipe argument above and return the data subject that will be used as a source.

  • targets (dict) – A dictionary where the keys are the names of targets that will be the subjects of testing. The values are methods that accept the pipe referenced in the pipe argument above and return the data subject that will be used as a target.

  • target_case_collectors (dict) – A dictionary where the keys are the names of the targets that will be the subjects of testing. The values are CaseCollector objects that tie a field in the scenario’s target to the field in a given factory. Every named target needs to have a case collector.

  • selector (str) – A string representing a regular expression. Any case names that do not match this regex will be excluded from testing.

  • usefixtures (str) – Name of a Pytest fixture to use for the scenario. Often used for database setup/teardown options.

class pemi.testing.Case(name, scenario)[source]

A Case is a set of Conditions and Expectations that describe how the pipe is supposed to function.

Parameters
  • name (str) – The name of the case. The names of cases within a scenario must be unique.

  • scenario (pemi.testing.Scenario) – The scenario object that this case is associated with.

expect_exception(exception)[source]

Used to indicate that the test case is expected to fail with exception exception. If the test case raises this exception, then it will pass. If it does not raise the exception, then it will fail.

then(*funcs)[source]

Accepts a list of functions that are used to test the result data for a specific case. Each of the functions should accept one argument, which is the case object. See pemi.testing.then for examples.

when(*funcs)[source]

Accepts a list of functions that are used to set up the data for a specific case. Each of the functions should accept one argument, which is the case object. See pemi.testing.when for examples.

class pemi.testing.when[source]

Contains methods used to set up conditions for a testing case.

static example_for_source(source, table)[source]

Set specific rows and columns to specific values.

Parameters
  • source (scenario.sources[]) – The scenario source data subject.

  • table (pemi.data.Table) – Pemi data table to use for specifying data.

Example

Given a Pemi data table, specify rows and columns for the source main:

case.when(
    when.example_for_source(
        scenario.sources['main'],
        pemi.data.Table(
            '''
            | id       | name  |
            | -        | -     |
            | {sid[1]} | Spike |
            | {sid[2]} | Angel |
            '''.format(
                sid=scenario.factories['vampires']['id']
            )
        )
    )
)
static source_conforms_to_schema(source, key_factories=None)[source]

Creates 3 records and fills out data for a data source subject that conforms to the data types specified by the data subject’s schema.

Parameters
  • source (scenario.sources[]) – The scenario source data subject.

  • key_factories (dict) – A dictionary where the keys are the names of fields

  • the values are the field value generator originating from a scenario (and) –

  • factory (key) –

Example

For the source subject ‘main’, this will generate faked data that conforms to the schema defined for main. It will also populate the id field with values generated from the id field in the vampires factory:

case.when(
    when.source_conforms_to_schema(
        scenario.sources['main'],
        {'id': scenario.factories['vampires']['id']}
    )
)
static source_field_has_value(source, field, value)[source]

Sets the value of a specific field to a specific value.

Parameters
  • source (scenario.sources[]) – The scenario source data subject.

  • field (str) – Name of field.

  • value (str, iter) – Value to set for the field.

Examples

Set the value of the field name to the string value Buffy in the scenario source main:

case.when(
    when.source_field_has_value(scenario.sources['main'], 'name', 'Buffy')
)
static source_fields_have_values(source, mapping)[source]

Sets the value of a multiples fields to a specific values.

Parameters
  • source (scenario.sources[]) – The scenario source data subject.

  • mapping (dict) – Dictionary where the keys are the names of fields and the values are the values those fields are to be set to.

Examples

Set the value of the field name to the string value Buffy and the value of the field vampires_slain to 133 in the scenario source main:

case.when(
    when.source_fields_have_values(
        scenario.sources['main'],
        {
            'name': 'Buffy',
            'vampires_slain': 133
        }
    )
)
class pemi.testing.then[source]

Contains methods used to test that actual outcome is equal to expected outcome.

static field_is_copied(source, source_field, target, target_field, by=None, source_by=None, target_by=None)[source]

Asserts that a field value is copied from the source to the target.

Parameters
  • source (scenario.sources[]) – The scenario source data subject.

  • source_field (str) – The name of the source field.

  • target (scenario.targets[]) – The scenario target data subject.

  • target_field (str) – The name of the target field.

  • by (list) – A list of field names to sort the data by before performing the comparison.

  • source_by (list) – A list of field names to sort the source data by before performing the comparison (uses by if not given).

  • target_by (list) – A list of field names to sort the target data by before performing the comparison (uses by if not given).

Examples

Asserts that the value of the source field name is copied to the target field slayer_name:

case.then(
    then.field_is_copied(
        scenario.sources['main'], 'name',
        scenario.targets['main'], 'slayer_name',
        by=['id']
    )
)
static fields_are_copied(source, target, mapping, by=None, source_by=None, target_by=None)[source]

Asserts that various field values are copied from the source to the target.

Parameters
  • source (scenario.sources[]) – The scenario source data subject.

  • target (scenario.targets[]) – The scenario target data subject.

  • mapping (list) – A list of tuples. Each tuple contains the source field name and target field name, in that order.

  • by (list) – A list of field names to sort the data by before performing the comparison.

  • source_by (list) – A list of field names to sort the source data by before performing the comparison (uses by if not given).

  • target_by (list) – A list of field names to sort the target data by before performing the comparison (uses by if not given).

Examples

Asserts that the value of the source field name is copied to the target field slayer_name and num is copied to vampires_slain:

case.then(
    then.fields_are_copied(
        scenario.sources['main'],
        scenario.targets['main'],
        [
            ('name', 'slayer_name'),
            ('num', 'vampires_slain')
        ],
        by=['id']
    )
)
static target_does_not_have_fields(target, fields)[source]

Asserts that the target does not have certain fields.

Parameters
  • target (scenario.targets[]) – The scenario target data subject.

  • fields (list) – List of field names that should not be on the target.

Examples

Asserts that the scenario target main does not have the fields sparkle_factor or is_werewolf:

case.then(
    then.target_does_not_have_fields(
        scenario.targets['main'],
        ['sparkle_factor', 'is_werewolf']
    )
)
static target_field_has_value(target, field, value)[source]

Asserts that a specific field has a specific value.

Parameters
  • target (scenario.targets[]) – The scenario target data subject.

  • field (str) – Name of field.

  • value (str) – Value of the field that is expected.

Examples

Asserts that the value of the field name is set to the string value Buffy in the scenario target main:

case.then(
    then.target_field_has_value(scenario.targets['main'], 'name', 'Buffy')
)
static target_fields_have_values(target, mapping)[source]

Asserts that multiple fields have specific values.

Parameters
  • target (scenario.targets[]) – The scenario target data subject.

  • mapping (dict) – Dictionary where the keys are the names of fields and the values are the expected values those fields.

Examples

Asserts that the value of the field name is the string value Buffy and the value of the field vampires_slain is 133 in the scenario target main:

case.then(
    then.target_fields_have_values(
        scenario.targets['main'],
        {
            'name': 'Buffy',
            'vampires_slain': 133
        }
    )
)
static target_has_fields(target, fields, only=False)[source]

Asserts that the target has certain fields.

Parameters
  • target (scenario.targets[]) – The scenario target data subject.

  • fields (list) – List of field names that should not be on the target.

  • only (bool) – Specifies whether the target should only have the fields listed. Raises an exception if there are additional fields.

Examples

Asserts that the scenario target main only has the fields name and vampires_slain:

case.then(
    then.target_has_fields(
        scenario.targets['main'],
        ['name', 'vampires_slain'],
        only=True
    )
)
static target_has_n_records(target, expected_n)[source]

Asserts that the target has a specific number of records.

Parameters
  • target (scenario.targets[]) – The scenario target data subject.

  • expected_n (int) – The number of records expected.

Examples

Asserts that the scenario target main has 3 records:

case.then(then.target_has_n_records(scenario.targets['main'], 3)
static target_is_empty(target)[source]

Asserts that the target has no records.

Parameters

target (scenario.targets[]) – The scenario target data subject.

Examples

Asserts that the scenario target errors does not have any records:

case.then(then.target_is_empty(scenario.targets['errors'])
static target_matches_example(target, expected_table, by=None, query=None)[source]

Asserts that a given target matches an example data table

Parameters
  • target (scenario.targets[]) – The scenario target data subject.

  • expected_table (pemi.data.Table) – Expected result data. If the table has fewer columns than the pipe generates, those extra columns are not considered in the comparison.

  • by (list) – A list of field names to sort the result data by before performing the comparison.

  • query (string) – A pandas query string that can be used to filter down target records prior to comparison

Examples

Asserts that the scenario target main conforms to the expected data:

case.then(
    then.target_matches_example(
        scenario.targets['main'],
        pemi.data.Table(
            '''
            | id       | name  |
            | -        | -     |
            | {sid[1]} | Spike |
            | {sid[2]} | Angel |
            '''.format(
                sid=scenario.factories['vampires']['id']
            )
        ),
        by=['id'] #esp important if the ids are generated randomly
    )
)