Add app providers, tests, docs
This commit is contained in:
parent
3cafa28ab9
commit
3a8bd1fce9
8 changed files with 288 additions and 0 deletions
|
@ -20,6 +20,7 @@ class TestAppHandler(TestCase):
|
|||
def setUp(self):
|
||||
self.config = WuttaConfig(appname='wuttatest')
|
||||
self.app = app.AppHandler(self.config)
|
||||
self.config.app = self.app
|
||||
|
||||
def test_init(self):
|
||||
self.assertIs(self.app.config, self.config)
|
||||
|
@ -109,3 +110,81 @@ class TestAppHandler(TestCase):
|
|||
session.execute(sa.text("insert into setting values ('foo', 'bar');"))
|
||||
value = self.app.get_setting(session, 'foo')
|
||||
self.assertEqual(value, 'bar')
|
||||
|
||||
|
||||
class TestAppProvider(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.config = WuttaConfig(appname='wuttatest')
|
||||
self.app = app.AppHandler(self.config)
|
||||
self.config.app = self.app
|
||||
|
||||
def test_constructor(self):
|
||||
|
||||
# config object is expected
|
||||
provider = app.AppProvider(self.config)
|
||||
self.assertIs(provider.config, self.config)
|
||||
self.assertIs(provider.app, self.app)
|
||||
|
||||
# but can pass app handler instead
|
||||
provider = app.AppProvider(self.app)
|
||||
self.assertIs(provider.config, self.config)
|
||||
self.assertIs(provider.app, self.app)
|
||||
|
||||
def test_get_all_providers(self):
|
||||
|
||||
class FakeProvider(app.AppProvider):
|
||||
pass
|
||||
|
||||
# nb. we specify *classes* here
|
||||
fake_providers = {'fake': FakeProvider}
|
||||
|
||||
with patch('wuttjamaican.app.load_entry_points') as load_entry_points:
|
||||
load_entry_points.return_value = fake_providers
|
||||
|
||||
# sanity check, we get *instances* back from this
|
||||
providers = self.app.get_all_providers()
|
||||
load_entry_points.assert_called_once_with('wuttatest.providers')
|
||||
self.assertEqual(len(providers), 1)
|
||||
self.assertIn('fake', providers)
|
||||
self.assertIsInstance(providers['fake'], FakeProvider)
|
||||
|
||||
def test_hasattr(self):
|
||||
|
||||
class FakeProvider(app.AppProvider):
|
||||
def fake_foo(self):
|
||||
pass
|
||||
|
||||
self.app.providers = {'fake': FakeProvider(self.config)}
|
||||
|
||||
self.assertTrue(hasattr(self.app, 'fake_foo'))
|
||||
self.assertFalse(hasattr(self.app, 'fake_method_does_not_exist'))
|
||||
|
||||
def test_getattr(self):
|
||||
|
||||
class FakeProvider(app.AppProvider):
|
||||
def fake_foo(self):
|
||||
return 42
|
||||
|
||||
# nb. using instances here
|
||||
fake_providers = {'fake': FakeProvider(self.config)}
|
||||
|
||||
with patch.object(self.app, 'get_all_providers') as get_all_providers:
|
||||
get_all_providers.return_value = fake_providers
|
||||
|
||||
self.assertNotIn('providers', self.app.__dict__)
|
||||
self.assertIs(self.app.providers, fake_providers)
|
||||
get_all_providers.assert_called_once_with()
|
||||
|
||||
def test_getattr_providers(self):
|
||||
|
||||
# collection of providers is loaded on demand
|
||||
self.assertNotIn('providers', self.app.__dict__)
|
||||
self.assertIsNotNone(self.app.providers)
|
||||
|
||||
# custom attr does not exist yet
|
||||
self.assertRaises(AttributeError, getattr, self.app, 'foo_value')
|
||||
|
||||
# but provider can supply the attr
|
||||
self.app.providers['mytest'] = MagicMock(foo_value='bar')
|
||||
self.assertEqual(self.app.foo_value, 'bar')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue