Skip to content

Commit

Permalink
Added transformers specific testing
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Jul 21, 2023
1 parent 622ad3e commit 1b52dce
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 37 deletions.
4 changes: 1 addition & 3 deletions esmerald/transformers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from esmerald.typing import Undefined
from esmerald.utils.constants import REQUIRED

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from esmerald.injector import Inject
from esmerald.transformers.datastructures import EsmeraldSignature, Parameter
from esmerald.types import ConnectionType
Expand Down Expand Up @@ -136,6 +136,4 @@ def get_field_definition_from_param(param: "Parameter") -> Tuple[Any, Any]:
definition = (param.annotation, param.default)
elif not param.optional:
definition = (param.annotation, ...)
else:
definition = (param.annotation, None)
return definition
31 changes: 0 additions & 31 deletions esmerald/utils/crypto.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
import hashlib
import random
import time

from esmerald.conf import settings

try:
_random = random.SystemRandom()
using_sysrandom = True
except NotImplementedError:
import warnings

warnings.warn(
"A secure pseudo-random number generator is not available "
"on your system. Falling back to Mersenne Twister.",
stacklevel=2,
)
using_sysrandom = False


def get_random_string(
Expand All @@ -27,20 +10,6 @@ def get_random_string(
The default length of 12 with the a-z, A-Z, 0-9 character set returns
a 71-bit value. log_2((26+26+10)^12) =~ 71 bits
"""
if not using_sysrandom:
# This is ugly, and a hack, but it makes things better than
# the alternative of predictability. This re-seeds the PRNG
# using a value that is hard for an attacker to predict, every
# time a random string is required. This may change the
# properties of the chosen random sequence slightly, but this
# is better than absolute predictability.
_random.seed(
hashlib.sha256(
("{}{}{}".format(random.getstate(), time.time(), settings.secret_key)).encode(
"utf-8"
)
).digest()
)
return "".join(random.choice(allowed_chars) for _ in range(length))


Expand Down
6 changes: 3 additions & 3 deletions esmerald/utils/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def inner(self, *args: Any) -> RT: # type: ignore
return inner


class LazyObject:
class LazyObject: # pragma: no cover
"""
A wrapper for another class that can be used to delay instantiation of the
wrapped class.
Expand Down Expand Up @@ -128,15 +128,15 @@ def __deepcopy__(self, memo: Any) -> Any:
__contains__: Any = new_method_proxy(operator.contains)


def unpickle_lazyobject(wrapped: Any) -> Any:
def unpickle_lazyobject(wrapped: Any) -> Any: # pragma: no cover
"""
Used to unpickle lazy objects. Just return its argument, which will be the
wrapped object.
"""
return wrapped


class SimpleLazyObject(LazyObject):
class SimpleLazyObject(LazyObject): # pragma: no cover
"""
A lazy object initialized from any function.
Designed for compound objects of unknown type. For builtins or objects of
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,12 @@ parallel = true
context = '${CONTEXT}'
source = ["tests", "esmerald"]
omit = [
"esmerald/types.py",
"esmerald/conf/__init__.py",
"esmerald/core/directives/*",
"esmerald/core/terminal/__init__.py",
"tests/databases/saffier/*",
"tests/utils/functional.py",
"tests/cli/*",
]

Expand Down
Empty file added tests/transformers/__init__.py
Empty file.
58 changes: 58 additions & 0 deletions tests/transformers/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest
from pydantic import BaseModel

from esmerald import File, ImproperlyConfigured, Param, ValidationErrorException, get
from esmerald.enums import ParamType
from esmerald.transformers.model import ParamSetting
from esmerald.transformers.utils import get_request_params, get_signature


class Test(BaseModel):
""""""


@get()
async def test() -> None:
""""""


def test_get_signature_improperly_configured():
test = Test()
with pytest.raises(ImproperlyConfigured):
get_signature(test)


def test_signature_model_is_none():
handler = get_signature(test)

assert handler is None


def test_get_request_params_raise_validation_error_exception():
param = Param(default=None)
expected_param = File(default=None)
param_setting = ParamSetting(
default_value=None,
field_alias="param",
field_name="param",
is_required=True,
param_type=ParamType.PATH,
field_info=param,
)

expected_param_setting = ParamSetting(
default_value=None,
field_alias="test",
field_name="test",
is_required=True,
param_type=ParamType.PATH,
field_info=expected_param,
)

set_params = {param_setting}
expected_params = {expected_param_setting}

with pytest.raises(ValidationErrorException):
get_request_params(
params=set_params, expected=expected_params, url="http://testserver.com"
)

0 comments on commit 1b52dce

Please sign in to comment.