Skip to content

Commit

Permalink
Develop (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
lk-geimfari committed Aug 16, 2020
1 parent 3f8819d commit 8f2f259
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 35 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Version 4.1.1
-------------

**Fix**:

- Fixed issue with non-unique uuid

Version 4.1.0
-------------

Expand All @@ -24,6 +31,7 @@ Version 4.1.0
**Removed**:

- Removed the deprecated ``download_image()`` function from the ``shortcuts`` module, use your own custom downloader instead.
- Removed parameter ``version`` for method ``Cryptographic().uuid()``

Version 4.0.0
-------------
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.1.0'
__version__ = '4.1.1'
__title__ = 'mimesis'
__description__ = 'Mimesis: fake data generator.'
__url__ = 'https://github.com/lk-geimfari/mimesis'
Expand Down
22 changes: 10 additions & 12 deletions mimesis/providers/cryptographic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import hashlib
import secrets
from typing import Optional, Union
from uuid import UUID
from uuid import UUID, uuid4

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

name = 'cryptographic'

def uuid(self, version: int = None,
as_object: bool = False) -> Union[UUID, str]:
"""Generate random UUID.
@staticmethod
def uuid(as_object: bool = False) -> Union[UUID, str]:
"""Generate random UUID4.
This method returns string by default,
but yoy can make it return uuid.UUID object using
but you 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.
:param as_object: Returns uuid.UUID.
:return: UUID.
"""
bits = self.random.getrandbits(128)
uuid_obj = UUID(int=bits, version=version)
_uuid = uuid4()

if not as_object:
return str(uuid_obj)
return str(_uuid)

return uuid_obj
return _uuid

def hash(self, algorithm: Algorithm = None) -> str: # noqa: A003
"""Generate random hash.
Expand All @@ -64,7 +62,7 @@ def hash(self, algorithm: Algorithm = None) -> str: # noqa: A003

if hasattr(hashlib, key):
fn = getattr(hashlib, key)
return fn(self.uuid().encode()).hexdigest()
return fn(self.uuid().encode()).hexdigest() # type: ignore

@staticmethod
def token_bytes(entropy: int = 32) -> bytes:
Expand Down
31 changes: 11 additions & 20 deletions tests/test_providers/test_cryptographic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,17 @@ def test_str(self, crypto):
assert re.match(patterns.PROVIDER_STR_REGEX, str(crypto))

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

@pytest.mark.parametrize(
'algorithm, length', [
Expand Down Expand Up @@ -106,11 +97,11 @@ def c2(self, seed):
return Cryptographic(seed=seed)

def test_uuid(self, c1, c2):
assert c1.uuid() == c2.uuid()
assert c1.uuid() != c2.uuid()

def test_hash(self, c1, c2):
assert c1.hash() == c2.hash()
assert c1.hash(algorithm=Algorithm.SHA512) == \
assert c1.hash() != c2.hash()
assert c1.hash(algorithm=Algorithm.SHA512) != \
c2.hash(algorithm=Algorithm.SHA512)

def test_mnemonic_phrase(self, c1, c2):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_providers/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def test_generic_code(self, g1, g2):
assert g1.code.issn() == g2.code.issn()

def test_generic_cryptographic(self, g1, g2):
assert g1.cryptographic.uuid() == g2.cryptographic.uuid()
assert g1.cryptographic.hash() == g2.cryptographic.hash()
assert g1.cryptographic.uuid() != g2.cryptographic.uuid()
assert g1.cryptographic.hash() != g2.cryptographic.hash()

def test_generic_datetime(self, g1, g2):
assert g1.datetime.week_date() == g2.datetime.week_date()
Expand Down

0 comments on commit 8f2f259

Please sign in to comment.