Skip to content

Commit

Permalink
Format Exists query object
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroanan committed Apr 6, 2014
1 parent 062d605 commit 9acbeca
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 17 deletions.
9 changes: 1 addition & 8 deletions aquarius/persistence/sqlitepersistence/AddBook.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,4 @@ def add_book(self, book):
# def add_format(self, book, f):
# if not self.__format_exists(book, f):
# self.__add_new_format(book, f)
#
# def __format_exists(self, book, book_format):
# sql = "SELECT 1 FROM BookFormat WHERE Book=? AND Format=?"
# return len(self.__connection.execute_sql_fetch_all_with_params(sql, (book.id, book_format.Format))) > 0
#
# def __add_new_format(self, book, f):
# sql = "INSERT INTO BookFormat (Book, Format, Location) VALUES (?, ?, ?)"
# self.__connection.execute_sql_with_params(sql, (book.id, f.Format, f.Location))

8 changes: 8 additions & 0 deletions aquarius/persistence/sqlitepersistence/FormatExists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class FormatExists(object):

def __init__(self, connection):
self.__connection = connection

def execute(self, book_id, book_format):
sql = "SELECT 1 FROM BookFormat WHERE Book=? AND Format=?"
return len(self.__connection.execute_sql_fetch_all_with_params(sql, (book_id, book_format))) > 0
8 changes: 8 additions & 0 deletions aquarius/persistence/sqlitepersistence/SqlitePersistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from aquarius.persistence.sqlitepersistence.DatabaseCreation import DatabaseCreation
from aquarius.persistence.sqlitepersistence.AddBook import AddBook
from aquarius.persistence.sqlitepersistence.AddBookType import AddBookType
from aquarius.persistence.sqlitepersistence.FormatExists import FormatExists
from aquarius.persistence.sqlitepersistence.GetBookByTitleAndAuthor import GetBookByTitleAndAuthor
from aquarius.persistence.sqlitepersistence.GetBookDetails import GetBookDetails
from aquarius.persistence.sqlitepersistence.GetBookType import GetBookType
Expand Down Expand Up @@ -57,6 +58,11 @@ def add_book_format(self, book_id, book_format):
get_add_book_format = self.get_add_book_format(conn)
get_add_book_format.execute(book_id, book_format)

def format_exists(self, book_id, book_format):
with Connection(self.__config) as conn:
format_exists = self.get_format_exists(conn)
return format_exists.execute(book_id, book_format)

def get_add_book(self, connection):
return AddBook(connection)

Expand All @@ -81,3 +87,5 @@ def get_get_book_by_title_and_author(self, connection):
def get_add_book_format(self, connection):
return AddBookFormat(connection)

def get_format_exists(self, connection):
return FormatExists(connection)
2 changes: 0 additions & 2 deletions tests/interactors/TestAddBookInteractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,5 @@ def test_execute_with_existing_book_does_not_add_it(self):
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)
25 changes: 25 additions & 0 deletions tests/persistence/sqlitepersistence/TestFormatExists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unittest
from unittest.mock import Mock
from aquarius.persistence.sqlitepersistence.Connection import Connection
from aquarius.persistence.sqlitepersistence.FormatExists import FormatExists


class TestFormatExists(unittest.TestCase):

def test_instantiation(self):
FormatExists(Mock(Connection))

def test_book_does_not_exist_return_false(self):
conn = self.setup_mock([])
a = FormatExists(conn)
self.assertFalse(a.execute(book_id=1, book_format="epub"))

def test_book_exists_returns_true(self):
conn = self.setup_mock([1])
a = FormatExists(conn)
self.assertTrue(a.execute(book_id=1, book_format="epub"))

def setup_mock(self, return_value):
conn = Mock(Connection)
conn.execute_sql_fetch_all_with_params = Mock(return_value=return_value)
return conn
19 changes: 12 additions & 7 deletions tests/persistence/sqlitepersistence/sqliteperistence_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from aquarius.persistence.sqlitepersistence.AddBookFormat import AddBookFormat
from aquarius.persistence.sqlitepersistence.AddBookType import AddBookType
from aquarius.persistence.sqlitepersistence.Connection import Connection
from aquarius.persistence.sqlitepersistence.FormatExists import FormatExists
from aquarius.persistence.sqlitepersistence.GetBookByTitleAndAuthor import GetBookByTitleAndAuthor
from aquarius.persistence.sqlitepersistence.GetBookDetails import GetBookDetails
from aquarius.persistence.sqlitepersistence.GetBookType import GetBookType
Expand All @@ -28,6 +29,7 @@ def __setup_mocks(self):
self.__setup_get_book_type_mock()
self.__setup_list_books_by_first_letter_mock()
self.__setup_add_book_format_mock()
self.__setup_format_exists()

def __setup_add_book_mock(self):
self.__add_book = AddBook(Mock(Connection))
Expand Down Expand Up @@ -64,6 +66,11 @@ def __setup_add_book_format_mock(self):
self.__add_book_format.execute = Mock()
self.__p.get_add_book_format = lambda x: self.__add_book_format

def __setup_format_exists(self):
self.__format_exists = FormatExists(Mock(Connection))
self.__format_exists.execute = Mock()
self.__p.get_format_exists = lambda x: self.__format_exists

def test_searching_books_causes_the_search_method_to_be_called(self):
self.__p.search_books("Moo")
self.assertTrue(self.__book_search.search_books.called)
Expand Down Expand Up @@ -95,11 +102,9 @@ def test_get_book_by_title_and_author(self):
self.assertTrue(get_book_by_title_and_author.execute.called)

def test_add_book_format_calls_command_object(self):
book_format = BookFormat()
book_format.Format = "epub"
book_format.location = "/dev/null"
book_id = 0

self.__p.add_book_format(book_id, book_format)
self.__p.add_book_format(book_id=0, book_format=None)
self.assertTrue(self.__add_book_format.execute.called)

self.assertTrue(self.__add_book_format.execute.called)
def test_format_exists_calls_command_object(self):
self.__p.format_exists(book_id=0, book_format=None)
self.assertTrue(self.__format_exists.execute.called)

0 comments on commit 9acbeca

Please sign in to comment.