Esempio n. 1
0
    def test_applies_new_migration_to_sqlite_database(self, fake_get):
        if self._db_type != 'sqlite':
            self.skipTest('SQLite tests are disabled.')

        # replace real migrations with tests migrations.

        test_migrations = [
            (100, 'test.functional.migrations.0100_init'),
            (101, 'test.functional.migrations.0101_add_column'),
            (102, 'test.functional.migrations.0102_create_table'),
            (103, 'test.functional.migrations.0103_not_ready')  # that should not apply
        ]

        fake_get.return_value = test_migrations

        # create database with initial schema
        with patch.object(database, 'SCHEMA_VERSION', 100):
            db = Database('sqlite:///{}'.format(self.sqlite_db_file))
            db.create_tables()
            db.close()

        # switch version and reconnect. Now both migrations should apply.
        with patch.object(database, 'SCHEMA_VERSION', 102):
            db = Database('sqlite:///{}'.format(self.sqlite_db_file))
            try:
                # check column created by migration 101.
                db.connection.execute('SELECT column1 FROM datasets;').fetchall()

                # check table created by migration 102.
                db.connection.execute('SELECT column1 FROM table1;').fetchall()

                # db version changed to 102
                self.assertEqual(db.connection.execute('PRAGMA user_version').fetchone()[0], 102)
            finally:
                db.close()
Esempio n. 2
0
    def test_creates_new_root_config(self):
        # prepare state
        db = Database('sqlite://')
        db.enable_delete = True

        # prevent _add_root_config call from create_tables
        with patch.object(db, '_add_config_root', Mock()):
            db.create_tables()
        query = db.session.query
        datasets = query(Dataset).all()
        self.assertEqual(len(datasets), 0)

        # testing
        # No call real _add_config_root and check result of the call.
        db._add_config_root()
        datasets = query(Dataset).all()
        self.assertEqual(len(datasets), 1)
        self.assertEqual(datasets[0].name, ROOT_CONFIG_NAME)
        self.assertEqual(datasets[0].vname, ROOT_CONFIG_NAME_V)
Esempio n. 3
0
    def test_creates_new_root_config(self):
        # prepare state
        db = Database('sqlite://')
        db.enable_delete = True

        # prevent _add_root_config call from create_tables
        with patch.object(db, '_add_config_root', Mock()):
            db.create_tables()
        query = db.session.query
        datasets = query(Dataset).all()
        self.assertEqual(len(datasets), 0)

        # testing
        # No call real _add_config_root and check result of the call.
        db._add_config_root()
        datasets = query(Dataset).all()
        self.assertEqual(len(datasets), 1)
        self.assertEqual(datasets[0].name, ROOT_CONFIG_NAME)
        self.assertEqual(datasets[0].vname, ROOT_CONFIG_NAME_V)
Esempio n. 4
0
    def test_applies_new_migration_to_sqlite_database(self, fake_get):
        if self._db_type != 'sqlite':
            self.skipTest('SQLite tests are disabled.')

        # replace real migrations with tests migrations.

        test_migrations = [
            (100, 'test.functional.migrations.0100_init'),
            (101, 'test.functional.migrations.0101_add_column'),
            (102, 'test.functional.migrations.0102_create_table'),
            (103, 'test.functional.migrations.0103_not_ready'
             )  # that should not apply
        ]

        fake_get.return_value = test_migrations

        # create database with initial schema
        with patch.object(database, 'SCHEMA_VERSION', 100):
            db = Database('sqlite:///{}'.format(self.sqlite_db_file))
            db.create_tables()
            db.close()

        # switch version and reconnect. Now both migrations should apply.
        with patch.object(database, 'SCHEMA_VERSION', 102):
            db = Database('sqlite:///{}'.format(self.sqlite_db_file))
            try:
                # check column created by migration 101.
                db.connection.execute(
                    'SELECT column1 FROM datasets;').fetchall()

                # check table created by migration 102.
                db.connection.execute('SELECT column1 FROM table1;').fetchall()

                # db version changed to 102
                self.assertEqual(
                    db.connection.execute('PRAGMA user_version').fetchone()[0],
                    102)
            finally:
                db.close()
Esempio n. 5
0
 def test_ignores_OperationalError_while_droping(self):
     db = Database('sqlite://')
     fake_drop = Mock(side_effect=OperationalError('select 1;', [], 'a'))
     with patch.object(db, 'drop', fake_drop):
         db.create_tables()
         fake_drop.assert_called_once_with()
Esempio n. 6
0
 def test_ignores_OperationalError_while_droping(self):
     db = Database('sqlite://')
     fake_drop = Mock(side_effect=OperationalError('select 1;', [], 'a'))
     with patch.object(db, 'drop', fake_drop):
         db.create_tables()
         fake_drop.assert_called_once_with()