Test Helpers

A collection of utilities for testing webgrid functionality in client applications

class webgrid.testing.GridBase[source]

Base test class for Flask or Keg apps.

Class Attributes:

grid_cls: Application grid class to use during testing

filters: Iterable of (name, op, value, expected) tuples to check for filter logic, or a callable returning such an iterable. name is the column key. op and value set the filter parameters. expected is either a SQL string or compiled regex to find when the filter is enabled.

sort_tests: Iterable of (name, expected) tuples to check for sort logic. name is the column key. expected is a SQL string to find when the sort is enabled.

assert_in_query(look_for, grid=None, _query_string=None, **kwargs)[source]

Verify the given SQL string is in the grid’s query.

Args:

look_for (str): SQL string to find.

grid (BaseGrid, optional): Grid to use instead of self.get_session_grid. Defaults to None.

kwargs (dict, optional): Additional args passed to self.get_session_grid.

assert_not_in_query(look_for, grid=None, _query_string=None, **kwargs)[source]

Verify the given SQL string is not in the grid’s query.

Args:

look_for (str): SQL string to find.

grid (BaseGrid, optional): Grid to use instead of self.get_session_grid. Defaults to None.

kwargs (dict, optional): Additional args passed to self.get_session_grid.

assert_regex_in_query(look_for, grid=None, _query_string=None, **kwargs)[source]

Verify the given regex matches the grid’s query.

Args:

look_for (str or regex): Regex to search (can be compiled or provided as string).

grid (BaseGrid, optional): Grid to use instead of self.get_session_grid. Defaults to None.

kwargs (dict, optional): Additional args passed to self.get_session_grid.

check_filter(name, op, value, expected)[source]

Assertions to perform on a filter test.

Args:

name (str): Column key to filter. op (str): Filter operator to enable. value (Any): Filter value to assign. expected (str or regex): SQL string or compiled regex to find.

check_sort(k, ex, asc)[source]

Assertions to perform on a sort test.

Args:

k (str): Column key to sort. ex (str or regex): SQL string to find. asc (bool): Flag indicating ascending/descending order.

expect_table_contents(expect, grid=None, _query_string=None, **kwargs)[source]

Run assertions to compare rendered data rows with expected data.

Args:

expect (list): List representation of expected table data.

grid (BaseGrid, optional): Grid to use instead of self.get_session_grid. Defaults to None.

kwargs (dict, optional): Additional args passed to self.get_session_grid.

expect_table_header(expect, grid=None, _query_string=None, **kwargs)[source]

Run assertions to compare rendered headings with expected data.

Args:

expect (list): List representation of expected table data.

grid (BaseGrid, optional): Grid to use instead of self.get_session_grid. Defaults to None.

kwargs (dict, optional): Additional args passed to self.get_session_grid.

get_grid(grid_args, *args, **kwargs)[source]

Construct grid from args and kwargs, and apply grid_args.

Args:

grid_args: grid query args

Returns:

grid instance

get_pyq(grid=None, _query_string=None, **kwargs)[source]

Turn provided/constructed grid into a rendered PyQuery object.

Args:

grid (BaseGrid, optional): Grid to use instead of self.get_session_grid. Defaults to None.

kwargs (dict, optional): Additional args passed to self.get_session_grid.

Returns:

PyQuery object

get_session_grid(*args, _query_string=None, **kwargs)[source]

Construct grid from args and kwargs, and apply query string.

Args:

_query_string: URL query string with grid query args

Returns:

grid instance

query_to_str(statement, bind=None)[source]

Render a SQLAlchemy query to a string.

test_filters()[source]

Use filters attribute/property/method to run assertions.

test_search_expr_passes(grid=None, _query_string=None)[source]

Assert that a single-search query executes without error.

test_sort()[source]

Use sort_tests attribute/property to run assertions.

class webgrid.testing.MSSQLGridBase[source]

MSSQL dialect produces some string oddities compared to other dialects, such as having the N’foo’ syntax for unicode strings instead of ‘foo’. This can clutter tests a bit. Using MSSQLGridBase will patch that into the asserts, so that look_for will match whether it has the N-prefix or not.

assert_in_query(look_for, grid=None, context=None, _query_string=None, **kwargs)[source]

Verify the given SQL string is in the grid’s query.

Args:

look_for (str): SQL string to find.

grid (BaseGrid, optional): Grid to use instead of self.get_session_grid. Defaults to None.

kwargs (dict, optional): Additional args passed to self.get_session_grid.

assert_not_in_query(look_for, grid=None, context=None, _query_string=None, **kwargs)[source]

Verify the given SQL string is not in the grid’s query.

Args:

look_for (str): SQL string to find.

grid (BaseGrid, optional): Grid to use instead of self.get_session_grid. Defaults to None.

kwargs (dict, optional): Additional args passed to self.get_session_grid.

assert_regex_in_query(look_for, grid=None, context=None, _query_string=None, **kwargs)[source]

Verify the given regex matches the grid’s query.

Args:

look_for (str or regex): Regex to search (can be compiled or provided as string).

grid (BaseGrid, optional): Grid to use instead of self.get_session_grid. Defaults to None.

kwargs (dict, optional): Additional args passed to self.get_session_grid.

query_to_str_replace_type(compiled_query)[source]

Same as query_to_str, but accounts for pyodbc type-specific rendering.

webgrid.testing.assert_list_equal(list1, list2)[source]

A list-specific equality assertion.

This method is based on the Python unittest.TestCase.assertListEqual method.

Parameters:
  • list1

  • list2

Returns:

webgrid.testing.assert_rendered_xlsx_matches(rendered_xlsx, xlsx_headers, xlsx_rows)[source]

Verifies that rendered_xlsx has a set of headers and values that match the given parameters.

NOTE: This method does not perform in-depth analysis of complex workbooks!

Assumes header rows and data rows are contiguous. Multiple worksheets or complex layouts are not verified!

Parameters:
  • rendered_xlsx – binary data passed to openpyxl as file contents

  • xlsx_headers – list of rows of column headers

  • xlsx_rows – list of rows in order as they will appear in the worksheet

webgrid.testing.query_to_str(statement, bind=None)[source]

returns a string of a sqlalchemy.orm.Query with parameters bound

WARNING: this is dangerous and ONLY for testing, executing the results of this function can result in an SQL Injection attack.