def test_get_related(self): doc = MockDocument( meta={'type': 'T', 'self': 'test://document/'} ) doc.add_service('s1', MockDocument(meta={'type': 'R1', 'self': 'test://related'}) ) doc.add_service('s2', MockDocument(meta={'type': 'R2', 'self': 'test://related'}) ) r = self._make_one(doc) self.assertEqual(r.service('s1'), 'specialized R1') self.assertEqual(r.service('s2'), 'specialized R2')
def test_get_related(self): doc = MockDocument(meta={'type': 'T', 'self': 'test://document/'}) doc.add_service( 's1', MockDocument(meta={ 'type': 'R1', 'self': 'test://related' })) doc.add_service( 's2', MockDocument(meta={ 'type': 'R2', 'self': 'test://related' })) r = self._make_one(doc) self.assertEqual(r.service('s1'), 'specialized R1') self.assertEqual(r.service('s2'), 'specialized R2')
class TestLazyDocument(unittest.TestCase): def setUp(self): self.mock_sm = MockStateMachine() self.registered_d = MockDocument( meta={ 'type': 'T', 'self': 'test://mock_resource/', 'class': 'C1', 'a': 'a', 'x': 'x', }, content={ 'b': 'b', 'y': 'y' }, ) self.mock_sm.add_document(self.registered_d.self, self.registered_d) self.d = MockDocument(meta={ 'type': 'T', 'self': 'test://mock_resource/', 'a': 'aa', }, content={'b': 'bb'}) def _get_target(self): from resty.documents import LazyDocument return LazyDocument def _make_one(self, doc): return self._get_target()(self.mock_sm, doc) def test_fast_accesss(self): ld = self._make_one(self.d) self.assertEqual(ld.meta.a, 'aa') self.assertEqual(ld.content.b, 'bb') def test_slow_accesss(self): ld = self._make_one(self.d) self.assertEqual(ld.meta.x, 'x') self.assertEqual(ld.content.y, 'y') def test_reload(self): ld = self._make_one(self.d) ld.meta.x # Trigger data reload self.assertEqual(ld.meta.a, 'a') self.assertEqual(ld.content.b, 'b') def test_attr_error(self): ld = self._make_one(self.d) self.assertRaises(AttributeError, getattr, ld.meta, 'not_existing') self.assertRaises(AttributeError, getattr, ld.content, 'not_existing') def test_deferred_related(self): sentinel = object() self.registered_d.add_related('R', sentinel) d = self._make_one(self.d) self.assertEqual(d.related('R'), sentinel) def test_deferred_service(self): sentinel = object() self.registered_d.add_service('S', sentinel) d = self._make_one(self.d) self.assertEqual(d.service('S'), sentinel) def test_deferred_items(self): sentinel = object() self.registered_d.add_item(sentinel) d = self._make_one(self.d) self.assertEqual(d.items(), [sentinel]) def test_deferred_page(self): sentinel = object() self.registered_d.add_page(1, sentinel) d = self._make_one(self.d) self.assertEqual(d.page(1), sentinel) def test_deferred_filter(self): sentinel = object() self.registered_d.add_filter('F', sentinel) d = self._make_one(self.d) self.assertEqual(d.filter('F'), sentinel) def test_fast_related(self): sentinel = object() self.d.add_related('R', sentinel) d = self._make_one(self.d) self.assertEqual(d.related('R'), sentinel) def test_fast_service(self): sentinel = object() self.d.add_service('S', sentinel) d = self._make_one(self.d) self.assertEqual(d.service('S'), sentinel) def test_fast_items(self): sentinel = object() self.d.add_item(sentinel) d = self._make_one(self.d) self.assertEqual(d.items(), [sentinel]) def test_fast_page(self): sentinel = object() self.d.add_page(1, sentinel) d = self._make_one(self.d) self.assertEqual(d.page(1), sentinel) def test_fast_filter(self): sentinel = object() self.registered_d.add_filter('F', sentinel) d = self._make_one(self.d) self.assertEqual(d.filter('F'), sentinel)
class TestLazyDocument(unittest.TestCase): def setUp(self): self.mock_sm = MockStateMachine() self.registered_d = MockDocument( meta={ 'type': 'T', 'self': 'test://mock_resource/', 'class': 'C1', 'a': 'a', 'x': 'x', }, content={'b': 'b', 'y': 'y'}, ) self.mock_sm.add_document(self.registered_d.self, self.registered_d) self.d = MockDocument( meta={ 'type': 'T', 'self': 'test://mock_resource/', 'a': 'aa', }, content={'b': 'bb'} ) def _get_target(self): from resty.documents import LazyDocument return LazyDocument def _make_one(self, doc): return self._get_target()(self.mock_sm, doc) def test_fast_accesss(self): ld = self._make_one(self.d) self.assertEqual(ld.meta.a, 'aa') self.assertEqual(ld.content.b, 'bb') def test_slow_accesss(self): ld = self._make_one(self.d) self.assertEqual(ld.meta.x, 'x') self.assertEqual(ld.content.y, 'y') def test_reload(self): ld = self._make_one(self.d) ld.meta.x # Trigger data reload self.assertEqual(ld.meta.a, 'a') self.assertEqual(ld.content.b, 'b') def test_attr_error(self): ld = self._make_one(self.d) self.assertRaises(AttributeError, getattr, ld.meta, 'not_existing') self.assertRaises(AttributeError, getattr, ld.content, 'not_existing') def test_deferred_related(self): sentinel = object() self.registered_d.add_related('R', sentinel) d = self._make_one(self.d) self.assertEqual(d.related('R'), sentinel) def test_deferred_service(self): sentinel = object() self.registered_d.add_service('S', sentinel) d = self._make_one(self.d) self.assertEqual(d.service('S'), sentinel) def test_deferred_items(self): sentinel = object() self.registered_d.add_item(sentinel) d = self._make_one(self.d) self.assertEqual(d.items(), [sentinel]) def test_deferred_page(self): sentinel = object() self.registered_d.add_page(1, sentinel) d = self._make_one(self.d) self.assertEqual(d.page(1), sentinel) def test_deferred_filter(self): sentinel = object() self.registered_d.add_filter('F', sentinel) d = self._make_one(self.d) self.assertEqual(d.filter('F'), sentinel) def test_fast_related(self): sentinel = object() self.d.add_related('R', sentinel) d = self._make_one(self.d) self.assertEqual(d.related('R'), sentinel) def test_fast_service(self): sentinel = object() self.d.add_service('S', sentinel) d = self._make_one(self.d) self.assertEqual(d.service('S'), sentinel) def test_fast_items(self): sentinel = object() self.d.add_item(sentinel) d = self._make_one(self.d) self.assertEqual(d.items(), [sentinel]) def test_fast_page(self): sentinel = object() self.d.add_page(1, sentinel) d = self._make_one(self.d) self.assertEqual(d.page(1), sentinel) def test_fast_filter(self): sentinel = object() self.registered_d.add_filter('F', sentinel) d = self._make_one(self.d) self.assertEqual(d.filter('F'), sentinel)