forked from lk-geimfari/mimesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
executable file
·131 lines (101 loc) · 3.02 KB
/
test_utils.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
# -*- coding: utf-8 -*-
import os
import socket
import sys
import pytest
from mimesis.exceptions import UnsupportedLocale, UnexpectedGender
from mimesis.utils import (check_gender, download_image, locale_info,
luhn_checksum, pull, update_dict)
def is_connected():
try:
host = socket.gethostbyname('https://github.com/')
socket.create_connection((host, 80), 2)
return True
except:
pass
return False
def test_luhn_checksum():
assert luhn_checksum('7992739871') == '3'
def test_pull():
data = pull('personal.json', 'en')
assert data['views_on'] is not None
assert isinstance(data['views_on'], list)
with pytest.raises(UnsupportedLocale):
pull('personal.json', 'w')
with pytest.raises(FileNotFoundError):
pull('something.json', 'en')
data = pull('address.json', 'en-gb')
assert 'city' in data
assert 'Aberystwyth' in data['city']
assert 'Addison' not in data['city']
data = pull('address.json', 'en-au')
assert 'city' in data
assert 'Melbourne' in data['city']
def test_download_image():
result = download_image(url=None)
assert result is None
url = 'https://github.com/lk-geimfari/mimesis/' \
'raw/master/media/mimesis.png'
if is_connected():
verified = download_image(url=url)
assert verified == 'mimesis.png'
os.remove(verified)
if sys.version_info.minor <= 3:
with pytest.raises(NotImplementedError):
download_image(url=None, unverified_ctx=True)
else:
unverified = download_image(url=None, unverified_ctx=True)
assert unverified is None
def test_locale_information():
result = locale_info(locale='ru')
assert result == 'Russian'
result_1 = locale_info(locale='is')
assert result_1 == 'Icelandic'
with pytest.raises(UnsupportedLocale):
locale_info(locale='w')
def test_update_dict():
first = {
'animals': {
'dogs': [
'spaniel',
],
},
}
second = {
'animals': {
'cats': [
'maine coon',
],
},
}
result = update_dict(first, second)
assert 'cats' in result['animals']
assert 'dogs' in result['animals']
third = {
'animals': {
'dogs': [
'golden retriever',
],
},
}
result = update_dict(first, third)
assert 'spaniel' not in result['animals']['dogs']
@pytest.mark.parametrize(
'abbr, gender',
[
('0', ('male', 'female')),
('9', ('male', 'female')),
('1', 'male'),
('2', 'female'),
('f', 'female'),
('female', 'female'),
('m', 'male'),
('male', 'male'),
],
)
def test_check_gender(abbr, gender):
result = check_gender(abbr)
assert result == gender or result in gender
def test_check_gender_invalid_gender():
with pytest.raises(UnexpectedGender):
check_gender(gender='other')