Skip to content

Commit 7768bad

Browse files
committed
feat: implement UUID v4 and String UUID v4 mothers
Signed-off-by: Adria Montoto <75563346+adriamontoto@users.noreply.github.com>
1 parent 04cc217 commit 7768bad

File tree

13 files changed

+286
-22
lines changed

13 files changed

+286
-22
lines changed

object_mother_pattern/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
StringDateMother,
1212
StringDatetimeMother,
1313
StringMother,
14-
StringUuidMother,
15-
UuidMother,
1614
)
1715

1816
__all__ = (
@@ -26,6 +24,4 @@
2624
'StringDateMother',
2725
'StringDatetimeMother',
2826
'StringMother',
29-
'StringUuidMother',
30-
'UuidMother',
3127
)

object_mother_pattern/mothers/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from enum import StrEnum, unique
22

33
from .dates import DateMother, DatetimeMother, StringDateMother, StringDatetimeMother
4-
from .identifiers import StringUuidMother, UuidMother
54
from .primitives import BooleanMother, BytesMother, FloatMother, IntegerMother, StringMother
65

76

@@ -27,6 +26,4 @@ class StringCase(StrEnum):
2726
'StringDateMother',
2827
'StringDatetimeMother',
2928
'StringMother',
30-
'StringUuidMother',
31-
'UuidMother',
3229
)
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from .string_uuid_mother import StringUuidMother
2-
from .uuid_mother import UuidMother
1+
from .uuid import StringUuidMother, StringUuidV4Mother, UuidMother, UuidV4Mother
32

43
__all__ = (
54
'StringUuidMother',
5+
'StringUuidV4Mother',
66
'UuidMother',
7+
'UuidV4Mother',
78
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from .string_uuid_mother import StringUuidMother
2+
from .string_uuid_v4_mother import StringUuidV4Mother
3+
from .uuid_mother import UuidMother
4+
from .uuid_v4_mother import UuidV4Mother
5+
6+
__all__ = (
7+
'StringUuidMother',
8+
'StringUuidV4Mother',
9+
'UuidMother',
10+
'UuidV4Mother',
11+
)

object_mother_pattern/mothers/identifiers/string_uuid_mother.py renamed to object_mother_pattern/mothers/identifiers/uuid/string_uuid_mother.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
else:
1010
from typing_extensions import override # pragma: no cover
1111

12-
from uuid import uuid4
12+
from random import choice
1313

1414
from object_mother_pattern.models import BaseMother
15-
from object_mother_pattern.mothers.primitives.string_mother import StringMother
15+
from object_mother_pattern.mothers.primitives import StringMother
16+
17+
from .string_uuid_v4_mother import StringUuidV4Mother
1618

1719

1820
class StringUuidMother(BaseMother[str]):
@@ -21,7 +23,7 @@ class StringUuidMother(BaseMother[str]):
2123
2224
Example:
2325
```python
24-
from object_mother_pattern import StringUuidMother
26+
from object_mother_pattern.mothers.identifiers import StringUuidMother
2527
2628
uuid = StringUuidMother.create()
2729
print(uuid)
@@ -43,11 +45,11 @@ def create(cls, *, value: str | None = None) -> str:
4345
TypeError: If the provided `value` is not a string.
4446
4547
Returns:
46-
str: A random string universally unique identifier value.
48+
str: A random string universally unique identifier value (UUID1, UUID2, UUID3, UUID4, or UUID5).
4749
4850
Example:
4951
```python
50-
from object_mother_pattern import StringUuidMother
52+
from object_mother_pattern.mothers.identifiers import StringUuidMother
5153
5254
uuid = StringUuidMother.create()
5355
print(uuid)
@@ -60,7 +62,11 @@ def create(cls, *, value: str | None = None) -> str:
6062

6163
return value
6264

63-
return str(object=uuid4())
65+
uuid_generators = [
66+
StringUuidV4Mother.create,
67+
]
68+
69+
return choice(seq=uuid_generators)() # noqa: S311
6470

6571
@classmethod
6672
def invalid_value(cls) -> str:
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""
2+
StringUuidV4Mother module.
3+
"""
4+
5+
from sys import version_info
6+
7+
if version_info >= (3, 12):
8+
from typing import override # pragma: no cover
9+
else:
10+
from typing_extensions import override # pragma: no cover
11+
12+
from uuid import uuid4
13+
14+
from object_mother_pattern.models import BaseMother
15+
from object_mother_pattern.mothers.primitives import StringMother
16+
17+
18+
class StringUuidV4Mother(BaseMother[str]):
19+
"""
20+
StringUuidV4Mother class is responsible for creating random string UUID4 values.
21+
22+
Example:
23+
```python
24+
from object_mother_pattern.mothers.identifiers import StringUuidV4Mother
25+
26+
uuid = StringUuidV4Mother.create()
27+
print(uuid)
28+
# >>> 3e9e0f3a-64a3-474f-9127-368e723f389f
29+
```
30+
"""
31+
32+
@classmethod
33+
@override
34+
def create(cls, *, value: str | None = None) -> str:
35+
"""
36+
Create a random string UUID value. If a specific string UUID value is provided via `value`, it is returned after
37+
validation. Otherwise, the method generates a random string UUID.
38+
39+
Args:
40+
value (str | None, optional): Specific value to return. Defaults to None.
41+
42+
Raises:
43+
TypeError: If the provided `value` is not a string.
44+
45+
Returns:
46+
str: A random string universally unique identifier value.
47+
48+
Example:
49+
```python
50+
from object_mother_pattern.mothers.identifiers import StringUuidV4Mother
51+
52+
uuid = StringUuidV4Mother.create()
53+
print(uuid)
54+
# >>> 3e9e0f3a-64a3-474f-9127-368e723f389f
55+
```
56+
"""
57+
if value is not None:
58+
if type(value) is not str:
59+
raise TypeError('StringUuidV4Mother value must be a string.')
60+
61+
return value
62+
63+
return str(object=uuid4())
64+
65+
@classmethod
66+
def invalid_value(cls) -> str:
67+
"""
68+
Create an invalid string value.
69+
70+
Returns:
71+
str: Invalid string.
72+
"""
73+
return StringMother.invalid_value()

object_mother_pattern/mothers/identifiers/uuid_mother.py renamed to object_mother_pattern/mothers/identifiers/uuid/uuid_mother.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
else:
1010
from typing_extensions import override # pragma: no cover
1111

12-
from uuid import UUID, uuid4
12+
from random import choice
13+
from uuid import UUID
1314

1415
from object_mother_pattern.models import BaseMother
1516

17+
from .uuid_v4_mother import UuidV4Mother
18+
1619

1720
class UuidMother(BaseMother[UUID]):
1821
"""
@@ -32,7 +35,7 @@ class UuidMother(BaseMother[UUID]):
3235
@override
3336
def create(cls, *, value: UUID | None = None) -> UUID:
3437
"""
35-
Create a random UUID value. If a specific UUID value is provided via `value`, it is returned after validation.
38+
Create a random UUID value. If a specific UUID value is provided via `value`, it is returned after validation.\
3639
Otherwise, the method generates a random UUID.
3740
3841
Args:
@@ -46,7 +49,7 @@ def create(cls, *, value: UUID | None = None) -> UUID:
4649
4750
Example:
4851
```python
49-
from object_mother_pattern import UuidMother
52+
from object_mother_pattern.mothers.identifiers import UuidMother
5053
5154
uuid = UuidMother.create()
5255
print(uuid)
@@ -59,4 +62,8 @@ def create(cls, *, value: UUID | None = None) -> UUID:
5962

6063
return value
6164

62-
return uuid4()
65+
uuid_generators = [
66+
UuidV4Mother.create,
67+
]
68+
69+
return choice(seq=uuid_generators)() # noqa: S311
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
UuidV4Mother module.
3+
"""
4+
5+
from sys import version_info
6+
7+
if version_info >= (3, 12):
8+
from typing import override # pragma: no cover
9+
else:
10+
from typing_extensions import override # pragma: no cover
11+
12+
from uuid import UUID, uuid4
13+
14+
from object_mother_pattern.models import BaseMother
15+
16+
17+
class UuidV4Mother(BaseMother[UUID]):
18+
"""
19+
UuidV4Mother class is responsible for creating random UUID4 (random) values.
20+
21+
Example:
22+
```python
23+
from object_mother_pattern.mothers.identifiers import UuidMother
24+
25+
uuid = UuidMother.create()
26+
print(uuid)
27+
# >>> 3e9e0f3a-64a3-474f-9127-368e723f389f
28+
```
29+
"""
30+
31+
@classmethod
32+
@override
33+
def create(cls, *, value: UUID | None = None) -> UUID:
34+
"""
35+
Create a random UUID4 value. If a specific UUID value is provided via `value`, it is returned after validation.
36+
Otherwise, the method generates a random UUID4.
37+
38+
Args:
39+
value (UUID | None, optional): Specific value to return. Defaults to None.
40+
41+
Raises:
42+
TypeError: If the provided `value` is not a UUID.
43+
44+
Returns:
45+
UUID: A random UUID4 (random) value.
46+
47+
Example:
48+
```python
49+
from object_mother_pattern.mothers.identifiers import UuidMother
50+
51+
uuid = UuidMother.create()
52+
print(uuid)
53+
# >>> 3e9e0f3a-64a3-474f-9127-368e723f389f
54+
```
55+
"""
56+
if value is not None:
57+
if type(value) is not UUID:
58+
raise TypeError('UuidV4Mother value must be a UUID.')
59+
60+
return value
61+
62+
return uuid4()

tests/mothers/identifiers/uuid/__init__.py

Whitespace-only changes.

tests/mothers/identifiers/test_string_uuid_mother.py renamed to tests/mothers/identifiers/uuid/test_string_uuid_mother.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from pytest import mark, raises as assert_raises
88

9-
from object_mother_pattern.mothers import StringUuidMother
9+
from object_mother_pattern.mothers.identifiers import StringUuidMother
1010

1111

1212
@mark.unit_testing
@@ -56,6 +56,6 @@ def test_string_uuid_mother_invalid_value_type() -> None:
5656
"""
5757
with assert_raises(
5858
expected_exception=TypeError,
59-
match='StringUuidMother value must be a string.',
59+
match=r'StringUuidMother value must be a string.',
6060
):
6161
StringUuidMother.create(value=StringUuidMother.invalid_type())

0 commit comments

Comments
 (0)