feat: add basic support for "reports" feature
not much here yet, but trying to keep it lean and unopinionated since implementations will probably vary a bit
This commit is contained in:
parent
1bfab90d35
commit
20d4d4d93f
9 changed files with 395 additions and 1 deletions
|
@ -0,0 +1 @@
|
|||
# nb. some tests require this to be a true package
|
|
@ -557,6 +557,12 @@ app_title = WuttaTest
|
|||
people = self.app.get_people_handler()
|
||||
self.assertIsInstance(people, PeopleHandler)
|
||||
|
||||
def test_get_report_handler(self):
|
||||
from wuttjamaican.reports import ReportHandler
|
||||
|
||||
handler = self.app.get_report_handler()
|
||||
self.assertIsInstance(handler, ReportHandler)
|
||||
|
||||
def test_send_email(self):
|
||||
from wuttjamaican.email import EmailHandler
|
||||
|
||||
|
|
|
@ -151,7 +151,7 @@ class TestEmailHandler(ConfigTestCase):
|
|||
self.assertEqual(len(modules), 1)
|
||||
self.assertIs(modules[0], mod)
|
||||
|
||||
# provider may specify modules as list
|
||||
# provider may specify modules as string
|
||||
providers = {
|
||||
'wuttatest': MagicMock(email_modules='wuttjamaican.email'),
|
||||
}
|
||||
|
|
111
tests/test_reports.py
Normal file
111
tests/test_reports.py
Normal file
|
@ -0,0 +1,111 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
from wuttjamaican import reports as mod
|
||||
from wuttjamaican.testing import ConfigTestCase
|
||||
|
||||
|
||||
class MockFooReport(mod.Report):
|
||||
report_key = 'mock_foo'
|
||||
report_title = "MOCK Report"
|
||||
|
||||
def make_data(self, params, **kwargs):
|
||||
return [
|
||||
{'foo': 'bar'},
|
||||
]
|
||||
|
||||
|
||||
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
|
||||
with patch.object(self.app, 'providers', new={}):
|
||||
handler = self.make_handler()
|
||||
self.assertEqual(handler.get_report_modules(), [])
|
||||
|
||||
# provider may specify modules as list
|
||||
providers = {
|
||||
'wuttatest': MagicMock(report_modules=['wuttjamaican.reports']),
|
||||
}
|
||||
with patch.object(self.app, 'providers', new=providers):
|
||||
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 = {
|
||||
'wuttatest': MagicMock(report_modules='wuttjamaican.reports'),
|
||||
}
|
||||
with patch.object(self.app, 'providers', new=providers):
|
||||
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
|
||||
with patch.object(self.app, 'providers', new={}):
|
||||
handler = self.make_handler()
|
||||
self.assertEqual(handler.get_reports(), {})
|
||||
|
||||
# provider may define reports (via modules)
|
||||
providers = {
|
||||
'wuttatest': MagicMock(report_modules=['tests.test_reports']),
|
||||
}
|
||||
with patch.object(self.app, 'providers', new=providers):
|
||||
handler = self.make_handler()
|
||||
reports = handler.get_reports()
|
||||
self.assertEqual(len(reports), 1)
|
||||
self.assertIn('mock_foo', reports)
|
||||
|
||||
def test_get_report(self):
|
||||
providers = {
|
||||
'wuttatest': MagicMock(report_modules=['tests.test_reports']),
|
||||
}
|
||||
|
||||
with patch.object(self.app, 'providers', new=providers):
|
||||
handler = self.make_handler()
|
||||
|
||||
# as instance
|
||||
report = handler.get_report('mock_foo')
|
||||
self.assertIsInstance(report, mod.Report)
|
||||
self.assertIsInstance(report, MockFooReport)
|
||||
|
||||
# as class
|
||||
report = handler.get_report('mock_foo', instance=False)
|
||||
self.assertTrue(issubclass(report, mod.Report))
|
||||
self.assertIs(report, MockFooReport)
|
||||
|
||||
def test_make_report_data(self):
|
||||
providers = {
|
||||
'wuttatest': MagicMock(report_modules=['tests.test_reports']),
|
||||
}
|
||||
|
||||
with patch.object(self.app, 'providers', new=providers):
|
||||
handler = self.make_handler()
|
||||
report = handler.get_report('mock_foo')
|
||||
|
||||
data = handler.make_report_data(report)
|
||||
self.assertEqual(len(data), 2)
|
||||
self.assertIn('output_title', data)
|
||||
self.assertEqual(data['output_title'], "MOCK Report")
|
||||
self.assertIn('data', data)
|
||||
self.assertEqual(data['data'], [{'foo': 'bar'}])
|
Loading…
Add table
Add a link
Reference in a new issue