def test_static_db_methods(monkeypatch, setup_mdb_dir):
    tmpdir = setup_mdb_dir
    os.chdir(tmpdir)
    mdb_file = os.path.join(TESTS_DIR, 'all_test_files', 'manga_db.sqlite.sql')
    memdb = load_db_from_sql_file(mdb_file, ':memory:', True)
    monkeypatch.setattr('manga_db.manga_db.MangaDB._load_or_create_sql_db', (lambda x, y, z: (memdb, None)))
    mdb = MangaDB(tmpdir, mdb_file)
    db_con = memdb
    tag_before = 'Large Breasts;Nakadashi;Blowjob;Threesome;Bikini;Group Sex;Swimsuit'.split(';')
    tag_change = ['Test1', 'Test2', 'Blabla']
    Book.add_assoc_col_on_book_id(mdb, 13, 'tag', tag_change, tag_before)
    tag = db_con.execute("\n        SELECT group_concat(Tag.name, ';')\n        FROM Books, BookTag bt, Tag\n        WHERE  Books.id = bt.book_id\n        AND Tag.id = bt.tag_id\n        AND Books.id = 13").fetchone()
    assert (tag[0].split(';')[(- 3):] == tag_change)
    Book.remove_assoc_col_on_book_id(mdb, 13, 'tag', tag_change, (tag_before + tag_change))
    tag = db_con.execute("\n        SELECT group_concat(Tag.name, ';')\n        FROM Books, BookTag bt, Tag\n        WHERE  Books.id = bt.book_id\n        AND Tag.id = bt.tag_id\n        AND Books.id = 13").fetchone()
    assert (tag[0].split(';') == tag_before)
    b = mdb.get_book(16)
    tag_before = 'Blowjob;Ahegao;Megane;Happy Sex;Threesome;Group Sex;Layer Cake;Selfcest'.split(';')
    tag_change = ['Test3', 'Test4', 'Blablabla']
    Book.add_assoc_col_on_book_id(mdb, 16, 'tag', tag_change, tag_before)
    tag = db_con.execute("\n        SELECT group_concat(Tag.name, ';')\n        FROM Books, BookTag bt, Tag\n        WHERE  Books.id = bt.book_id\n        AND Tag.id = bt.tag_id\n        AND Books.id = 16").fetchone()
    assert (tag[0].split(';')[(- 3):] == tag_change)
    assert (b.tag[(- 3):] == tag_change)
    Book.remove_assoc_col_on_book_id(mdb, 16, 'tag', tag_change, (tag_before + tag_change))
    tag = db_con.execute("\n        SELECT group_concat(Tag.name, ';')\n        FROM Books, BookTag bt, Tag\n        WHERE  Books.id = bt.book_id\n        AND Tag.id = bt.tag_id\n        AND Books.id = 16").fetchone()
    assert (tag[0].split(';') == tag_before)
    assert (b.tag == tag_before)
    Book.set_favorite_id(mdb, 2, 1)
    fav = db_con.execute('SELECT favorite FROM Books WHERE id = 2').fetchone()
    assert (1 == fav[0])
    b = mdb.get_book(7)
    Book.set_favorite_id(mdb, 7, 1)
    fav = db_con.execute('SELECT favorite FROM Books WHERE id = 7').fetchone()
    assert (1 == fav[0])
    assert (b.favorite == 1)
    Book.rate_book_id(mdb, 3, 3.5)
    rat = db_con.execute('SELECT my_rating FROM Books WHERE id = 3').fetchone()
    assert (3.5 == rat[0])
    b = mdb.get_book(8)
    Book.rate_book_id(mdb, 8, 4.25)
    rat = db_con.execute('SELECT my_rating FROM Books WHERE id = 8').fetchone()
    assert (4.25 == rat[0])
    assert (b.my_rating == 4.25)
def test_static_db_methods(monkeypatch, setup_mdb_dir):
    tmpdir = setup_mdb_dir
    os.chdir(tmpdir)
    mdb_file = os.path.join(TESTS_DIR, 'all_test_files', 'manga_db.sqlite.sql')
    memdb = load_db_from_sql_file(mdb_file, ':memory:', True)
    monkeypatch.setattr('manga_db.manga_db.MangaDB._load_or_create_sql_db', (lambda x, y, z: (memdb, None)))
    mdb = MangaDB(tmpdir, mdb_file)
    db_con = memdb
    tag_before = 'Large Breasts;Nakadashi;Blowjob;Threesome;Bikini;Group Sex;Swimsuit'.split(';')
    tag_change = ['Test1', 'Test2', 'Blabla']
    Book.add_assoc_col_on_book_id(mdb, 13, 'tag', tag_change, tag_before)
    tag = db_con.execute("\n        SELECT group_concat(Tag.name, ';')\n        FROM Books, BookTag bt, Tag\n        WHERE  Books.id = bt.book_id\n        AND Tag.id = bt.tag_id\n        AND Books.id = 13").fetchone()
    assert (tag[0].split(';')[(- 3):] == tag_change)
    Book.remove_assoc_col_on_book_id(mdb, 13, 'tag', tag_change, (tag_before + tag_change))
    tag = db_con.execute("\n        SELECT group_concat(Tag.name, ';')\n        FROM Books, BookTag bt, Tag\n        WHERE  Books.id = bt.book_id\n        AND Tag.id = bt.tag_id\n        AND Books.id = 13").fetchone()
    assert (tag[0].split(';') == tag_before)
    b = mdb.get_book(16)
    tag_before = 'Blowjob;Ahegao;Megane;Happy Sex;Threesome;Group Sex;Layer Cake;Selfcest'.split(';')
    tag_change = ['Test3', 'Test4', 'Blablabla']
    Book.add_assoc_col_on_book_id(mdb, 16, 'tag', tag_change, tag_before)
    tag = db_con.execute("\n        SELECT group_concat(Tag.name, ';')\n        FROM Books, BookTag bt, Tag\n        WHERE  Books.id = bt.book_id\n        AND Tag.id = bt.tag_id\n        AND Books.id = 16").fetchone()
    assert (tag[0].split(';')[(- 3):] == tag_change)
    assert (b.tag[(- 3):] == tag_change)
    Book.remove_assoc_col_on_book_id(mdb, 16, 'tag', tag_change, (tag_before + tag_change))
    tag = db_con.execute("\n        SELECT group_concat(Tag.name, ';')\n        FROM Books, BookTag bt, Tag\n        WHERE  Books.id = bt.book_id\n        AND Tag.id = bt.tag_id\n        AND Books.id = 16").fetchone()
    assert (tag[0].split(';') == tag_before)
    assert (b.tag == tag_before)
    Book.set_favorite_id(mdb, 2, 1)
    fav = db_con.execute('SELECT favorite FROM Books WHERE id = 2').fetchone()
    assert (1 == fav[0])
    b = mdb.get_book(7)
    Book.set_favorite_id(mdb, 7, 1)
    fav = db_con.execute('SELECT favorite FROM Books WHERE id = 7').fetchone()
    assert (1 == fav[0])
    assert (b.favorite == 1)
    Book.rate_book_id(mdb, 3, 3.5)
    rat = db_con.execute('SELECT my_rating FROM Books WHERE id = 3').fetchone()
    assert (3.5 == rat[0])
    b = mdb.get_book(8)
    Book.rate_book_id(mdb, 8, 4.25)
    rat = db_con.execute('SELECT my_rating FROM Books WHERE id = 8').fetchone()
    assert (4.25 == rat[0])
    assert (b.my_rating == 4.25)