Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tests/query/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,49 @@ def test_list_templates(self):
executor.register_template(template)

assert "test_query" in executor.list_templates()

def test_execute_on_all_devices(self):
"""Test execute_on_all_devices runs template on each device."""
mock_context = _make_mock_context(device_ids=[0, 1])
executor = QueryExecutor(mock_context)
executor.register_template(
QueryTemplate(
name="device_test",
description="Test",
query="SELECT {{ device_id }} as dev",
)
)

results = executor.execute_on_all_devices("device_test")
assert 0 in results
assert 1 in results

def test_execute_on_all_devices_template_not_found(self):
"""Test execute_on_all_devices raises for missing template."""
mock_context = _make_mock_context(device_ids=[0])
executor = QueryExecutor(mock_context)

from pt_snap_cli.query.executor import QueryExecutionError

with pytest.raises(QueryExecutionError, match="Template not found"):
executor.execute_on_all_devices("nonexistent")

def test_execute_on_all_devices_empty_device_list(self):
"""Test execute_on_all_devices with no devices returns empty dict."""
mock_context = _make_mock_context(device_ids=[])
executor = QueryExecutor(mock_context)
executor.register_template(
QueryTemplate(name="empty_test", description="Test", query="SELECT 1")
)

results = executor.execute_on_all_devices("empty_test")
assert results == {}


def _make_mock_context(device_ids: list[int]):
"""Create a mock Context with the given device IDs."""
from unittest.mock import MagicMock

mock = MagicMock()
mock.device_ids = device_ids
return mock
35 changes: 35 additions & 0 deletions tests/query/test_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,41 @@ def __init__(self, data):
assert len(models) == 2
assert all(isinstance(m, MockModel) for m in models)

def test_map_to_model_not_registered_raises_keyerror(self):
mapper = ResultMapper()
with pytest.raises(KeyError) as exc_info:
mapper.map_to_model({"id": 1}, "MissingModel")
assert "MissingModel" in str(exc_info.value)

def test_map_all_to_model_propagates_keyerror(self):
mapper = ResultMapper()
with pytest.raises(KeyError):
mapper.map_all_to_model([{"id": 1}], "MissingModel")

def test_map_bool_string_values(self):
mapper = ResultMapper()
row = {"flag_true": "true", "flag_false": "false", "flag_1": "1", "flag_0": "0"}
schema = [
{"column": "flag_true", "type": "bool"},
{"column": "flag_false", "type": "bool"},
{"column": "flag_1", "type": "bool"},
{"column": "flag_0", "type": "bool"},
]

result = mapper.map(row, schema)
assert result["flag_true"] is True
assert result["flag_false"] is False
assert result["flag_1"] is True
assert result["flag_0"] is False

def test_map_conversion_error_keeps_original_value(self):
mapper = ResultMapper()
row = {"value": "not_an_int"}
schema = [{"column": "value", "type": "int"}]

result = mapper.map(row, schema)
assert result["value"] == "not_an_int"


class TestModuleFunctions:
def test_map_result(self):
Expand Down
Loading