-
Notifications
You must be signed in to change notification settings - Fork 16
/
test.py
121 lines (105 loc) · 3.11 KB
/
test.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
# noinspection PyProtectedMember
from genderize import Genderize, GenderizeException, _chunked
def test_integration():
"""
Integration test from the readme. Calls the real Genderize.io API server.
"""
expected_genders = {
'James': 'male',
'Eva': 'female',
'Thunderhorse': None,
}
actual_genders = dict((elem['name'], elem['gender'])
for elem in Genderize().get(expected_genders.keys()))
assert expected_genders == actual_genders,\
"Expected {0}, got {1}".format(expected_genders, actual_genders)
def test_integration_single():
"""
Retrieve a single name.
"""
expected = 'male'
actual = Genderize().get1('Peter')['gender']
assert expected == actual,\
"Expected {0}, got {1}".format(expected, actual)
def test_invalid_api_key():
"""
Calls the API server with an invalid API key.
Should result in an exception.
"""
caught = False
try:
Genderize(api_key='invalid_api_key').get1('Peter')
except GenderizeException:
caught = True
assert caught, "Expected a GenderizeException to be thrown"
def test_with_headers():
"""
Retrieve a single name with response headers.
"""
response = Genderize().get(['Peter'], retheader=True)
assert response['data'][0]['name'] == 'Peter',\
"Expected name data to be returned"
assert response['headers'], "Expected response headers to be returned"
expected_headers = [
'X-Rate-Limit-Limit',
'X-Rate-Limit-Remaining',
'X-Rate-Reset',
]
for header in expected_headers:
assert header in response['headers'],\
"Expected {0} header to be returned".format(header)
def test_chunked_full_blocks():
"""
Test chunking when the input length is a multiple of the block length.
"""
chunks = list(_chunked("abcd", 2))
assert len(chunks) == 2
assert len(chunks[0]) == 2
assert len(chunks[1]) == 2
def test_chunked_uneven_blocks():
"""
Test chunking when the input length isn't a multiple of the block length.
"""
chunks = list(_chunked("abcde", 2))
assert len(chunks) == 3
assert len(chunks[0]) == 2
assert len(chunks[1]) == 2
assert len(chunks[2]) == 1
def test_chunked_empty():
"""
Test chunking when the input is empty.
"""
chunks = list(_chunked("", 2))
assert len(chunks) == 0
def test_more_than_10_names():
"""
Retrieve 20 names, which requires multiple HTTP requests
now that the API has a 10-name limit.
"""
names = [
"Emma",
"Olivia",
"Ava",
"Isabella",
"Sophia",
"Mia",
"Charlotte",
"Amelia",
"Evelyn",
"Abigail",
"Liam",
"Noah",
"William",
"James",
"Logan",
"Benjamin",
"Mason",
"Elijah",
"Oliver",
"Jacob",
]
response = Genderize().get(names)
assert len(names) == len(response)
for name, namedata in zip(names, response):
assert name == namedata['name'],\
'Expected names to be returned in same order'