how to make a person: tests and solution
how to make a person with strings: tests
The code in
person/tests/test_person.pyfrom how to make a person with strings1def joe(): 2 return 'joe, blow, M, 1996' 3 4 5def jane(): 6 return 'jane, doe, F, 1991' 7 8 9def john(): 10 return 'john, smith, M, 1580' 11 12 13def mary(): 14 return 'mary, public, F, 2000' 15 16 17def test_joe(): 18 assert joe() == 'joe, blow, M, 1996' 19 20 21def test_jane(): 22 assert jane() == 'jane, doe, F, 1991' 23 24 25def test_john(): 26 assert john() == 'john, smith, M, 1580' 27 28 29def test_mary(): 30 assert mary() == 'mary, public, F, 2000' 31 32 33# Exceptions seen 34# AssertionError 35# NameError 36# TypeError
how to make a person with f-strings: tests and solution
how to make a person with f-strings: tests
The code in person/tests/test_person.py from how to make a person with f-strings
1import src.person
2
3
4def test_joe():
5 first_name = 'joe'
6 last_name = 'blow'
7 sex = 'M'
8 year_of_birth = 1996
9
10 reality = src.person.factory(
11 first_name=first_name,
12 last_name=last_name,
13 sex=sex,
14 year_of_birth=year_of_birth,
15 )
16 my_expectation = (
17 f'{first_name}, {last_name},'
18 f' {sex}, {year_of_birth}'
19 )
20 assert reality == my_expectation
21
22 reality = src.person.say_hello(
23 first_name=first_name,
24 last_name=last_name,
25 year_of_birth=year_of_birth,
26 )
27 my_expectation = (
28 f'Hello, my name is {first_name}'
29 f' {last_name} and I am'
30 f' {2026-year_of_birth}.'
31 )
32 assert reality == my_expectation
33
34
35def test_jane():
36 first_name = 'jane'
37 last_name = 'doe'
38 sex = 'F'
39 year_of_birth = 1991
40
41 reality = src.person.factory(
42 first_name=first_name,
43 last_name=last_name,
44 sex=sex,
45 year_of_birth=year_of_birth,
46 )
47 my_expectation = (
48 f'{first_name}, {last_name},'
49 f' {sex}, {year_of_birth}'
50 )
51 assert reality == my_expectation
52
53 reality = src.person.say_hello(
54 first_name=first_name,
55 last_name=last_name,
56 year_of_birth=year_of_birth,
57 )
58 my_expectation = (
59 f'Hello, my name is {first_name}'
60 f' {last_name} and I am'
61 f' {2026-year_of_birth}.'
62 )
63 assert reality == my_expectation
64
65
66def test_john():
67 first_name = 'john'
68 last_name = 'smith'
69 sex = 'M'
70 year_of_birth = 1580
71
72 reality = src.person.factory(
73 first_name=first_name,
74 last_name=last_name,
75 sex=sex,
76 year_of_birth=year_of_birth,
77 )
78 my_expectation = (
79 f'{first_name}, {last_name},'
80 f' {sex}, {year_of_birth}'
81 )
82 assert reality == my_expectation
83
84 reality = src.person.say_hello(
85 first_name=first_name,
86 last_name=last_name,
87 year_of_birth=year_of_birth,
88 )
89 my_expectation = (
90 f'Hello, my name is {first_name}'
91 f' {last_name} and I am'
92 f' {2026-year_of_birth}.'
93 )
94 assert reality == my_expectation
95
96
97def test_mary():
98 first_name = 'mary'
99 last_name = 'public'
100 sex = 'F'
101 year_of_birth = 2000
102
103 reality = src.person.factory(
104 first_name=first_name,
105 last_name=last_name,
106 sex=sex,
107 year_of_birth=year_of_birth,
108 )
109 my_expectation = (
110 f'{first_name}, {last_name},'
111 f' {sex}, {year_of_birth}'
112 )
113 assert reality == my_expectation
114
115 reality = src.person.say_hello(
116 first_name=first_name,
117 last_name=last_name,
118 year_of_birth=year_of_birth,
119 )
120 my_expectation = (
121 f'Hello, my name is {first_name}'
122 f' {last_name} and I am'
123 f' {2026-year_of_birth}.'
124 )
125 assert reality == my_expectation
126
127
128# Exceptions seen
129# AssertionError
130# NameError
131# TypeError
132# AttributeError
how to make a person with f-strings: solutions
The code in person/src/person.py from how to make a person with f-strings
1def say_hello(
2 first_name, last_name, year_of_birth,
3):
4 return (
5 f'Hello, my name is {first_name}'
6 f' {last_name} and I am'
7 f' {2026-year_of_birth}.'
8 )
9
10
11def factory(
12 first_name, last_name,
13 sex, year_of_birth,
14 ):
15 return (
16 f'{first_name}, {last_name},'
17 f' {sex}, {year_of_birth}'
18 )
how to make a person with a class: tests and solution
how to make a person with a class: tests
The code in person/tests/test_person.py from how to make a person with a class
1import src.person
2
3
4def test_joe():
5 first_name = 'joe'
6 last_name = 'blow'
7 sex = 'M'
8 year_of_birth = 1996
9
10 reality = src.person.factory(
11 first_name=first_name,
12 last_name=last_name,
13 sex=sex,
14 year_of_birth=year_of_birth,
15 )
16 my_expectation = (
17 f'{first_name}, {last_name},'
18 f' {sex}, {year_of_birth}'
19 )
20 assert reality == my_expectation
21
22 reality = src.person.say_hello(
23 first_name=first_name,
24 last_name=last_name,
25 year_of_birth=year_of_birth,
26 )
27 my_expectation = (
28 f'Hello, my name is {first_name}'
29 f' {last_name} and I am'
30 f' {2026-year_of_birth}.'
31 )
32 assert reality == my_expectation
33
34 joe = src.person.Person(
35 first_name=first_name,
36 last_name=last_name,
37 sex=sex,
38 year_of_birth=year_of_birth,
39 )
40
41 reality = joe.say_hello()
42 assert reality == my_expectation
43
44
45def test_jane():
46 first_name = 'jane'
47 last_name = 'doe'
48 sex = 'F'
49 year_of_birth = 1991
50
51 reality = src.person.factory(
52 first_name=first_name,
53 last_name=last_name,
54 sex=sex,
55 year_of_birth=year_of_birth,
56 )
57 my_expectation = (
58 f'{first_name}, {last_name},'
59 f' {sex}, {year_of_birth}'
60 )
61 assert reality == my_expectation
62
63 reality = src.person.say_hello(
64 first_name=first_name,
65 last_name=last_name,
66 year_of_birth=year_of_birth,
67 )
68 my_expectation = (
69 f'Hello, my name is {first_name}'
70 f' {last_name} and I am'
71 f' {2026-year_of_birth}.'
72 )
73 assert reality == my_expectation
74
75 jane = src.person.Person(
76 first_name=first_name,
77 last_name=last_name,
78 sex=sex,
79 year_of_birth=year_of_birth,
80 )
81
82 reality = jane.say_hello()
83 assert reality == my_expectation
84
85
86def test_john():
87 first_name = 'john'
88 last_name = 'smith'
89 sex = 'M'
90 year_of_birth = 1580
91
92 reality = src.person.factory(
93 first_name=first_name,
94 last_name=last_name,
95 sex=sex,
96 year_of_birth=year_of_birth,
97 )
98 my_expectation = (
99 f'{first_name}, {last_name},'
100 f' {sex}, {year_of_birth}'
101 )
102 assert reality == my_expectation
103
104 reality = src.person.say_hello(
105 first_name=first_name,
106 last_name=last_name,
107 year_of_birth=year_of_birth,
108 )
109 my_expectation = (
110 f'Hello, my name is {first_name}'
111 f' {last_name} and I am'
112 f' {2026-year_of_birth}.'
113 )
114 assert reality == my_expectation
115
116 john = src.person.Person(
117 first_name=first_name,
118 last_name=last_name,
119 sex=sex,
120 year_of_birth=year_of_birth,
121 )
122
123 reality = john.say_hello()
124 assert reality == my_expectation
125
126
127def test_mary():
128 first_name = 'mary'
129 last_name = 'public'
130 sex = 'F'
131 year_of_birth = 2000
132
133 reality = src.person.factory(
134 first_name=first_name,
135 last_name=last_name,
136 sex=sex,
137 year_of_birth=year_of_birth,
138 )
139 my_expectation = (
140 f'{first_name}, {last_name},'
141 f' {sex}, {year_of_birth}'
142 )
143 assert reality == my_expectation
144
145 reality = src.person.say_hello(
146 first_name=first_name,
147 last_name=last_name,
148 year_of_birth=year_of_birth,
149 )
150 my_expectation = (
151 f'Hello, my name is {first_name}'
152 f' {last_name} and I am'
153 f' {2026-year_of_birth}.'
154 )
155 assert reality == my_expectation
156
157 mary = src.person.Person(
158 first_name=first_name,
159 last_name=last_name,
160 sex=sex,
161 year_of_birth=year_of_birth,
162 )
163
164 reality = mary.say_hello()
165 assert reality == my_expectation
166
167
168def test_dir_person_class():
169 reality = dir(src.person.Person)
170 my_expectation = [
171 '__class__',
172 '__delattr__',
173 '__dict__',
174 '__dir__',
175 '__doc__',
176 '__eq__',
177 '__firstlineno__',
178 '__format__',
179 '__ge__',
180 '__getattribute__',
181 '__getstate__',
182 '__gt__',
183 '__hash__',
184 '__init__',
185 '__init_subclass__',
186 '__le__',
187 '__lt__',
188 '__module__',
189 '__ne__',
190 '__new__',
191 '__reduce__',
192 '__reduce_ex__',
193 '__repr__',
194 '__setattr__',
195 '__sizeof__',
196 '__static_attributes__',
197 '__str__',
198 '__subclasshook__',
199 '__weakref__',
200 'say_hello'
201 ]
202 assert reality == my_expectation
203
204
205def test_dir_person_instance():
206 an_instance_of_person = src.person.Person(
207 first_name='first_name',
208 last_name='last_name',
209 sex='M',
210 year_of_birth=2026,
211 )
212
213 reality = dir(an_instance_of_person)
214 my_expectation = [
215 '__class__',
216 '__delattr__',
217 '__dict__',
218 '__dir__',
219 '__doc__',
220 '__eq__',
221 '__firstlineno__',
222 '__format__',
223 '__ge__',
224 '__getattribute__',
225 '__getstate__',
226 '__gt__',
227 '__hash__',
228 '__init__',
229 '__init_subclass__',
230 '__le__',
231 '__lt__',
232 '__module__',
233 '__ne__',
234 '__new__',
235 '__reduce__',
236 '__reduce_ex__',
237 '__repr__',
238 '__setattr__',
239 '__sizeof__',
240 '__static_attributes__',
241 '__str__',
242 '__subclasshook__',
243 '__weakref__',
244 'first_name',
245 'last_name',
246 'say_hello',
247 'sex',
248 'year_of_birth',
249 ]
250 assert reality == my_expectation
251
252
253# Exceptions seen
254# AssertionError
255# NameError
256# TypeError
257# AttributeError
258# SyntaxError
how to make a person with a class: solutions
The code in person/src/person.py from how to make a person with a class
1class Person:
2
3 def __init__(
4 self, first_name, last_name,
5 sex, year_of_birth,
6 ):
7 self.first_name = first_name
8 self.last_name = last_name
9 self.year_of_birth = year_of_birth
10 self.sex = sex
11
12 def say_hello(self):
13 return (
14 f'Hello, my name is {self.first_name}'
15 f' {self.last_name} and I am'
16 f' {2026-self.year_of_birth}.'
17 )
18
19
20def say_hello(
21 first_name, last_name, year_of_birth,
22):
23 return (
24 f'Hello, my name is {first_name}'
25 f' {last_name} and I am'
26 f' {2026-year_of_birth}.'
27 )
28
29
30def factory(
31 first_name, last_name,
32 sex, year_of_birth,
33 ):
34 return (
35 f'{first_name}, {last_name},'
36 f' {sex}, {year_of_birth}'
37 )
test person with unittest: tests
The code in person/tests/test_person.py from test person with unittest
1import src.person
2import unittest
3
4
5class TestPerson(unittest.TestCase):
6
7 def test_joe(self):
8 first_name = 'joe'
9 last_name = 'blow'
10 sex = 'M'
11 year_of_birth = 1996
12
13 reality = src.person.factory(
14 first_name=first_name,
15 last_name=last_name,
16 sex=sex,
17 year_of_birth=year_of_birth,
18 )
19 my_expectation = (
20 f'{first_name}, {last_name},'
21 f' {sex}, {year_of_birth}'
22 )
23 assert reality == my_expectation
24 self.assertEqual(reality, my_expectation)
25
26 reality = src.person.say_hello(
27 first_name=first_name,
28 last_name=last_name,
29 year_of_birth=year_of_birth,
30 )
31 my_expectation = (
32 f'Hello, my name is {first_name}'
33 f' {last_name} and I am'
34 f' {2026-year_of_birth}.'
35 )
36 assert reality == my_expectation
37 self.assertEqual(reality, my_expectation)
38
39 joe = src.person.Person(
40 first_name=first_name,
41 last_name=last_name,
42 sex=sex,
43 year_of_birth=year_of_birth,
44 )
45
46 reality = joe.say_hello()
47 assert reality == my_expectation
48 self.assertEqual(reality, my_expectation)
49
50 def test_jane(self):
51 first_name = 'jane'
52 last_name = 'doe'
53 sex = 'F'
54 year_of_birth = 1991
55
56 reality = src.person.factory(
57 first_name=first_name,
58 last_name=last_name,
59 sex=sex,
60 year_of_birth=year_of_birth,
61 )
62 my_expectation = (
63 f'{first_name}, {last_name},'
64 f' {sex}, {year_of_birth}'
65 )
66 assert reality == my_expectation
67 self.assertEqual(reality, my_expectation)
68
69 reality = src.person.say_hello(
70 first_name=first_name,
71 last_name=last_name,
72 year_of_birth=year_of_birth,
73 )
74 my_expectation = (
75 f'Hello, my name is {first_name}'
76 f' {last_name} and I am'
77 f' {2026-year_of_birth}.'
78 )
79 assert reality == my_expectation
80 self.assertEqual(reality, my_expectation)
81
82 jane = src.person.Person(
83 first_name=first_name,
84 last_name=last_name,
85 sex=sex,
86 year_of_birth=year_of_birth,
87 )
88
89 reality = jane.say_hello()
90 assert reality == my_expectation
91 self.assertEqual(reality, my_expectation)
92
93 def test_john(self):
94 first_name = 'john'
95 last_name = 'smith'
96 sex = 'M'
97 year_of_birth = 1580
98
99 reality = src.person.factory(
100 first_name=first_name,
101 last_name=last_name,
102 sex=sex,
103 year_of_birth=year_of_birth,
104 )
105 my_expectation = (
106 f'{first_name}, {last_name},'
107 f' {sex}, {year_of_birth}'
108 )
109 assert reality == my_expectation
110 self.assertEqual(reality, my_expectation)
111
112 reality = src.person.say_hello(
113 first_name=first_name,
114 last_name=last_name,
115 year_of_birth=year_of_birth,
116 )
117 my_expectation = (
118 f'Hello, my name is {first_name}'
119 f' {last_name} and I am'
120 f' {2026-year_of_birth}.'
121 )
122 assert reality == my_expectation
123 self.assertEqual(reality, my_expectation)
124
125 john = src.person.Person(
126 first_name=first_name,
127 last_name=last_name,
128 sex=sex,
129 year_of_birth=year_of_birth,
130 )
131
132 reality = john.say_hello()
133 assert reality == my_expectation
134 self.assertEqual(reality, my_expectation)
135
136 def test_mary(self):
137 first_name = 'mary'
138 last_name = 'public'
139 sex = 'F'
140 year_of_birth = 2000
141
142 reality = src.person.factory(
143 first_name=first_name,
144 last_name=last_name,
145 sex=sex,
146 year_of_birth=year_of_birth,
147 )
148 my_expectation = (
149 f'{first_name}, {last_name},'
150 f' {sex}, {year_of_birth}'
151 )
152 assert reality == my_expectation
153 self.assertEqual(reality, my_expectation)
154
155 reality = src.person.say_hello(
156 first_name=first_name,
157 last_name=last_name,
158 year_of_birth=year_of_birth,
159 )
160 my_expectation = (
161 f'Hello, my name is {first_name}'
162 f' {last_name} and I am'
163 f' {2026-year_of_birth}.'
164 )
165 assert reality == my_expectation
166 self.assertEqual(reality, my_expectation)
167
168 mary = src.person.Person(
169 first_name=first_name,
170 last_name=last_name,
171 sex=sex,
172 year_of_birth=year_of_birth,
173 )
174
175 reality = mary.say_hello()
176 assert reality == my_expectation
177 self.assertEqual(reality, my_expectation)
178
179 def test_dir_person_class(self):
180 reality = dir(src.person.Person)
181 my_expectation = [
182 '__class__',
183 '__delattr__',
184 '__dict__',
185 '__dir__',
186 '__doc__',
187 '__eq__',
188 '__firstlineno__',
189 '__format__',
190 '__ge__',
191 '__getattribute__',
192 '__getstate__',
193 '__gt__',
194 '__hash__',
195 '__init__',
196 '__init_subclass__',
197 '__le__',
198 '__lt__',
199 '__module__',
200 '__ne__',
201 '__new__',
202 '__reduce__',
203 '__reduce_ex__',
204 '__repr__',
205 '__setattr__',
206 '__sizeof__',
207 '__static_attributes__',
208 '__str__',
209 '__subclasshook__',
210 '__weakref__',
211 'say_hello'
212 ]
213 assert reality == my_expectation
214 self.assertEqual(reality, my_expectation)
215
216 def test_dir_person_instance(self):
217 an_instance_of_person = src.person.Person(
218 first_name='first_name',
219 last_name='last_name',
220 sex='M',
221 year_of_birth=2026,
222 )
223
224 reality = dir(an_instance_of_person)
225 my_expectation = [
226 '__class__',
227 '__delattr__',
228 '__dict__',
229 '__dir__',
230 '__doc__',
231 '__eq__',
232 '__firstlineno__',
233 '__format__',
234 '__ge__',
235 '__getattribute__',
236 '__getstate__',
237 '__gt__',
238 '__hash__',
239 '__init__',
240 '__init_subclass__',
241 '__le__',
242 '__lt__',
243 '__module__',
244 '__ne__',
245 '__new__',
246 '__reduce__',
247 '__reduce_ex__',
248 '__repr__',
249 '__setattr__',
250 '__sizeof__',
251 '__static_attributes__',
252 '__str__',
253 '__subclasshook__',
254 '__weakref__',
255 'first_name',
256 'last_name',
257 'say_hello',
258 'sex',
259 'year_of_birth',
260 ]
261 assert reality == my_expectation
262 self.assertEqual(reality, my_expectation)
263
264
265# Exceptions seen
266# AssertionError
267# NameError
268# TypeError
269# AttributeError
270# SyntaxError
test person with datetime: tests and solutions
test person with datetime: tests
The code in person/tests/test_person.py from test person with datetime
1import datetime
2import src.person
3import unittest
4
5
6class TestPerson(unittest.TestCase):
7
8 @staticmethod
9 def calculate_age(year_of_birth):
10 return (
11 datetime.date.today().year
12 - year_of_birth
13 )
14
15 def test_joe(self):
16 first_name = 'joe'
17 last_name = 'blow'
18 sex = 'M'
19 year_of_birth = 1996
20
21 reality = src.person.factory(
22 first_name=first_name,
23 last_name=last_name,
24 sex=sex,
25 year_of_birth=year_of_birth,
26 )
27 my_expectation = (
28 f'{first_name}, {last_name},'
29 f' {sex}, {year_of_birth}'
30 )
31 assert reality == my_expectation
32 self.assertEqual(reality, my_expectation)
33
34 reality = src.person.say_hello(
35 first_name=first_name,
36 last_name=last_name,
37 year_of_birth=year_of_birth,
38 )
39 my_expectation = (
40 f'Hello, my name is {first_name}'
41 f' {last_name} and I am'
42 f' {self.calculate_age(year_of_birth)}.'
43 )
44 assert reality == my_expectation
45 self.assertEqual(reality, my_expectation)
46
47 joe = src.person.Person(
48 first_name=first_name,
49 last_name=last_name,
50 sex=sex,
51 year_of_birth=year_of_birth,
52 )
53
54 reality = joe.say_hello()
55 assert reality == my_expectation
56 self.assertEqual(reality, my_expectation)
57
58 def test_jane(self):
59 first_name = 'jane'
60 last_name = 'doe'
61 sex = 'F'
62 year_of_birth = 1991
63
64 reality = src.person.factory(
65 first_name=first_name,
66 last_name=last_name,
67 sex=sex,
68 year_of_birth=year_of_birth,
69 )
70 my_expectation = (
71 f'{first_name}, {last_name},'
72 f' {sex}, {year_of_birth}'
73 )
74 assert reality == my_expectation
75 self.assertEqual(reality, my_expectation)
76
77 reality = src.person.say_hello(
78 first_name=first_name,
79 last_name=last_name,
80 year_of_birth=year_of_birth,
81 )
82 my_expectation = (
83 f'Hello, my name is {first_name}'
84 f' {last_name} and I am'
85 f' {self.calculate_age(year_of_birth)}.'
86 )
87 assert reality == my_expectation
88 self.assertEqual(reality, my_expectation)
89
90 jane = src.person.Person(
91 first_name=first_name,
92 last_name=last_name,
93 sex=sex,
94 year_of_birth=year_of_birth,
95 )
96
97 reality = jane.say_hello()
98 assert reality == my_expectation
99 self.assertEqual(reality, my_expectation)
100
101 def test_john(self):
102 first_name = 'john'
103 last_name = 'smith'
104 sex = 'M'
105 year_of_birth = 1980
106 # year_of_birth = 1580
107 # raises AssertionError
108 # because older than 120
109
110 reality = src.person.factory(
111 first_name=first_name,
112 last_name=last_name,
113 sex=sex,
114 year_of_birth=year_of_birth,
115 )
116 my_expectation = (
117 f'{first_name}, {last_name},'
118 f' {sex}, {year_of_birth}'
119 )
120 assert reality == my_expectation
121 self.assertEqual(reality, my_expectation)
122
123 reality = src.person.say_hello(
124 first_name=first_name,
125 last_name=last_name,
126 year_of_birth=year_of_birth,
127 )
128 my_expectation = (
129 f'Hello, my name is {first_name}'
130 f' {last_name} and I am'
131 f' {self.calculate_age(year_of_birth)}.'
132 )
133 assert reality == my_expectation
134 self.assertEqual(reality, my_expectation)
135
136 john = src.person.Person(
137 first_name=first_name,
138 last_name=last_name,
139 sex=sex,
140 year_of_birth=year_of_birth,
141 )
142
143 reality = john.say_hello()
144 assert reality == my_expectation
145 self.assertEqual(reality, my_expectation)
146
147 def test_mary(self):
148 first_name = 'mary'
149 last_name = 'public'
150 sex = 'F'
151 year_of_birth = 2000
152
153 reality = src.person.factory(
154 first_name=first_name,
155 last_name=last_name,
156 sex=sex,
157 year_of_birth=year_of_birth,
158 )
159 my_expectation = (
160 f'{first_name}, {last_name},'
161 f' {sex}, {year_of_birth}'
162 )
163 assert reality == my_expectation
164 self.assertEqual(reality, my_expectation)
165
166 reality = src.person.say_hello(
167 first_name=first_name,
168 last_name=last_name,
169 year_of_birth=year_of_birth,
170 )
171 my_expectation = (
172 f'Hello, my name is {first_name}'
173 f' {last_name} and I am'
174 f' {self.calculate_age(year_of_birth)}.'
175 )
176 assert reality == my_expectation
177 self.assertEqual(reality, my_expectation)
178
179 mary = src.person.Person(
180 first_name=first_name,
181 last_name=last_name,
182 sex=sex,
183 year_of_birth=year_of_birth,
184 )
185
186 reality = mary.say_hello()
187 assert reality == my_expectation
188 self.assertEqual(reality, my_expectation)
189
190 def test_when_year_of_birth_is_not_an_integer(self):
191 person = src.person.Person(
192 first_name='first_name',
193 last_name='last_name',
194 sex='M',
195 # year_of_birth=None, # fails
196 # year_of_birth=False, # fails
197 # year_of_birth=2026.0, # fails
198 # year_of_birth='2026', # fails
199 # year_of_birth=(2026,), # fails
200 )
201 # person.say_hello()
202 # fails if year_of_birth is not an integer
203
204 def test_dir_person_class(self):
205 reality = dir(src.person.Person)
206 my_expectation = [
207 '__class__',
208 '__delattr__',
209 '__dict__',
210 '__dir__',
211 '__doc__',
212 '__eq__',
213 '__firstlineno__',
214 '__format__',
215 '__ge__',
216 '__getattribute__',
217 '__getstate__',
218 '__gt__',
219 '__hash__',
220 '__init__',
221 '__init_subclass__',
222 '__le__',
223 '__lt__',
224 '__module__',
225 '__ne__',
226 '__new__',
227 '__reduce__',
228 '__reduce_ex__',
229 '__repr__',
230 '__setattr__',
231 '__sizeof__',
232 '__static_attributes__',
233 '__str__',
234 '__subclasshook__',
235 '__weakref__',
236 'say_hello'
237 ]
238 assert reality == my_expectation
239 self.assertEqual(reality, my_expectation)
240
241 def test_dir_person_instance(self):
242 an_instance_of_person = src.person.Person(
243 first_name='first_name',
244 last_name='last_name',
245 sex='M',
246 year_of_birth=2026,
247 )
248
249 reality = dir(an_instance_of_person)
250 my_expectation = [
251 '__class__',
252 '__delattr__',
253 '__dict__',
254 '__dir__',
255 '__doc__',
256 '__eq__',
257 '__firstlineno__',
258 '__format__',
259 '__ge__',
260 '__getattribute__',
261 '__getstate__',
262 '__gt__',
263 '__hash__',
264 '__init__',
265 '__init_subclass__',
266 '__le__',
267 '__lt__',
268 '__module__',
269 '__ne__',
270 '__new__',
271 '__reduce__',
272 '__reduce_ex__',
273 '__repr__',
274 '__setattr__',
275 '__sizeof__',
276 '__static_attributes__',
277 '__str__',
278 '__subclasshook__',
279 '__weakref__',
280 'first_name',
281 'last_name',
282 'say_hello',
283 'sex',
284 'year_of_birth',
285 ]
286 assert reality == my_expectation
287 self.assertEqual(reality, my_expectation)
288
289
290# Exceptions seen
291# AssertionError
292# NameError
293# TypeError
294# AttributeError
295# SyntaxError
test person with datetime: solutions
The code in person/src/person.py from test person with datetime
1import datetime
2
3
4class Person:
5
6 def __init__(
7 self, first_name, last_name,
8 sex, year_of_birth=None,
9 ):
10 self.first_name = first_name
11 self.last_name = last_name
12 self.year_of_birth = year_of_birth
13 self.sex = sex
14
15 def say_hello(self):
16 return (
17 f'Hello, my name is {self.first_name}'
18 f' {self.last_name} and I am'
19 f' {calculate_age(self.year_of_birth)}.'
20 )
21
22
23def calculate_age(year_of_birth):
24 assert isinstance(year_of_birth, int)
25 age = (
26 datetime.date.today().year
27 - year_of_birth
28 )
29 assert age <= 120
30 return age
31
32
33def say_hello(
34 first_name, last_name, year_of_birth,
35):
36 return (
37 f'Hello, my name is {first_name}'
38 f' {last_name} and I am'
39 f' {calculate_age(year_of_birth)}.'
40 )
41
42
43def factory(
44 first_name, last_name,
45 sex, year_of_birth,
46 ):
47 return (
48 f'{first_name}, {last_name},'
49 f' {sex}, {year_of_birth}'
50 )
how to make a person with conditions: tests and solutions
how to make a person with conditions: tests
The code in person/tests/test_person.py from how to make a person with conditions
1import datetime
2import src.person
3import unittest
4
5
6class TestPerson(unittest.TestCase):
7
8 @staticmethod
9 def calculate_age(year_of_birth):
10 return (
11 datetime.date.today().year
12 - year_of_birth
13 )
14
15 def test_joe(self):
16 first_name = 'joe'
17 last_name = 'blow'
18 sex = 'M'
19 year_of_birth = 1996
20
21 reality = src.person.factory(
22 first_name=first_name,
23 last_name=last_name,
24 sex=sex,
25 year_of_birth=year_of_birth,
26 )
27 my_expectation = (
28 f'{first_name}, {last_name},'
29 f' {sex}, {year_of_birth}'
30 )
31 assert reality == my_expectation
32 self.assertEqual(reality, my_expectation)
33
34 reality = src.person.say_hello(
35 first_name=first_name,
36 last_name=last_name,
37 year_of_birth=year_of_birth,
38 )
39 my_expectation = (
40 f'Hello, my name is {first_name}'
41 f' {last_name} and I am'
42 f' {self.calculate_age(year_of_birth)}.'
43 )
44 assert reality == my_expectation
45 self.assertEqual(reality, my_expectation)
46
47 joe = src.person.Person(
48 first_name=first_name,
49 last_name=last_name,
50 sex=sex,
51 year_of_birth=year_of_birth,
52 )
53
54 reality = joe.say_hello()
55 assert reality == my_expectation
56 self.assertEqual(reality, my_expectation)
57 self.assertEqual(joe.can_vote(), True)
58 self.assertEqual(joe.can_get_license(), False)
59
60 def test_jane(self):
61 first_name = 'jane'
62 last_name = 'doe'
63 sex = 'F'
64 year_of_birth = 1991
65
66 reality = src.person.factory(
67 first_name=first_name,
68 last_name=last_name,
69 sex=sex,
70 year_of_birth=year_of_birth,
71 )
72 my_expectation = (
73 f'{first_name}, {last_name},'
74 f' {sex}, {year_of_birth}'
75 )
76 assert reality == my_expectation
77 self.assertEqual(reality, my_expectation)
78
79 reality = src.person.say_hello(
80 first_name=first_name,
81 last_name=last_name,
82 year_of_birth=year_of_birth,
83 )
84 my_expectation = (
85 f'Hello, my name is {first_name}'
86 f' {last_name} and I am'
87 f' {self.calculate_age(year_of_birth)}.'
88 )
89 assert reality == my_expectation
90 self.assertEqual(reality, my_expectation)
91
92 jane = src.person.Person(
93 first_name=first_name,
94 last_name=last_name,
95 sex=sex,
96 year_of_birth=year_of_birth,
97 passed_test=True,
98 )
99
100 reality = jane.say_hello()
101 assert reality == my_expectation
102 self.assertEqual(reality, my_expectation)
103 self.assertEqual(jane.can_vote(), True)
104 self.assertEqual(jane.can_get_license(), True)
105
106 def test_john(self):
107 first_name = 'john'
108 last_name = 'smith'
109 sex = 'M'
110 year_of_birth = 1980
111 # year_of_birth = 1580
112 # raises AssertionError
113 # because older than 120
114
115 reality = src.person.factory(
116 first_name=first_name,
117 last_name=last_name,
118 sex=sex,
119 year_of_birth=year_of_birth,
120 )
121 my_expectation = (
122 f'{first_name}, {last_name},'
123 f' {sex}, {year_of_birth}'
124 )
125 assert reality == my_expectation
126 self.assertEqual(reality, my_expectation)
127
128 reality = src.person.say_hello(
129 first_name=first_name,
130 last_name=last_name,
131 year_of_birth=year_of_birth,
132 )
133 my_expectation = (
134 f'Hello, my name is {first_name}'
135 f' {last_name} and I am'
136 f' {self.calculate_age(year_of_birth)}.'
137 )
138 assert reality == my_expectation
139 self.assertEqual(reality, my_expectation)
140
141 john = src.person.Person(
142 first_name=first_name,
143 last_name=last_name,
144 sex=sex,
145 year_of_birth=year_of_birth,
146 is_citizen=False,
147 )
148
149 reality = john.say_hello()
150 assert reality == my_expectation
151 self.assertEqual(reality, my_expectation)
152 self.assertEqual(john.can_vote(), False)
153 self.assertEqual(john.can_get_license(), False)
154
155 def test_mary(self):
156 first_name = 'mary'
157 last_name = 'public'
158 sex = 'F'
159 year_of_birth = 2000
160
161 reality = src.person.factory(
162 first_name=first_name,
163 last_name=last_name,
164 sex=sex,
165 year_of_birth=year_of_birth,
166 )
167 my_expectation = (
168 f'{first_name}, {last_name},'
169 f' {sex}, {year_of_birth}'
170 )
171 assert reality == my_expectation
172 self.assertEqual(reality, my_expectation)
173
174 reality = src.person.say_hello(
175 first_name=first_name,
176 last_name=last_name,
177 year_of_birth=year_of_birth,
178 )
179 my_expectation = (
180 f'Hello, my name is {first_name}'
181 f' {last_name} and I am'
182 f' {self.calculate_age(year_of_birth)}.'
183 )
184 assert reality == my_expectation
185 self.assertEqual(reality, my_expectation)
186
187 mary = src.person.Person(
188 first_name=first_name,
189 last_name=last_name,
190 sex=sex,
191 year_of_birth=year_of_birth,
192 is_citizen=False,
193 passed_test=True,
194 )
195
196 reality = mary.say_hello()
197 assert reality == my_expectation
198 self.assertEqual(reality, my_expectation)
199 self.assertEqual(mary.can_vote(), False)
200 self.assertEqual(mary.can_get_license(), True)
201
202 def test_underage_citizen(self):
203 person = src.person.Person(
204 first_name='first_name',
205 last_name='last_name',
206 sex='M',
207 year_of_birth=datetime.date.today().year-17,
208 is_citizen=True,
209 passed_test=True,
210 )
211 self.assertEqual(person.can_vote(), False)
212 self.assertEqual(
213 person.can_get_license(), False
214 )
215
216 @unittest.skip('will always fail')
217 def test_when_year_of_birth_is_not_an_integer(self):
218 person = src.person.Person(
219 first_name='first_name',
220 last_name='last_name',
221 sex='M',
222 # year_of_birth=None, # fails
223 # year_of_birth=False, # fails
224 # year_of_birth=2026.0, # fails
225 # year_of_birth='2026', # fails
226 # year_of_birth=(2026,), # fails
227 )
228 # person.say_hello()
229 # fails if year_of_birth is not an integer
230
231 def test_dir_person_class(self):
232 reality = dir(src.person.Person)
233 my_expectation = [
234 '__class__',
235 '__delattr__',
236 '__dict__',
237 '__dir__',
238 '__doc__',
239 '__eq__',
240 '__firstlineno__',
241 '__format__',
242 '__ge__',
243 '__getattribute__',
244 '__getstate__',
245 '__gt__',
246 '__hash__',
247 '__init__',
248 '__init_subclass__',
249 '__le__',
250 '__lt__',
251 '__module__',
252 '__ne__',
253 '__new__',
254 '__reduce__',
255 '__reduce_ex__',
256 '__repr__',
257 '__setattr__',
258 '__sizeof__',
259 '__static_attributes__',
260 '__str__',
261 '__subclasshook__',
262 '__weakref__',
263 'can_get_license',
264 'can_vote',
265 'check_age',
266 'say_hello',
267 ]
268 assert reality == my_expectation
269 self.assertEqual(reality, my_expectation)
270
271 def test_dir_person_instance(self):
272 an_instance_of_person = src.person.Person(
273 first_name='first_name',
274 last_name='last_name',
275 sex='M',
276 year_of_birth=2026,
277 )
278
279 reality = dir(an_instance_of_person)
280 my_expectation = [
281 '__class__',
282 '__delattr__',
283 '__dict__',
284 '__dir__',
285 '__doc__',
286 '__eq__',
287 '__firstlineno__',
288 '__format__',
289 '__ge__',
290 '__getattribute__',
291 '__getstate__',
292 '__gt__',
293 '__hash__',
294 '__init__',
295 '__init_subclass__',
296 '__le__',
297 '__lt__',
298 '__module__',
299 '__ne__',
300 '__new__',
301 '__reduce__',
302 '__reduce_ex__',
303 '__repr__',
304 '__setattr__',
305 '__sizeof__',
306 '__static_attributes__',
307 '__str__',
308 '__subclasshook__',
309 '__weakref__',
310 'age',
311 'can_get_license',
312 'can_vote',
313 'check_age',
314 'first_name',
315 'is_citizen',
316 'last_name',
317 'passed_test',
318 'say_hello',
319 'sex',
320 'year_of_birth',
321 ]
322 assert reality == my_expectation
323 self.assertEqual(reality, my_expectation)
324
325
326# Exceptions seen
327# AssertionError
328# NameError
329# TypeError
330# AttributeError
331# SyntaxError
how to make a person with conditions: solutions
The code in person/src/person.py from how to make a person with conditions
1import datetime
2
3
4class Person:
5
6 def __init__(
7 self, first_name, last_name,
8 sex, year_of_birth=None,
9 is_citizen=True,
10 passed_test=False,
11 ):
12 self.first_name = first_name
13 self.last_name = last_name
14 self.year_of_birth = year_of_birth
15 self.sex = sex
16 self.is_citizen = is_citizen
17 self.passed_test = passed_test
18 self.age = calculate_age(year_of_birth)
19
20 def can_get_license(self):
21 return self.check_age(
22 self.age, self.passed_test
23 )
24
25 def can_vote(self):
26 return self.check_age(
27 self.age, self.is_citizen
28 )
29
30 @staticmethod
31 def check_age(age, response):
32 if age < 18:
33 return False
34 return response
35
36 def say_hello(self):
37 return (
38 f'Hello, my name is {self.first_name}'
39 f' {self.last_name} and I am'
40 f' {self.age}.'
41 )
42
43
44def calculate_age(year_of_birth):
45 assert isinstance(year_of_birth, int)
46 age = (
47 datetime.date.today().year
48 - year_of_birth
49 )
50 assert age <= 120
51 return age
52
53
54def say_hello(
55 first_name, last_name, year_of_birth,
56):
57 return (
58 f'Hello, my name is {first_name}'
59 f' {last_name} and I am'
60 f' {calculate_age(year_of_birth)}.'
61 )
62
63
64def factory(
65 first_name, last_name,
66 sex, year_of_birth,
67 ):
68 return (
69 f'{first_name}, {last_name},'
70 f' {sex}, {year_of_birth}'
71 )