def lookup(isbn): '''Takes an isbn number and returns a dictionary of book metadata. minimal parameters is a list of strings. These strings are the attributes that we are querying for. The Book class stores them as "book.attribute". For example, "book.title".''' book = Book(isbn) book.getMetadata(minimal_parameters) # API request made here data = {} for param in minimal_parameters + ['isbn']: if hasattr(book, param): data[param] = getattr(book, param) return data
def test_api(): results = [] for isbn in tests: info = tests[isbn] print("Testing: '%s'" % (info['title'])) book = Book(isbn) assert book.isbn == isbn book.collect_all(minimal_parameters) # Gather all possible information for attribute in info: result = getattr(book, attribute) == info[attribute] print("\tIs %s correct?: %r" % (attribute, result)) assert result results.append(result) if all(results): print("All of the tests have passed.") else: print("Not all of the tests have passed.")