2025-01-11 19:04:30 -06:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
|
|
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
|
|
|
|
from wuttjamaican import reports as mod
|
|
|
|
from wuttjamaican.testing import ConfigTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class MockFooReport(mod.Report):
|
2025-08-30 21:25:44 -05:00
|
|
|
report_key = "mock_foo"
|
2025-01-11 19:04:30 -06:00
|
|
|
report_title = "MOCK Report"
|
|
|
|
|
|
|
|
def make_data(self, params, **kwargs):
|
|
|
|
return [
|
2025-08-30 21:25:44 -05:00
|
|
|
{"foo": "bar"},
|
2025-01-11 19:04:30 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class TestReport(ConfigTestCase):
|
|
|
|
|
|
|
|
def test_get_output_columns(self):
|
|
|
|
report = mod.Report(self.config)
|
|
|
|
self.assertRaises(NotImplementedError, report.get_output_columns)
|
|
|
|
|
|
|
|
def test_make_data(self):
|
|
|
|
report = mod.Report(self.config)
|
|
|
|
self.assertRaises(NotImplementedError, report.make_data, {})
|
|
|
|
|
|
|
|
|
|
|
|
class TestReportHandler(ConfigTestCase):
|
|
|
|
|
|
|
|
def make_handler(self):
|
|
|
|
return mod.ReportHandler(self.config)
|
|
|
|
|
|
|
|
def test_get_report_modules(self):
|
|
|
|
|
|
|
|
# no providers, no report modules
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch.object(self.app, "providers", new={}):
|
2025-01-11 19:04:30 -06:00
|
|
|
handler = self.make_handler()
|
|
|
|
self.assertEqual(handler.get_report_modules(), [])
|
|
|
|
|
|
|
|
# provider may specify modules as list
|
|
|
|
providers = {
|
2025-08-30 21:25:44 -05:00
|
|
|
"wuttatest": MagicMock(report_modules=["wuttjamaican.reports"]),
|
2025-01-11 19:04:30 -06:00
|
|
|
}
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch.object(self.app, "providers", new=providers):
|
2025-01-11 19:04:30 -06:00
|
|
|
handler = self.make_handler()
|
|
|
|
modules = handler.get_report_modules()
|
|
|
|
self.assertEqual(len(modules), 1)
|
|
|
|
self.assertIs(modules[0], mod)
|
|
|
|
|
|
|
|
# provider may specify modules as string
|
|
|
|
providers = {
|
2025-08-30 21:25:44 -05:00
|
|
|
"wuttatest": MagicMock(report_modules="wuttjamaican.reports"),
|
2025-01-11 19:04:30 -06:00
|
|
|
}
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch.object(self.app, "providers", new=providers):
|
2025-01-11 19:04:30 -06:00
|
|
|
handler = self.make_handler()
|
|
|
|
modules = handler.get_report_modules()
|
|
|
|
self.assertEqual(len(modules), 1)
|
|
|
|
self.assertIs(modules[0], mod)
|
|
|
|
|
|
|
|
def test_get_reports(self):
|
|
|
|
|
|
|
|
# no providers, no reports
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch.object(self.app, "providers", new={}):
|
2025-01-11 19:04:30 -06:00
|
|
|
handler = self.make_handler()
|
|
|
|
self.assertEqual(handler.get_reports(), {})
|
|
|
|
|
|
|
|
# provider may define reports (via modules)
|
|
|
|
providers = {
|
2025-08-30 21:25:44 -05:00
|
|
|
"wuttatest": MagicMock(report_modules=["tests.test_reports"]),
|
2025-01-11 19:04:30 -06:00
|
|
|
}
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch.object(self.app, "providers", new=providers):
|
2025-01-11 19:04:30 -06:00
|
|
|
handler = self.make_handler()
|
|
|
|
reports = handler.get_reports()
|
|
|
|
self.assertEqual(len(reports), 1)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIn("mock_foo", reports)
|
2025-01-11 19:04:30 -06:00
|
|
|
|
|
|
|
def test_get_report(self):
|
|
|
|
providers = {
|
2025-08-30 21:25:44 -05:00
|
|
|
"wuttatest": MagicMock(report_modules=["tests.test_reports"]),
|
2025-01-11 19:04:30 -06:00
|
|
|
}
|
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch.object(self.app, "providers", new=providers):
|
2025-01-11 19:04:30 -06:00
|
|
|
handler = self.make_handler()
|
|
|
|
|
|
|
|
# as instance
|
2025-08-30 21:25:44 -05:00
|
|
|
report = handler.get_report("mock_foo")
|
2025-01-11 19:04:30 -06:00
|
|
|
self.assertIsInstance(report, mod.Report)
|
|
|
|
self.assertIsInstance(report, MockFooReport)
|
|
|
|
|
|
|
|
# as class
|
2025-08-30 21:25:44 -05:00
|
|
|
report = handler.get_report("mock_foo", instance=False)
|
2025-01-11 19:04:30 -06:00
|
|
|
self.assertTrue(issubclass(report, mod.Report))
|
|
|
|
self.assertIs(report, MockFooReport)
|
|
|
|
|
2025-08-30 16:36:52 -05:00
|
|
|
# not found
|
2025-08-30 21:25:44 -05:00
|
|
|
report = handler.get_report("unknown")
|
2025-08-30 16:36:52 -05:00
|
|
|
self.assertIsNone(report)
|
|
|
|
|
2025-01-11 19:04:30 -06:00
|
|
|
def test_make_report_data(self):
|
|
|
|
providers = {
|
2025-08-30 21:25:44 -05:00
|
|
|
"wuttatest": MagicMock(report_modules=["tests.test_reports"]),
|
2025-01-11 19:04:30 -06:00
|
|
|
}
|
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch.object(self.app, "providers", new=providers):
|
2025-01-11 19:04:30 -06:00
|
|
|
handler = self.make_handler()
|
2025-08-30 21:25:44 -05:00
|
|
|
report = handler.get_report("mock_foo")
|
2025-01-11 19:04:30 -06:00
|
|
|
|
|
|
|
data = handler.make_report_data(report)
|
|
|
|
self.assertEqual(len(data), 2)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIn("output_title", data)
|
|
|
|
self.assertEqual(data["output_title"], "MOCK Report")
|
|
|
|
self.assertIn("data", data)
|
|
|
|
self.assertEqual(data["data"], [{"foo": "bar"}])
|