Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize tests to match their code location #10191

Merged
merged 11 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions tests/unit/test_jinja.py → tests/unit/clients/test_jinja.py
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This we converted from unit test to pytest

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing from unit test to pytest

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import unittest
from contextlib import contextmanager

import pytest
Expand Down Expand Up @@ -367,7 +366,7 @@ def expected_id(arg):


@pytest.mark.parametrize("value,text_expectation,native_expectation", jinja_tests, ids=expected_id)
def test_jinja_rendering(value, text_expectation, native_expectation):
def test_jinja_rendering_string(value, text_expectation, native_expectation):
foo_value = yaml.safe_load(value)["foo"]
ctx = {"a_str": "100", "a_int": 100, "b_str": "hello"}
with text_expectation as text_result:
Expand All @@ -377,40 +376,41 @@ def test_jinja_rendering(value, text_expectation, native_expectation):
assert native_result == get_rendered(foo_value, ctx, native=True)


class TestJinja(unittest.TestCase):
def test_do(self):
s = "{% set my_dict = {} %}\n{% do my_dict.update(a=1) %}"
def test_do():
s = "{% set my_dict = {} %}\n{% do my_dict.update(a=1) %}"

template = get_template(s, {})
mod = template.make_module()
self.assertEqual(mod.my_dict, {"a": 1})
template = get_template(s, {})
mod = template.make_module()
assert mod.my_dict == {"a": 1}

def test_regular_render(self):
s = '{{ "some_value" | as_native }}'
value = get_rendered(s, {}, native=False)
assert value == "some_value"
s = "{{ 1991 | as_native }}"
value = get_rendered(s, {}, native=False)
assert value == "1991"

s = '{{ "some_value" | as_text }}'
value = get_rendered(s, {}, native=False)
assert value == "some_value"
s = "{{ 1991 | as_text }}"
value = get_rendered(s, {}, native=False)
assert value == "1991"
def test_regular_render():
s = '{{ "some_value" | as_native }}'
value = get_rendered(s, {}, native=False)
assert value == "some_value"
s = "{{ 1991 | as_native }}"
value = get_rendered(s, {}, native=False)
assert value == "1991"

def test_native_render(self):
s = '{{ "some_value" | as_native }}'
value = get_rendered(s, {}, native=True)
assert value == "some_value"
s = "{{ 1991 | as_native }}"
value = get_rendered(s, {}, native=True)
assert value == 1991
s = '{{ "some_value" | as_text }}'
value = get_rendered(s, {}, native=False)
assert value == "some_value"
s = "{{ 1991 | as_text }}"
value = get_rendered(s, {}, native=False)
assert value == "1991"

s = '{{ "some_value" | as_text }}'
value = get_rendered(s, {}, native=True)
assert value == "some_value"
s = "{{ 1991 | as_text }}"
value = get_rendered(s, {}, native=True)
assert value == "1991"

def test_native_render():
s = '{{ "some_value" | as_native }}'
value = get_rendered(s, {}, native=True)
assert value == "some_value"
s = "{{ 1991 | as_native }}"
value = get_rendered(s, {}, native=True)
assert value == 1991

s = '{{ "some_value" | as_text }}'
value = get_rendered(s, {}, native=True)
assert value == "some_value"
s = "{{ 1991 | as_text }}"
value = get_rendered(s, {}, native=True)
assert value == "1991"
188 changes: 187 additions & 1 deletion tests/unit/config/test_selectors.py
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consolidated test_manifest_selectors.py to this file

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import yaml

import dbt.exceptions
from dbt.config.selectors import SelectorConfig, selector_config_from_data
from dbt.config.selectors import SelectorConfig, SelectorDict, selector_config_from_data
from dbt.exceptions import DbtSelectorsError


def get_selector_dict(txt: str) -> dict:
Expand Down Expand Up @@ -201,3 +202,188 @@ def test_multiple_default_true(self):
dbt.exceptions.DbtSelectorsError, "Found multiple selectors with `default: true`:"
):
selector_config_from_data(dct)

def test_compare_cli_non_cli(self):
dct = get_selector_dict(
"""\
selectors:
- name: nightly_diet_snowplow
description: "This uses more CLI-style syntax"
definition:
union:
- intersection:
- '@source:snowplow'
- 'tag:nightly'
- 'models/export'
- exclude:
- intersection:
- 'package:snowplow'
- 'config.materialized:incremental'
- export_performance_timing
- name: nightly_diet_snowplow_full
description: "This is a fuller YAML specification"
definition:
union:
- intersection:
- method: source
value: snowplow
childrens_parents: true
- method: tag
value: nightly
- method: path
value: models/export
- exclude:
- intersection:
- method: package
value: snowplow
- method: config.materialized
value: incremental
- method: fqn
value: export_performance_timing
"""
)

sel_dict = SelectorDict.parse_from_selectors_list(dct["selectors"])
assert sel_dict
with_strings = sel_dict["nightly_diet_snowplow"]["definition"]
no_strings = sel_dict["nightly_diet_snowplow_full"]["definition"]
self.assertEqual(with_strings, no_strings)

def test_single_string_definition(self):
dct = get_selector_dict(
"""\
selectors:
- name: nightly_selector
definition:
'tag:nightly'
"""
)

sel_dict = SelectorDict.parse_from_selectors_list(dct["selectors"])
assert sel_dict
expected = {"method": "tag", "value": "nightly"}
definition = sel_dict["nightly_selector"]["definition"]
self.assertEqual(expected, definition)

def test_single_key_value_definition(self):
dct = get_selector_dict(
"""\
selectors:
- name: nightly_selector
definition:
tag: nightly
"""
)

sel_dict = SelectorDict.parse_from_selectors_list(dct["selectors"])
assert sel_dict
expected = {"method": "tag", "value": "nightly"}
definition = sel_dict["nightly_selector"]["definition"]
self.assertEqual(expected, definition)

def test_parent_definition(self):
dct = get_selector_dict(
"""\
selectors:
- name: kpi_nightly_selector
definition:
'+exposure:kpi_nightly'
"""
)

sel_dict = SelectorDict.parse_from_selectors_list(dct["selectors"])
assert sel_dict
expected = {"method": "exposure", "value": "kpi_nightly", "parents": True}
definition = sel_dict["kpi_nightly_selector"]["definition"]
self.assertEqual(expected, definition)

def test_plus_definition(self):
dct = get_selector_dict(
"""\
selectors:
- name: my_model_children_selector
definition:
'my_model+2'
"""
)

sel_dict = SelectorDict.parse_from_selectors_list(dct["selectors"])
assert sel_dict
expected = {"method": "fqn", "value": "my_model", "children": True, "children_depth": "2"}
definition = sel_dict["my_model_children_selector"]["definition"]
self.assertEqual(expected, definition)

def test_selector_definition(self):
dct = get_selector_dict(
"""\
selectors:
- name: default
definition:
union:
- intersection:
- tag: foo
- tag: bar
- name: inherited
definition:
method: selector
value: default
"""
)

sel_dict = SelectorDict.parse_from_selectors_list(dct["selectors"])
assert sel_dict
definition = sel_dict["default"]["definition"]
expected = sel_dict["inherited"]["definition"]
self.assertEqual(expected, definition)

def test_selector_definition_with_exclusion(self):
dct = get_selector_dict(
"""\
selectors:
- name: default
definition:
union:
- intersection:
- tag: foo
- tag: bar
- name: inherited
definition:
union:
- method: selector
value: default
- exclude:
- tag: bar
- name: comparison
definition:
union:
- union:
- intersection:
- tag: foo
- tag: bar
- exclude:
- tag: bar
"""
)

sel_dict = SelectorDict.parse_from_selectors_list((dct["selectors"]))
assert sel_dict
definition = sel_dict["inherited"]["definition"]
expected = sel_dict["comparison"]["definition"]
self.assertEqual(expected, definition)

def test_missing_selector(self):
dct = get_selector_dict(
"""\
selectors:
- name: inherited
definition:
method: selector
value: default
"""
)
with self.assertRaises(DbtSelectorsError) as err:
SelectorDict.parse_from_selectors_list((dct["selectors"]))

self.assertEqual(
"Existing selector definition for default not found.", str(err.exception.msg)
)
Empty file.
File renamed without changes.
Loading
Loading