tests.utils

Module Contents

Classes

TestAppContainer

TestVersion

Special version supporting latest keyword to ignore safeguard check of warn_version() during development.

MockMagpieHandler

Helper class that provides a standard way to create an ABC using

MockAnyHandlerBase

Helper class that provides a standard way to create an ABC using

MockAnyHandler

Helper class that provides a standard way to create an ABC using

Functions

clear_handlers_instances()

config_setup_from_ini(config_ini_file_path)

get_test_app(→ webtest.app.TestApp)

Instantiate a local test application.

get_app_or_url(→ TestAppOrUrlType)

Obtains the referenced test application, local application or remote URL from Test Case implementation.

get_hostname(→ str)

Obtains stored hostname in the class implementation.

get_headers(→ cowbird.typedefs.HeadersType)

Obtains stored headers in the class implementation.

get_response_content_types_list(→ List[str])

Obtains the specified response Content-Type header(s) without additional formatting parameters.

get_json_body(→ cowbird.typedefs.JSON)

Obtains the JSON payload of the response regardless of its class implementation.

json_msg(→ str)

Generates a message string with formatted JSON body for display with easier readability.

mock_get_settings(test)

Decorator to mock cowbird.utils.get_settings() to allow retrieval of settings from DummyRequest.

mock_request(→ pyramid.request.Request)

Generates a fake request with provided arguments.

test_request(→ cowbird.typedefs.AnyResponseType)

Calls the request using either a webtest.TestApp instance or requests.Request from a string URL.

visual_repr(→ str)

format_test_val_ref(val, ref[, pre, msg])

all_equal(iter_val, iter_ref[, any_order])

check_all_equal(→ None)

param iter_val:

tested values.

check_val_equal(→ None)

raises AssertionError:

if val is not equal to ref.

check_val_not_equal(→ None)

raises AssertionError:

if val is equal to ref.

check_val_is_in(→ None)

raises AssertionError:

if val is not in to ref.

check_val_not_in(→ None)

raises AssertionError:

if val is in to ref.

check_val_type(→ None)

raises AssertionError:

if val is not an instanced of ref.

check_raises(→ Exception)

Calls the callable and verifies that the specific exception was raised.

check_no_raise(→ Any)

Calls the callable and verifies that no exception was raised.

check_response_basic_info(...)

Validates basic Cowbird API response metadata. For UI pages, employ check_ui_response_basic_info() instead.

check_error_param_structure(→ None)

Validates error response param information based on different Cowbird version formats.

check_path_permissions(→ None)

Checks if the path has the right permissions, by verifying the last digits of the octal permissions.

check_mock_has_calls(mocked_fct, calls)

Attributes

TEST_INI_FILE

TEST_CFG_FILE

LOGGER

TestAppOrUrlType

AnyTestItemType

_TestVersion

LatestVersion

AnyTestVersion

tests.utils.TEST_INI_FILE[source]
tests.utils.TEST_CFG_FILE[source]
tests.utils.LOGGER[source]
class tests.utils.TestAppContainer[source]

Bases: object

test_app: webtest.app.TestApp | None[source]
app: webtest.app.TestApp | None[source]
url: str | None[source]
tests.utils.TestAppOrUrlType[source]
tests.utils.AnyTestItemType[source]
tests.utils._TestVersion: typing_extensions.TypeAlias = 'TestVersion'[source]
tests.utils.LatestVersion[source]
tests.utils.AnyTestVersion[source]
class tests.utils.TestVersion(vstring: AnyTestVersion)[source]

Bases: packaging.version.Version

Special version supporting latest keyword to ignore safeguard check of warn_version() during development.

See also

Environment variable COWBIRD_TEST_VERSION should be set with the desired version or latest to evaluate even new features above the last tagged version.

Initialize a Version object.

Parameters:

version – The string representation of a version which will be parsed and normalized before use.

Raises:

InvalidVersion – If the version does not conform to PEP 440 in any way then this exception will be raised.

property version: Tuple[int | str, Ellipsis] | str[source]
__test__ = False[source]
_cmp(other: Any) int[source]
__lt__(other: Any) bool[source]

Return self<value.

__le__(other: Any) bool[source]

Return self<=value.

__gt__(other: Any) bool[source]

Return self>value.

__ge__(other: Any) bool[source]

Return self>=value.

__eq__(other: Any) bool[source]

Return self==value.

__ne__(other: Any) bool[source]

Return self!=value.

class tests.utils.MockMagpieHandler(settings, name, **kwargs)[source]

Bases: cowbird.handlers.handler.Handler

Helper class that provides a standard way to create an ABC using inheritance.

Parameters:
  • settings – Cowbird settings for convenience

  • name – Handler name

  • kwargs – The base class handle, but doesn’t require the following variables:

  • url – Location of the web service represented by the cowbird handler

  • workspace_dir – Workspace directory

  • priority – Relative priority between handlers while handling events. Lower value has higher priority, default value is last.

required_params = [][source]
json()[source]
get_geoserver_workspace_res_id(user_name)[source]
user_created(user_name)[source]
user_deleted(user_name)[source]
permission_created(permission)[source]
permission_deleted(permission)[source]
create_permission(permission)[source]
delete_permission(permission)[source]
delete_resource(res_id)[source]
resync()[source]
static get_service_types() List[str][source]

Returns the list of service types available on Magpie.

class tests.utils.MockAnyHandlerBase(settings: cowbird.typedefs.SettingsType, name: str, **kwargs: Any)[source]

Bases: cowbird.handlers.handler.Handler

Helper class that provides a standard way to create an ABC using inheritance.

Parameters:
  • settings – Cowbird settings for convenience

  • name – Handler name

  • kwargs – The base class handle, but doesn’t require the following variables:

  • url – Location of the web service represented by the cowbird handler

  • workspace_dir – Workspace directory

  • priority – Relative priority between handlers while handling events. Lower value has higher priority, default value is last.

ResourceId = 1000[source]
user_created(user_name)[source]
user_deleted(user_name)[source]
permission_created(permission)[source]
permission_deleted(permission)[source]
resync()[source]
class tests.utils.MockAnyHandler(settings: cowbird.typedefs.SettingsType, name: str, **kwargs: Any)[source]

Bases: MockAnyHandlerBase

Helper class that provides a standard way to create an ABC using inheritance.

Parameters:
  • settings – Cowbird settings for convenience

  • name – Handler name

  • kwargs – The base class handle, but doesn’t require the following variables:

  • url – Location of the web service represented by the cowbird handler

  • workspace_dir – Workspace directory

  • priority – Relative priority between handlers while handling events. Lower value has higher priority, default value is last.

required_params = [][source]
tests.utils.clear_handlers_instances()[source]
tests.utils.config_setup_from_ini(config_ini_file_path)[source]
tests.utils.get_test_app(settings: cowbird.typedefs.SettingsType | None = None) webtest.app.TestApp[source]

Instantiate a local test application.

tests.utils.get_app_or_url(test_item: AnyTestItemType) TestAppOrUrlType[source]

Obtains the referenced test application, local application or remote URL from Test Case implementation.

tests.utils.get_hostname(test_item: AnyTestItemType) str[source]

Obtains stored hostname in the class implementation.

tests.utils.get_headers(app_or_url: TestAppOrUrlType, header_dict: cowbird.typedefs.AnyHeadersType) cowbird.typedefs.HeadersType[source]

Obtains stored headers in the class implementation.

tests.utils.get_response_content_types_list(response: cowbird.typedefs.AnyResponseType) List[str][source]

Obtains the specified response Content-Type header(s) without additional formatting parameters.

tests.utils.get_json_body(response: cowbird.typedefs.AnyResponseType) cowbird.typedefs.JSON[source]

Obtains the JSON payload of the response regardless of its class implementation.

tests.utils.json_msg(json_body: cowbird.typedefs.JSON, msg: str | None = null) str[source]

Generates a message string with formatted JSON body for display with easier readability.

tests.utils.mock_get_settings(test)[source]

Decorator to mock cowbird.utils.get_settings() to allow retrieval of settings from DummyRequest.

Warning

Only apply on test methods (not on class TestCase) to ensure that pytest can collect them correctly.

tests.utils.mock_request(request_path_query: str = '', method: str = 'GET', params: Dict[str, str] | None = None, body: str | cowbird.typedefs.JSON = '', content_type: str | None = None, headers: cowbird.typedefs.AnyHeadersType | None = None, cookies: cowbird.typedefs.AnyCookiesType | None = None, settings: cowbird.typedefs.SettingsType = None) pyramid.request.Request[source]

Generates a fake request with provided arguments.

Can be employed by functions that expect a request object as input to retrieve details such as body content, the request path, or internal settings, but that no actual request needs to be accomplished.

tests.utils.test_request(test_item: AnyTestItemType, method: str, path: str, data: cowbird.typedefs.JSON | str | None = None, json: cowbird.typedefs.JSON | str | None = None, body: cowbird.typedefs.JSON | str | None = None, params: Dict[str, str] | None = None, timeout: int = 10, retries: int = 3, allow_redirects: bool = True, content_type: str | None = None, headers: cowbird.typedefs.AnyHeadersType | None = None, cookies: cowbird.typedefs.AnyCookiesType | None = None, **kwargs: Any) cowbird.typedefs.AnyResponseType[source]

Calls the request using either a webtest.TestApp instance or requests.Request from a string URL.

Keyword arguments json, data and body are all looked for to obtain the data.

Header Content-Type is set with respect to explicit json or via provided headers when available. Explicit content_type can also be provided to override all of these.

Request cookies are set according to cookies, or can be interpreted from Set-Cookie header.

Warning

When using TestApp, some internal cookies can be stored from previous requests to retain the active user. Make sure to provide new set of cookies (or logout user explicitly) if different session must be used, otherwise they will be picked up automatically. For ‘empty’ cookies, provide an empty dictionary.

Parameters:
  • test_item – one of BaseTestCase, webtest.TestApp or remote server URL to call with requests

  • method – request method (GET, POST, PATCH, PUT, DELETE)

  • path – test path starting at base path that will be appended to the application’s endpoint.

  • params – query parameters added to the request path.

  • json – explicit JSON body content to use as request body.

  • data – body content string to use as request body, can be JSON if matching Content-Type is identified.

  • body – alias to data.

  • content_type – Enforce specific content-type of provided data body. Otherwise, attempt to retrieve it from request headers. Inferred JSON content-type when json is employed, unless overridden explicitly.

  • headers – Set of headers to send the request. Header Content-Type is looked for if not overridden.

  • cookies – Cookies to provide to the request.

  • timeout – passed down to requests when using URL, otherwise ignored (unsupported).

  • retries – number of retry attempts in case the requested failed due to timeout (only when using URL).

  • allow_redirects – Passed down to requests when using URL, handled manually for same behaviour when using TestApp.

  • kwargs – any additional keywords that will be forwarded to the request call.

Returns:

response of the request

tests.utils.visual_repr(item: Any) str[source]
tests.utils.format_test_val_ref(val, ref, pre='Fail', msg=None)[source]
tests.utils.all_equal(iter_val, iter_ref, any_order=False)[source]
tests.utils.check_all_equal(iter_val: Collection[Any], iter_ref: Collection[Any] | cowbird.utils.NullType, msg: str | None = None, any_order: bool = False) None[source]
Parameters:
  • iter_val – tested values.

  • iter_ref – reference values.

  • msg – override message to display if failing test.

  • any_order – allow equal values to be provided in any order, otherwise order must match as well as values.

Raises:

AssertionError – If all values in iter_val are not equal to values within iter_ref. If any_order is False, also raises if equal items are not in the same order.

tests.utils.check_val_equal(val: Any, ref: Any | cowbird.utils.NullType, msg: str | None = None) None[source]
Raises:

AssertionError – if val is not equal to ref.

tests.utils.check_val_not_equal(val: Any, ref: Any | cowbird.utils.NullType, msg: str | None = None) None[source]
Raises:

AssertionError – if val is equal to ref.

tests.utils.check_val_is_in(val: Any, ref: Any | cowbird.utils.NullType, msg: str | None = None) None[source]
Raises:

AssertionError – if val is not in to ref.

tests.utils.check_val_not_in(val: Any, ref: Any | cowbird.utils.NullType, msg: str | None = None) None[source]
Raises:

AssertionError – if val is in to ref.

tests.utils.check_val_type(val: Any, ref: Type[Any] | cowbird.utils.NullType | Iterable[Type[Any]], msg: str | None = None) None[source]
Raises:

AssertionError – if val is not an instanced of ref.

tests.utils.check_raises(func: Callable[[], Any], exception_type: Type[Exception], msg: str | None = None) Exception[source]

Calls the callable and verifies that the specific exception was raised.

Raises:

AssertionError – on failing exception check or missing raised exception.

Returns:

raised exception of expected type if it was raised.

tests.utils.check_no_raise(func: Callable[[], Any], msg: str | None = None) Any[source]

Calls the callable and verifies that no exception was raised.

Raises:

AssertionError – on any raised exception.

tests.utils.check_response_basic_info(response: cowbird.typedefs.AnyResponseType, expected_code: int = 200, expected_type: str = CONTENT_TYPE_JSON, expected_method: str = 'GET', extra_message: str | None = None) cowbird.typedefs.JSON | str[source]

Validates basic Cowbird API response metadata. For UI pages, employ check_ui_response_basic_info() instead.

If the expected content-type is JSON, further validations are accomplished with specific metadata fields that are always expected in the response body. Otherwise, minimal validation of basic fields that can be validated regardless of content-type is done.

Parameters:
  • response – response to validate.

  • expected_code – status code to validate from the response.

  • expected_type – Content-Type to validate from the response.

  • expected_method – method ‘GET’, ‘POST’, etc. to validate from the response if an error.

  • extra_message – additional message to append to every specific test message if provided.

Returns:

json body of the response for convenience.

tests.utils.check_error_param_structure(body: cowbird.typedefs.JSON, param_value: Any | None = null, param_name: str | None = null, param_compare: Any | None = null, param_name_exists: bool = False, param_compare_exists: bool = False) None[source]

Validates error response param information based on different Cowbird version formats.

Parameters:
  • body – JSON body of the response to validate.

  • param_value – Expected ‘value’ of param the parameter. Contained field value not verified if null, only presence of the field.

  • param_name – Expected ‘name’ of param. Ignored for older Cowbird version that did not provide this information. Contained field value not verified if null and param_name_exists is True (only its presence). If provided, automatically implies param_name_exists=True. Skipped otherwise.

  • param_compare – Expected ‘compare’/’param_compare’ value (filed name according to version) Contained field value not verified if null and param_compare_exists is True (only its presence). If provided, automatically implies param_compare_exists=True. Skipped otherwise.

  • param_name_exists – verify that ‘name’ is in the body, not validating its value.

  • param_compare_exists – verify that ‘compare’/’param_compare’ is in the body, not validating its value.

Raises:

AssertionError – on any failing condition

tests.utils.check_path_permissions(path: str | os.PathLike, permissions: int, check_others_only: bool = False) None[source]

Checks if the path has the right permissions, by verifying the last digits of the octal permissions.

tests.utils.check_mock_has_calls(mocked_fct, calls)[source]