how to make a person: tests and solution

how to make a person with strings: tests

  • The code in person/tests/test_person.py from how to make a person with strings

    person/tests/test_person.py``
     1def 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

person/tests/test_person.py
 1import src.person
 2
 3
 4def assert_say_hello_works(
 5        first_name, last_name,
 6        year_of_birth,
 7    ):
 8    reality = src.person.say_hello(
 9        first_name=first_name,
10        last_name=last_name,
11        year_of_birth=year_of_birth,
12    )
13    my_expectation = (
14        f'Hello, my name is {first_name}'
15        f' {last_name} and I am'
16        f' {2026-year_of_birth}.'
17    )
18    assert reality == my_expectation
person/tests/test_person.py
21def assert_person_factory_works(
22        first_name, last_name,
23        sex, year_of_birth
24    ):
25    reality = src.person.person(
26        first_name=first_name,
27        last_name=last_name,
28        sex=sex,
29        year_of_birth=year_of_birth,
30    )
31    my_expectation = (
32        f'{first_name}, {last_name},'
33        f' {sex}, {year_of_birth}'
34    )
35    assert reality == my_expectation
person/tests/test_person.py
38def test_joe():
39    first_name = 'joe'
40    last_name = 'blow'
41    sex = 'M'
42    year_of_birth = 1996
43
44    assert_person_factory_works(
45        first_name=first_name,
46        last_name=last_name,
47        sex=sex,
48        year_of_birth=year_of_birth,
49    )
50
51    assert_say_hello_works(
52        first_name=first_name,
53        last_name=last_name,
54        year_of_birth=year_of_birth,
55    )
person/tests/test_person.py
58def test_jane():
59    first_name = 'jane'
60    last_name = 'doe'
61    sex = 'F'
62    year_of_birth = 1991
63
64    assert_person_factory_works(
65        first_name=first_name,
66        last_name=last_name,
67        sex=sex,
68        year_of_birth=year_of_birth,
69    )
70
71    assert_say_hello_works(
72        first_name=first_name,
73        last_name=last_name,
74        year_of_birth=year_of_birth,
75    )
person/tests/test_person.py
78def test_john():
79    first_name = 'john'
80    last_name = 'smith'
81    sex = 'M'
82    year_of_birth = 1580
83
84    assert_person_factory_works(
85        first_name=first_name,
86        last_name=last_name,
87        sex=sex,
88        year_of_birth=year_of_birth,
89    )
90
91    assert_say_hello_works(
92        first_name=first_name,
93        last_name=last_name,
94        year_of_birth=year_of_birth,
95    )
person/tests/test_person.py
 98def test_mary():
 99    first_name = 'mary'
100    last_name = 'public'
101    sex = 'F'
102    year_of_birth = 2000
103
104    assert_person_factory_works(
105        first_name=first_name,
106        last_name=last_name,
107        sex=sex,
108        year_of_birth=year_of_birth,
109    )
110
111    assert_say_hello_works(
112        first_name=first_name,
113        last_name=last_name,
114        year_of_birth=year_of_birth,
115    )
116
117
118# Exceptions seen
119# AssertionError
120# NameError
121# TypeError
122# AttributeError

how to make a person with f-strings: solutions


The code in person/src/person/__init__.py from how to make a person with f-strings

person/src/person/__init__.py
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    )
person/src/person/__init__.py
11def person(
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 an object: tests and solution

how to make a person with an object: tests


The code in person/tests/test_person.py from how to make a person with an object

person/tests/test_person.py
1import src.person
2
3
4def assert_equal(left, right):
5    assert left == right
person/tests/test_person.py
 8def assert_person_can_say_hello(
 9        first_name, last_name,
10        sex, year_of_birth,
11    ):
12    assert_equal(
13        src.person.Person(
14            first_name=first_name,
15            last_name=last_name,
16            sex=sex,
17            year_of_birth=year_of_birth,
18        ).say_hello(),
19        (
20            f'Hello, my name is {first_name}'
21            f' {last_name} and I am'
22            f' {2026-year_of_birth}.'
23        )
24    )
person/tests/test_person.py
27def assert_say_hello_works(
28        first_name, last_name,
29        year_of_birth,
30    ):
31    assert_equal(
32        src.person.say_hello(
33            first_name=first_name,
34            last_name=last_name,
35            year_of_birth=year_of_birth
36        ),
37        (
38            f'Hello, my name is {first_name}'
39            f' {last_name} and I am'
40            f' {2026-year_of_birth}.'
41        )
42    )
person/tests/test_person.py
45def assert_person_factory_works(
46        first_name, last_name,
47        sex, year_of_birth
48    ):
49    assert_equal(
50        src.person.person(
51            first_name=first_name,
52            last_name=last_name,
53            sex=sex,
54            year_of_birth=year_of_birth,
55        ),
56        (
57            f'{first_name}, {last_name},'
58            f' {sex}, {year_of_birth}'
59        )
60    )
person/tests/test_person.py
63def test_joe():
64    first_name = 'joe'
65    last_name = 'blow'
66    sex = 'M'
67    year_of_birth = 1996
68
69    assert_person_factory_works(
70        first_name=first_name,
71        last_name=last_name,
72        sex=sex,
73        year_of_birth=year_of_birth
74    )
75
76    assert_say_hello_works(
77        first_name=first_name,
78        last_name=last_name,
79        year_of_birth=year_of_birth,
80    )
81
82    assert_person_can_say_hello(
83        first_name=first_name,
84        last_name=last_name,
85        sex=sex,
86        year_of_birth=year_of_birth
87    )
person/tests/test_person.py
 90def test_jane():
 91    first_name = 'jane'
 92    last_name = 'doe'
 93    sex = 'F'
 94    year_of_birth = 1991
 95
 96    assert_person_factory_works(
 97        first_name=first_name,
 98        last_name=last_name,
 99        sex=sex,
100        year_of_birth=year_of_birth,
101    )
102
103    assert_say_hello_works(
104        first_name=first_name,
105        last_name=last_name,
106        year_of_birth=year_of_birth,
107    )
108
109    assert_person_can_say_hello(
110        first_name=first_name,
111        last_name=last_name,
112        sex=sex,
113        year_of_birth=year_of_birth,
114    )
person/tests/test_person.py
117def test_john():
118    first_name = 'john'
119    last_name = 'smith'
120    sex = 'M'
121    year_of_birth = 1580
122
123    assert_person_factory_works(
124        first_name=first_name,
125        last_name=last_name,
126        sex=sex,
127        year_of_birth=year_of_birth,
128    )
129
130    assert_say_hello_works(
131        first_name=first_name,
132        last_name=last_name,
133        year_of_birth=year_of_birth,
134    )
135
136    assert_person_can_say_hello(
137        first_name=first_name,
138        last_name=last_name,
139        sex=sex,
140        year_of_birth=year_of_birth,
141    )
person/tests/test_person.py
144def test_mary():
145    first_name = 'mary'
146    last_name = 'public'
147    sex = 'F'
148    year_of_birth = 2000
149
150    assert_person_factory_works(
151        first_name=first_name,
152        last_name=last_name,
153        sex=sex,
154        year_of_birth=year_of_birth,
155    )
156
157    assert_say_hello_works(
158        first_name=first_name,
159        last_name=last_name,
160        year_of_birth=year_of_birth,
161    )
162
163    assert_person_can_say_hello(
164        first_name=first_name,
165        last_name=last_name,
166        sex=sex,
167        year_of_birth=year_of_birth,
168    )
person/tests/test_person.py
171def test_dir_person_object():
172    assert_equal(
173        dir(src.person.Person),
174        [
175            '__class__', '__delattr__', '__dict__',
176            '__dir__', '__doc__', '__eq__',
177            '__firstlineno__', '__format__', '__ge__',
178            '__getattribute__', '__getstate__', '__gt__',
179            '__hash__', '__init__', '__init_subclass__',
180            '__le__', '__lt__', '__module__', '__ne__',
181            '__new__', '__reduce__', '__reduce_ex__',
182            '__repr__', '__setattr__', '__sizeof__',
183            '__static_attributes__', '__str__',
184            '__subclasshook__', '__weakref__', 'say_hello'
185        ]
186    )
person/tests/test_person.py
188def test_dir_person_instance():
189    assert_equal(
190        dir(
191            src.person.Person(
192                first_name='first_name',
193                last_name='last_name',
194                sex='M',
195                year_of_birth=2026,
196            )
197        ),
198        [
199            '__class__', '__delattr__', '__dict__',
200            '__dir__', '__doc__', '__eq__',
201            '__firstlineno__', '__format__', '__ge__',
202            '__getattribute__', '__getstate__', '__gt__',
203            '__hash__', '__init__', '__init_subclass__',
204            '__le__', '__lt__', '__module__', '__ne__',
205            '__new__', '__reduce__', '__reduce_ex__',
206            '__repr__', '__setattr__', '__sizeof__',
207            '__static_attributes__', '__str__',
208            '__subclasshook__', '__weakref__', 'first_name',
209            'last_name', 'say_hello', 'sex', 'year_of_birth',
210        ]
211    )
212
213
214# Exceptions seen
215# AssertionError
216# NameError
217# TypeError
218# AttributeError
219# SyntaxError

how to make a person with an object: solutions


The code in person/src/person/__init__.py from how to make a person with an object

person/src/person/__init__.py
 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        )
person/src/person/__init__.py
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 person(
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

person/tests/test_person.py
 1import src.person
 2import unittest
 3
 4
 5class TestPerson(unittest.TestCase):
 6
 7    def assert_person_factory_works(
 8            self, first_name, last_name,
 9            sex, year_of_birth
10        ):
11        self.assertEqual(
12            src.person.person(
13                first_name=first_name,
14                last_name=last_name,
15                sex=sex,
16                year_of_birth=year_of_birth,
17            ),
18            (
19                f'{first_name}, {last_name},'
20                f' {sex}, {year_of_birth}'
21            )
22        )
person/tests/test_person.py
24    def assert_say_hello_works(
25            self, first_name, last_name,
26            year_of_birth,
27        ):
28        self.assertEqual(
29            src.person.say_hello(
30                first_name=first_name,
31                last_name=last_name,
32                year_of_birth=year_of_birth
33            ),
34            (
35                f'Hello, my name is {first_name}'
36                f' {last_name} and I am'
37                f' {2026-year_of_birth}.'
38            )
39        )
person/tests/test_person.py
41    def assert_person_can_say_hello(
42            self,first_name, last_name,
43            sex, year_of_birth,
44        ):
45        self.assertEqual(
46            src.person.Person(
47                first_name=first_name,
48                last_name=last_name,
49                sex=sex,
50                year_of_birth=year_of_birth,
51            ).say_hello(),
52            (
53                f'Hello, my name is {first_name}'
54                f' {last_name} and I am'
55                f' {2026-year_of_birth}.'
56            )
57        )
person/tests/test_person.py
59    def test_joe(self):
60        first_name = 'joe'
61        last_name = 'blow'
62        sex = 'M'
63        year_of_birth = 1996
64
65        self.assert_person_factory_works(
66            first_name=first_name,
67            last_name=last_name,
68            sex=sex,
69            year_of_birth=year_of_birth
70        )
71
72        self.assert_say_hello_works(
73            first_name=first_name,
74            last_name=last_name,
75            year_of_birth=year_of_birth,
76        )
77
78        self.assert_person_can_say_hello(
79            first_name=first_name,
80            last_name=last_name,
81            sex=sex,
82            year_of_birth=year_of_birth
83        )
person/tests/test_person.py
 85    def test_jane(self):
 86        first_name = 'jane'
 87        last_name = 'doe'
 88        sex = 'F'
 89        year_of_birth = 1991
 90
 91        self.assert_person_factory_works(
 92            first_name=first_name,
 93            last_name=last_name,
 94            sex=sex,
 95            year_of_birth=year_of_birth,
 96        )
 97
 98        self.assert_say_hello_works(
 99            first_name=first_name,
100            last_name=last_name,
101            year_of_birth=year_of_birth,
102        )
103
104        self.assert_person_can_say_hello(
105            first_name=first_name,
106            last_name=last_name,
107            sex=sex,
108            year_of_birth=year_of_birth,
109        )
person/tests/test_person.py
111    def test_john(self):
112        first_name = 'john'
113        last_name = 'smith'
114        sex = 'M'
115        year_of_birth = 1580
116
117        self.assert_person_factory_works(
118            first_name=first_name,
119            last_name=last_name,
120            sex=sex,
121            year_of_birth=year_of_birth,
122        )
123
124        self.assert_say_hello_works(
125            first_name=first_name,
126            last_name=last_name,
127            year_of_birth=year_of_birth,
128        )
129
130        self.assert_person_can_say_hello(
131            first_name=first_name,
132            last_name=last_name,
133            sex=sex,
134            year_of_birth=year_of_birth,
135        )
person/tests/test_person.py
137    def test_mary(self):
138        first_name = 'mary'
139        last_name = 'public'
140        sex = 'F'
141        year_of_birth = 2000
142
143        self.assert_person_factory_works(
144            first_name=first_name,
145            last_name=last_name,
146            sex=sex,
147            year_of_birth=year_of_birth,
148        )
149
150        self.assert_say_hello_works(
151            first_name=first_name,
152            last_name=last_name,
153            year_of_birth=year_of_birth,
154        )
155
156        self.assert_person_can_say_hello(
157            first_name=first_name,
158            last_name=last_name,
159            sex=sex,
160            year_of_birth=year_of_birth,
161        )
person/tests/test_person.py
163    def test_dir_person_object(self):
164        self.assertEqual(
165            dir(src.person.Person),
166            [
167                '__class__', '__delattr__', '__dict__',
168                '__dir__', '__doc__', '__eq__',
169                '__firstlineno__', '__format__', '__ge__',
170                '__getattribute__', '__getstate__', '__gt__',
171                '__hash__', '__init__', '__init_subclass__',
172                '__le__', '__lt__', '__module__', '__ne__',
173                '__new__', '__reduce__', '__reduce_ex__',
174                '__repr__', '__setattr__', '__sizeof__',
175                '__static_attributes__', '__str__',
176                '__subclasshook__', '__weakref__', 'say_hello'
177            ]
178        )
person/tests/test_person.py
180    def test_dir_person_instance(self):
181        self.assertEqual(
182            dir(
183                src.person.Person(
184                    first_name='first_name',
185                    last_name='last_name',
186                    sex='M',
187                    year_of_birth=2026,
188                )
189            ),
190            [
191                '__class__', '__delattr__', '__dict__',
192                '__dir__', '__doc__', '__eq__',
193                '__firstlineno__', '__format__', '__ge__',
194                '__getattribute__', '__getstate__', '__gt__',
195                '__hash__', '__init__', '__init_subclass__',
196                '__le__', '__lt__', '__module__', '__ne__',
197                '__new__', '__reduce__', '__reduce_ex__',
198                '__repr__', '__setattr__', '__sizeof__',
199                '__static_attributes__', '__str__',
200                '__subclasshook__', '__weakref__', 'first_name',
201                'last_name', 'say_hello', 'sex', 'year_of_birth',
202            ]
203        )
204
205
206# Exceptions seen
207# AssertionError
208# NameError
209# TypeError
210# AttributeError
211# SyntaxError

test person with datetime: tests and solutions

test person with datetime: tests


The code added to person/tests/test_person.py from test person with datetime

person/tests/test_person.py
 1import datetime
 2import src.person
 3import unittest
 4
 5
 6class TestPerson(unittest.TestCase):
 7
 8    this_year = datetime.date.today().year
 9
10    def assert_person_factory_works(
11            self, first_name, last_name,
12            sex, year_of_birth
13        ):
person/tests/test_person.py
27    def calculate_age(self, year_of_birth):
28        return self.this_year - year_of_birth
person/tests/test_person.py
30    def assert_say_hello_works(
31            self, first_name, last_name,
32            year_of_birth,
33        ):
34        self.assertEqual(
35            src.person.say_hello(
36                first_name=first_name,
37                last_name=last_name,
38                year_of_birth=year_of_birth
39            ),
40            (
41                f'Hello, my name is {first_name}'
42                f' {last_name} and I am'
43                f' {self.calculate_age(year_of_birth)}.'
44            )
45        )
person/tests/test_person.py
47    def assert_person_can_say_hello(
48            self,first_name, last_name,
49            sex, year_of_birth,
50        ):
51        self.assertEqual(
52            src.person.Person(
53                first_name=first_name,
54                last_name=last_name,
55                sex=sex,
56                year_of_birth=year_of_birth,
57            ).say_hello(),
58            (
59                f'Hello, my name is {first_name}'
60                f' {last_name} and I am'
61                f' {self.calculate_age(year_of_birth)}.'
62            )
63        )
person/tests/test_person.py
117    def test_john(self):
118        first_name = 'john'
119        last_name = 'smith'
120        sex = 'M'
121        year_of_birth = 1980
122
123        self.assert_person_factory_works(
124            first_name=first_name,
125            last_name=last_name,
126            sex=sex,
127            year_of_birth=year_of_birth,
128        )
129
130        self.assert_say_hello_works(
131            first_name=first_name,
132            last_name=last_name,
133            year_of_birth=year_of_birth,
134        )
135
136        self.assert_person_can_say_hello(
137            first_name=first_name,
138            last_name=last_name,
139            sex=sex,
140            year_of_birth=year_of_birth,
141        )
person/tests/test_person.py
169    def test_when_person_is_older_than_120(self):
170        src.person.Person(
171            first_name='first_name',
172            last_name='last_name',
173            sex='M',
174            year_of_birth=self.this_year-121
175        )
176        # ).say_hello() fails
177        # because age > 120
person/tests/test_person.py
179    def test_when_year_of_birth_is_the_future(self):
180        src.person.Person(
181            first_name='first_name',
182            last_name='last_name',
183            sex='F',
184            year_of_birth=self.this_year+1,
185        )
186        # ).say_hello() fails
187        # because age < 0
person/tests/test_person.py
189    def test_when_year_of_birth_is_not_an_integer(self):
190        src.person.Person(
191            first_name='first_name',
192            last_name='last_name',
193            sex='M',
194            # year_of_birth=None,     # fails
195            # year_of_birth=2026.0,   # fails
196            # year_of_birth='2026',   # fails
197            # year_of_birth=(2026,),  # fails
198        )
199        # ).say_hello() fails
200        # because year_of_birth is not an integer
201
202    def test_dir_person_object(self):

test person with datetime: solutions


The code in person/src/person/__init__.py from test person with datetime

src/person/__init__.py
 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
src/person/__init__.py
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        )
src/person/__init__.py
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 >= 0
30    assert age <= 120
31    return age
src/person/__init__.py
34def say_hello(
35        first_name, last_name, year_of_birth
36    ):
37    return (
38        f'Hello, my name is {first_name}'
39        f' {last_name} and I am'
40        f' {calculate_age(year_of_birth)}.'
41    )
src/person/__init__.py
44def person(
45        first_name, last_name,
46        sex, year_of_birth,
47    ):
48    return (
49        f'{first_name}, {last_name},'
50        f' {sex}, {year_of_birth}'
51    )

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

person/tests/test_person.py
65    def test_joe(self):
66        first_name = 'joe'
67        last_name = 'blow'
68        sex = 'M'
69        year_of_birth = 1996
70
71        self.assert_person_factory_works(
72            first_name=first_name,
73            last_name=last_name,
74            sex=sex,
75            year_of_birth=year_of_birth
76        )
77
78        self.assert_say_hello_works(
79            first_name=first_name,
80            last_name=last_name,
81            year_of_birth=year_of_birth,
82        )
83
84        self.assert_person_can_say_hello(
85            first_name=first_name,
86            last_name=last_name,
87            sex=sex,
88            year_of_birth=year_of_birth
89        )
90
91        joe = src.person.Person(
92            first_name=first_name,
93            last_name=last_name,
94            sex=sex,
95            year_of_birth=year_of_birth,
96        )
97        self.assertEqual(joe.can_vote(), True)
98        self.assertEqual(joe.can_get_license(), False)
person/tests/test_person.py
100    def test_jane(self):
101        first_name = 'jane'
102        last_name = 'doe'
103        sex = 'F'
104        year_of_birth = 1991
105
106        self.assert_person_factory_works(
107            first_name=first_name,
108            last_name=last_name,
109            sex=sex,
110            year_of_birth=year_of_birth,
111        )
112
113        self.assert_say_hello_works(
114            first_name=first_name,
115            last_name=last_name,
116            year_of_birth=year_of_birth,
117        )
118
119        self.assert_person_can_say_hello(
120            first_name=first_name,
121            last_name=last_name,
122            sex=sex,
123            year_of_birth=year_of_birth,
124        )
125
126        jane = src.person.Person(
127            first_name=first_name,
128            last_name=last_name,
129            sex=sex,
130            year_of_birth=year_of_birth,
131            passed_test=True,
132        )
133        self.assertEqual(jane.can_vote(), True)
134        self.assertEqual(jane.can_get_license(), True)
person/tests/test_person.py
136    def test_john(self):
137        first_name = 'john'
138        last_name = 'smith'
139        sex = 'M'
140        year_of_birth = 1980
141
142        self.assert_person_factory_works(
143            first_name=first_name,
144            last_name=last_name,
145            sex=sex,
146            year_of_birth=year_of_birth,
147        )
148
149        self.assert_say_hello_works(
150            first_name=first_name,
151            last_name=last_name,
152            year_of_birth=year_of_birth,
153        )
154
155        self.assert_person_can_say_hello(
156            first_name=first_name,
157            last_name=last_name,
158            sex=sex,
159            year_of_birth=year_of_birth,
160        )
161
162        john = src.person.Person(
163            first_name=first_name,
164            last_name=last_name,
165            sex=sex,
166            year_of_birth=year_of_birth,
167            is_citizen=False,
168            passed_test=False,
169        )
170        self.assertEqual(john.can_vote(), False)
171        self.assertEqual(john.can_get_license(), False)
person/tests/test_person.py
173    def test_mary(self):
174        first_name = 'mary'
175        last_name = 'public'
176        sex = 'F'
177        year_of_birth = 2000
178
179        self.assert_person_factory_works(
180            first_name=first_name,
181            last_name=last_name,
182            sex=sex,
183            year_of_birth=year_of_birth,
184        )
185
186        self.assert_say_hello_works(
187            first_name=first_name,
188            last_name=last_name,
189            year_of_birth=year_of_birth,
190        )
191
192        self.assert_person_can_say_hello(
193            first_name=first_name,
194            last_name=last_name,
195            sex=sex,
196            year_of_birth=year_of_birth,
197        )
198
199        mary = src.person.Person(
200            first_name=first_name,
201            last_name=last_name,
202            sex=sex,
203            year_of_birth=year_of_birth,
204            is_citizen=False,
205            passed_test=True,
206        )
207        self.assertEqual(mary.can_vote(), False)
208        self.assertEqual(mary.can_get_license(), True)
person/tests/test_person.py
210    def test_underage_citizen(self):
211        underage = src.person.Person(
212            first_name='first_name',
213            last_name='last_name',
214            sex='M',
215            year_of_birth=self.this_year-17,
216            is_citizen=True,
217            passed_test=True,
218        )
219        self.assertEqual(underage.can_vote(), False)
220        self.assertEqual(underage.can_get_license(), False)
person/tests/test_person.py
222    @unittest.skip('fails because age > 120')
223    def test_when_person_is_older_than_120(self):
224        src.person.Person(
225            first_name='first_name',
226            last_name='last_name',
227            sex='M',
228            year_of_birth=self.this_year-121
229        )
person/tests/test_person.py
231    @unittest.skip('fails because age < 0')
232    def test_when_year_of_birth_is_the_future(self):
233        src.person.Person(
234            first_name='first_name',
235            last_name='last_name',
236            sex='F',
237            year_of_birth=self.this_year+1,
238        )
person/tests/test_person.py
240    @unittest.skip(
241        'fails because year_of_birth is not an integer'
242    )
243    def test_when_year_of_birth_is_not_an_integer(self):
244        src.person.Person(
245            first_name='first_name',
246            last_name='last_name',
247            sex='M',
248            # year_of_birth=None,     # fails
249            # year_of_birth=2026.0,   # fails
250            # year_of_birth='2026',   # fails
251            # year_of_birth=(2026,),  # fails
252        )
person/tests/test_person.py
254    def test_dir_person_object(self):
255        self.assertEqual(
256            dir(src.person.Person),
257            [
258                '__class__', '__delattr__', '__dict__',
259                '__dir__', '__doc__', '__eq__',
260                '__firstlineno__', '__format__', '__ge__',
261                '__getattribute__', '__getstate__', '__gt__',
262                '__hash__', '__init__', '__init_subclass__',
263                '__le__', '__lt__', '__module__', '__ne__',
264                '__new__', '__reduce__', '__reduce_ex__',
265                '__repr__', '__setattr__', '__sizeof__',
266                '__static_attributes__', '__str__',
267                '__subclasshook__', '__weakref__',
268                'can_get_license', 'can_vote', 'check_age',
269                'say_hello',
270            ]
271        )
person/tests/test_person.py
273    def test_dir_person_instance(self):
274        self.assertEqual(
275            dir(
276                src.person.Person(
277                    first_name='first_name',
278                    last_name='last_name',
279                    sex='M',
280                    year_of_birth=2026,
281                )
282            ),
283            [
284                '__class__', '__delattr__', '__dict__',
285                '__dir__', '__doc__', '__eq__',
286                '__firstlineno__', '__format__', '__ge__',
287                '__getattribute__', '__getstate__', '__gt__',
288                '__hash__', '__init__', '__init_subclass__',
289                '__le__', '__lt__', '__module__', '__ne__',
290                '__new__', '__reduce__', '__reduce_ex__',
291                '__repr__', '__setattr__', '__sizeof__',
292                '__static_attributes__', '__str__',
293                '__subclasshook__', '__weakref__',
294                'age', 'can_get_license', 'can_vote',
295                'check_age', 'first_name', 'is_citizen',
296                'last_name', 'passed_test', 'say_hello',
297                'sex', 'year_of_birth',
298            ]
299        )
300
301
302# Exceptions seen
303# AssertionError
304# NameError
305# TypeError
306# AttributeError
307# SyntaxError

how to make a person with conditions: solutions


The code in person/src/person/__init__.py after how to make a person with conditions

person/src/person/__init__.py
 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, passed_test=False,
10        ):
11        self.first_name = first_name
12        self.last_name = last_name
13        self.year_of_birth = year_of_birth
14        self.sex = sex
15        self.is_citizen = is_citizen
16        self.passed_test = passed_test
17        self.age = calculate_age(year_of_birth)
person/src/person/__init__.py
19    def check_age(self, status):
20        if self.age < 18:
21            return False
22        return status
23
24    def can_get_license(self):
25        return self.check_age(self.passed_test)
person/src/person/__init__.py
27    def can_vote(self):
28        return self.check_age(self.is_citizen)
29
30    def say_hello(self):
31        return (
32            f'Hello, my name is {self.first_name}'
33            f' {self.last_name} and I am {self.age}.'
34        )
person/src/person/__init__.py
37def calculate_age(year_of_birth):
38    assert isinstance(year_of_birth, int)
39    age = (
40        datetime.date.today().year
41      - year_of_birth
42    )
43    assert age >= 0
44    assert age <= 120
45    return age
person/src/person/__init__.py
48def say_hello(
49        first_name, last_name, year_of_birth
50    ):
51    return (
52        f'Hello, my name is {first_name}'
53        f' {last_name} and I am'
54        f' {calculate_age(year_of_birth)}.'
55    )
person/src/person/__init__.py
58def person(
59        first_name, last_name,
60        sex, year_of_birth,
61    ):
62    return (
63        f'{first_name}, {last_name},'
64        f' {sex}, {year_of_birth}'
65    )

how to make a person with Exceptions: tests and solutions

how to make a person with Exceptions: tests


The tests I changed in person/tests/test_person.py after how to make a person with Exceptions

person/tests/test_person.py
222    def test_when_person_is_older_than_120(self):
223        try:
224            src.person.Person(
225                first_name='first_name',
226                last_name='last_name',
227                sex='M',
228                year_of_birth=self.this_year-121
229            )
230        except ValueError:
231            pass
person/tests/test_person.py
233    def test_when_year_of_birth_is_the_future(self):
234        try:
235            src.person.Person(
236                first_name='first_name',
237                last_name='last_name',
238                sex='F',
239                year_of_birth=self.this_year+1,
240            )
241        except ValueError:
242            pass
person/tests/test_person.py
244    def test_when_year_of_birth_is_not_an_integer(self):
245        try:
246            src.person.Person(
247                first_name='first_name',
248                last_name='last_name',
249                sex='M',
250                year_of_birth=None,
251            )
252        except TypeError:
253            pass
person/tests/test_person.py
255        try:
256            src.person.Person(
257                first_name='first_name',
258                last_name='last_name',
259                sex='F',
260                year_of_birth=2026.0,
261            )
262        except TypeError:
263            pass
person/tests/test_person.py
265        try:
266            src.person.Person(
267                first_name='first_name',
268                last_name='last_name',
269                sex='M',
270                year_of_birth='2026',
271            )
272        except TypeError:
273            pass
person/tests/test_person.py
275        try:
276            src.person.Person(
277                first_name='first_name',
278                last_name='last_name',
279                sex='F',
280                year_of_birth=(2026,),
281            )
282        except TypeError:
283            pass

how to make a person with Exceptions: solutions


The code in person/src/person/__init__.py after how to make a person with Exceptions

person/src/person/__init__.py
 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, passed_test=False,
10        ):
11        self.first_name = first_name
12        self.last_name = last_name
13        self.year_of_birth = year_of_birth
14        self.sex = sex
15        self.is_citizen = is_citizen
16        self.passed_test = passed_test
17        self.age = calculate_age(year_of_birth)
person/src/person/__init__.py
19    def check_age(self, status):
20        if self.age < 18:
21            return False
22        return status
23
24    def can_get_license(self):
25        return self.check_age(self.passed_test)
person/src/person/__init__.py
27    def can_vote(self):
28        return self.check_age(self.is_citizen)
29
30    def say_hello(self):
31        return (
32            f'Hello, my name is {self.first_name}'
33            f' {self.last_name} and I am {self.age}.'
34        )
person/src/person/__init__.py
37def calculate_age(year_of_birth):
38    if not isinstance(year_of_birth, int):
39        raise TypeError
40
41    age = (
42        datetime.date.today().year
43      - year_of_birth
44    )
45
46    if age < 0 or age > 120:
47        raise ValueError
48    return age
person/src/person/__init__.py
51def say_hello(
52        first_name, last_name, year_of_birth
53    ):
54    return (
55        f'Hello, my name is {first_name}'
56        f' {last_name} and I am'
57        f' {calculate_age(year_of_birth)}.'
58    )
person/src/person/__init__.py
61def person(
62        first_name, last_name,
63        sex, year_of_birth,
64    ):
65    return (
66        f'{first_name}, {last_name},'
67        f' {sex}, {year_of_birth}'
68    )

how to make a person with loops: tests and solutions

how to make a person with loops: tests


The tests I changed in person/tests/test_person.py after how to make a person with loops

person/tests/test_person.py
 1import datetime
 2import src.person
 3import unittest
 4
 5
 6class TestPerson(unittest.TestCase):
 7
 8    this_year = datetime.date.today().year
 9    younger_than_18 = this_year - 17
10    not_younger_than_18 = this_year - 18
11
12    first_name = 'first_name'
13    last_name = 'last_name'
14    sex = 'M'
person/tests/test_person.py
16    people = (
17        (
18            # is_citizen, passed_test
19            not_younger_than_18, True, True,
20            True, True,
21        ),
22        (
23            # is_citizen, not passed_test
24            not_younger_than_18, True, False,
25            True, False,
26        ),
27        (
28            # not is_citizen, passed_test
29            not_younger_than_18, False, True,
30            False, True,
31        ),
person/tests/test_person.py
32        (
33            # not is_citizen, not passed_test
34            not_younger_than_18, False, False,
35            False, False,
36        ),
37        (
38            # underage, is_citizen, passed_test
39            younger_than_18, True, True,
40            False, False,
41        ),
42        (
43            # underage, not is_citizen, passed_test
44            younger_than_18, False, True,
45            False, False,
46        ),
person/tests/test_person.py
47        (
48            # underage, is_citizen, not passed_test
49            younger_than_18, True, False,
50            False, False,
51        ),
52        (
53            # underage, not is_citizen, not passed_test
54            younger_than_18, False, False,
55            False, False,
56        ),
57        # year_of_birth is not an integer
58        (None, False, False,),
59        (2026.0, False, False,),
60        ('2026', False, False,),
61        ((2026,), False, False,),
62    )
63
64    def calculate_age(self, year_of_birth):
65        return self.this_year - year_of_birth
person/tests/test_person.py
67    def test_person_object(self):
68        for a_person in self.people:
69            year_of_birth = a_person[0]
70
71            with self.subTest(a_person):
72                try:
73                    an_instance = src.person.Person(
74                        first_name=self.first_name,
75                        last_name=self.last_name,
76                        sex=self.sex,
77                        year_of_birth=year_of_birth,
78                        is_citizen=a_person[1],
79                        passed_test=a_person[2],
80                    )
81                except TypeError as error:
82                    self.assertEqual(
83                        error.args[0],
84                        f"'{year_of_birth}' is not an integer"
85                    )
86                else:
87                    self.assertEqual(
88                        an_instance.say_hello(),
89                        (
90                            f'Hello, my name is {self.first_name}'
91                            f' {self.last_name} and I am'
92                            f' {self.calculate_age(year_of_birth)}.'
93                        )
94                    )
95                    self.assertEqual(an_instance.can_vote(), a_person[3])
96                    self.assertEqual(
97                        an_instance.can_get_license(), a_person[4]
98                    )
99
person/tests/test_person.py
100    def test_say_hello_function(self):
101        for a_person in self.people:
102            year_of_birth = a_person[0]
103
104            with self.subTest(a_person):
105                try:
106                    reality = src.person.say_hello(
107                        first_name=self.first_name,
108                        last_name=self.last_name,
109                        year_of_birth=year_of_birth,
110                    )
111                except TypeError as error:
112                    self.assertEqual(
113                        error.args[0],
114                        f"'{year_of_birth}' is not an integer"
115                    )
116                else:
117                    self.assertEqual(
118                        reality,
119                        (
120                            f'Hello, my name is {self.first_name}'
121                            f' {self.last_name} and I am'
122                            f' {self.calculate_age(year_of_birth)}.'
123                        )
124                    )
person/tests/test_person.py
126    def test_person_factory(self):
127        for a_person in self.people:
128            year_of_birth = a_person[0]
129
130            with self.subTest(a_person):
131                self.assertEqual(
132                    src.person.person(
133                        first_name=self.first_name,
134                        last_name=self.last_name,
135                        sex=self.sex,
136                        year_of_birth=year_of_birth,
137                    ),
138                    (
139                        f'{self.first_name}, {self.last_name},'
140                        f' {self.sex}, {year_of_birth}'
141                    )
142                )
143
person/tests/test_person.py
144    def test_if_person_is_not_alive(self):
145        for year_of_birth in (
146            self.this_year+1,
147            self.this_year-121,
148        ):
149            age = self.calculate_age(year_of_birth)
150
151            with (
152                self.subTest(year_of_birth),
153                self.assertRaisesRegex(
154                    ValueError,
155                    f'a {age} year old person is not alive.'
156                ),
157            ):
158                src.person.Person(
159                    first_name=self.first_name,
160                    last_name=self.last_name,
161                    sex=self.sex,
162                    year_of_birth=year_of_birth,
163                )
person/tests/test_person.py
165    def test_dir_person_object(self):
166        self.assertEqual(
167            dir(src.person.Person),
168            [
169                '__class__', '__delattr__', '__dict__',
170                '__dir__', '__doc__', '__eq__',
171                '__firstlineno__', '__format__', '__ge__',
172                '__getattribute__', '__getstate__', '__gt__',
173                '__hash__', '__init__', '__init_subclass__',
174                '__le__', '__lt__', '__module__', '__ne__',
175                '__new__', '__reduce__', '__reduce_ex__',
176                '__repr__', '__setattr__', '__sizeof__',
177                '__static_attributes__', '__str__',
178                '__subclasshook__', '__weakref__',
179                'can_get_license', 'can_vote', 'check_age',
180                'say_hello',
181            ]
182        )
person/tests/test_person.py
184    def test_dir_person_instance(self):
185        self.assertEqual(
186            dir(
187                src.person.Person(
188                    first_name='first_name',
189                    last_name='last_name',
190                    sex='M',
191                    year_of_birth=2026,
192                )
193            ),
194            [
195                '__class__', '__delattr__', '__dict__',
196                '__dir__', '__doc__', '__eq__',
197                '__firstlineno__', '__format__', '__ge__',
198                '__getattribute__', '__getstate__', '__gt__',
199                '__hash__', '__init__', '__init_subclass__',
200                '__le__', '__lt__', '__module__', '__ne__',
201                '__new__', '__reduce__', '__reduce_ex__',
202                '__repr__', '__setattr__', '__sizeof__',
203                '__static_attributes__', '__str__',
204                '__subclasshook__', '__weakref__',
205                'age', 'can_get_license', 'can_vote',
206                'check_age', 'first_name', 'is_citizen',
207                'last_name', 'passed_test', 'say_hello',
208                'sex', 'year_of_birth',
209            ]
210        )
person/tests/test_person.py
213# Exceptions seen
214# AssertionError
215# NameError
216# TypeError
217# AttributeError
218# SyntaxError

how to make a person with loops: solutions


The code in person/src/person/__init__.py after how to make a person with loops

person/src/person/__init__.py
 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, passed_test=False,
10        ):
11        self.first_name = first_name
12        self.last_name = last_name
13        self.year_of_birth = year_of_birth
14        self.sex = sex
15        self.is_citizen = is_citizen
16        self.passed_test = passed_test
17        self.age = calculate_age(year_of_birth)
person/src/person/__init__.py
19    def check_age(self, status):
20        if self.age < 18:
21            return False
22        return status
23
24    def can_get_license(self):
25        return self.check_age(self.passed_test)
person/src/person/__init__.py
27    def can_vote(self):
28        return self.check_age(self.is_citizen)
29
30    def say_hello(self):
31        return (
32            f'Hello, my name is {self.first_name}'
33            f' {self.last_name} and I am {self.age}.'
34        )
person/src/person/__init__.py
37def calculate_age(year_of_birth):
38    if not isinstance(year_of_birth, int):
39        raise TypeError(
40            f"'{year_of_birth}' is not an integer"
41        )
42
43    age = (
44        datetime.date.today().year
45      - year_of_birth
46    )
47
48    if age < 0 or age > 120:
49        raise ValueError(
50            f'a {age} year old person is not alive.'
51        )
52    return age
person/src/person/__init__.py
55def say_hello(
56        first_name, last_name, year_of_birth
57    ):
58    return (
59        f'Hello, my name is {first_name}'
60        f' {last_name} and I am'
61        f' {calculate_age(year_of_birth)}.'
62    )
person/src/person/__init__.py
65def person(
66        first_name, last_name,
67        sex, year_of_birth,
68    ):
69    return (
70        f'{first_name}, {last_name},'
71        f' {sex}, {year_of_birth}'
72    )

test person with lists: tests

I change these tests in test person with lists

person/tests/test_person.py
165    def test_dir_person_object(self):
166        dir_person = dir(object)
167        dir_person.extend((
168            '__dict__', '__firstlineno__',
169            '__module__', '__static_attributes__',
170            '__weakref__',
171            'can_get_license', 'can_vote',
172            'check_age', 'say_hello',
173        ))
174        dir_person.sort()
175        self.assertEqual(
176            dir(src.person.Person),
177            dir_person
178        )
person/tests/test_person.py
180    def test_dir_person_instance(self):
181        self.maxDiff = None
182        dir_person_instance = dir(src.person.Person)
183        dir_person_instance.extend((
184            'age', 'first_name', 'is_citizen',
185            'last_name', 'passed_test',
186            'sex', 'year_of_birth',
187        ))
188        dir_person_instance.sort()
189        self.assertEqual(
190            dir(
191                src.person.Person(
192                    first_name='first_name',
193                    last_name='last_name',
194                    sex='M',
195                    year_of_birth=2026,
196                )
197            ),
198            dir_person_instance
199        )
200
201
202# Exceptions seen
203# AssertionError
204# NameError
205# TypeError
206# AttributeError
207# SyntaxError