Skip to content

Commit

Permalink
Add more types to tests (home-assistant#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Dec 26, 2022
1 parent 038dac2 commit f0210f6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
4 changes: 2 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
LANGUAGES = [p.name for p in SENTENCES_DIR.iterdir() if p.is_dir()]


def load_sentences(language: str):
def load_sentences(language: str) -> dict[str, Any]:
"""Load sentences from sentences/ for a language"""
lang_dir = SENTENCES_DIR / language
files: Dict[str, Any] = {}
Expand All @@ -30,6 +30,6 @@ def load_sentences(language: str):
return files


def load_test(language: str, test_name: str):
def load_test(language: str, test_name: str) -> dict[str, Any]:
"""Load test sentences from tests/ for a language"""
return yaml.safe_load((TESTS_DIR / language / f"{test_name}.yaml").read_text())
15 changes: 11 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import dataclasses
from typing import Any

import pytest
import yaml
Expand All @@ -27,27 +28,33 @@ def pytest_generate_tests(metafunc):


@pytest.fixture(scope="session")
def intent_schemas():
def intent_schemas() -> dict[str, Any]:
"""Loads the base intents file"""
with open(INTENTS_FILE, "r", encoding="utf-8") as schema_file:
return yaml.safe_load(schema_file)


@pytest.fixture(name="language_sentences_yaml", scope="session")
def language_sentences_yaml_fixture(language: str):
def language_sentences_yaml_fixture(language: str) -> dict[str, Any]:
"""Loads the language sentences."""
return load_sentences(language)


@pytest.fixture(name="language_sentences_common", scope="session")
def language_sentences_common_fixture(language, language_sentences_yaml):
def language_sentences_common_fixture(
language: str,
language_sentences_yaml: dict[str, Any],
) -> Intents:
"""Loads the common language intents."""
language_sentences_yaml["_common.yaml"].setdefault("intents", {})
return Intents.from_dict(language_sentences_yaml["_common.yaml"])


@pytest.fixture(scope="session")
def language_sentences(language_sentences_yaml: dict, language_sentences_common):
def language_sentences(
language_sentences_yaml: dict[str, Any],
language_sentences_common: Intents,
) -> Intents:
"""Parse language sentences."""
merged: dict = {}
for file_name, intents_dict in language_sentences_yaml.items():
Expand Down
37 changes: 22 additions & 15 deletions tests/test_language_intents.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Test language intents."""
from __future__ import annotations

import dataclasses
import sys
from typing import Any, Dict, Iterable, Set
from pathlib import Path
from typing import Any, Iterable

from hassil import Intents
from hassil.expression import Expression, ListReference, RuleReference, Sequence
Expand All @@ -11,9 +14,9 @@


def test_language_common(
language,
language_sentences_yaml: Dict[str, Any],
):
language: str,
language_sentences_yaml: dict[str, Any],
) -> None:
"""Test the language common file."""
common_files = [key for key in language_sentences_yaml if key.startswith("_")]

Expand All @@ -39,10 +42,10 @@ def test_language_common(

def do_test_language_sentences(
file_name: str,
intent_schemas: Dict[str, Any],
language_sentences_yaml: Dict[str, Any],
intent_schemas: dict[str, Any],
language_sentences_yaml: dict[str, Any],
language_sentences_common: Intents,
):
) -> None:
"""Ensure all language sentences contain valid slots, lists, rules, etc."""
parsed_sentences_without_common = Intents.from_dict(
language_sentences_yaml[file_name]
Expand All @@ -65,7 +68,7 @@ def do_test_language_sentences(

for data in intent.data:
for sentence in data.sentences:
found_slots: Set[str] = set()
found_slots: set[str] = set()
for expression in _flatten(sentence):
_verify(
expression,
Expand Down Expand Up @@ -100,10 +103,10 @@ def _verify(
expression: Expression,
intents: Intents,
intent_name: str,
slot_schema: Dict[str, Any],
visited_rules: Set[str],
found_slots: Set[str],
):
slot_schema: dict[str, Any],
visited_rules: set[str],
found_slots: set[str],
) -> None:
if isinstance(expression, ListReference):
list_ref: ListReference = expression

Expand Down Expand Up @@ -153,8 +156,12 @@ def _flatten(expression: Expression) -> Iterable[Expression]:
yield expression


def gen_test(test_file):
def test_func(intent_schemas, language_sentences_yaml, language_sentences_common):
def gen_test(test_file: Path) -> None:
def test_func(
intent_schemas: dict[str, Any],
language_sentences_yaml: dict[str, Any],
language_sentences_common: Intents,
) -> None:
do_test_language_sentences(
test_file.name,
intent_schemas,
Expand All @@ -166,7 +173,7 @@ def test_func(intent_schemas, language_sentences_yaml, language_sentences_common
setattr(sys.modules[__name__], test_func.__name__, test_func)


def gen_tests():
def gen_tests() -> None:
lang_dir = SENTENCES_DIR / "en"

for test_file in lang_dir.glob("*.yaml"):
Expand Down
26 changes: 18 additions & 8 deletions tests/test_language_sentences.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
"""Test language sentences."""
from __future__ import annotations

import sys
from pathlib import Path

import pytest
from hassil import recognize
from hassil.intents import TextSlotList
from hassil import Intents, recognize
from hassil.intents import SlotList, TextSlotList

from . import TESTS_DIR, load_test


@pytest.fixture(name="slot_lists", scope="session")
def slot_lists_fixture(language):
def slot_lists_fixture(language: str) -> dict[str, SlotList]:
"""Loads the slot lists for the language."""
fixtures = load_test(language, "_fixtures")
return {
Expand All @@ -23,8 +26,11 @@ def slot_lists_fixture(language):


def do_test_language_sentences_file(
language, test_file, slot_lists, language_sentences
):
language: str,
test_file: str,
slot_lists: dict[str, SlotList],
language_sentences: Intents,
) -> None:
"""Tests recognition all of the test sentences for a language"""
_testing_domain, testing_intent = test_file.split("_", 1)

Expand Down Expand Up @@ -56,8 +62,12 @@ def do_test_language_sentences_file(
assert result.entities[slot_name].value == slot_dict["value"]


def gen_test(test_file):
def test_func(language, slot_lists, language_sentences):
def gen_test(test_file: Path) -> None:
def test_func(
language: str,
slot_lists: dict[str, SlotList],
language_sentences: Intents,
) -> None:
do_test_language_sentences_file(
language, test_file.stem, slot_lists, language_sentences
)
Expand All @@ -66,7 +76,7 @@ def test_func(language, slot_lists, language_sentences):
setattr(sys.modules[__name__], test_func.__name__, test_func)


def gen_tests():
def gen_tests() -> None:
lang_dir = TESTS_DIR / "en"

for test_file in lang_dir.glob("*.yaml"):
Expand Down

0 comments on commit f0210f6

Please sign in to comment.