Skip to content

Commit

Permalink
Fix #918 (#922)
Browse files Browse the repository at this point in the history
* Fix code duplication in docs

* Add version testing for Cryptographic.uuid

* Fix #918

* Change version in __init__.py
  • Loading branch information
lk-geimfari committed Aug 16, 2020
1 parent eecd329 commit 3f8819d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 5.0.0
Version 4.1.0
-------------

.. warning:: Work still in progress.
Expand All @@ -8,6 +8,7 @@ Version 5.0.0
- Added method ``manufacturer()`` for class ``Transport()``
- Added ``sk`` (Slovak) locale support
- Added new parameter ``unique`` for method ``Person().email()``
- Added new parameter ``as_object`` for method ``Cryptographic().uuid()``

**Updated**:

Expand Down
2 changes: 1 addition & 1 deletion mimesis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
'__license__',
]

__version__ = '4.0.0'
__version__ = '4.1.0'
__title__ = 'mimesis'
__description__ = 'Mimesis: fake data generator.'
__url__ = 'https://github.com/lk-geimfari/mimesis'
Expand Down
21 changes: 16 additions & 5 deletions mimesis/providers/cryptographic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import hashlib
import secrets
import uuid
from typing import Optional
from typing import Optional, Union
from uuid import UUID

from mimesis.enums import Algorithm
from mimesis.providers.base import BaseProvider
Expand All @@ -30,14 +30,25 @@ class Meta:

name = 'cryptographic'

def uuid(self, version: int = None) -> str:
def uuid(self, version: int = None,
as_object: bool = False) -> Union[UUID, str]:
"""Generate random UUID.
This method returns string by default,
but yoy can make it return uuid.UUID object using
parameter **as_object**
:param as_object: Returns uuid.UUID object instead of string.
:param version: UUID version.
:return: UUID
:return: UUID.
"""
bits = self.random.getrandbits(128)
return str(uuid.UUID(int=bits, version=version))
uuid_obj = UUID(int=bits, version=version)

if not as_object:
return str(uuid_obj)

return uuid_obj

def hash(self, algorithm: Algorithm = None) -> str: # noqa: A003
"""Generate random hash.
Expand Down
24 changes: 22 additions & 2 deletions tests/test_providers/test_cryptographic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import uuid

import pytest

Expand All @@ -18,8 +19,27 @@ def crypto(self):
def test_str(self, crypto):
assert re.match(patterns.PROVIDER_STR_REGEX, str(crypto))

def test_uuid(self, crypto):
assert re.match(patterns.UUID_REGEX, crypto.uuid())
@pytest.mark.parametrize(
'version, as_object', [
(0, False),
(1, True),
(2, False),
(3, True),
(4, True),
(5, False),
(6, False),
],
)
def test_uuid(self, crypto, version, as_object):
if 1 <= version <= 5:
uuid_result = crypto.uuid(version, as_object=as_object)
if as_object:
assert isinstance(uuid_result, uuid.UUID)
else:
assert re.match(patterns.UUID_REGEX, crypto.uuid(version))
else:
with pytest.raises(ValueError):
re.match(patterns.UUID_REGEX, crypto.uuid(version))

@pytest.mark.parametrize(
'algorithm, length', [
Expand Down

0 comments on commit 3f8819d

Please sign in to comment.