How to use dbt - 10 common examples

To help you get started, we’ve selected a few dbt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fishtown-analytics / dbt / test / unit / test_context.py View on Github external
def test_var_defined_is_missing(self):
        var = Var(self.model, self.context, overrides={})
        var.assert_var_defined('foo', 'bar')
        with self.assertRaises(dbt.exceptions.CompilationException):
            var.assert_var_defined('foo', None)
github fishtown-analytics / dbt / test / integration / base.py View on Github external
register_adapter(self.config)
        adapter = get_adapter(self.config)
        if adapter is not self.adapter:
            adapter.cleanup_connections()
        if not hasattr(self, 'adapter'):
            self.adapter = adapter

        self._drop_schemas()

        self.adapter.cleanup_connections()
        reset_adapters()
        os.chdir(INITIAL_ROOT)
        try:
            shutil.rmtree(self.test_root_dir)
        except EnvironmentError:
            logger.exception('Could not clean up after test - {} not removable'
                             .format(self.test_root_dir))
github fishtown-analytics / dbt / test / unit / test_docs_blocks.py View on Github external
system.find_matching.return_value = [{
            'relative_path': 'test_file.md',
            'absolute_path': '/test_root/test_subdir/test_file.md',
            'searched_path': '/test_root/test_subdir',
        }]
        results = list(docs.DocumentationParser.load_file(
            'some_package', '/test_root', ['test_subdir'])
        )
        self.assertEqual(len(results), 1)
        result = results[0]
        self.assertEqual(result.package_name, 'some_package')
        self.assertEqual(result.file_contents, TEST_DOCUMENTATION_FILE)
        self.assertEqual(result.original_file_path,
                         '/test_root/test_subdir/test_file.md')
        self.assertEqual(result.root_path, '/test_root')
        self.assertEqual(result.resource_type, NodeType.Documentation)
        self.assertEqual(result.path, 'test_file.md')
github fishtown-analytics / dbt-spark / test / unit / utils.py View on Github external
from dbt.node_types import NodeType
    name_sql = {}
    for component in ('database', 'schema', 'alias'):
        if component == 'alias':
            source = 'node.name'
        else:
            source = f'target.{component}'
        name = f'generate_{component}_name'
        sql = f'{{% macro {name}(value, node) %}} {{% if value %}} {{{{ value }}}} {{% else %}} {{{{ {source} }}}} {{% endif %}} {{% endmacro %}}'
        name_sql[name] = sql

    all_sql = '\n'.join(name_sql.values())
    for name, sql in name_sql.items():
        pm = ParsedMacro(
            name=name,
            resource_type=NodeType.Macro,
            unique_id=f'macro.{package}.{name}',
            package_name=package,
            original_file_path=normalize('macros/macro.sql'),
            root_path='./dbt_modules/root',
            path=normalize('macros/macro.sql'),
            raw_sql=all_sql,
            macro_sql=sql,
        )
        yield pm
github fishtown-analytics / dbt / test / integration / 051_query_comments_test / test_query_comments.py View on Github external
def run_assert_comments(self):
        with self.assertRaises(dbt.exceptions.RuntimeException):
            self.run_get_json(expect_pass=False)
github fishtown-analytics / dbt / test / unit / test_graph.py View on Github external
def tearDown(self):
        nx.write_gpickle = self.real_write_gpickle
        dbt.utils.dependency_projects = self.real_dependency_projects
        dbt.clients.system.find_matching = self.real_find_matching
        dbt.clients.system.load_file_contents = self.real_load_file_contents
github fishtown-analytics / dbt / test / unit / test_utils.py View on Github external
def test__noop(self):
        actual = dbt.utils.deep_map(lambda x, _: x, self.input_value)
        self.assertEqual(actual, self.input_value)
github fishtown-analytics / dbt / test / rpc / util.py View on Github external
def built_schema(project_dir, schema, profiles_dir, test_kwargs, project_def):
    # make our args, write our project out
    args = TestArgs(profiles_dir=profiles_dir, kwargs=test_kwargs)
    project_def.write_to(project_dir)
    # build a config of our own
    os.chdir(project_dir)
    start = os.getcwd()
    try:
        cfg = RuntimeConfig.from_args(args)
    finally:
        os.chdir(start)
    register_adapter(cfg)
    adapter = get_adapter(cfg)
    execute(adapter, 'drop schema if exists {} cascade'.format(schema))
    execute(adapter, 'create schema {}'.format(schema))
    yield
    adapter = get_adapter(cfg)
    adapter.cleanup_connections()
    execute(adapter, 'drop schema if exists {} cascade'.format(schema))
github fishtown-analytics / dbt / test / unit / test_system_client.py View on Github external
def test__executable_does_not_exist(self):
        with self.assertRaises(ExecutableError) as exc:
            dbt.clients.system.run_cmd(self.run_dir, [self.does_not_exist])

        msg = str(exc.exception).lower()

        self.assertIn('path', msg)
        self.assertIn('could not find', msg)
        self.assertIn(self.does_not_exist.lower(), msg)
github fishtown-analytics / dbt / test / integration / 042_sources_test / test_sources.py View on Github external
def test_postgres_malformed_schema_nonstrict_will_break_run(self):
        with self.assertRaises(CompilationException):
            self.run_dbt_with_vars(['seed'], strict=False)