Skip to content

Commit

Permalink
Fix #9715: Simplify error message if test severity isn't 'warn' or 'e…
Browse files Browse the repository at this point in the history
…rror' (#10015)
  • Loading branch information
aranke authored Apr 23, 2024
1 parent 27943a5 commit 1014a6d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20240423-120112.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Simplify error message if test severity isn't 'warn' or 'error'
time: 2024-04-23T12:01:12.374904+02:00
custom:
Author: aranke
Issue: "9715"
8 changes: 8 additions & 0 deletions core/dbt/artifacts/resources/v1/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

from dbt_common.dataclass_schema import dbtClassMixin, ValidationError
from typing import Optional, List, Any, Dict, Union
from typing_extensions import Annotated
Expand Down Expand Up @@ -250,6 +252,12 @@ def same_contents(cls, unrendered: Dict[str, Any], other: Dict[str, Any]) -> boo

@classmethod
def validate(cls, data):
if data.get("severity") and not re.match(SEVERITY_PATTERN, data.get("severity")):
raise ValidationError(
f"Severity must be either 'warn' or 'error'. Got '{data.get('severity')}'"
)

super().validate(data)

if data.get("materialized") and data.get("materialized") != "test":
raise ValidationError("A test must have a materialized value of 'test'")
32 changes: 28 additions & 4 deletions tests/unit/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import unittest
from argparse import Namespace
from copy import deepcopy
from unittest import mock

Expand All @@ -8,12 +9,12 @@
import dbt.flags
import dbt.parser
from dbt import tracking
from dbt.artifacts.resources import ModelConfig
from dbt.artifacts.resources import RefArgs
from dbt.context.context_config import ContextConfig
from dbt.contracts.files import SourceFile, FileHash, FilePath, SchemaSourceFile
from dbt.contracts.graph.manifest import Manifest
from dbt.contracts.graph.model_config import NodeConfig, TestConfig, SnapshotConfig
from dbt.artifacts.resources import ModelConfig
from dbt.contracts.graph.nodes import (
ModelNode,
Macro,
Expand All @@ -23,7 +24,8 @@
AnalysisNode,
UnpatchedSourceDefinition,
)
from dbt.exceptions import CompilationError, ParsingError
from dbt.exceptions import CompilationError, ParsingError, SchemaConfigError
from dbt.flags import set_from_args
from dbt.node_types import NodeType
from dbt.parser import (
ModelParser,
Expand Down Expand Up @@ -53,8 +55,6 @@
from dbt.parser.search import FileBlock
from dbt.parser.sources import SourcePatcher
from .utils import config_from_parts_or_dicts, normalize, generate_name_macros, MockNode
from dbt.flags import set_from_args
from argparse import Namespace

set_from_args(Namespace(WARN_ERROR=False), None)

Expand Down Expand Up @@ -273,6 +273,22 @@ def assertEqualNodes(node_one, node_two):
arg: 100
"""

SINGLE_TABLE_MODEL_TESTS_WRONG_SEVERITY = """
models:
- name: my_model
description: A description of my model
columns:
- name: color
description: The color value
data_tests:
- not_null:
severity: WARNING
- accepted_values:
values: ['red', 'blue', 'green']
- foreign_package.test_case:
arg: 100
"""


MULTIPLE_TABLE_VERSIONED_MODEL_TESTS = """
models:
Expand Down Expand Up @@ -577,6 +593,14 @@ def test__read_basic_model_tests(self):
self.assertEqual(len(list(self.parser.manifest.sources)), 0)
self.assertEqual(len(list(self.parser.manifest.nodes)), 4)

def test__read_basic_model_tests_wrong_severity(self):
block = self.yaml_block_for(SINGLE_TABLE_MODEL_TESTS_WRONG_SEVERITY, "test_one.yml")
dct = yaml_from_file(block.file)
with self.assertRaisesRegex(
SchemaConfigError, "Severity must be either 'warn' or 'error'. Got 'WARNING'"
):
self.parser.parse_file(block, dct)

def test__parse_basic_model_tests(self):
block = self.file_block_for(SINGLE_TABLE_MODEL_TESTS, "test_one.yml")
self.parser.manifest.files[block.file.file_id] = block.file
Expand Down

0 comments on commit 1014a6d

Please sign in to comment.