Skip to content

Commit

Permalink
Reorganize create_library code to be more readable.
Browse files Browse the repository at this point in the history
Tries to address #131 but stil fails. Maybe this is now related to #132?
  • Loading branch information
skytreader committed Jan 1, 2019
1 parent 254ea12 commit dc89f53
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
16 changes: 7 additions & 9 deletions librarian/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,17 @@ def create_library(

# Query books for their ids. Note that this bit assumes that the only books
# in the DB right now are those just-created.
books = session.query(Book.id, Book.isbn).all()
isbn_id_map = {isbn: book_id for book_id, isbn in books}
books = session.query(Book).all()
isbn_book_map = {book.isbn: book for book in books}

library = {}

# initialize the role fields
for isbn in book_isbns:
library[isbn] = {}
library[isbn] = {
"title": isbn_book_map[isbn].title,
"publisher": isbn_book_map[isbn].publisher.name
}
for role in roles:
library[isbn][role.name.lower()] = []

Expand All @@ -120,10 +123,6 @@ def create_library(
rand_role = random.choice(roles)
_role = rand_role.name.lower()

# slightly uneconomical: keep reassigning the same value, over and over
library[rand_isbn]["title"] = rand_book.title
library[rand_isbn]["publisher"] = rand_book.publisher.name
# these bits are NOT repeated
library[rand_isbn][_role].append(Person(lastname=rand_person.lastname,
firstname=rand_person.firstname))

Expand All @@ -139,9 +138,8 @@ def create_library(
for isbn in library.keys():
book = library[isbn]
book["isbn"] = isbn
book["id"] = isbn_id_map[isbn]
book["id"] = isbn_book_map[isbn].id
book["genre"] = "".join((random.choice(string.ascii_lowercase) for _ in range(8)))
app.logger.info("the book %s" % str(book))
library_list.insert(0, BookRecord(**book))

return library_list
4 changes: 1 addition & 3 deletions librarian/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ def get_bookrecord(book_id):
).filter(Book.publisher_id == BookCompany.id)
.filter(Book.genre_id == Genre.id)
).first()
app.logger.info("le book %s" % str(book))
app.logger.info("listify? %s" % str(list(book)))
# <3 duck typing
book = list(book)

Expand All @@ -162,9 +160,9 @@ def get_bookrecord(book_id):
book.insert(4, None)
# Role.name
book.insert(5, None)
app.logger.info("Asking to assemble %s" % book)
return BookRecord.assembler((book,), as_obj=False)[0]
else:
app.logger.info("Still can't find anything for %s" % book_id)
return None

@classmethod
Expand Down

0 comments on commit dc89f53

Please sign in to comment.