def test_query(self): foo = DB("bar") foo.conn = fake_connection() bar = foo.query("some query") assert bar == True assert foo.conn.called_execute == True
def get_books(db_file): ''' Get the previously stored books data ''' database = DB(db_file) database.create_connection() books = database.query('select * from rankings') database.close_connection() return books
def test_update_book_given_id(self, monkeypatch): fdh = fake_data_handler() monkeypatch.setattr('sorter.lib.data_handler.get_by_isbn', fdh.get_by_isbn) monkeypatch.setattr('sorter.lib.data_handler.parse_isbn13_response', fdh.parse_isbn13_response) monkeypatch.setattr('sorter.lib.data_handler.get_by_id', fdh.get_by_id) monkeypatch.setattr('sorter.lib.data_handler.parse_id_response', lambda *args: (1,999,9999,4,5,1942,7,8,9,10)) from sorter.lib.db import DB database = DB(':memory:') database.create_connection() monkeypatch.setattr("sorter.lib.data_handler.DB", lambda *args: wrapped_db(database)) qry = CREATE_RANKINGS database.execute(qry) fake_books = [( 1, 2, 3, 4, 5, 6, 7, 8, 9,10), (11,12,13,14,15,16,17,18,19,20), (21,22,23,24,25,26,27,28,29,30), (31,32,33,34,35,36,37,38,39,40)] query = '''INSERT INTO rankings(id, isbn, isbn13, title, image_url, publication_year, ratings_count, average_rating, author, link) VALUES(?,?,?,?,?,?,?,?,?,?)''' for fake_book in fake_books: database.insertupdate(query, fake_book) update_book((1,None,None,4,5,6,7,8,9,10), 'foo', defaults) test_books = database.query('select * from rankings where id = 1') database.close_connection() database = None assert test_books[0][0] == 1 assert test_books[0][1] == 999 assert test_books[0][2] == 9999 assert test_books[0][3] == 4 assert test_books[0][4] == 5 assert test_books[0][5] == 1942 assert test_books[0][6] == 7 assert test_books[0][7] == 8 assert test_books[0][8] == 9 assert test_books[0][9] == 10 assert fdh.called_get_by_isbn is False assert fdh.called_parse_isbn13_response is False assert fdh.called_get_by_id is True
def get_books_with_missing_data(db_file): ''' Get the previously stored books data ''' database = DB(db_file) database.create_connection() books = database.query( 'select * from rankings where publication_year is null') database.close_connection() return books
def test_manually_update_book_id_noupdate(self, monkeypatch): from sorter.lib.db import DB database = DB(':memory:') database.create_connection() monkeypatch.setattr("sorter.lib.data_handler.DB", lambda *args: wrapped_db(database)) qry = CREATE_RANKINGS database.execute(qry) fake_books = [( 1, 2, 3, 4, 5, 6, 7, 8, 9,10)] query = '''INSERT INTO rankings(id, isbn, isbn13, title, image_url, publication_year, ratings_count, average_rating, author, link) VALUES(?,?,?,?,?,?,?,?,?,?)''' for fake_book in fake_books: database.insertupdate(query, fake_book) to_update = [ { 'book_id': 1, 'attr': 'id', 'value': 'foo' } ] manually_update_books(to_update, 'foo') test_books = database.query('select * from rankings where id = 1') database.close_connection() database = None assert test_books[0] == (1, 2, 3, 4, 5, 6, 7, 8, 9, 10,0.0)
def test_manually_update_book_all_fields(self, monkeypatch): from sorter.lib.db import DB database = DB(':memory:') database.create_connection() monkeypatch.setattr("sorter.lib.data_handler.DB", lambda *args: wrapped_db(database)) qry = CREATE_RANKINGS database.execute(qry) fake_books = [( 1, 2, 3, 4, 5, 6, 7, 8, 9,10), (11,12,13,14,15,16,17,18,19,20), (21,22,23,24,25,26,27,28,29,30), (31,32,33,34,35,36,37,38,39,40)] query = '''INSERT INTO rankings(id, isbn, isbn13, title, image_url, publication_year, ratings_count, average_rating, author, link) VALUES(?,?,?,?,?,?,?,?,?,?)''' for fake_book in fake_books: database.insertupdate(query, fake_book) to_update = [ { 'book_id': 1, 'attr': 'isbn', 'value': 'foo' }, { 'book_id': 1, 'attr': 'isbn13', 'value': 'bar' }, { 'book_id': 1, 'attr': 'title', 'value': 'baz' }, { 'book_id': 1, 'attr': 'image_url', 'value': 'bang' }, { 'book_id': 1, 'attr': 'publication_year', 'value': 1980 }, { 'book_id': 1, 'attr': 'ratings_count', 'value': 56 }, { 'book_id': 1, 'attr': 'average_rating', 'value': 57 }, { 'book_id': 1, 'attr': 'author', 'value': 'ipsum' }, { 'book_id': 1, 'attr': 'link', 'value': 'dolet' }, { 'book_id': 1, 'attr': 'preference_adjustment', 'value': 12 }, { 'book_id': 11, 'attr': 'ISBN13', 'value': 'brown' } ] manually_update_books(to_update, 'foo') test_books = database.query('select * from rankings where id in (1,11,21,31)') database.close_connection() database = None assert test_books[0] == (1, 'foo', 'bar', 'baz', 'bang', 1980, 56, 57, 'ipsum', 'dolet', 12.0) assert test_books[1] == (11,12,'brown',14,15,16,17,18,19,20,0.0) assert test_books[2] == (21,22,23,24,25,26,27,28,29,30,0.0) assert test_books[3] == (31,32,33,34,35,36,37,38,39,40,0.0)