def test_data_provider(self): """ Context will pull missing attributes from the data provider and cache them within its own data. """ class AbcProvider(Provider): def __init__(self, a, b, c): self._a = a self._b = b self._c = c @Provider.data_point def a(self, context): return self._a @Provider.data_point def ab(self, context): return context.a + self._b @Provider.data_point def abc(self, context): return context.ab + self._c c = Context(data_provider=AbcProvider('A', 'b', 'C')) self.assertEqual(c.abc, 'AbC') c._data_provider = None self.assertEqual(c.a, 'A') self.assertEqual(c.ab, 'Ab') self.assertEqual(c.abc, 'AbC') del c['ab'] self.assertNotIn('ab', c) self.assertEqual(len(c), 2)
def test_nested_context(self): c = Context() nc = Context(data_provider=c) c.foo = 'hello' self.assertEqual(nc.foo, 'hello') c.foo = 'goodbye' self.assertEqual(nc.foo, 'hello') self.assertEqual(c.foo, 'goodbye')
def test_basic_subclass(self): class FooBarProvider(Provider): def __init__(self, foo='Whatever'): self._foo = foo @Provider.data_point('foo', 'Foo', 'Gives self.foo') def foo(self, context): return self._foo @Provider.data_point def bar(self, context): """Return context.bar""" return context.bar prov = FooBarProvider('foo value') self.assertEqual(prov.foo.key, 'foo') self.assertEqual(prov.foo.name, 'Foo') self.assertEqual(prov.foo.doc, 'Gives self.foo') self.assertEqual(prov.bar.key, 'bar') self.assertEqual(prov.bar.name, 'bar') self.assertEqual(prov.bar.doc, 'Return context.bar') self.assertEqual(prov.foo({}), 'foo value') self.assertEqual(prov.bar(Context({'bar': 'is bar'})), 'is bar') self.assertEqual(prov.get_data_points(), { 'foo': prov.foo, 'bar': prov.bar })
def test_backward_propagation(self): c1 = Context() c2 = Context(data_provider=c1) c3 = Context(data_provider=c2) c4 = Context(data_provider=c3) c4.msg = 'hello' c4.get_data_points() self.assertEqual(c2.get('msg'), None) self.assertEqual(c3.get('msg'), None)
def test_to_dict(self): c = Context(foo='bar', bar='baz') self.assertEqual(c.to_dict(), {'foo': 'bar', 'bar': 'baz'})
def setUp(self): self.d = Context({'foo': 'bar', 'bar': 'baz'})