Skip to content

Commit

Permalink
Make tox.ini conform to 4.0.0 release requirements (dbt-labs#230)
Browse files Browse the repository at this point in the history
* Make tox.ini conform to 4.0.0 release requirements

* Had to update classes per dbt core 6384 which broke unit tests.

* Fix up utils by tossing dead/error code.

* caught a bug in here

* Add changelog

* Get rido of the changelog, unneeded.

Co-authored-by: Mila Page <[email protected]>
  • Loading branch information
VersusFacit and VersusFacit authored Dec 9, 2022
1 parent 46eca25 commit 7f2166c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 140 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ test: ## Runs unit tests with py38 and code checks against staged changes.
pre-commit run mypy-check --hook-stage manual | grep -v "INFO"

.PHONY: integration
integration: ## Runs snowflake integration tests with py38.
integration: ## Runs redshift integration tests with py38.
@\
tox -e py38-snowflake --
tox -e py38-redshift --

.PHONY: clean
@echo "cleaning repo"
Expand Down
40 changes: 25 additions & 15 deletions tests/unit/test_context.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import unittest
import os
import pytest
import unittest

from unittest import mock

import pytest
from .utils import config_from_parts_or_dicts, inject_adapter, clear_plugin
from .mock_adapter import adapter_factory
import dbt.exceptions

from dbt.adapters import redshift
from dbt.adapters import factory
from dbt.contracts.graph.parsed import (
ParsedModelNode, NodeConfig, DependsOn, ParsedMacro
from dbt.adapters import (
redshift,
factory,
)
from dbt.contracts.graph.model_config import (
NodeConfig,
)
from dbt.contracts.graph.nodes import (
ModelNode,
DependsOn,
Macro
)
from dbt.context import providers
from dbt.node_types import NodeType
import dbt.exceptions
from .utils import config_from_parts_or_dicts, inject_adapter, clear_plugin
from .mock_adapter import adapter_factory


class TestRuntimeWrapper(unittest.TestCase):
def setUp(self):
Expand All @@ -27,6 +36,7 @@ def setUp(self):
self.mock_adapter, self.namespace)
self.responder = self.mock_adapter.responder


PROFILE_DATA = {
'target': 'test',
'quoting': {},
Expand All @@ -53,7 +63,7 @@ def setUp(self):


def model():
return ParsedModelNode(
return ModelNode(
alias='model_one',
name='model_one',
database='dbt',
Expand Down Expand Up @@ -85,9 +95,10 @@ def model():
columns={}
)


def mock_macro(name, package_name):
macro = mock.MagicMock(
__class__=ParsedMacro,
__class__=Macro,
package_name=package_name,
resource_type='macro',
unique_id=f'macro.{package_name}.{name}',
Expand All @@ -107,7 +118,7 @@ def mock_manifest(config):

def mock_model():
return mock.MagicMock(
__class__=ParsedModelNode,
__class__=ModelNode,
alias='model_one',
name='model_one',
database='dbt',
Expand Down Expand Up @@ -210,12 +221,11 @@ def test_resolve_specific(config, manifest_extended, redshift_adapter, get_inclu

# override 'dbt' namespace search order, dispatch to 'root' first
ctx['adapter'].config.dispatch = [{'macro_namespace': 'dbt', 'search_order': ['root', 'dbt']}]
assert ctx['adapter'].dispatch('some_macro', macro_namespace = 'dbt').macro is package_rs_macro
assert ctx['adapter'].dispatch('some_macro', macro_namespace='dbt').macro is package_rs_macro

# override 'dbt' namespace search order, dispatch to 'dbt' only
ctx['adapter'].config.dispatch = [{'macro_namespace': 'dbt', 'search_order': ['dbt']}]
assert ctx['adapter'].dispatch('some_macro', macro_namespace = 'dbt').macro is rs_macro
assert ctx['adapter'].dispatch('some_macro', macro_namespace='dbt').macro is rs_macro

# override 'root' namespace search order, dispatch to 'dbt' first
ctx['adapter'].config.dispatch = [{'macro_namespace': 'root', 'search_order': ['dbt', 'root']}]
assert ctx['adapter'].dispatch('some_macro', macro_namespace = 'root').macro is rs_macro
123 changes: 2 additions & 121 deletions tests/unit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def mock_connection(name, state='open'):
def profile_from_dict(profile, profile_name, cli_vars='{}'):
from dbt.config import Profile
from dbt.config.renderer import ProfileRenderer
from dbt.context.base import generate_base_context
from dbt.config.utils import parse_cli_vars
if not isinstance(cli_vars, dict):
cli_vars = parse_cli_vars(cli_vars)
Expand All @@ -54,8 +53,6 @@ def profile_from_dict(profile, profile_name, cli_vars='{}'):


def project_from_dict(project, profile, packages=None, selectors=None, cli_vars='{}'):
from dbt.context.target import generate_target_context
from dbt.config import Project
from dbt.config.renderer import DbtProjectYamlRenderer
from dbt.config.utils import parse_cli_vars
if not isinstance(cli_vars, dict):
Expand All @@ -74,7 +71,6 @@ def project_from_dict(project, profile, packages=None, selectors=None, cli_vars=
return partial.render(renderer)



def config_from_parts_or_dicts(project, profile, packages=None, selectors=None, cli_vars='{}'):
from dbt.config import Project, Profile, RuntimeConfig
from copy import deepcopy
Expand Down Expand Up @@ -155,7 +151,7 @@ def assert_from_dict(self, obj, dct, cls=None):
if cls is None:
cls = self.ContractType
cls.validate(dct)
self.assertEqual(cls.from_dict(dct), obj)
self.assertEqual(cls.from_dict(dct), obj)

def assert_symmetric(self, obj, dct, cls=None):
self.assert_to_dict(obj, dct)
Expand Down Expand Up @@ -219,33 +215,6 @@ def assert_fails_validation(dct, cls):
cls.from_dict(dct)


def generate_name_macros(package):
from dbt.contracts.graph.parsed import ParsedMacro
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

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_packages/root',
path=normalize('macros/macro.sql'),
macro_sql=sql,
)
yield pm


class TestAdapterConversions(TestCase):
def _get_tester_for(self, column_type):
from dbt.clients import agate_helper
Expand All @@ -268,99 +237,11 @@ def _make_table_of(self, rows, column_types):
return table


def MockMacro(package, name='my_macro', **kwargs):
from dbt.contracts.graph.parsed import ParsedMacro
from dbt.node_types import NodeType

mock_kwargs = dict(
resource_type=NodeType.Macro,
package_name=package,
unique_id=f'macro.{package}.{name}',
original_file_path='/dev/null',
)

mock_kwargs.update(kwargs)

macro = mock.MagicMock(
spec=ParsedMacro,
**mock_kwargs
)
macro.name = name
return macro


def MockMaterialization(package, name='my_materialization', adapter_type=None, **kwargs):
if adapter_type is None:
adapter_type = 'default'
kwargs['adapter_type'] = adapter_type
return MockMacro(package, f'materialization_{name}_{adapter_type}', **kwargs)


def MockGenerateMacro(package, component='some_component', **kwargs):
name = f'generate_{component}_name'
return MockMacro(package, name=name, **kwargs)


def MockSource(package, source_name, name, **kwargs):
from dbt.node_types import NodeType
from dbt.contracts.graph.parsed import ParsedSourceDefinition
src = mock.MagicMock(
__class__=ParsedSourceDefinition,
resource_type=NodeType.Source,
source_name=source_name,
package_name=package,
unique_id=f'source.{package}.{source_name}.{name}',
search_name=f'{source_name}.{name}',
**kwargs
)
src.name = name
return src


def MockNode(package, name, resource_type=None, **kwargs):
from dbt.node_types import NodeType
from dbt.contracts.graph.parsed import ParsedModelNode, ParsedSeedNode
if resource_type is None:
resource_type = NodeType.Model
if resource_type == NodeType.Model:
cls = ParsedModelNode
elif resource_type == NodeType.Seed:
cls = ParsedSeedNode
else:
raise ValueError(f'I do not know how to handle {resource_type}')
node = mock.MagicMock(
__class__=cls,
resource_type=resource_type,
package_name=package,
unique_id=f'{str(resource_type)}.{package}.{name}',
search_name=name,
**kwargs
)
node.name = name
return node


def MockDocumentation(package, name, **kwargs):
from dbt.node_types import NodeType
from dbt.contracts.graph.parsed import ParsedDocumentation
doc = mock.MagicMock(
__class__=ParsedDocumentation,
resource_type=NodeType.Documentation,
package_name=package,
search_name=name,
unique_id=f'{package}.{name}',
**kwargs
)
doc.name = name
return doc


def load_internal_manifest_macros(config, macro_hook = lambda m: None):
def load_internal_manifest_macros(config, macro_hook=lambda m: None):
from dbt.parser.manifest import ManifestLoader
return ManifestLoader.load_macros(config, macro_hook)



def dict_replace(dct, **kwargs):
dct = dct.copy()
dct.update(kwargs)
Expand Down
9 changes: 7 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ envlist = py37,py38,py39,py310
[testenv:{unit,py37,py38,py39,py310,py}]
description = unit testing
skip_install = true
passenv = DBT_* PYTEST_ADDOPTS
passenv =
DBT_*
PYTEST_ADDOPTS
commands = {envpython} -m pytest {posargs} tests/unit
deps =
-rdev-requirements.txt
Expand All @@ -14,7 +16,10 @@ deps =
[testenv:{integration,py37,py38,py39,py310,py}-{redshift}]
description = adapter plugin integration testing
skip_install = true
passenv = DBT_* REDSHIFT_TEST_* PYTEST_ADDOPTS
passenv =
DBT_*
REDSHIFT_TEST_*
PYTEST_ADDOPTS
commands =
redshift: {envpython} -m pytest {posargs} -m profile_redshift tests/integration
redshift: {envpython} -m pytest {posargs} tests/functional
Expand Down

0 comments on commit 7f2166c

Please sign in to comment.