tests.utils
Attributes
Classes
Special version supporting |
|
Helper class that provides a standard way to create an ABC using |
|
Helper class that provides a standard way to create an ABC using |
|
Helper class that provides a standard way to create an ABC using |
Functions
|
|
|
Instantiate a local test application. |
|
Obtains the referenced test application, local application or remote URL from Test Case implementation. |
|
Obtains stored hostname in the class implementation. |
|
Obtains stored headers in the class implementation. |
|
Obtains the specified response Content-Type header(s) without additional formatting parameters. |
|
Obtains the JSON payload of the response regardless of its class implementation. |
|
Generates a message string with formatted JSON body for display with easier readability. |
|
Decorator to mock |
|
Generates a fake request with provided arguments. |
|
Calls the request using either a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Calls the callable and verifies that the specific exception was raised. |
|
Calls the callable and verifies that no exception was raised. |
Validates basic Cowbird API response metadata. For UI pages, employ |
|
|
Validates error response |
|
Checks if the path has the right permissions, by verifying the last digits of the octal permissions. |
|
Module Contents
- class tests.utils.TestVersion(vstring: AnyTestVersion)[source]
Bases:
packaging.version.Version
Special version supporting
latest
keyword to ignore safeguard check ofwarn_version()
during development.See also
Environment variable
COWBIRD_TEST_VERSION
should be set with the desired version orlatest
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.
- 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.
- 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.
- 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.
- 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 fromDummyRequest
.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 orrequests.Request
from a string URL.Keyword arguments
json
,data
andbody
are all looked for to obtain the data.Header
Content-Type
is set with respect to explicitjson
or via providedheaders
when available. Explicitcontent_type
can also be provided to override all of these.Request cookies are set according to
cookies
, or can be interpreted fromSet-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 usingTestApp
.kwargs – any additional keywords that will be forwarded to the request call.
- Returns:
response of the request
- 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 withiniter_ref
. Ifany_order
isFalse
, 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 toref
.
- 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 toref
.
- 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 toref
.
- 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 toref
.
- 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 ofref
.
- 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
andparam_name_exists
isTrue
(only its presence). If provided, automatically impliesparam_name_exists=True
. Skipped otherwise.param_compare – Expected ‘compare’/’param_compare’ value (filed name according to version) Contained field value not verified if
null
andparam_compare_exists
isTrue
(only its presence). If provided, automatically impliesparam_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