forked from lk-geimfari/mimesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_random.py
135 lines (105 loc) · 2.91 KB
/
test_random.py
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
import pytest
from mimesis.enums import Gender
from mimesis.random import get_random_item
from mimesis.random import random as _random
@pytest.fixture
def random():
return _random
def test_randints(random):
result = random.randints()
# Length of default list is 3
assert len(result) == 3
result = random.randints(25, 1, 1)
assert len(result) == 25
# All elements in result_custom equals to 1.
assert result[0] == 1 and result[-1] == 1
with pytest.raises(ValueError):
random.randints(amount=0)
@pytest.mark.parametrize(
'size', (8, 16, 32, 64),
)
def test_urandom(random, size):
result = random.urandom(size=size)
assert len(result) == size
assert isinstance(result, bytes)
@pytest.mark.parametrize(
'str_seq, length', [
('U', 10),
('A', 20),
],
)
def test_generate_string(random, str_seq, length):
result = random.generate_string(str_seq, length)
assert len(result) == length
@pytest.mark.parametrize(
'precision', [
4,
6,
8,
],
)
def test_uniform(random, precision):
result = random.uniform(2.3, 10.5, precision)
assert isinstance(result, float)
result = str(result).split('.')[1]
assert precision >= len(result)
@pytest.mark.parametrize(
'mask, digit, char', [
('##-FA-@@', '#', '@'),
('**-AF-$$', '*', '$'),
('**-š好-$$', '*', '$'),
],
)
def test_custom_code(random, mask, digit, char):
result = random.custom_code(mask=mask, char=char, digit=digit)
digit, middle, char = result.split('-')
_, middle_mask, _ = mask.split('-')
assert char.isalpha()
assert digit.isdigit()
assert middle == middle_mask
@pytest.mark.parametrize(
'mask, digit, char', [
('??-FF-??', '?', '?'),
('@@-FF-@@', '@', '@'),
],
)
def test_custom_code_with_same_placeholders(random, mask, digit, char):
with pytest.raises(ValueError):
random.custom_code(mask=mask, char=char, digit=digit)
@pytest.mark.parametrize(
'seed, expected', [
(32, 'C239'),
(0xff, 'B670'),
('👽', 'B806'),
],
)
def test_custom_code_with_seed(random, seed, expected):
random.seed(seed)
assert random.custom_code() == expected
def test_get_random_item(random):
result = get_random_item(Gender)
assert result in Gender
random.seed(0xf)
result_1 = get_random_item(Gender, rnd=random)
result_2 = get_random_item(Gender, rnd=random)
assert result_1 == result_2
@pytest.mark.parametrize(
'length', [
64,
128,
256,
],
)
def test_randstr(random, length):
result = random.randstr(length=length)
assert len(result) == length
@pytest.mark.parametrize(
'count', [
1000,
10000,
100000,
],
)
def test_randstr_unique(random, count):
results = [random.randstr(unique=True) for _ in range(count)]
assert len(results) == len(set(results))