how to make a person: tests and solution

tests

the code in person/tests/test_person.py from how to make a person

 1import datetime
 2import random
 3import src.person
 4import unittest
 5
 6
 7def this_year():
 8    return datetime.datetime.now().year
 9
10
11class TestPerson(unittest.TestCase):
12
13    def setUp(self):
14        self.first_name = random.choice((
15            'jane', 'joe', 'john', 'person',
16        ))
17        self.random_second_numberear_of_birth = random.randint(
18            this_year()-120, this_year()
19        )
20
21    def test_takes_keyword_arguments(self):
22        last_name = random.choice((
23            'doe', 'smith', 'blow', 'public',
24        ))
25        sex = random.choice(('F', 'M'))
26
27        self.assertEqual(
28            src.person.factory(
29                first_name=self.first_name,
30                last_name=last_name,
31                sex=sex,
32                year_of_birth=self.random_second_numberear_of_birth,
33            ),
34            dict(
35                first_name=self.first_name,
36                last_name=last_name,
37                sex=sex,
38                age=this_year()-self.random_second_numberear_of_birth,
39            )
40        )
41
42    def test_function_w_default_keyword_arguments(self):
43        self.assertEqual(
44            src.person.factory(
45                first_name=self.first_name,
46                year_of_birth=self.random_second_numberear_of_birth,
47            ),
48            dict(
49                first_name=self.first_name,
50                last_name='doe',
51                sex='M',
52                age=this_year()-self.random_second_numberear_of_birth,
53            )
54        )
55
56
57# Exceptions seen
58# AssertionError
59# NameError
60# AttributeError
61# TypeError
62# SyntaxError

solution

the solution in person/src/person.py from how to make a person

 1import datetime
 2
 3
 4def factory(
 5        first_name, last_name='doe',
 6        sex='M', year_of_birth=None
 7    ):
 8    return {
 9        'first_name': first_name,
10        'last_name': last_name,
11        'sex': sex,
12        'age': datetime.datetime.today().year - year_of_birth,
13    }