@@ -0,0 +1,2494 @@
+from __future__ import print_function, unicode_literals%0Aimport os%0Afrom twisted.python import filepath%0Afrom twisted.trial import unittest%0Afrom .. import database%0Afrom ..database import get_db, TARGET_VERSION, dump_db%0A%0Aclass Get(unittest.TestCase):%0A    def test_create_default(self):%0A        db_url = %22:memory:%22%0A        db = get_db(db_url)%0A        rows = db.execute(%22SELECT * FROM version%22).fetchall()%0A        self.assertEqual(len(rows), 1)%0A        self.assertEqual(rows%5B0%5D%5B%22version%22%5D, TARGET_VERSION)%0A%0A    def test_failed_create_allows_subsequent_create(self):%0A        patch = self.patch(database, %22get_schema%22, lambda version: b%22this is a broken schema%22)%0A        dbfile = filepath.FilePath(self.mktemp())%0A        self.assertRaises(Exception, lambda: get_db(dbfile.path))%0A        patch.restore()%0A        get_db(dbfile.path)%0A%0A    def OFF_test_upgrade(self): # disabled until we add a v2 schema%0A        basedir = self.mktemp()%0A        os.mkdir(basedir)%0A        fn = os.path.join(basedir, %22upgrade.db%22)%0A        self.assertNotEqual(TARGET_VERSION, 2)%0A%0A        # create an old-version DB in a file%0A        db = get_db(fn, 2)%0A        rows = db.execute(%22SELECT * FROM version%22).fetchall()%0A        self.assertEqual(len(rows), 1)%0A        self.assertEqual(rows%5B0%5D%5B%22version%22%5D, 2)%0A        del db%0A%0A        # then upgrade the file to the latest version%0A        dbA = get_db(fn, TARGET_VERSION)%0A        rows = dbA.execute(%22SELECT * FROM version%22).fetchall()%0A        self.assertEqual(len(rows), 1)%0A        self.assertEqual(rows%5B0%5D%5B%22version%22%5D, TARGET_VERSION)%0A        dbA_text = dump_db(dbA)%0A        del dbA%0A%0A        # make sure the upgrades got committed to disk%0A        dbB = get_db(fn, TARGET_VERSION)%0A        dbB_text = dump_db(dbB)%0A        del dbB%0A        self.assertEqual(dbA_text, dbB_text)%0A%0A        # The upgraded schema should be equivalent to that of a new DB.%0A        # However a text dump will differ because ALTER TABLE always appends%0A        # the new column to the end of a table, whereas our schema puts it%0A        # somewhere in the middle (wherever it fits naturally). Also ALTER%0A        # TABLE doesn't include comments.%0A        if False:%0A            latest_db = get_db(%22:memory:%22, TARGET_VERSION)%0A            latest_text = dump_db(latest_db)%0A            with open(%22up.sql%22,%22w%22) as f: f.write(dbA_text)%0A            with open(%22new.sql%22,%22w%22) as f: f.write(latest_text)%0A            # check with %22diff -u _trial_temp/up.sql _trial_temp/new.sql%22%0A            self.assertEqual(dbA_text, latest_text)%0A

copy test_database from magic-wormhole package