Skip to content

Commit

Permalink
Merge branch 'feat/assert-to-val-error' into 'master'
Browse files Browse the repository at this point in the history
feat[node]: validation error instead of assertion

See merge request utilities/ntc!15
  • Loading branch information
Vladimir Mikhaylov committed Oct 16, 2020
2 parents 1e9c606 + 4237d62 commit d3ecbcc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ntc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .leaf import *
from .node import *

__version__ = "0.7.0"
__version__ = "0.8.0"
15 changes: 13 additions & 2 deletions ntc/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@

import yaml

from ntc.errors import MissingRequired, NodeFrozenError, NodeReassignment, SaveError, SchemaError, SchemaFrozenError
from ntc.errors import (
MissingRequired,
NodeFrozenError,
NodeReassignment,
SaveError,
SchemaError,
SchemaFrozenError,
ValidationError,
)
from ntc.utils import add_yaml_str_representer, import_module, merge_cfg_module

from .leaf import CfgLeaf
Expand Down Expand Up @@ -153,7 +161,10 @@ def validate(self) -> None:
"""
validators = [CfgNode.validate_required] + self._validators
for validator in validators:
validator(self)
try:
validator(self)
except AssertionError as exc:
raise ValidationError from exc

def run_hooks(self) -> None:
"""
Expand Down
5 changes: 3 additions & 2 deletions tests/test_post_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from ntc import CN, SchemaFrozenError
from ntc.errors import ValidationError

DATA_DIR = Path(__file__).parent / "data" / "post_load"

Expand All @@ -14,12 +15,12 @@ def test_transform():


def test_validate():
with pytest.raises(AssertionError):
with pytest.raises(ValidationError):
CN.load(DATA_DIR / "validate_changes.py")


def test_bad_validate():
with pytest.raises(AssertionError):
with pytest.raises(ValidationError):
CN.load(DATA_DIR / "bad_validate.py")


Expand Down

0 comments on commit d3ecbcc

Please sign in to comment.