info_outline

Solutions will be available when this assignment is resolved, or after a few failing attempts.

Add book to purchase

Write a function add_book_to_purchase that receives a dictionary with purchase information and adds a book to the given purchase dict. Each purchase dict has a key books that contains a list of books. Your job is to append the book to that list.

Example:

purchase = {
    'id': 99,
    'books': [],  # Empty list of books
    'total': 0
}
add_book_to_purchase(
    purchase,
    title='The Odyssey',
    author='Homer',
    price=7.99)
print(purchase)

Prints:

{
    'id': 99,
    'books': [{
        'title': 'The Odyssey',
        'author': 'Homer',
        'price': 7.99
    }],  # The list of books has one book
    'total': 0
}

Test Cases

test add book to purchase base use case - Run Test

def test_add_book_to_purchase_base_use_case():
    purchase = {
        'id': 99,
        'books': [{
            'title': 'The Raven',
            'author': 'Edgar Allan Poe',
            'price': 19.99
        }, {
            'title': 'Ulysses',
            'author': 'James Joyce',
            'price': 23.99
        }],
        'total': 0
    }

    add_book_to_purchase(
        purchase,
        title='The Odyssey',
        author='Homer',
        price=7.99)

    assert len(purchase['books']) == 3
    assert purchase['books'] == [{
        'title': 'The Raven',
        'author': 'Edgar Allan Poe',
        'price': 19.99
    }, {
        'title': 'Ulysses',
        'author': 'James Joyce',
        'price': 23.99
    }, {
        'title': 'The Odyssey',
        'author': 'Homer',
        'price': 7.99
    }]

test add book to purchase default price - Run Test

def test_add_book_to_purchase_default_price():
    purchase = {
        'id': 99,
        'books': [{
            'title': 'The Raven',
            'author': 'Edgar Allan Poe',
            'price': 19.99
        }, {
            'title': 'Ulysses',
            'author': 'James Joyce',
            'price': 23.99
        }],
        'total': 0
    }

    add_book_to_purchase(
        purchase,
        title='The Odyssey',
        author='Homer')

    assert len(purchase['books']) == 3
    assert purchase['books'] == [{
        'title': 'The Raven',
        'author': 'Edgar Allan Poe',
        'price': 19.99
    }, {
        'title': 'Ulysses',
        'author': 'James Joyce',
        'price': 23.99
    }, {
        'title': 'The Odyssey',
        'author': 'Homer',
        'price': 0.99
    }]
def add_book_to_purchase(purchase_dict, title, author, price=0.99): pass