Beispiel #1
0
    def _run_test(self):
        try:
            database_resource = provision.DatabaseResource(
                self.DRIVER, provision_new_database=True)
        except exception.BackendNotAvailable:
            self.skip("database not available")

        schema_resource = provision.SchemaResource(database_resource,
                                                   self._gen_schema)

        schema = schema_resource.getResource()

        conn = schema.database.engine.connect()
        engine = utils.NonCommittingEngine(conn)

        with engine.connect() as conn:
            rows = conn.execute(self.test_table.select())
            self.assertEqual([], rows.fetchall())

            trans = conn.begin()
            conn.execute(self.test_table.insert(), {"x": 1, "y": 2})
            trans.rollback()

            rows = conn.execute(self.test_table.select())
            self.assertEqual([], rows.fetchall())

            trans = conn.begin()
            conn.execute(self.test_table.insert(), {"x": 2, "y": 3})
            trans.commit()

            rows = conn.execute(self.test_table.select())
            self.assertEqual([(2, 3)], rows.fetchall())

        engine._dispose()
        schema_resource.finishedWith(schema)
Beispiel #2
0
    def resources_collection(cls, test):
        # reimplement current oslo.db code.
        # FIXME(zzzeek) The patterns here are up in the air enough
        # that I think keeping this totally separate will give us the
        # most leverage in being able to fix oslo.db in an upcoming
        # release, then port neutron back to the working version.

        driver = test.DRIVER

        if driver not in test._database_resources:
            try:
                test._database_resources[driver] = \
                    provision.DatabaseResource(driver)
            except oslodb_exception.BackendNotAvailable:
                test._database_resources[driver] = None

        database_resource = test._database_resources[driver]
        if database_resource is None:
            return []

        key = (driver, None)
        if test.BUILD_SCHEMA:
            if key not in test._schema_resources:
                test._schema_resources[key] = provision.SchemaResource(
                    database_resource,
                    cls._generate_schema_w_migrations
                    if test.BUILD_WITH_MIGRATIONS else cls._generate_schema,
                    teardown=False)

            schema_resource = test._schema_resources[key]
            return [('schema', schema_resource), ('db', database_resource)]
        else:
            return [('db', database_resource)]
Beispiel #3
0
 def _init_resources(cls):
     cls.schema_resource = provision.SchemaResource(
         provision.DatabaseResource("sqlite"),
         cls._generate_schema, teardown=False)
     dependency_resources = {}
     for name, resource in cls.schema_resource.resources:
         dependency_resources[name] = resource.getResource()
     cls.schema_resource.make(dependency_resources)
     cls.engine = dependency_resources['database'].engine
Beispiel #4
0
 def _init_resources(cls):
     if cls._GLOBAL_RESOURCES:
         return
     else:
         cls._GLOBAL_RESOURCES = True
         cls.database_resource = provision.DatabaseResource("sqlite")
         dependency_resources = {}
         for name, resource in cls.database_resource.resources:
             dependency_resources[name] = resource.getResource()
         cls.engine = dependency_resources['backend'].engine
Beispiel #5
0
    def _resources_for_driver(self, driver, schema_scope, generate_schema):
        # testresources relies on the identity and state of the
        # TestResourceManager objects in play to correctly manage
        # resources, and it also hardcodes to looking at the
        # ".resources" attribute on the test object, even though the
        # setUpResources() function passes the list of resources in,
        # so we have to code the TestResourceManager logic into the
        # .resources attribute and ensure that the same set of test
        # variables always produces the same TestResourceManager objects.

        if driver not in self._database_resources:
            try:
                self._database_resources[driver] = \
                    provision.DatabaseResource(driver,
                                               provision_new_database=True)
            except exception.BackendNotAvailable as bne:
                self._database_resources[driver] = None
                self._db_not_available[driver] = str(bne)

        database_resource = self._database_resources[driver]
        if database_resource is None:
            return []

        if schema_scope:
            key = (driver, schema_scope)
            if key not in self._schema_resources:
                schema_resource = provision.SchemaResource(
                    database_resource, generate_schema)

                transaction_resource = provision.TransactionResource(
                    database_resource, schema_resource)

                self._schema_resources[key] = \
                    transaction_resource

            transaction_resource = self._schema_resources[key]

            return [
                ('transaction_engine', transaction_resource),
                ('db', database_resource),
            ]
        else:
            key = (driver, None)
            if key not in self._schema_resources:
                self._schema_resources[key] = provision.SchemaResource(
                    database_resource, generate_schema, teardown=True)

            schema_resource = self._schema_resources[key]
            return [
                ('schema', schema_resource),
                ('db', database_resource)
            ]
Beispiel #6
0
    def _test_new_db_is_provisioned_by_default(self, base_cls):
        try:
            provision.DatabaseResource(base_cls.FIXTURE.DRIVER)
        except exception.BackendNotAvailable:
            self.skipTest("Backend %s is not available" %
                          base_cls.FIXTURE.DRIVER)

        class SomeTest(base_cls):
            def runTest(self):
                pass
        st = SomeTest()

        db_resource = dict(st.resources)['db']
        self.assertTrue(db_resource.provision_new_database)
Beispiel #7
0
 def _init_resources(cls):
     # this is a classlevel version of what testresources
     # does w/ the resources attribute as well as the
     # setUpResources() step (which requires a test instance, that
     # SqlFixture does not have).  Because this is a SQLite memory
     # database, we don't actually tear it down, so we can keep
     # it running throughout all tests.
     if cls._GLOBAL_RESOURCES:
         return
     else:
         cls._GLOBAL_RESOURCES = True
         cls.schema_resource = provision.SchemaResource(
             provision.DatabaseResource("sqlite"),
             cls._generate_schema, teardown=False)
         dependency_resources = {}
         for name, resource in cls.schema_resource.resources:
             dependency_resources[name] = resource.getResource()
         cls.schema_resource.make(dependency_resources)
         cls.engine = dependency_resources['database'].engine
Beispiel #8
0
    def _run_test(self):
        try:
            database_resource = provision.DatabaseResource(self.DRIVER)
        except exception.BackendNotAvailable:
            self.skip("database not available")

        schema_resource = provision.SchemaResource(
            database_resource, self._gen_schema)
        transaction_resource = provision.TransactionResource(
            database_resource, schema_resource)

        engine = transaction_resource.getResource()

        with engine.connect() as conn:
            rows = conn.execute(self.test_table.select())
            self.assertEqual([], rows.fetchall())

            trans = conn.begin()
            conn.execute(
                self.test_table.insert(),
                {"x": 1, "y": 2}
            )
            trans.rollback()

            rows = conn.execute(self.test_table.select())
            self.assertEqual([], rows.fetchall())

            trans = conn.begin()
            conn.execute(
                self.test_table.insert(),
                {"x": 2, "y": 3}
            )
            trans.commit()

            rows = conn.execute(self.test_table.select())
            self.assertEqual([(2, 3)], rows.fetchall())

        transaction_resource.finishedWith(engine)
Beispiel #9
0
 def _generate_database_resource(self, _enginefacade):
     return provision.DatabaseResource(self.driver,
                                       _enginefacade,
                                       provision_new_database=True)
Beispiel #10
0
 def _generate_database_resource(self, _enginefacade):
     return provision.DatabaseResource(self.driver,
                                       _enginefacade,
                                       ad_hoc_url=self.url,
                                       provision_new_database=False)