Beispiel #1
0
def test_resolve_specific(config, manifest_extended, redshift_adapter,
                          get_include_paths):
    rs_macro = manifest_extended.macros[
        'macro.dbt_redshift.redshift__some_macro']
    package_rs_macro = manifest_extended.macros[
        'macro.root.redshift__some_macro']

    ctx = providers.generate_runtime_model(
        model=mock_model(),
        config=config,
        manifest=manifest_extended,
    )

    # macro_a exists, but default__macro_a and redshift__macro_a do not
    with pytest.raises(dbt.exceptions.CompilationException):
        ctx['adapter'].dispatch('macro_a').macro

    assert ctx['adapter'].dispatch('some_macro').macro is package_rs_macro
    assert ctx['adapter'].dispatch('some_macro', packages=['dbt'
                                                           ]).macro is rs_macro
    assert ctx['adapter'].dispatch('some_macro',
                                   packages=['root']).macro is package_rs_macro
    assert ctx['adapter'].dispatch(
        'some_macro', packages=['root', 'dbt']).macro is package_rs_macro
    assert ctx['adapter'].dispatch('some_macro', packages=['dbt', 'root'
                                                           ]).macro is rs_macro
Beispiel #2
0
    def execute(self, model, manifest):
        context = generate_runtime_model(model, self.config, manifest)

        materialization_macro = manifest.find_materialization_macro_by_name(
            self.config.project_name, model.get_materialization(),
            self.adapter.type())

        if materialization_macro is None:
            missing_materialization(model, self.adapter.type())

        if 'config' not in context:
            raise InternalException(
                'Invalid materialization context generated, missing config: {}'
                .format(context))
        context_config = context['config']

        hook_ctx = self.adapter.pre_model_hook(context_config)
        try:
            result = MacroGenerator(materialization_macro, context)()
        finally:
            self.adapter.post_model_hook(context_config, hook_ctx)

        for relation in self._materialization_relations(result, model):
            self.adapter.cache_added(relation.incorporate(dbt_created=True))

        return self._build_run_model_result(model, context)
Beispiel #3
0
def test_model_runtime_context(config, manifest, get_adapter):
    ctx = providers.generate_runtime_model(
        model=mock_model(),
        config=config,
        manifest=manifest,
    )
    assert_has_keys(REQUIRED_MODEL_KEYS, MAYBE_KEYS, ctx)
Beispiel #4
0
    def _create_node_context(
        self,
        node: NonSourceCompiledNode,
        manifest: Manifest,
        extra_context: Dict[str, Any],
    ) -> Dict[str, Any]:
        context = generate_runtime_model(node, self.config, manifest)
        context.update(extra_context)
        if isinstance(node, CompiledSchemaTestNode):
            # for test nodes, add a special keyword args value to the context
            dbt.clients.jinja.add_rendered_test_kwargs(context, node)

        return context
Beispiel #5
0
def test_resolve_errors(config, manifest_extended, redshift_adapter,
                        get_include_paths):
    ctx = providers.generate_runtime_model(
        model=mock_model(),
        config=config,
        manifest=manifest_extended,
    )
    with pytest.raises(dbt.exceptions.CompilationException) as exc:
        ctx['adapter'].dispatch('no_default_exists')
    assert 'default__no_default_exists' in str(exc.value)
    assert 'redshift__no_default_exists' in str(exc.value)

    with pytest.raises(dbt.exceptions.CompilationException) as exc:
        ctx['adapter'].dispatch('namespace.no_default_exists')
    assert '"." is not a valid macro name component' in str(exc.value)
    assert 'adapter.dispatch' in str(exc.value)
    assert 'packages=["namespace"]' in str(exc.value)
Beispiel #6
0
    def execute_test(self, test: Union[CompiledDataTestNode,
                                       CompiledSchemaTestNode],
                     manifest: Manifest) -> int:
        context = generate_runtime_model(test, self.config, manifest)

        materialization_macro = manifest.find_materialization_macro_by_name(
            self.config.project_name, test.get_materialization(),
            self.adapter.type())

        if materialization_macro is None:
            missing_materialization(test, self.adapter.type())

        if 'config' not in context:
            raise InternalException(
                'Invalid materialization context generated, missing config: {}'
                .format(context))

        # generate materialization macro
        # simple `select(*)` of the compiled test node
        macro_func = MacroGenerator(materialization_macro, context)
        # execute materialization macro
        macro_func()
        # load results from context
        # could eventually be returned directly by materialization
        result = context['load_result']('main')
        table = result['table']
        num_rows = len(table.rows)
        if num_rows != 1:
            num_cols = len(table.columns)
            # since we just wrapped our query in `select count(*)`, we are in
            # big trouble!
            raise InternalException(
                f"dbt internally failed to execute {test.unique_id}: "
                f"Returned {num_rows} rows and {num_cols} cols, but expected "
                f"1 row and 1 column")
        return int(table[0][0])