Skip to content

Commit

Permalink
Adding selectbox options schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogenesAnalytics committed Mar 5, 2024
1 parent aa36a57 commit 138d284
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
29 changes: 28 additions & 1 deletion tests/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Defines the various schema conatained in config.json."""

import warnings
from abc import ABC
from abc import abstractmethod
from dataclasses import dataclass
from dataclasses import field
from typing import Any
Expand All @@ -9,15 +11,40 @@
from typing import Optional


class Schema(ABC):
"""Base class for schema validation."""

@abstractmethod
def __post_init__(self):
"""Abstract method for validation."""
pass

def validate_type(self):
"""Default implementation of type validation."""
for field_name, field_type in self.__annotations__.items():
if field_name in self.__dataclass_fields__:
actual_type = type(getattr(self, field_name))
if not issubclass(actual_type, field_type):
raise TypeError(
f"Expected {field_name:!r} to be of type "
f"{field_type.__name__}, got {actual_type.__name__}"
)


@dataclass
class SelectBoxOptions:
class SelectBoxOptions(Schema):
"""Defines the selectbox options schema for config.json."""

label: str
value: str
selected: Optional[bool] = field(default=None)
disabled: Optional[bool] = field(default=None)

def __post_init__(self):
"""Perform validation."""
# check type
self.validate_type()


@dataclass
class Question:
Expand Down
55 changes: 55 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Test the schemas of the various config.json section."""

from typing import Dict
from typing import Generator
from typing import Tuple
from typing import Union

import pytest

from tests.schema import SelectBoxOptions


def selectbox_option_test_data() -> (
Generator[Tuple[Dict[str, Union[bool, str]], bool], None, None]
):
"""Generates all possible test data for testing selectbox options schema."""
# valid selectbox option
yield {
"label": "Option 1",
"value": "option1",
"selected": True,
"disabled": False,
}, True

# valid selectbox option with defaults
yield {"label": "Option 2", "value": "option2"}, True

# invalid selectbox option: wrong field types
yield {"label": "Option 3", "value": "option3", "selected": "true"}, False
yield {"label": "Option 4", "value": "option4", "disabled": "false"}, False

# invalid selectbox option: missing required keys
yield {"label": "Option 5"}, False
yield {"value": "option6"}, False

# invalid selectbox option: wrong key name
yield {"label": "Option 7", "val": "option7"}, False
yield {
"label": "Option 8",
"value": "option8",
"selected": True,
"disbled": False,
}, False


@pytest.mark.schema
@pytest.mark.parametrize("option_data, expected_result", selectbox_option_test_data())
def test_selectbox_option(option_data, expected_result) -> None:
"""Tests that every selectbox option conforms to expected result."""
try:
option = SelectBoxOptions(**option_data)
assert option is not None
assert (option.label == option_data["label"]) == expected_result
except (TypeError, ValueError):
assert not expected_result

0 comments on commit 138d284

Please sign in to comment.