-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_unit_tests.py
117 lines (91 loc) · 4.53 KB
/
check_unit_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# mypy: disable-error-code="union-attr"
import logging
from typing import TYPE_CHECKING, List, Literal
import semver
from pydantic import Field
from dbt_bouncer.check_base import BaseCheck
if TYPE_CHECKING:
import warnings
from dbt_bouncer.parsers import DbtBouncerManifest
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
from dbt_artifacts_parser.parsers.manifest.manifest_v12 import (
UnitTests,
)
class CheckUnitTestExpectFormats(BaseCheck):
name: Literal["check_unit_test_expect_format"]
permitted_formats: List[Literal["csv", "dict", "sql"]] = Field(
default=["csv", "dict", "sql"],
)
def check_unit_test_expect_format(
manifest_obj: "DbtBouncerManifest",
unit_test: "UnitTests",
permitted_formats: List[Literal["csv", "dict", "sql"]] = ["csv", "dict", "sql"], # noqa: B006
**kwargs,
) -> None:
"""Unit tests can only use the specified formats.
!!! warning
This check is only supported for dbt 1.8.0 and above.
Parameters:
manifest_obj (DbtBouncerManifest): The DbtBouncerManifest object parsed from `manifest.json`.
permitted_formats (Optional[List[Literal["csv", "dict", "sql"]]]): A list of formats that are allowed to be used for `expect` input in a unit test.
unit_test (UnitTests): The UnitTests object to check.
Other Parameters:
exclude (Optional[str]): Regex pattern to match the unit test path (i.e the .yml file where the unit test is configured). Unit test paths that match the pattern will not be checked.
include (Optional[str]): Regex pattern to match the unit test path (i.e the .yml file where the unit test is configured). Only unit test paths that match the pattern will be checked.
severity (Optional[Literal["error", "warn"]]): Severity level of the check. Default: `error`.
Example(s):
```yaml
manifest_checks:
- name: check_unit_test_expect_format
permitted_formats:
- csv
```
"""
if semver.Version.parse(manifest_obj.manifest.metadata.dbt_version) >= "1.8.0":
assert (
unit_test.expect.format.value in permitted_formats
), f"Unit test `{unit_test.name}` has an `expect` format that is not permitted. Permitted formats are: {permitted_formats}."
else:
logging.warning(
"The `check_unit_test_expect_format` check is only supported for dbt 1.8.0 and above.",
)
class CheckUnitTestGivenFormats(BaseCheck):
name: Literal["check_unit_test_given_formats"]
permitted_formats: List[Literal["csv", "dict", "sql"]] = Field(
default=["csv", "dict", "sql"],
)
def check_unit_test_given_formats(
manifest_obj: "DbtBouncerManifest",
unit_test: "UnitTests",
permitted_formats: List[Literal["csv", "dict", "sql"]] = ["csv", "dict", "sql"], # noqa: B006
**kwargs,
) -> None:
"""Unit tests can only use the specified formats.
!!! warning
This check is only supported for dbt 1.8.0 and above.
Parameters:
manifest_obj (DbtBouncerManifest): The DbtBouncerManifest object parsed from `manifest.json`.
permitted_formats (Optional[List[Literal["csv", "dict", "sql"]]]): A list of formats that are allowed to be used for `expect` input in a unit test.
unit_test (UnitTests): The UnitTests object to check.
Other Parameters:
exclude (Optional[str]): Regex pattern to match the unit test path (i.e the .yml file where the unit test is configured). Unit test paths that match the pattern will not be checked.
include (Optional[str]): Regex pattern to match the unit test path (i.e the .yml file where the unit test is configured). Only unit test paths that match the pattern will be checked.
severity (Optional[Literal["error", "warn"]]): Severity level of the check. Default: `error`.
Example(s):
```yaml
manifest_checks:
- name: check_unit_test_given_formats
permitted_formats:
- csv
```
"""
if semver.Version.parse(manifest_obj.manifest.metadata.dbt_version) >= "1.8.0":
given_formats = [i.format.value for i in unit_test.given]
assert all(
e in permitted_formats for e in given_formats
), f"Unit test `{unit_test.name}` has given formats which are not permitted. Permitted formats are: {permitted_formats}."
else:
logging.warning(
"The `check_unit_test_given_formats` check is only supported for dbt 1.8.0 and above.",
)