Exemple #1
0
 def test_iter_simple(self):
     doc = MockDocument(meta={'elements': 'ET'})
     doc.add_item(MockDocument(meta={'type': 'T1'}))
     doc.add_item(MockDocument(meta={'type': 'T2'}))
     r = self._make_one(doc)
     items = list(r.items())
     self.assertEqual(items, ['specialized T1', 'specialized T2'])
Exemple #2
0
    def test_specialize(self):

        loader = MockHttpLoader()
        c = self._make_one(loader)

        class Mock(object):
            def __init__(self, doc):
                self.doc = doc

        class Mock1(Mock):
            pass

        class Mock2(Mock):
            pass

        c.register_document('type1', Mock1)
        c.register_document('type2', Mock2)

        doc1 = MockDocument(meta={'type': 'type1'})
        doc2 = MockDocument(meta={'type': 'type2'})

        s1 = c.specialize(doc1)
        self.assertTrue(isinstance(s1, Mock1))
        self.assertEqual(s1.doc, doc1)

        s2 = c.specialize(doc2)
        self.assertTrue(isinstance(s2, Mock2))
        self.assertEqual(s2.doc, doc2)
Exemple #3
0
 def test_filter(self):
     doc = MockDocument(meta={'elements': 'ET'})
     doc.add_filter('zipcode', MockDocument(meta={'type': 'ZIP'}))
     doc.add_filter('IP', MockDocument(meta={'type': 'IP'}))
     r = self._make_one(doc)
     self.assertEqual(r.filter('zipcode'), 'specialized ZIP')
     self.assertEqual(r.filter('IP'), 'specialized IP')
     self.assertRaises(ValueError, r.filter, 'err')
Exemple #4
0
    def setUp(self):
        self.mock_sm = MockStateMachine()

        mock_d = MockDocument(
            meta={
                'type': 'T',
                'self': 'test://mock_resource/1/',
                'class': 'C1',
                'a': 'a',
                'x': 'x',
            },
            content={
                'b': 'b',
                'y': 'y'
            },
        )
        self.mock_sm.add_document(mock_d.self, mock_d)

        mock_d = MockDocument(
            meta={
                'type': 'T',
                'self': 'test://mock_resource/2/',
                'class': 'C2',
                'a': 'a',
                'x': 'x',
            },
            content={
                'b': 'b',
                'y': 'y'
            },
        )
        self.mock_sm.add_document(mock_d.self, mock_d)

        mock_d = MockDocument(
            meta={
                'type': 'T',
                'self': 'test://mock_resource/3/',
                'class': 'C3',
                'a': 'a',
                'x': 'x',
            },
            content={
                'b': 'b',
                'y': 'y'
            },
        )
        self.mock_sm.add_document(mock_d.self, mock_d)

        self.sentinel = object()
        self.mock_sm.add_document('test://a/b/', self.sentinel)
        self.mock_sm.add_document('test://test/', self.sentinel)
        self.mock_sm.add_document('test://test/1/', self.sentinel)
        self.mock_sm.add_document('test://test/2/', self.sentinel)
Exemple #5
0
 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')
Exemple #6
0
 def test_get_related(self):
     doc = MockDocument(
         meta={'type': 'T', 'self': 'test://document/', 'class': 'C'}
     )
     doc.add_related(
         'relationship1',
         MockDocument(meta={'type': 'R1', 'self': 'test://related'})
     )
     doc.add_related(
         'relationship2',
         MockDocument(meta={'type': 'R2', 'self': 'test://related'})
     )
     r = self._make_one(doc)
     self.assertEqual(r.related('relationship1'), 'specialized R1')
     self.assertEqual(r.related('relationship2'), 'specialized R2')
Exemple #7
0
 def test_filter(self):
     doc = MockDocument(meta={'elements': 'ET'})
     doc.add_filter('zipcode', MockDocument(meta={'type': 'ZIP'}))
     doc.add_filter('IP', MockDocument(meta={'type': 'IP'}))
     r = self._make_one(doc)
     self.assertEqual(r.filter('zipcode'), 'specialized ZIP')
     self.assertEqual(r.filter('IP'), 'specialized IP')
     self.assertRaises(ValueError, r.filter, 'err')
Exemple #8
0
 def test_content(self):
     r = self._make_one(
         MockDocument(meta={'class': 'C'}, content={
             'a': 'a',
             'b': 'b'
         }))
     self.assertEqual(r.content.a, 'a')
     self.assertEqual(r.content.b, 'b')
Exemple #9
0
    def test_known_meta(self):
        r = self._make_one(
            MockDocument(meta={
                'self': 'S',
                'elements': 'ET',
                'hash': 'hash',
            }))

        self.assertEqual(r.self, 'S')
        self.assertEqual(r.elements, 'ET')
        self.assertEqual(r.hash, 'hash')
Exemple #10
0
    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'}
        )
Exemple #11
0
 def test_iter_simple(self):
     doc = MockDocument(meta={'elements': 'ET'})
     doc.add_item(MockDocument(meta={'type': 'T1'}))
     doc.add_item(MockDocument(meta={'type': 'T2'}))
     r = self._make_one(doc)
     items = list(r.items())
     self.assertEqual(items, ['specialized T1', 'specialized T2'])
Exemple #12
0
 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')
Exemple #13
0
    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'})
Exemple #14
0
    def test_known_meta(self):
        r = self._make_one(
            MockDocument(
                meta={
                    'self': 'S',
                    'hash': 'hash',
                    'class': 'Class',
                    'id': 'ID',
                    'created': 'created',
                    'edited': 'edited',
                }))

        self.assertEqual(r.self, 'S')
        self.assertEqual(r.hash, 'hash')
        self.assertEqual(r.class_, 'Class')
        self.assertEqual(r.id, 'ID')
        self.assertEqual(r.created, 'created')
        self.assertEqual(r.edited, 'edited')
Exemple #15
0
 def test_get_related(self):
     doc = MockDocument(meta={
         'type': 'T',
         'self': 'test://document/',
         'class': 'C'
     })
     doc.add_related(
         'relationship1',
         MockDocument(meta={
             'type': 'R1',
             'self': 'test://related'
         }))
     doc.add_related(
         'relationship2',
         MockDocument(meta={
             'type': 'R2',
             'self': 'test://related'
         }))
     r = self._make_one(doc)
     self.assertEqual(r.related('relationship1'), 'specialized R1')
     self.assertEqual(r.related('relationship2'), 'specialized R2')
Exemple #16
0
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)
Exemple #17
0
 def test_attr_error(self):
     r = self._make_one(MockDocument())
     self.assertRaises(AttributeError, getattr, r.content, 'not_found')
     self.assertRaises(AttributeError, getattr, r, 'edited')
Exemple #18
0
    def test_iter_multi_page(self):

        page1 = MockDocument(meta={'elements': 'ET'})
        page1.add_item(MockDocument(meta={'type': 'T1'}))
        page1.add_item(MockDocument(meta={'type': 'T2'}))

        page2 = MockDocument(meta={'elements': 'ET'})
        page2.add_item(MockDocument(meta={'type': 'T3'}))
        page2.add_item(MockDocument(meta={'type': 'T4'}))

        page3 = MockDocument(meta={'elements': 'ET'})
        page3.add_item(MockDocument(meta={'type': 'T5'}))

        page1.meta.page_size = page2.meta.page_size = page3.meta.page_size = 2
        page1.meta.items_count = 5
        page2.meta.items_count = 5
        page3.meta.items_count = 5
        page1.meta.page, page2.meta.page, page3.meta.page = 1, 2, 3

        page1.add_page(1, page1)
        page1.add_page(2, page2)
        page1.add_page(3, page3)

        page2.add_page(1, page1)
        page2.add_page(2, page2)
        page2.add_page(3, page3)

        page3.add_page(1, page1)
        page3.add_page(2, page2)
        page3.add_page(3, page3)

        r = self._make_one(page1)
        items = list(r.items())
        self.assertEqual(items, [
            'specialized T1',
            'specialized T2',
            'specialized T3',
            'specialized T4',
            'specialized T5',
        ])
Exemple #19
0
    def test_iter_multi_page(self):

        page1 = MockDocument(meta={'elements': 'ET'})
        page1.add_item(MockDocument(meta={'type': 'T1'}))
        page1.add_item(MockDocument(meta={'type': 'T2'}))

        page2 = MockDocument(meta={'elements': 'ET'})
        page2.add_item(MockDocument(meta={'type': 'T3'}))
        page2.add_item(MockDocument(meta={'type': 'T4'}))

        page3 = MockDocument(meta={'elements': 'ET'})
        page3.add_item(MockDocument(meta={'type': 'T5'}))

        page1.meta.page_size = page2.meta.page_size = page3.meta.page_size = 2
        page1.meta.items_count = page2.meta.items_count = page3.meta.items_count = 5
        page1.meta.page, page2.meta.page, page3.meta.page = 1, 2, 3

        page1.add_page(1, page1)
        page1.add_page(2, page2)
        page1.add_page(3, page3)

        page2.add_page(1, page1)
        page2.add_page(2, page2)
        page2.add_page(3, page3)

        page3.add_page(1, page1)
        page3.add_page(2, page2)
        page3.add_page(3, page3)

        r = self._make_one(page1)
        items = list(r.items())
        self.assertEqual(items, [
            'specialized T1',
            'specialized T2',
            'specialized T3',
            'specialized T4',
            'specialized T5',
        ])
Exemple #20
0
 def test_attr_error(self):
     r = self._make_one(MockDocument(meta={'elements': 'ET'}))
     self.assertRaises(AttributeError, getattr, r, 'hash')
Exemple #21
0
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)