validation.py

Last source

View documentation

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""Validation helpers for BDD tests for isshub entities."""

from typing import Any, Callable, List, Optional, Tuple, Type
from uuid import UUID

import pytest

from attr.exceptions import FrozenAttributeError

from isshub.domain.utils.entity import BaseEntity


ValuesValidation = List[Tuple[Any, Optional[Type[Exception]]]]

integer_only: ValuesValidation = [
    ("foo", TypeError),
    (-123, ValueError),
    (-1.5, TypeError),
    (-1, ValueError),
    (-0.001, TypeError),
    (0.001, TypeError),
    (1, None),
    (1.5, TypeError),
    (123, None),
]

no_zero: ValuesValidation = [(0, ValueError)]

positive_integer_only: ValuesValidation = integer_only + no_zero

string_only: ValuesValidation = [("foo", None), (1, TypeError), (-0.1, TypeError)]

uuid4_only: ValuesValidation = [
    (UUID("19f49bc8-06e5-11eb-8465-bf44725d7bd3"), TypeError),
    ("7298d61a-f08f-4f83-b75e-934e786eb43d", TypeError),
    (UUID("7298d61a-f08f-4f83-b75e-934e786eb43d"), None),
]


def check_field(obj: BaseEntity, field_name: str) -> None:
    """Assert that the given `obj` has an attribute named `field_name`.

    Parameters
    ----------
    obj : BaseEntity
        The object to test
    field_name : str
        The field name to search for

    Raises
    ------
    AssertionError
        If `obj` does not contain any attribute named `field_name`

    """
    assert hasattr(obj, field_name)


def check_field_value(
    factory: Callable[..., BaseEntity],
    field_name: str,
    value: Any,
    exception: Optional[Type[Exception]],
    **factory_kwargs: Any,
) -> None:
    """Assert that an object can or cannot have a specific value for a specific field.

    Parameters
    ----------
    factory : Callable[...,BaseEntity]
        The factory to use to create the object to test
    field_name : str
        The name of the field to check
    value : Any
        The value to set to the field
    exception :  Optional[Type[Exception]]
        The exception expected to be raised. If ``None``, no exception is expected to be raised.
    factory_kwargs : Any
        Any kwargs to pass to the factory to create the object

    Raises
    ------
    AssertionError
        If an exception is raised when setting the `value` to the field when creating or updating
        an object (when calling ``validate`` in case of updating), and `exception` is ``None``,
        or if no exception is raised if `exception` is not ``None`` (or the wrong exception).

    """
    factory_kwargs_copy = factory_kwargs.copy()
    factory_kwargs_copy.pop(field_name, None)

    if exception:
        # When creating an instance
        with pytest.raises(exception):
            factory(**{field_name: value}, **factory_kwargs_copy)
        # When updating the value
        obj = factory(**factory_kwargs)
        try:
            setattr(obj, field_name, value)
        except FrozenAttributeError:
            pass
        else:
            with pytest.raises(exception):
                obj.validate()
    else:
        # When creating an instance
        factory(**{field_name: value}, **factory_kwargs_copy)
        # When updating the value
        obj = factory(**factory_kwargs)
        try:
            setattr(obj, field_name, value)
        except FrozenAttributeError:
            pass
        else:
            obj.validate()


def check_field_not_nullable(
    factory: Callable[..., BaseEntity], field_name: str, **factory_kwargs: Any
) -> None:
    """Assert that an object cannot have a specific field set to ``None``.

    Parameters
    ----------
    factory : Callable[...,BaseEntity]
        The factory to use to create the object to test
    field_name : str
        The name of the field to check
    factory_kwargs : Any
        Any kwargs to pass to the factory to create the object

    Raises
    ------
    AssertionError
        If the field can be set to ``None`` while creating or updating an object (when calling
        ``validate`` in case of updating)

    """
    # When creating an instance
    factory_kwargs_copy = factory_kwargs.copy()
    factory_kwargs_copy.pop(field_name, None)
    with pytest.raises(TypeError):
        factory(**{field_name: None}, **factory_kwargs_copy)

    # When updating the value
    obj = factory(**factory_kwargs)
    try:
        setattr(obj, field_name, None)
    except FrozenAttributeError:
        pass
    else:
        with pytest.raises(TypeError):
            obj.validate()


def check_field_nullable(
    factory: Callable[..., BaseEntity], field_name: str, **factory_kwargs: Any
) -> None:
    """Assert that an object can have a specific field set to ``None``.

    Parameters
    ----------
    factory : Callable[...,BaseEntity]
        The factory to use to create the object to test
    field_name : str
        The name of the field to check
    factory_kwargs : Any
        Any kwargs to pass to the factory to create the object

    Raises
    ------
    AssertionError
        If the field cannot be set to ``None`` while creating or updating an object (when calling
        ``validate`` in case of updating)

    """
    # When creating an instance
    factory_kwargs_copy = factory_kwargs.copy()
    factory_kwargs_copy.pop(field_name, None)
    try:
        factory(**{field_name: None}, **factory_kwargs_copy)
    except TypeError:
        pytest.fail(f"DID RAISE {TypeError}")

    # When updating the value
    obj = factory(**factory_kwargs)
    try:
        setattr(obj, field_name, None)
    except FrozenAttributeError:
        pass
    else:
        try:
            obj.validate()
        except TypeError:
            pytest.fail(f"DID RAISE {TypeError}")

Changes

fix(entity): id changed from int to uuid4, renamed to identifier

Commit
Hash

79f704bde4575a9ddeb623d67d8965a62138adc9

Date

2020-10-05 10:51:49 +0200

Type

Modified

Stats

+7 -0

@@ -1,6 +1,7 @@
 """Validation helpers for BDD tests for isshub entities."""

 from typing import Any, Callable, List, Optional, Tuple, Type
+from uuid import UUID

 import pytest

@@ -29,6 +30,12 @@ positive_integer_only: ValuesValidation = integer_only + no_zero

 string_only: ValuesValidation = [("foo", None), (1, TypeError), (-0.1, TypeError)]

+uuid4_only: ValuesValidation = [
+    (UUID("19f49bc8-06e5-11eb-8465-bf44725d7bd3"), TypeError),
+    ("7298d61a-f08f-4f83-b75e-934e786eb43d", TypeError),
+    (UUID("7298d61a-f08f-4f83-b75e-934e786eb43d"), None),
+]
+

 def check_field(obj: BaseEntity, field_name: str) -> None:
     """Assert that the given `obj` has an attribute named `field_name`.

style(entity): Change the world “model” by “entity”

Commit
Hash

c2dd0fc606636dd72c1b55d30095bbeb622b788d

Date

2020-10-04 21:07:00 +0200

Type

Modified

Stats

+9 -9

@@ -6,7 +6,7 @@ import pytest

 from attr.exceptions import FrozenAttributeError

-from isshub.domain.utils.entity import BaseModel
+from isshub.domain.utils.entity import BaseEntity


 ValuesValidation = List[Tuple[Any, Optional[Type[Exception]]]]
@@ -30,12 +30,12 @@ positive_integer_only: ValuesValidation = integer_only + no_zero
 string_only: ValuesValidation = [("foo", None), (1, TypeError), (-0.1, TypeError)]


-def check_field(obj: BaseModel, field_name: str) -> None:
+def check_field(obj: BaseEntity, field_name: str) -> None:
     """Assert that the given `obj` has an attribute named `field_name`.

     Parameters
     ----------
-    obj : BaseModel
+    obj : BaseEntity
         The object to test
     field_name : str
         The field name to search for
@@ -50,7 +50,7 @@ def check_field(obj: BaseModel, field_name: str) -> None:


 def check_field_value(
-    factory: Callable[..., BaseModel],
+    factory: Callable[..., BaseEntity],
     field_name: str,
     value: Any,
     exception: Optional[Type[Exception]],
@@ -60,7 +60,7 @@ def check_field_value(

     Parameters
     ----------
-    factory : Callable[...,BaseModel]
+    factory : Callable[...,BaseEntity]
         The factory to use to create the object to test
     field_name : str
         The name of the field to check
@@ -109,13 +109,13 @@ def check_field_value(


 def check_field_not_nullable(
-    factory: Callable[..., BaseModel], field_name: str, **factory_kwargs: Any
+    factory: Callable[..., BaseEntity], field_name: str, **factory_kwargs: Any
 ) -> None:
     """Assert that an object cannot have a specific field set to ``None``.

     Parameters
     ----------
-    factory : Callable[...,BaseModel]
+    factory : Callable[...,BaseEntity]
         The factory to use to create the object to test
     field_name : str
         The name of the field to check
@@ -147,13 +147,13 @@ def check_field_not_nullable(


 def check_field_nullable(
-    factory: Callable[..., BaseModel], field_name: str, **factory_kwargs: Any
+    factory: Callable[..., BaseEntity], field_name: str, **factory_kwargs: Any
 ) -> None:
     """Assert that an object can have a specific field set to ``None``.

     Parameters
     ----------
-    factory : Callable[...,BaseModel]
+    factory : Callable[...,BaseEntity]
         The factory to use to create the object to test
     field_name : str
         The name of the field to check

fix(entities): Entities id field are frozen once set

Commit
Hash

ef8edc20b6a674bfc98c79028684279bcc9ed324

Date

2020-09-27 09:56:59 +0200

Type

Modified

Stats

+30 -12

@@ -4,6 +4,8 @@ from typing import Any, Callable, List, Optional, Tuple, Type

 import pytest

+from attr.exceptions import FrozenAttributeError
+
 from isshub.domain.utils.entity import BaseModel


@@ -86,16 +88,24 @@ def check_field_value(
             factory(**{field_name: value}, **factory_kwargs_copy)
         # When updating the value
         obj = factory(**factory_kwargs)
-        setattr(obj, field_name, value)
-        with pytest.raises(exception):
-            obj.validate()
+        try:
+            setattr(obj, field_name, value)
+        except FrozenAttributeError:
+            pass
+        else:
+            with pytest.raises(exception):
+                obj.validate()
     else:
         # When creating an instance
         factory(**{field_name: value}, **factory_kwargs_copy)
         # When updating the value
         obj = factory(**factory_kwargs)
-        setattr(obj, field_name, value)
-        obj.validate()
+        try:
+            setattr(obj, field_name, value)
+        except FrozenAttributeError:
+            pass
+        else:
+            obj.validate()


 def check_field_not_nullable(
@@ -127,9 +137,13 @@ def check_field_not_nullable(

     # When updating the value
     obj = factory(**factory_kwargs)
-    setattr(obj, field_name, None)
-    with pytest.raises(TypeError):
-        obj.validate()
+    try:
+        setattr(obj, field_name, None)
+    except FrozenAttributeError:
+        pass
+    else:
+        with pytest.raises(TypeError):
+            obj.validate()


 def check_field_nullable(
@@ -163,8 +177,12 @@ def check_field_nullable(

     # When updating the value
     obj = factory(**factory_kwargs)
-    setattr(obj, field_name, None)
     try:
-        obj.validate()
-    except TypeError:
-        pytest.fail(f"DID RAISE {TypeError}")
+        setattr(obj, field_name, None)
+    except FrozenAttributeError:
+        pass
+    else:
+        try:
+            obj.validate()
+        except TypeError:
+            pytest.fail(f"DID RAISE {TypeError}")

docs(examples): Add examples in functions docstring

Commit
Hash

74b050b5285baf8199876881b35345830944a806

Date

2020-09-26 12:03:01 +0200

Type

Modified

Stats

+18 -12

@@ -1,9 +1,8 @@
 """Validation helpers for BDD tests for isshub entities."""

-from typing import Any, List, Optional, Tuple, Type
+from typing import Any, Callable, List, Optional, Tuple, Type

 import pytest
-from factory import Factory

 from isshub.domain.utils.entity import BaseModel

@@ -49,7 +48,7 @@ def check_field(obj: BaseModel, field_name: str) -> None:


 def check_field_value(
-    factory: Type[Factory],
+    factory: Callable[..., BaseModel],
     field_name: str,
     value: Any,
     exception: Optional[Type[Exception]],
@@ -59,7 +58,7 @@ def check_field_value(

     Parameters
     ----------
-    factory : Type[Factory]
+    factory : Callable[...,BaseModel]
         The factory to use to create the object to test
     field_name : str
         The name of the field to check
@@ -78,10 +77,13 @@ def check_field_value(
         or if no exception is raised if `exception` is not ``None`` (or the wrong exception).

     """
+    factory_kwargs_copy = factory_kwargs.copy()
+    factory_kwargs_copy.pop(field_name, None)
+
     if exception:
         # When creating an instance
         with pytest.raises(exception):
-            factory(**{field_name: value}, **factory_kwargs)
+            factory(**{field_name: value}, **factory_kwargs_copy)
         # When updating the value
         obj = factory(**factory_kwargs)
         setattr(obj, field_name, value)
@@ -89,7 +91,7 @@ def check_field_value(
             obj.validate()
     else:
         # When creating an instance
-        factory(**{field_name: value}, **factory_kwargs)
+        factory(**{field_name: value}, **factory_kwargs_copy)
         # When updating the value
         obj = factory(**factory_kwargs)
         setattr(obj, field_name, value)
@@ -97,13 +99,13 @@ def check_field_value(


 def check_field_not_nullable(
-    factory: Type[Factory], field_name: str, **factory_kwargs: Any
+    factory: Callable[..., BaseModel], field_name: str, **factory_kwargs: Any
 ) -> None:
     """Assert that an object cannot have a specific field set to ``None``.

     Parameters
     ----------
-    factory : Type[Factory]
+    factory : Callable[...,BaseModel]
         The factory to use to create the object to test
     field_name : str
         The name of the field to check
@@ -118,8 +120,10 @@ def check_field_not_nullable(

     """
     # When creating an instance
+    factory_kwargs_copy = factory_kwargs.copy()
+    factory_kwargs_copy.pop(field_name, None)
     with pytest.raises(TypeError):
-        factory(**{field_name: None}, **factory_kwargs)
+        factory(**{field_name: None}, **factory_kwargs_copy)

     # When updating the value
     obj = factory(**factory_kwargs)
@@ -129,13 +133,13 @@ def check_field_not_nullable(


 def check_field_nullable(
-    factory: Type[Factory], field_name: str, **factory_kwargs: Any
+    factory: Callable[..., BaseModel], field_name: str, **factory_kwargs: Any
 ) -> None:
     """Assert that an object can have a specific field set to ``None``.

     Parameters
     ----------
-    factory : Type[Factory]
+    factory : Callable[...,BaseModel]
         The factory to use to create the object to test
     field_name : str
         The name of the field to check
@@ -150,8 +154,10 @@ def check_field_nullable(

     """
     # When creating an instance
+    factory_kwargs_copy = factory_kwargs.copy()
+    factory_kwargs_copy.pop(field_name, None)
     try:
-        factory(**{field_name: None}, **factory_kwargs)
+        factory(**{field_name: None}, **factory_kwargs_copy)
     except TypeError:
         pytest.fail(f"DID RAISE {TypeError}")

feat(namespace): Add Namespace entity in core domain context

Commit
Hash

3a2dd3cc6f6de1fa1b03db95eaa357628824f075

Date

2019-06-07 21:03:50 +0200

Type

Modified

Stats

+141 -1

@@ -1,7 +1,12 @@
-"""Validation helpers for BDD tests for isshub entity models."""
+"""Validation helpers for BDD tests for isshub entities."""

 from typing import Any, List, Optional, Tuple, Type

+import pytest
+from factory import Factory
+
+from isshub.domain.utils.entity import BaseModel
+

 ValuesValidation = List[Tuple[Any, Optional[Type[Exception]]]]

@@ -22,3 +27,138 @@ no_zero: ValuesValidation = [(0, ValueError)]
 positive_integer_only: ValuesValidation = integer_only + no_zero

 string_only: ValuesValidation = [("foo", None), (1, TypeError), (-0.1, TypeError)]
+
+
+def check_field(obj: BaseModel, field_name: str) -> None:
+    """Assert that the given `obj` has an attribute named `field_name`.
+
+    Parameters
+    ----------
+    obj : BaseModel
+        The object to test
+    field_name : str
+        The field name to search for
+
+    Raises
+    ------
+    AssertionError
+        If `obj` does not contain any attribute named `field_name`
+
+    """
+    assert hasattr(obj, field_name)
+
+
+def check_field_value(
+    factory: Type[Factory],
+    field_name: str,
+    value: Any,
+    exception: Optional[Type[Exception]],
+    **factory_kwargs: Any,
+) -> None:
+    """Assert that an object can or cannot have a specific value for a specific field.
+
+    Parameters
+    ----------
+    factory : Type[Factory]
+        The factory to use to create the object to test
+    field_name : str
+        The name of the field to check
+    value : Any
+        The value to set to the field
+    exception :  Optional[Type[Exception]]
+        The exception expected to be raised. If ``None``, no exception is expected to be raised.
+    factory_kwargs : Any
+        Any kwargs to pass to the factory to create the object
+
+    Raises
+    ------
+    AssertionError
+        If an exception is raised when setting the `value` to the field when creating or updating
+        an object (when calling ``validate`` in case of updating), and `exception` is ``None``,
+        or if no exception is raised if `exception` is not ``None`` (or the wrong exception).
+
+    """
+    if exception:
+        # When creating an instance
+        with pytest.raises(exception):
+            factory(**{field_name: value}, **factory_kwargs)
+        # When updating the value
+        obj = factory(**factory_kwargs)
+        setattr(obj, field_name, value)
+        with pytest.raises(exception):
+            obj.validate()
+    else:
+        # When creating an instance
+        factory(**{field_name: value}, **factory_kwargs)
+        # When updating the value
+        obj = factory(**factory_kwargs)
+        setattr(obj, field_name, value)
+        obj.validate()
+
+
+def check_field_not_nullable(
+    factory: Type[Factory], field_name: str, **factory_kwargs: Any
+) -> None:
+    """Assert that an object cannot have a specific field set to ``None``.
+
+    Parameters
+    ----------
+    factory : Type[Factory]
+        The factory to use to create the object to test
+    field_name : str
+        The name of the field to check
+    factory_kwargs : Any
+        Any kwargs to pass to the factory to create the object
+
+    Raises
+    ------
+    AssertionError
+        If the field can be set to ``None`` while creating or updating an object (when calling
+        ``validate`` in case of updating)
+
+    """
+    # When creating an instance
+    with pytest.raises(TypeError):
+        factory(**{field_name: None}, **factory_kwargs)
+
+    # When updating the value
+    obj = factory(**factory_kwargs)
+    setattr(obj, field_name, None)
+    with pytest.raises(TypeError):
+        obj.validate()
+
+
+def check_field_nullable(
+    factory: Type[Factory], field_name: str, **factory_kwargs: Any
+) -> None:
+    """Assert that an object can have a specific field set to ``None``.
+
+    Parameters
+    ----------
+    factory : Type[Factory]
+        The factory to use to create the object to test
+    field_name : str
+        The name of the field to check
+    factory_kwargs : Any
+        Any kwargs to pass to the factory to create the object
+
+    Raises
+    ------
+    AssertionError
+        If the field cannot be set to ``None`` while creating or updating an object (when calling
+        ``validate`` in case of updating)
+
+    """
+    # When creating an instance
+    try:
+        factory(**{field_name: None}, **factory_kwargs)
+    except TypeError:
+        pytest.fail(f"DID RAISE {TypeError}")
+
+    # When updating the value
+    obj = factory(**factory_kwargs)
+    setattr(obj, field_name, None)
+    try:
+        obj.validate()
+    except TypeError:
+        pytest.fail(f"DID RAISE {TypeError}")

feat(repository): Introduce entities validation (for Repository entity)

Commit
Hash

86ad505796b742a391684e2ef93695fdfb077abb

Date

2019-06-07 21:03:50 +0200

Type

Added

Stats

+24 -0

@@ -0,0 +1,24 @@
+"""Validation helpers for BDD tests for isshub entity models."""
+
+from typing import Any, List, Optional, Tuple, Type
+
+
+ValuesValidation = List[Tuple[Any, Optional[Type[Exception]]]]
+
+integer_only: ValuesValidation = [
+    ("foo", TypeError),
+    (-123, ValueError),
+    (-1.5, TypeError),
+    (-1, ValueError),
+    (-0.001, TypeError),
+    (0.001, TypeError),
+    (1, None),
+    (1.5, TypeError),
+    (123, None),
+]
+
+no_zero: ValuesValidation = [(0, ValueError)]
+
+positive_integer_only: ValuesValidation = integer_only + no_zero
+
+string_only: ValuesValidation = [("foo", None), (1, TypeError), (-0.1, TypeError)]