Пример #1
0
def update_book(book, db_file, defaults):
    '''
    Add the missing book data
    '''
    qry = None
    if book[2] is not None:
        xml_response = get_by_isbn(book[2], defaults)
        new_book = parse_isbn13_response(xml_response)
        qry = 'UPDATE rankings set publication_year = ? where isbn13 = ?'
        vals = [new_book[5], book[2]]

    elif book[0] is not None:
        xml_response = get_by_id(book[0], defaults)
        new_book = parse_id_response(xml_response)
        qry = 'UPDATE rankings set publication_year = ?, isbn = ?, isbn13 = ? where id = ?'
        vals = [new_book[5], new_book[1], new_book[2], book[0]]

    if qry is not None:
        database = DB(db_file)

        database.create_connection()

        database.insertupdate(qry, vals)

        database.close_connection()
Пример #2
0
    def test_insertupdate(self):
        foo = DB("bar")
        foo.conn = fake_connection()

        foo.insertupdate("some query", ["val1", "val2"])

        assert foo.conn.called_close == False
        assert foo.conn.called_commit == True
        assert foo.conn.fake_cursor.called_execute == True
Пример #3
0
    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
Пример #4
0
def store_data(books, db_file):
    '''
    Store the book data in the provided database
    '''
    database = DB(db_file)
    database.create_connection()

    query = '''INSERT INTO rankings(id, isbn, isbn13, title,
        image_url, publication_year, ratings_count, average_rating, 
        author, link) VALUES(?,?,?,?,?,?,?,?,?,?)'''

    for book in books:
        database.insertupdate(query, book)

    database.close_connection()
Пример #5
0
def manually_update_books(data, db_file):
    '''
    Update books based on parsed POST data
    '''
    database = DB(db_file)
    database.create_connection()

    for book in data:
        if book['attr'] == 'id':
            continue

        qry = 'UPDATE rankings set %s = ? where id = ?' % book['attr']
        vals = [book['value'], int(book['book_id'])]
        database.insertupdate(qry, vals)

    database.close_connection()
Пример #6
0
    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)
Пример #7
0
    def test_insertupdate_catches_exception(self):
        foo = DB("bar")
        foo.conn = fake_failed_connection()

        bar = foo.insertupdate("some query", ["val1", "val2"])
        assert bar == None
Пример #8
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)