def test_remove_book(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)
    import shutil
    os.makedirs(os.path.join(tmpdir, 'thumbs'))
    cover_path = os.path.join(tmpdir, 'thumbs', '16')
    shutil.copyfile(os.path.join(tmpdir, os.pardir, 'book_test_files', '16'), cover_path)
    db_con = memdb
    b = mdb.get_book(16)
    b.remove()
    assert (b._in_db is False)
    with pytest.raises(KeyError):
        mdb.id_map[b.key]
    b_row = db_con.execute('SELECT id FROM Books WHERE id = 16').fetchall()
    assert (not b_row)
    ei_rows = db_con.execute('SELECT id FROM ExternalInfo WHERE id IN (16, 18)').fetchall()
    assert (not ei_rows)
    assert (not os.path.exists(cover_path))
def test_remove_book(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)
    import shutil
    os.makedirs(os.path.join(tmpdir, 'thumbs'))
    cover_path = os.path.join(tmpdir, 'thumbs', '16')
    shutil.copyfile(os.path.join(tmpdir, os.pardir, 'book_test_files', '16'), cover_path)
    db_con = memdb
    b = mdb.get_book(16)
    b.remove()
    assert (b._in_db is False)
    with pytest.raises(KeyError):
        mdb.id_map[b.key]
    b_row = db_con.execute('SELECT id FROM Books WHERE id = 16').fetchall()
    assert (not b_row)
    ei_rows = db_con.execute('SELECT id FROM ExternalInfo WHERE id IN (16, 18)').fetchall()
    assert (not ei_rows)
    assert (not os.path.exists(cover_path))