def test_fix_case_duplicates(self): # {{{ ' Test fixing of databases that have items in is_many fields that differ only by case ' ae = self.assertEqual cache = self.init_cache() conn = cache.backend.conn conn.execute('INSERT INTO publishers (name) VALUES ("mūs")') lid = conn.last_insert_rowid() conn.execute('INSERT INTO publishers (name) VALUES ("MŪS")') uid = conn.last_insert_rowid() conn.execute('DELETE FROM books_publishers_link') conn.execute('INSERT INTO books_publishers_link (book,publisher) VALUES (1, %d)' % lid) conn.execute('INSERT INTO books_publishers_link (book,publisher) VALUES (2, %d)' % uid) conn.execute('INSERT INTO books_publishers_link (book,publisher) VALUES (3, %d)' % uid) cache.reload_from_db() t = cache.fields['publisher'].table for x in (lid, uid): self.assertIn(x, t.id_map) self.assertIn(x, t.col_book_map) ae(t.book_col_map[1], lid) ae(t.book_col_map[2], uid) t.fix_case_duplicates(cache.backend) for c in (cache, self.init_cache()): t = c.fields['publisher'].table self.assertNotIn(uid, t.id_map) self.assertNotIn(uid, t.col_book_map) for bid in (1, 2, 3): ae(c.field_for('publisher', bid), "mūs") c.close() cache = self.init_cache() conn = cache.backend.conn conn.execute('INSERT INTO tags (name) VALUES ("mūūs")') lid = conn.last_insert_rowid() conn.execute('INSERT INTO tags (name) VALUES ("MŪŪS")') uid = conn.last_insert_rowid() conn.execute('INSERT INTO tags (name) VALUES ("mūŪS")') mid = conn.last_insert_rowid() conn.execute('INSERT INTO tags (name) VALUES ("t")') norm = conn.last_insert_rowid() conn.execute('DELETE FROM books_tags_link') for book_id, vals in {1:(lid, uid), 2:(uid, mid), 3:(lid, norm)}.iteritems(): conn.executemany('INSERT INTO books_tags_link (book,tag) VALUES (?,?)', tuple((book_id, x) for x in vals)) cache.reload_from_db() t = cache.fields['tags'].table for x in (lid, uid, mid): self.assertIn(x, t.id_map) self.assertIn(x, t.col_book_map) t.fix_case_duplicates(cache.backend) for c in (cache, self.init_cache()): t = c.fields['tags'].table for x in (uid, mid): self.assertNotIn(x, t.id_map) self.assertNotIn(x, t.col_book_map) ae(c.field_for('tags', 1), (t.id_map[lid],)) ae(c.field_for('tags', 2), (t.id_map[lid],), 'failed for book 2') ae(c.field_for('tags', 3), (t.id_map[lid], t.id_map[norm]))
def test_fix_case_duplicates(self): # {{{ ' Test fixing of databases that have items in is_many fields that differ only by case ' ae = self.assertEqual cache = self.init_cache() conn = cache.backend.conn conn.execute('INSERT INTO publishers (name) VALUES ("mūs")') lid = conn.last_insert_rowid() conn.execute('INSERT INTO publishers (name) VALUES ("MŪS")') uid = conn.last_insert_rowid() conn.execute('DELETE FROM books_publishers_link') conn.execute('INSERT INTO books_publishers_link (book,publisher) VALUES (1, %d)' % lid) conn.execute('INSERT INTO books_publishers_link (book,publisher) VALUES (2, %d)' % uid) conn.execute('INSERT INTO books_publishers_link (book,publisher) VALUES (3, %d)' % uid) cache.reload_from_db() t = cache.fields['publisher'].table for x in (lid, uid): self.assertIn(x, t.id_map) self.assertIn(x, t.col_book_map) ae(t.book_col_map[1], lid) ae(t.book_col_map[2], uid) t.fix_case_duplicates(cache.backend) for c in (cache, self.init_cache()): t = c.fields['publisher'].table self.assertNotIn(uid, t.id_map) self.assertNotIn(uid, t.col_book_map) for bid in (1, 2, 3): ae(c.field_for('publisher', bid), "mūs") c.close() cache = self.init_cache() conn = cache.backend.conn conn.execute('INSERT INTO tags (name) VALUES ("mūūs")') lid = conn.last_insert_rowid() conn.execute('INSERT INTO tags (name) VALUES ("MŪŪS")') uid = conn.last_insert_rowid() conn.execute('INSERT INTO tags (name) VALUES ("mūŪS")') mid = conn.last_insert_rowid() conn.execute('INSERT INTO tags (name) VALUES ("t")') norm = conn.last_insert_rowid() conn.execute('DELETE FROM books_tags_link') for book_id, vals in iteritems({1:(lid, uid), 2:(uid, mid), 3:(lid, norm)}): conn.executemany('INSERT INTO books_tags_link (book,tag) VALUES (?,?)', tuple((book_id, x) for x in vals)) cache.reload_from_db() t = cache.fields['tags'].table for x in (lid, uid, mid): self.assertIn(x, t.id_map) self.assertIn(x, t.col_book_map) t.fix_case_duplicates(cache.backend) for c in (cache, self.init_cache()): t = c.fields['tags'].table for x in (uid, mid): self.assertNotIn(x, t.id_map) self.assertNotIn(x, t.col_book_map) ae(c.field_for('tags', 1), (t.id_map[lid],)) ae(c.field_for('tags', 2), (t.id_map[lid],), 'failed for book 2') ae(c.field_for('tags', 3), (t.id_map[lid], t.id_map[norm]))