예제 #1
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)
            ]
예제 #2
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)