This repository has been archived by the owner on May 12, 2022. It is now read-only.
forked from treyhunner/names
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_names.py
executable file
·142 lines (117 loc) · 5.05 KB
/
test_names.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
136
137
138
139
140
141
142
#!/usr/bin/env python
import unittest
import sys
from os.path import abspath, join, dirname
try:
from StringIO import StringIO
except:
from io import StringIO
from collections import defaultdict
import names
from names.main import main
full_path = lambda filename: abspath(join(dirname(__file__), filename))
class patch_stdout(object):
def __init__(self):
self.stdout = StringIO()
self.real_stdout = sys.stdout
def __enter__(self):
sys.stdout = self.stdout
return sys.stdout
def __exit__(self, exc_type, exc_value, traceback):
sys.stdout.close()
sys.stdout = self.real_stdout
class patch_file:
def __init__(self, file_dict):
self.files = file_dict
def __enter__(self):
self.old_files = names.FILES
names.FILES = self.files
names.CACHED_FILES = {}
def __exit__(self, type, value, traceback):
names.FILES = self.old_files
names.CACHED_FILES = {}
test_files = {
'first:male': full_path('test/male.txt'),
'first:female': full_path('test/female.txt'),
'last': full_path('test/last.txt'),
}
class NamesTest(unittest.TestCase):
def test_get_name(self):
counts = defaultdict(int)
rounds = 5000.0
test_file = full_path('test/file1.txt')
for i in range(1, int(rounds)):
counts[names.get_name(test_file)] += 1
self.assertAlmostEqual(counts['Test1'] / rounds, 0.333, delta=0.05)
self.assertAlmostEqual(counts['Test2'] / rounds, 0.277, delta=0.05)
self.assertAlmostEqual(counts['Test3'] / rounds, 0.222, delta=0.05)
self.assertAlmostEqual(counts['Test4'] / rounds, 0.166, delta=0.05)
self.assertEqual(counts['Test5'] / rounds, 0)
def test_get_name_cached(self):
counts = defaultdict(int)
rounds = 5000.0
test_file = full_path('test/file1.txt')
for i in range(1, int(rounds)):
counts[names.get_name(test_file, True)] += 1
self.assertAlmostEqual(counts['Test1'] / rounds, 0.333, delta=0.05)
self.assertAlmostEqual(counts['Test2'] / rounds, 0.277, delta=0.05)
self.assertAlmostEqual(counts['Test3'] / rounds, 0.222, delta=0.05)
self.assertAlmostEqual(counts['Test4'] / rounds, 0.166, delta=0.05)
self.assertEqual(counts['Test5'] / rounds, 0)
def test_random_gender(self):
counts = defaultdict(int)
rounds = 5000.0
with patch_file(test_files):
for i in range(int(rounds)):
name = names.get_first_name()
counts[name] += 1
self.assertAlmostEqual(counts['Male'] / rounds, 0.500, delta=0.05)
self.assertAlmostEqual(counts['Female'] / rounds, 0.500, delta=0.05)
def test_random_gender_cached(self):
counts = defaultdict(int)
rounds = 5000.0
with patch_file(test_files):
for i in range(int(rounds)):
name = names.get_first_name(cached=True)
counts[name] += 1
self.assertAlmostEqual(counts['Male'] / rounds, 0.500, delta=0.05)
self.assertAlmostEqual(counts['Female'] / rounds, 0.500, delta=0.05)
def test_correct_files(self):
with patch_file(test_files):
self.assertEqual(names.get_first_name(gender='male'), "Male")
self.assertEqual(names.get_first_name(gender='female'), "Female")
self.assertEqual(names.get_last_name(), "Last")
# Test with cached responses
self.assertEqual(names.get_first_name(gender='male',
cached=True), "Male")
self.assertEqual(names.get_first_name(gender='female',
cached=True), "Female")
self.assertEqual(names.get_last_name(True), "Last")
def test_empty_file(self):
empty_files = {
'first:male': full_path('test/empty.txt'),
'first:female': full_path('test/empty.txt'),
'last': full_path('test/empty.txt'),
}
with patch_file(empty_files):
self.assertEqual(names.get_first_name(gender='male'), "")
self.assertEqual(names.get_first_name(gender='female'), "")
self.assertEqual(names.get_last_name(), "")
# Test with cached responses
self.assertEqual(names.get_first_name(gender='male',
cached=True), "")
self.assertEqual(names.get_first_name(gender='female',
cached=True), "")
self.assertEqual(names.get_last_name(True), "")
def test_only_male_and_female_gender_are_supported(self):
with self.assertRaises(ValueError):
names.get_first_name(gender='other')
class CommandLineTest(unittest.TestCase):
def test_cli(self):
with patch_stdout() as stdout:
with patch_file(test_files):
main()
self.assertIn(stdout.getvalue(),
["Male Last\n", "Female Last\n"])
if __name__ == '__main__':
unittest.main()