def _get_adapter(self, tmpdir):
        project_path = os.path.join(tmpdir, 'project')
        args = parse_args([
            'compile', '--profile', 'dbt-pytest', '--target', 'default',
            '--project-dir', project_path, '--profiles-dir', tmpdir,
            '--vars', yaml.safe_dump(self._base_vars()),
        ])
        with open(os.path.join(args.profiles_dir, 'profiles.yml')) as fp:
            data = yaml.safe_load(fp)
            try:
                profile = data[args.profile]
            except KeyError:
                raise ValueError(f'profile {args.profile} not found')
            try:
                outputs = profile['outputs']
            except KeyError:
                raise ValueError(f'malformed profile {args.profile}')
            try:
                target = outputs[args.target]
            except KeyError:
                raise ValueError(
                    f'target {args.target} not found in {args.profile}'
                )
            try:
                adapter_type = target['type']
            except KeyError:
                raise ValueError(
                    f'target {args.target} in {args.profile} has no type')
        _ = FACTORY.load_plugin(adapter_type)
        config = RuntimeConfig.from_args(args)

        FACTORY.register_adapter(config)
        adapter = FACTORY.lookup_adapter(config.credentials.type)
        return adapter
Exemple #2
0
    def runtest(self):
        FACTORY.reset_adapters()
        with tempfile.TemporaryDirectory() as tmpdir:
            self._write_profile(tmpdir)
            self.project.write(tmpdir)
            self.adapter = self._get_adapter(tmpdir)

            self.schema_relation = self.adapter.Relation.create(
                database=self.adapter.config.credentials.database,
                schema=self.adapter.config.credentials.schema,
                quote_policy=self.adapter.config.quoting,
            )

            try:
                for idx, test_item in enumerate(self.sequence):
                    self.run_test_item(idx, test_item, tmpdir)
            finally:
                with self.adapter.connection_named('__test'):
                    if self.config.getoption('drop_schema'):
                        self.adapter.drop_schema(self.schema_relation)

        return True
Exemple #3
0
def inject_plugin_for(config):
    # from dbt.adapters.postgres import Plugin, PostgresAdapter
    from dbt.adapters.factory import FACTORY
    FACTORY.load_plugin(config.credentials.type)
    adapter = FACTORY.get_adapter(config)
    return adapter