Testing Utilities
drizm_commons.testing.biased
Contains utilities that are specifically biased towards certain Drizm organization standards.
from drizm_commons.testing.biased import *
self_to_id(body, force_str=False)
Extracts the ID from a Drizm-HATEOAS compliant response body.
Example body:
{
"self": {"href": "http://example.net/resources/1/"}
}
For the above body, the returned value would be 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
body |
Dict[str, Union[str, dict]] |
The JSONified response body. |
required |
force_str |
Optional[bool] |
If |
False |
Returns:
Type | Description |
---|---|
Union[str, int] |
The extracted identifier. |
drizm_commons.testing.conversion
Contains utilities for the conversion of data.
from drizm_commons.testing.conversion import *
image_file_to_b64(image_file)
Encodes an image file as Base64.
To obtain the stringified Base64 version of the image, you can convert the output like so:
image_file_to_b64(my_image_file).decode()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_file |
BytesIO |
The BytesIO file object to be converted. |
required |
Returns:
Type | Description |
---|---|
bytes |
Bytes representation of the Base64 encoded image. |
drizm_commons.testing.truthiness
Contains utilities to check for the truthiness of assertions.
from drizm_commons.testing.truthiness import *
all_items_equal(sequence)
Check whether all hashable items in a sequence are equal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequence |
Sequence[Hashable] |
A sequence of hashables to be checked. |
required |
Returns:
Type | Description |
---|---|
bool |
|
all_items_present(sequence, values)
Check whether all provided values
are present at any index
in the provided sequence
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequence |
Sequence[Hashable] |
An iterable of Hashable values to check for values in. |
required |
values |
Sequence[Hashable] |
An iterable of Hashable values for whose presence to
check |
required |
Returns:
Type | Description |
---|---|
bool |
|
all_keys_present(dictionary, keys, strict=False)
Checks whether all provided keys are present in the dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dictionary |
Dict[Hashable, Any] |
The dictionary to be checked. |
required |
keys |
Sequence[Hashable] |
A sequence of hashable values to be searched for. Must not contain any duplicate values. |
required |
strict |
Optional[bool] |
If |
False |
Returns:
Type | Description |
---|---|
bool |
|
all_nested_zipped_equal(sequence)
Check whether all items, at the same indexes, in a nested sequence are equal.
For example:
example_list = [[1, 2], [1, 2], [1, 2]]
all_nested_zipped_equal(example_list)
Is the same as:
example_list[0][0] == example_list[1][0] == example_list[2][0]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequence |
Sequence[Sequence] |
A nested sequence to be checked. |
required |
Returns:
Type | Description |
---|---|
bool |
|
is_class_method(klass, name)
Check if the attribute of the passed klass
is a @classmethod
.
This is a strict check, meaning it actually checks for the
@classmethod
descriptor.
Credit for this implementation goes to: https://stackoverflow.com/a/19228282`.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
klass |
|
Class object or instance to check for the attribute on. |
required |
name |
str |
Name of the attribute to check for. |
required |
Returns:
Type | Description |
---|---|
bool |
|
is_dunder(name)
Check whether a given attribute name
is a dunder, e.g. __name__
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str |
The provided attribute name. |
required |
Returns:
Type | Description |
---|---|
bool |
|
is_instance_method(klass, name)
Check if the attribute of the passed klass
is an instance method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
klass |
|
Class object or instance to check for the attribute on. |
required |
name |
str |
Name of the attribute to check for. |
required |
Returns:
Type | Description |
---|---|
bool |
|
is_static_method(klass, name)
Check if the attribute of the passed klass
is a @staticmethod
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
klass |
|
Class object or instance to check for the attribute on. |
required |
name |
str |
Name of the attribute to check for. |
required |
Returns:
Type | Description |
---|---|
bool |
|
uri_is_http(uri)
Check whether a string is a valid HTTP URI.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri |
str |
The string to check. |
required |
Returns:
Type | Description |
---|---|
bool |
|
uuid4_is_valid(value)
Check whether a given string is a valid UUIDv4.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
str |
The string to be tested. |
required |
Returns:
Type | Description |
---|---|
bool |
|