how to make a person: tests and solution

how to make a person with strings: tests

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

     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

  • the code from person/tests/test_person.py from how to make a person with f-strings

    import src.person
    
    
    def test_joe():
        first_name = 'joe'
        last_name = 'blow'
        sex = 'M'
        year_of_birth = 1996
    
        reality = src.person.factory(
            first_name=first_name,
            last_name=last_name,
            sex=sex,
            year_of_birth=year_of_birth,
        )
        my_expectation = (
            f'{first_name}, {last_name},'
            f' {sex}, {year_of_birth}'
        )
        assert reality == my_expectation
    
        reality = src.person.say_hi(
            first_name=first_name,
            last_name=last_name,
            year_of_birth=year_of_birth,
        )
        my_expectation = (
            f'Hi, my name is {first_name}'
            f' {last_name} and I am'
            f' {2026-year_of_birth}.'
        )
        assert reality == my_expectation
    
    
    def test_jane():
        first_name = 'jane'
        last_name = 'doe'
        sex = 'F'
        year_of_birth = 1991
    
        reality = src.person.factory(
            first_name=first_name,
            last_name=last_name,
            sex=sex,
            year_of_birth=year_of_birth,
        )
        my_expectation = (
            f'{first_name}, {last_name},'
            f' {sex}, {year_of_birth}'
        )
        assert reality == my_expectation
    
        reality = src.person.say_hi(
            first_name=first_name,
            last_name=last_name,
            year_of_birth=year_of_birth,
        )
        my_expectation = (
            f'Hi, my name is {first_name}'
            f' {last_name} and I am'
            f' {2026-year_of_birth}.'
        )
        assert reality == my_expectation
    
    
    def test_john():
        first_name = 'john'
        last_name = 'smith'
        sex = 'M'
        year_of_birth = 1580
    
        reality = src.person.factory(
            first_name=first_name,
            last_name=last_name,
            sex=sex,
            year_of_birth=year_of_birth,
        )
        my_expectation = (
            f'{first_name}, {last_name},'
            f' {sex}, {year_of_birth}'
        )
        assert reality == my_expectation
    
        reality = src.person.say_hi(
            first_name=first_name,
            last_name=last_name,
            year_of_birth=year_of_birth,
        )
        my_expectation = (
            f'Hi, my name is {first_name}'
            f' {last_name} and I am'
            f' {2026-year_of_birth}.'
        )
        assert reality == my_expectation
    
    
    def test_mary():
        first_name = 'mary'
        last_name = 'public'
        sex = 'F'
        year_of_birth = 2000
    
        reality = src.person.factory(
            first_name=first_name,
            last_name=last_name,
            sex=sex,
            year_of_birth=year_of_birth,
        )
        my_expectation = (
            f'{first_name}, {last_name},'
            f' {sex}, {year_of_birth}'
        )
        assert reality == my_expectation
    
        reality = src.person.say_hi(
            first_name=first_name,
            last_name=last_name,
            year_of_birth=year_of_birth,
        )
        my_expectation = (
            f'Hi, my name is {first_name}'
            f' {last_name} and I am'
            f' {2026-year_of_birth}.'
        )
        assert reality == my_expectation
    
    
    # Exceptions seen
    # AssertionError
    # NameError
    # TypeError
    # AttributeError
    
  • the code from person/src/person.py from how to make a person with f-strings

    def say_hi(
        first_name, last_name, year_of_birth
    ):
        return (
            f'Hi, my name is {first_name}'
            f' {last_name} and I am'
            f' {2026-year_of_birth}.'
        )
    
    
    def factory(
            first_name, last_name,
            sex, year_of_birth
        ):
        return (
            f'{first_name}, {last_name},'
            f' {sex}, {year_of_birth}'
        )