-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_person.py
250 lines (194 loc) · 11.2 KB
/
test_person.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 13 08:09:10 2017
@author: ismailnabeel
"""
""" Test Person
:Author: Arthur Goldberg <Arthur.Goldberg@mssm.edu>
:Date: 2017-12-09
:Copyright: 2017, Arthur Goldberg
:License: MIT
"""
import unittest
from person import Person, Gender, PersonError
class TestGender(unittest.TestCase):
def test_gender(self):
self.assertEqual(Gender().get_gender('Male'), Gender.MALE)
self.assertEqual(Gender().get_gender('female'), Gender.FEMALE)
self.assertEqual(Gender().get_gender('FEMALE'), Gender.FEMALE)
self.assertEqual(Gender().get_gender('NA'), Gender.UNKNOWN)
with self.assertRaises(PersonError) as context:
Gender().get_gender('---')
self.assertIn('Illegal gender', str(context.exception))
def test_genders_string_mapping(self):
'''Testing genders_string_mapping'''
rv = 'Legal genders, which are case insensitive, map to gender constants'
self.assertIn(rv, Gender().genders_string_mappings())
self.assertIn('-> M', Gender().genders_string_mappings())
self.assertIn('-> unknown', Gender().genders_string_mappings())
self.assertIn('-> F', Gender().genders_string_mappings())
self.assertIn('-> F',Gender().genders_string_mappings())
class TestPerson(unittest.TestCase):
def setUp(self):
# create a Person
self.child = Person('kid', 'NA')
self.mom = Person('mom', 'f')
self.dad = Person('dad', 'm')
# make a deep family history
self.generations = 4
self.people = people = []
self.root_child = Person('root_child', Gender.UNKNOWN)
people.append(self.root_child)
def add_parents(child, depth, max_depth):
if depth+1 < max_depth:
dad = Person(child.name + '_dad', Gender.MALE)
mom = Person(child.name + '_mom', Gender.FEMALE)
people.append(dad)
people.append(mom)
child.set_father(dad)
child.set_mother(mom)
add_parents(dad, depth+1, max_depth)
add_parents(mom, depth+1, max_depth)
add_parents(self.root_child, 0, self.generations)
def test_set_mother(self):
'''Testing set_mother'''
self.child.set_mother(self.mom)
self.assertEqual(self.child.mother, self.mom)
self.assertIn(self.child, self.mom.children)
self.mom.gender = Gender.MALE
with self.assertRaises(PersonError) as context:
self.child.set_mother(self.mom)
self.assertIn("is not female", str(context.exception))
def test_set_father(self):
'''Testing set_father'''
self.child.set_father(self.dad)
self.assertEqual(self.child.father, self.dad)
self.assertIn(self.child, self.dad.children)
self.dad.gender = Gender.FEMALE
with self.assertRaises(PersonError) as context:
self.child.set_father(self.dad)
self.assertIn('is not male', str(context.exception))
def test_remove_father(self):
'''Testing function remove_father'''
with self.assertRaises(Exception) as context:
self.child.remove_father()
self.assertIn('is not known', str(context.exception))
self.child.set_father(self.dad)
self.dad.children.remove(self.child)
with self.assertRaises(Exception) as context:
self.child.remove_father()
self.assertIn('is not a child of father', str(context.exception))
self.child.set_father(self.dad)
self.child.remove_father()
self.assertNotEqual(self.dad,self.child.father)
self.assertNotIn(self.child, self.dad.children)
def test_remove_mother(self):
'''Testing function remove_mother'''
with self.assertRaises(Exception) as context:
self.child.remove_mother()
self.assertTrue('is not know', str(context.exception))
self.child.set_mother(self.mom)
self.mom.children.remove(self.child)
with self.assertRaises(Exception) as context:
self.child.remove_mother()
self.assertTrue('is not a child of mother', str(context.exception))
self.child.set_mother(self.mom)
self.child.remove_mother()
self.assertNotEqual(self.mom, self.child.mother)
self.assertNotIn(self.child, self.mom.children) # self.mom noted in setUp
def test_add_child(self):
''' Testing add_child '''
self.assertNotIn(self.child, self.mom.children)
self.mom.add_child(self.child)
self.assertEqual(self.child.mother, self.mom)
self.assertIn(self.child, self.mom.children)
self.assertNotIn(self.child, self.dad.children)
self.dad.add_child(self.child)
self.assertEqual(self.child.father, self.dad)
self.assertIn(self.child, self.dad.children)
def test_add_child_error(self):
''' Test add_child_error '''
self.dad.gender = Gender.UNKNOWN
with self.assertRaises(PersonError) as context:
self.dad.add_child(self.child)
self.assertIn('cannot add child', str(context.exception)) # added the str to test
self.assertIn('with unknown gender', str(context.exception)) # added the str to test
'''def test_remove_father(self):
self.child.set_father(self.dad)
self.child.remove_father()
self.assertNotIn(self.child, self.dad.children)
'''
def test_get_persons_name(self):
'''Testing get_persons_name '''
self.assertEqual('root_child', self.root_child.get_persons_name(self.root_child))
self.assertEqual(Person.get_persons_name(self.child),self.child.name)
self.assertEqual(Person.get_persons_name(None), 'NA')
self.assertIs(Person.get_persons_name(self.child),self.child.name)
self.assertTrue(Person.get_persons_name(self.child),self.child.name)
def test_grandparents(self):
'''Testing grandparents '''
self.assertIn(self.root_child.father.father, self.root_child.grandparents())
self.assertIn(self.root_child.father.mother, self.root_child.grandparents())
self.assertIn(self.root_child.mother.father,self.root_child.grandparents())
self.assertIn(self.root_child.mother.mother,self.root_child.grandparents())
def test_all_grandparents(self):
''' Testing all_grandparents '''
self.assertIn(self.root_child.father.father.father, self.root_child.all_grandparents())
self.assertIn(self.root_child.father.father.mother, self.root_child.all_grandparents())
self.assertIn(self.root_child.father.mother.father, self.root_child.all_grandparents())
self.assertIn(self.root_child.father.mother.father, self.root_child.all_grandparents())
self.assertIn(self.root_child.mother.mother.mother, self.root_child.all_grandparents())
self.assertIn(self.root_child.mother.mother.father, self.root_child.all_grandparents())
self.assertIn(self.root_child.mother.father.father, self.root_child.all_grandparents())
self.assertIn(self.root_child.mother.father.mother, self.root_child.all_grandparents())
self.assertIn(self.root_child.mother.mother.father, self.root_child.all_grandparents())
self.assertIn(self.root_child.mother.father.father, self.root_child.all_grandparents())
self.assertIsNot(self.root_child.mother.father.father,self.root_child.all_grandparents())
self.assertIsNotNone(self.root_child.mother.father.father,self.root_child.all_grandparents())
self.assertIsNot(self.root_child,self.root_child.all_grandparents())
self.assertIn(self.root_child.mother.mother.mother, self.root_child.all_grandparents())
def test_all_ancestors(self):
''' Testing all_ancestors '''
self.assertNotEqual(self.root_child.father,self.root_child.all_ancestors())
self.assertIn(self.root_child.father.father.father, self.root_child.all_ancestors())
self.assertIn(self.root_child.mother.mother.mother, self.root_child.all_ancestors())
self.assertIn(self.root_child.father.mother.mother, self.root_child.all_ancestors())
self.assertIn(self.root_child.mother.father.mother, self.root_child.all_ancestors())
self.assertIn(self.root_child.mother,self.root_child.all_ancestors())
self.assertIn(self.root_child.mother.mother, self.root_child.all_ancestors())
self.assertIn(self.root_child.mother.father,self.root_child.all_ancestors())
self.assertIsNot(self.root_child.mother.father.father.father,self.root_child.all_ancestors())
self.assertIsNot(self.root_child.mother.mother.mother.mother,self.root_child.all_ancestors())
self.assertIn(self.root_child.father, self.root_child.all_ancestors())
self.assertIn(self.root_child.father.father,self.root_child.all_ancestors())
self.assertNotIn(self.root_child,self.root_child.all_ancestors())
self.assertIn(self.root_child.mother.mother.mother, self.root_child.all_ancestors())
def test_ancestors(self):
''' Testing ancestors '''
self.assertIn(self.root_child.father,self.root_child.ancestors(1))
self.assertIn(self.root_child.father.father,self.root_child.ancestors(2))
self.assertIn(self.root_child.father.mother,self.root_child.ancestors(2))
self.assertIn(self.root_child.mother.father,self.root_child.ancestors(2))
self.assertIn(self.root_child.mother.mother,self.root_child.ancestors(2))
self.assertIn(self.root_child.father.father.father,self.root_child.ancestors(3))
self.assertIn(self.root_child.father.father.mother,self.root_child.ancestors(3))
self.assertIn(self.root_child.father.mother.father,self.root_child.ancestors(3))
self.assertIn(self.root_child.father.mother.mother,self.root_child.ancestors(3))
self.assertIn(self.root_child.mother.mother.father,self.root_child.ancestors(3))
self.assertIn(self.root_child.mother.mother.mother,self.root_child.ancestors(3))
self.assertIn(self.root_child.mother.father.mother,self.root_child.ancestors(3))
self.assertIn(self.root_child.mother.father.mother,self.root_child.ancestors(3))
self.assertIn(self.root_child.mother,self.root_child.ancestors(1))
self.assertIn(self.root_child.mother.mother,self.root_child.ancestors(2))
self.assertIn(self.root_child.mother.mother.mother,self.root_child.ancestors(3))
#self.assertIn(self.root_child.father.father.father.father,self.root_child.ancestors(4)) # not present
self.assertNotIn(self.root_child.father,self.root_child.ancestors(0))
with self.assertRaises(PersonError) as context:
self.root_child.ancestors(min_depth = 2, max_depth = 1)
self.assertIn('max_depth (1) cannot be less than min_depth (2)', str(context.exception))
def test_parent(self):
self.assertEqual(self.root_child.father, self.root_child.father)
self.assertEqual(self.root_child.mother, self.root_child.mother)
if __name__ == '__main__':
unittest.main()