gm-assistant/tests/test_random_choice_oracle.py

66 lines
1.6 KiB
Python

# SPDX-FileCopyrightText: 2025 2025
# SPDX-FileContributor: Gergely Polonkai
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Tests for the Random Choice Oracle"""
import pytest
from pytest_mock import MockerFixture
from gm_assistant.oracle.random_choice import RandomChoiceOracle
TEST_DATA = {
"type": "random-choice",
"name": "Test Oracle",
"source": "Test Source",
"choices": ["One", "Two", "Three"],
}
def test_init_missing_choices() -> None:
"""Test initalising with missing choices"""
oracle_data = TEST_DATA.copy()
del oracle_data["choices"]
with pytest.raises(KeyError):
RandomChoiceOracle(oracle_data)
def test_init_empty_choices() -> None:
"""Test initialising with an empty choice list"""
oracle_data = TEST_DATA.copy()
oracle_data["choices"] = []
with pytest.raises(ValueError):
RandomChoiceOracle(oracle_data)
def test_init_nonlist_choices() -> None:
"""Test initialising with a choice list that is not a list"""
oracle_data = TEST_DATA.copy()
oracle_data["choices"] = {"a": "b"} # type: ignore[assignment]
with pytest.raises(TypeError):
RandomChoiceOracle(oracle_data)
def test_init() -> None:
"""Test initialisation"""
oracle = RandomChoiceOracle(TEST_DATA)
assert oracle.choices == ["One", "Two", "Three"]
def test_generate(mocker: MockerFixture) -> None:
"""Test generate()"""
mocked_choice = mocker.patch("gm_assistant.oracle.random_choice.random.choice", return_value="Result")
oracle = RandomChoiceOracle(TEST_DATA)
assert oracle.generate() == "Result"
mocked_choice.assert_called_once_with(["One", "Two", "Three"])