Skip to content

Commit

Permalink
Fix #1177
Browse files Browse the repository at this point in the history
  • Loading branch information
lk-geimfari committed Jan 25, 2022
1 parent b6516bf commit 5aab3f4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
5 changes: 2 additions & 3 deletions mimesis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
Repository: https://github.com/lk-geimfari/mimesis
"""

from mimesis.schema import Field, Schema

from .providers import (
from mimesis.providers import (
Address,
BaseDataProvider,
BaseProvider,
Expand All @@ -32,6 +30,7 @@
Text,
Transport,
)
from mimesis.schema import Field, Schema

__all__ = [
"Address",
Expand Down
3 changes: 1 addition & 2 deletions mimesis/providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
from mimesis.providers.transport import Transport

__all__ = [
# The main class:
# Base classes:
"BaseProvider",
# Data base class:
"BaseDataProvider",
# Data providers:
"Address",
Expand Down
17 changes: 14 additions & 3 deletions mimesis/providers/internet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import urllib.parse
import urllib.request
from ipaddress import IPv4Address, IPv6Address
from typing import Any, Dict, Final, List, Optional, Union
from typing import Any, Dict, Final, List, Optional, Set, Union

from mimesis.data import (
EMOJI,
Expand Down Expand Up @@ -321,14 +321,25 @@ def query_string(self, length: Optional[int] = None) -> str:
def query_parameters(self, length: Optional[int] = None) -> Dict[str, str]:
"""Generate arbitrary query parameters as a dict.
:param length: Length of dictionary (key/value pair).
:param length: Length of query parameters dictionary (maximum is 32).
:return: Dict of query parameters.
"""

def pick_unique_words(quantity: int = 5) -> List[str]:
words: Set[str] = set()

while len(words) != quantity:
words.add(self.text.word())

return list(words)

if not length:
length = self.random.randint(1, 10)

return dict(zip(self.text.words(length), self.text.words(length)))
if length > 32:
raise ValueError("Maximum allowed length of query parameters is 32.")

return dict(zip(pick_unique_words(length), self.text.words(length)))

def top_level_domain(self, tld_type: Optional[TLDType] = None) -> str:
"""Generates random top level domain.
Expand Down
32 changes: 18 additions & 14 deletions tests/test_providers/test_internet.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,29 @@ def test_uri_with_parameters(
assert uri.split(":")[0].strip() == scheme.value
assert validators.url(uri)

@pytest.mark.parametrize("length", [4, 6, 8, None])
@pytest.mark.parametrize("length", [4, 6, 8])
def test_query_string(self, net, length):
query_string = net.query_string(length)
query_params_count = len(query_string.split("&"))
assert len(net.query_string(length).split("&")) == length

if length is None:
assert 1 <= query_params_count <= 10
else:
assert query_params_count == length
def test_query_string_raise_error_on_invalid_length(self, net):
with pytest.raises(ValueError):
net.query_string(33)

def test_query_string_with_length_of_none(self, net):
query_params_count = len(net.query_string().split("&"))
assert 1 <= query_params_count <= 10

@pytest.mark.parametrize("length", [4, 6, 8, None])
@pytest.mark.parametrize("length", [4, 6, 8])
def test_query_parameters(self, net, length):
query_dict = net.query_parameters(length)
query_params_count = len(query_dict)
assert len(net.query_parameters(length)) == length

def test_query_parameters_raise_error_on_invalid_length(self, net):
with pytest.raises(ValueError):
net.query_parameters(33)

if length is None:
assert 1 <= query_params_count <= 10
else:
assert query_params_count == length
def test_query_parameters_with_length_of_none(self, net):
query_params_count = len(net.query_parameters())
assert 1 <= query_params_count <= 10

def test_slug(self, net):
with pytest.raises(ValueError):
Expand Down

0 comments on commit 5aab3f4

Please sign in to comment.