Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: unit tests DATA variables to be dictionaries #996

Open
mtache opened this issue Jan 7, 2025 · 0 comments
Open

refactor: unit tests DATA variables to be dictionaries #996

mtache opened this issue Jan 7, 2025 · 0 comments

Comments

@mtache
Copy link
Collaborator

mtache commented Jan 7, 2025

Currently, the unit tests DATA variables are list[AntaUnitTest]. This has led to duplicates in the past.

class AntaUnitTest(TypedDict):
    """The parameters required for a unit test of an AntaTest subclass."""

    name: str
    test: type[AntaTest]
    inputs: NotRequired[dict[str, Any]]
    eos_data: list[dict[str, Any] | str]
    expected: Expected

We should refactor the DATA variables to be dict[Tuple[type[AntaTest], str], AntaUnitTest] instead where AntaUnitTest is:

class AntaUnitTest(TypedDict):
    """The parameters required for a unit test of an AntaTest subclass."""

    inputs: NotRequired[dict[str, Any]]
    eos_data: list[dict[str, Any] | str]
    expected: Expected
DATA: list[AntaUnitTest] = [
   {
        # Arbitrary test name.
        "name": "success",
        # Must be an AntaTest subclass definition
        "test": VerifyUptime,
        # JSON output of the 'show uptime' EOS command as defined in VerifyUptime.commands
        "eos_data": [{"upTime": 1186689.15, "loadAvg": [0.13, 0.12, 0.09], "users": 1, "currentTime": 1683186659.139859}],
        # Dictionary to instantiate VerifyUptime.Input
        "inputs": {"minimum": 666},
        # Expected test result
        "expected": {"result": "success"},
    },
    {
        "name": "failure",
        "test": VerifyUptime,
        "eos_data": [{"upTime": 665.15, "loadAvg": [0.13, 0.12, 0.09], "users": 1, "currentTime": 1683186659.139859}],
        "inputs": {"minimum": 666},
        # If the test returns messages, it needs to be expected otherwise test will fail.
        # The expected message can be a substring of the actual message.
        "expected": {"result": "failure", "messages": ["Device uptime is 665.15 seconds"]},
    },
]
DATA: dict[Tuple[type[AntaTest], str], AntaUnitTest] = {
   (VerifyUptime, "success"): {   # First element is an AntaTest subclass definition and the second an arbitrary test name.
        # JSON output of the 'show uptime' EOS command as defined in VerifyUptime.commands
        "eos_data": [{"upTime": 1186689.15, "loadAvg": [0.13, 0.12, 0.09], "users": 1, "currentTime": 1683186659.139859}],
        "inputs": {"minimum": 666},  # Dictionary to instantiate VerifyUptime.Input
        # Expected test result
        "expected": {"result": "success"},
    },
    (VerifyUptime, "failure"): {
        "eos_data": [{"upTime": 665.15, "loadAvg": [0.13, 0.12, 0.09], "users": 1, "currentTime": 1683186659.139859}],
        "inputs": {"minimum": 666},
        # If the test returns messages, it needs to be expected otherwise test will fail.
        # The expected message can be a substring of the actual message.
        "expected": {"result": "failure", "messages": ["Device uptime is 665.15 seconds"]},
    },
}
@mtache mtache changed the title refactor: use dictionary as unit test data refactor: unit tests DATA variables to be dictionaries Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant