Skip to content

Commit

Permalink
Introduce CommandTable.is_valid()
Browse files Browse the repository at this point in the history
* is_valid() can be used to validate the command table
  • Loading branch information
markheik committed Nov 2, 2023
1 parent 4c95859 commit d94c30f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/zhinst/toolkit/command_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ class CommandTable:
No validation happens during command table entry modifications, thus
making the creation of the command table faster.
Method `is_valid()` can be used for command table validation when active
validation is disabled. It is recommended to avoid it in production code.
.. versionadded:: 0.5.0
The ``active_validation`` parameter was added.
Expand Down Expand Up @@ -483,6 +486,36 @@ def as_dict(self) -> dict:
_validate_instance(result, self._ct_schema)
return result

def is_valid(self, raise_for_invalid: bool = False) -> bool:
"""Checks if the command table is valid.
Args:
raise_for_invalid: Raises exception if the command table is invalid.
The flag can be used for getting feedback on what is wrong in
the command table.
Returns:
True if the command table is valid.
Raises:
ValidationError: If `raise_for_invalid` was set to `True` and the
command table is invalid.
.. versionadded:: 0.6.2
"""
# Due to active_validation having a state, we need to force it to True.
orig_state = self._active_validation
try:
self._active_validation = True if self._active_validation is False else True
self.as_dict()
self._active_validation = orig_state
return True
except ValidationError as e:
self._active_validation = orig_state
if raise_for_invalid:
raise e
return False

def update(self, command_table: t.Union[str, dict]) -> None:
"""Update the existing instance of ``CommandTable`` with command table JSON.
Expand Down
18 changes: 17 additions & 1 deletion tests/command_table/test_command_table.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from unittest.mock import patch
from unittest.mock import patch, Mock

import jsonref
import jsonschema
Expand Down Expand Up @@ -162,11 +162,27 @@ def test_command_table_active_validation_table(command_table_schema):
ct = CommandTable(command_table_schema, active_validation=False)
ct.table[999999].amplitude00.value = 1
ct.as_dict()
assert ct.is_valid() is False
ct.active_validation = True
with pytest.raises(ValidationError):
ct.as_dict()


@pytest.mark.parametrize("state", [True, False])
def test_table_is_valid_active_validation_state_persists(command_table, state):
command_table.active_validation = state
command_table.is_valid() is True
assert command_table.active_validation is state

command_table.as_dict = Mock(side_effect=ValidationError)
command_table.is_valid() is False
assert command_table.active_validation is state

with pytest.raises(ValidationError):
command_table.is_valid(raise_for_invalid=True)
assert command_table.active_validation is state


def test_command_table_active_validation_parent_entry(command_table_schema):
ct = CommandTable(command_table_schema, active_validation=True)
ct.table[0]
Expand Down

0 comments on commit d94c30f

Please sign in to comment.