def test_migration_fail(self): # Test that a sqlite3 exception will be caught. def raise_err(err): raise err migrations = [(1, lambda c: raise_err(sqlite3.Error))] success = lgd.db_setup(self.conn, migrations) self.assertFalse(success, "db_setup failed to catch migration failure")
def test_migrations(self): # App version before migrations current_version = lgd.get_user_version(self.conn) self.assertNotEqual(current_version, lgd.DB_USER_VERSION) success = lgd.db_setup(self.conn, lgd.DB_MIGRATIONS) self.assertTrue(success, "DB migrations failed!") # After migrations current_version = lgd.get_user_version(self.conn) self.assertEqual(current_version, lgd.DB_USER_VERSION)
def test_tag_relation_export(self): # Insert and then retrieve tag-relations. _, _ = lgd.tag_import(self.conn, get_tag_csv()) tag_relations1 = set(lgd.select_related_tags_all(self.conn)) self.assertEqual(len(tag_relations1), len(TAG_RELATIONS)) # Export tag-relations from the initial DB. tagfile = io.StringIO() _ = lgd.tag_export(self.conn, tagfile) tagfile.seek(0) # Set up a second DB. # Import tag-relations from the 1st DB into the 2nd. conn2 = lgd.get_connection(DB_IN_MEM) lgd.db_setup(conn2, lgd.DB_MIGRATIONS) _, _ = lgd.tag_import(conn2, tagfile) # Retrieve the tag-relations from the 2nd DB. # Check that the tag-relations retrieved from both DBs are equal. tag_relations2 = set(lgd.select_related_tags_all(conn2)) self.assertEqual(tag_relations1, tag_relations2)
def test_note_export_equality(self): # Retrieve Notes inserted during setup. notes1 = set(lgd.select_notes(self.conn)) self.assertEqual(len(notes1), len(NOTES)) # Export from the setUp DB. notes = lgd.select_notes(self.conn, localtime=False) notefile = io.StringIO() _ = lgd.note_export(self.conn, notes, notefile) notefile.seek(0) # Set up a second DB conn2 = lgd.get_connection(DB_IN_MEM) lgd.db_setup(conn2, lgd.DB_MIGRATIONS) # Import the Notes from the 1st DB into the 2nd. _, _ = lgd.note_import(conn2, notefile) # Retrieve the Notes from the 2nd DB. # Check that the notes retrieved from both DBs are equal. notes2 = set(lgd.select_notes(conn2)) self.assertEqual(notes1, notes2)
def test_migration_intermediate(self): BAD_TABLE = "fail" def bad_migration(conn): conn.execute( f"CREATE TABLE {BAD_TABLE} (id INT PRIMARY KEY, data BLOB);") conn.execute( f"INSERT INTO {BAD_TABLE} (id, data) VALUES (1, 'abcd');") cursor = conn.execute("SELECT * FROM fail WHERE id = ?", (1, )) self.assertEqual(cursor.fetchone()[1], "abcd") raise sqlite3.Error def good_migration(conn): conn.execute("CREATE table success (id INT PRIMARY KEY);") migrations = [ (1, good_migration), (2, bad_migration), ] # Expect failure to finish all migrations success = lgd.db_setup(self.conn, migrations) self.assertFalse(success) # Ensure good_migration succeeded. db_version = lgd.get_user_version(self.conn) self.assertEqual(db_version, 1) # Ensure bad_migration failed and did not fail in an intermediate state cursor = self.conn.execute( "SELECT count(*) FROM sqlite_master WHERE type='table' and name = ?", (BAD_TABLE, ), ) count = cursor.fetchone()[0] table_exists = bool(count) self.assertFalse(table_exists, f"count == {count}; table exists!")
def setUp(self): self.conn = lgd.get_connection(DB_IN_MEM) lgd.db_setup(self.conn, lgd.DB_MIGRATIONS)
def setUp(self): self.conn = lgd.get_connection(DB_IN_MEM) lgd.db_setup(self.conn, lgd.DB_MIGRATIONS) self.inserted, self.updated = lgd.note_import(self.conn, get_note_csv())