Skip to content

Commit

Permalink
More work on AddBookInteractor
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroanan committed Mar 26, 2014
1 parent 1b494e0 commit c408149
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
5 changes: 4 additions & 1 deletion aquarius/interactors/AddBookInteractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ def __init__(self, persistence):
self.__persistence = persistence

def execute(self, book):
self.__persistence.get_book_by_title_and_author(book)
b = self.__persistence.get_book_by_title_and_author(book)
if b.id == "":
self.__persistence.add_book(book)
#TODO: AddBookFormatInteractor
10 changes: 1 addition & 9 deletions aquarius/persistence/sqlitepersistence/AddBook.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@ def __init__(self, connection):
self.__connection = connection

def add_book(self, book):
self.__add_new_book_returning_its_id(book)
#book_from_db = GetBookByTitleAndAuthor(self.__connection).execute(book)
#if book_from_db.id == "":
#
#else:
# book.id = book_from_db.id
#self.__add_book_formats(book)

def __add_new_book_returning_its_id(self, book):
sql = "INSERT INTO Book (Title, Author) VALUES (?, ?)"
self.__connection.execute_sql_with_params(sql, (book.title, book.author))
return self.__connection.get_last_row_id()


def __add_book_formats(self, book):
for f in book.formats:
self.add_format(book, f)
Expand Down
24 changes: 21 additions & 3 deletions tests/interactors/TestAddBookInteractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ class TestAddBookInteractor(unittest.TestCase):

def test_execute_gets_book_from_persistence(self):
persistence = Mock(SqlitePersistence)
o = AddBookInteractor(persistence)
o.execute(Book())
self.assertTrue(persistence.get_book_by_title_and_author.called)
target = AddBookInteractor(persistence)
target.execute(Book())
self.assertTrue(persistence.get_book_by_title_and_author.called)

def test_execute_with_new_book_adds_it(self):
persistence = Mock(SqlitePersistence)
persistence.get_book_by_title_and_author = Mock(return_value=Book())
target = AddBookInteractor(persistence)
target.execute(Book())
self.assertTrue(persistence.add_book.called)

def test_execute_with_existing_book_does_not_add_it(self):
my_book = Book()
my_book.id = 1337
persistence = Mock(SqlitePersistence)
persistence.get_book_by_title_and_author = Mock(return_value=my_book)
target = AddBookInteractor(persistence)

target.execute(my_book)

self.assertFalse(persistence.add_book.called)

0 comments on commit c408149

Please sign in to comment.