Smarter Unit Test Base Classes

Project level base classes for unit tests.

class smarter.lib.unittest.base_classes.SmarterTestBase(methodName='runTest')[source]

Bases: TestCase, SmarterHelperMixin

Base class for all unit tests.

It gives each test class a unique hash_suffix, name and uid, from which tests name the resources they create, so that test classes don’t collide. It clears the Django cache and the waffle switch cache before each test class, logs a banner at the start and end of each test, and reads test data files.

It has no transaction rollback: a test deletes everything that it creates.

create_generic_request(url='http://example.com')[source]

Create a generic POST request for testing purposes.

The request’s body is an empty JSON object. The sample chat body that this method builds is not sent.

Parameters:

url (str) – The request’s path.

Return type:

HttpRequest

Returns:

The request, from Django’s RequestFactory.

static generate_hash_suffix(length=16)[source]

Generate a unique hash suffix for test data.

Parameters:

length (int) – The suffix’s length.

Return type:

str

Returns:

A random hash.

classmethod generate_uid()[source]

Generate a unique identifier for the test.

Return type:

str

Returns:

A random 64 character hash.

classmethod get_readonly_csv_file(file_path)[source]

Read a CSV file in read-only mode.

Parameters:

file_path (str) – The path of the file. Its first row holds the column names.

Return type:

list[dict[str, str]]

Returns:

One dict per row, from column name to value.

classmethod get_readonly_json_file(file_path)[source]

Read a JSON file in read-only mode.

Parameters:

file_path (str) – The path of the file.

Return type:

dict[str, Any] | list[dict[str, Any]]

Returns:

What the file holds: an object, or a list of objects.

classmethod get_readonly_yaml_file(file_path)[source]

Read a YAML file in read-only mode.

Parameters:

file_path (str) – The path of the file.

Return type:

dict[str, Any] | list | None

Returns:

What the file holds, usually a dict, e.g. a manifest; a list; or None if the file is empty.

hash_suffix: str
line_width = 150

The width of the banners that are logged at the start and end of each test.

name: str
setUp()[source]

Set up the test: log a banner with the test method’s name.

Return type:

None

classmethod setUpClass()[source]

Set up the test class: give it a unique hash_suffix, name and uid, and clear the caches.

The Django cache and the waffle switch cache are cleared, so that a test class doesn’t see values that an earlier one cached.

Return type:

None

tearDown()[source]

Tear down the test: log a banner with the test method’s name.

Return type:

None

classmethod tearDownClass()[source]

Tear down the test class.

Return type:

None

uid: str