def test_upd_assoc_col(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
    b = Book(mdb, in_db=False, id=12, last_change=datetime.date.today())
    ei_row = db_con.execute('SELECT * FROM ExternalInfo WHERE id = 12').fetchone()
    ei = ExternalInfo(mdb, b, **ei_row)
    tags = 'Anal;Femdom;Large Breasts;Nakadashi;Straight Shota;Big Ass;Short Hair;Hat;Royalty;Dark Skin;Huge Penis;Big Areola;Defloration;Double Penetration;Elder Sister;Tall Girl'.split(';')
    artists = ['Kaneda Asou']
    category = ['Doujinshi']
    groups = ['Dokumushi Shokeitai']
    lists = ['to-read']
    assoc_cols = b.get_associated_columns()
    assert (assoc_cols['tag'] == tags)
    assert (assoc_cols['artist'] == artists)
    assert (assoc_cols['category'] == category)
    assert (assoc_cols['groups'] == groups)
    assert (assoc_cols['list'] == lists)
    assert (assoc_cols['character'] == [])
    assert (assoc_cols['collection'] == [])
    assert (assoc_cols['parody'] == [])
    assert (assoc_cols['ext_infos'] == [ei])
    b.tag = ['delchange1', 'delchange']
    b.category = ['testcat']
    b.update_assoc_columns_from_db()
    assert (not b._committed_state)
    assert (b.tag == tags)
    assert (b.artist == artists)
    assert (b.category == category)
    assert (b.groups == groups)
    assert (b.list == lists)
    assert (b.character == [])
    assert (b.collection == [])
    assert (b.parody == [])
    assert (b.ext_infos == [ei])
    b = Book(mdb, in_db=False, id=16, last_change=datetime.date.today())
    ei_rows = db_con.execute('SELECT * FROM ExternalInfo WHERE id IN (16, 18)').fetchall()
    ei1 = ExternalInfo(mdb, b, **ei_rows[0])
    ei2 = ExternalInfo(mdb, b, **ei_rows[1])
    tags = 'Blowjob;Ahegao;Megane;Happy Sex;Threesome;Group Sex;Layer Cake;Selfcest'.split(';')
    artists = ['bariun']
    category = ['Doujinshi']
    characters = ['Akira Kurusu', 'Futaba Sakura']
    parodies = ['Persona 5 / ペルソナ5']
    lists = ['to-read']
    assoc_cols = b.get_associated_columns()
    assert (assoc_cols['tag'] == tags)
    assert (assoc_cols['artist'] == artists)
    assert (assoc_cols['category'] == category)
    assert (assoc_cols['groups'] == [])
    assert (assoc_cols['list'] == lists)
    assert (assoc_cols['character'] == characters)
    assert (assoc_cols['collection'] == [])
    assert (assoc_cols['parody'] == parodies)
    assert (assoc_cols['ext_infos'] == [ei1, ei2])
    b.groups = ['delchange1', 'delchange']
    b.artist = ['tartist']
    b.update_assoc_columns_from_db()
    assert (not b._committed_state)
    assert (b.tag == tags)
    assert (b.artist == artists)
    assert (b.category == category)
    assert (b.groups == [])
    assert (b.list == lists)
    assert (b.character == characters)
    assert (b.collection == [])
    assert (b.parody == parodies)
    assert (b.ext_infos == [ei1, ei2])
def test_upd_assoc_col(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
    b = Book(mdb, in_db=False, id=12, last_change=datetime.date.today())
    ei_row = db_con.execute('SELECT * FROM ExternalInfo WHERE id = 12').fetchone()
    ei = ExternalInfo(mdb, b, **ei_row)
    tags = 'Anal;Femdom;Large Breasts;Nakadashi;Straight Shota;Big Ass;Short Hair;Hat;Royalty;Dark Skin;Huge Penis;Big Areola;Defloration;Double Penetration;Elder Sister;Tall Girl'.split(';')
    artists = ['Kaneda Asou']
    category = ['Doujinshi']
    groups = ['Dokumushi Shokeitai']
    lists = ['to-read']
    assoc_cols = b.get_associated_columns()
    assert (assoc_cols['tag'] == tags)
    assert (assoc_cols['artist'] == artists)
    assert (assoc_cols['category'] == category)
    assert (assoc_cols['groups'] == groups)
    assert (assoc_cols['list'] == lists)
    assert (assoc_cols['character'] == [])
    assert (assoc_cols['collection'] == [])
    assert (assoc_cols['parody'] == [])
    assert (assoc_cols['ext_infos'] == [ei])
    b.tag = ['delchange1', 'delchange']
    b.category = ['testcat']
    b.update_assoc_columns_from_db()
    assert (not b._committed_state)
    assert (b.tag == tags)
    assert (b.artist == artists)
    assert (b.category == category)
    assert (b.groups == groups)
    assert (b.list == lists)
    assert (b.character == [])
    assert (b.collection == [])
    assert (b.parody == [])
    assert (b.ext_infos == [ei])
    b = Book(mdb, in_db=False, id=16, last_change=datetime.date.today())
    ei_rows = db_con.execute('SELECT * FROM ExternalInfo WHERE id IN (16, 18)').fetchall()
    ei1 = ExternalInfo(mdb, b, **ei_rows[0])
    ei2 = ExternalInfo(mdb, b, **ei_rows[1])
    tags = 'Blowjob;Ahegao;Megane;Happy Sex;Threesome;Group Sex;Layer Cake;Selfcest'.split(';')
    artists = ['bariun']
    category = ['Doujinshi']
    characters = ['Akira Kurusu', 'Futaba Sakura']
    parodies = ['Persona 5 / ペルソナ5']
    lists = ['to-read']
    assoc_cols = b.get_associated_columns()
    assert (assoc_cols['tag'] == tags)
    assert (assoc_cols['artist'] == artists)
    assert (assoc_cols['category'] == category)
    assert (assoc_cols['groups'] == [])
    assert (assoc_cols['list'] == lists)
    assert (assoc_cols['character'] == characters)
    assert (assoc_cols['collection'] == [])
    assert (assoc_cols['parody'] == parodies)
    assert (assoc_cols['ext_infos'] == [ei1, ei2])
    b.groups = ['delchange1', 'delchange']
    b.artist = ['tartist']
    b.update_assoc_columns_from_db()
    assert (not b._committed_state)
    assert (b.tag == tags)
    assert (b.artist == artists)
    assert (b.category == category)
    assert (b.groups == [])
    assert (b.list == lists)
    assert (b.character == characters)
    assert (b.collection == [])
    assert (b.parody == parodies)
    assert (b.ext_infos == [ei1, ei2])