def test_404_when_page_does_not_exist(self):
        """Test 404 when AnnotationPage does not exist."""
        collection = CollectionFactory()

        endpoint = u'/annotations/{0}/?page={1}'.format(collection.id, 0)
        res = self.app_get_json_ld(endpoint)
        assert_equal(res.status_code, 404, res.data)

        per_page = current_app.config.get('ANNOTATIONS_PER_PAGE')
        AnnotationFactory.create_batch(per_page, collection=collection)
        endpoint = u'/annotations/{0}/?page={1}'.format(collection.id, 1)
        res = self.app_get_json_ld(endpoint)
        assert_equal(res.status_code, 404, res.data)
Esempio n. 2
0
    def test_list_all_collections(self):
        """Test a list of all Annotation Collections is returned."""
        collection1 = CollectionFactory()
        collection2 = CollectionFactory()
        AnnotationFactory.create_batch(3, collection=collection1)
        expected = {
            '@context':
            'http://www.w3.org/ns/anno.jsonld',
            'id':
            url_for('api.index', _external=True),
            'label':
            'All collections',
            'type': ['AnnotationCollection', 'BasicContainer'],
            'total':
            2,
            'items': [{
                'id':
                url_for('api.collections',
                        collection_id=collection1.id,
                        _external=True),
                'type':
                collection1.data['type'],
                'created':
                '1984-11-19T00:00:00Z',
                'generated':
                '1984-11-19T00:00:00Z',
                'total':
                3
            }, {
                'id':
                url_for('api.collections',
                        collection_id=collection2.id,
                        _external=True),
                'type':
                collection2.data['type'],
                'created':
                '1984-11-19T00:00:00Z',
                'generated':
                '1984-11-19T00:00:00Z',
                'total':
                0
            }]
        }

        endpoint = '/annotations/'
        res = self.app_get_json_ld(endpoint)
        data = json.loads(res.data.decode('utf8'))
        assert_dict_equal(data, expected)
Esempio n. 3
0
 def test_offset(self):
     """Test search with offset."""
     size = 5
     offset = 2
     annotations = AnnotationFactory.create_batch(size)
     results = self.search.search(offset=offset)
     assert_equal(len(results), size - offset)
    def test_get_multiple_pages(self):
        """Test get multiple AnnotationPage."""
        collection = CollectionFactory()
        n_pages = 3
        per_page = current_app.config.get('ANNOTATIONS_PER_PAGE')
        last_page = n_pages - 1
        annotations = AnnotationFactory.create_batch(per_page * n_pages,
                                                     collection=collection)

        current_page = 1
        start = current_page * per_page
        items = []
        for anno in annotations[start:start + per_page]:
            items.append({
                'id':
                url_for('api.annotations',
                        collection_id=collection.id,
                        annotation_id=anno.id),
                'type':
                'Annotation',
                'body':
                anno.data['body'],
                'target':
                anno.data['target'],
                'created':
                '1984-11-19T00:00:00Z',
                'generated':
                '1984-11-19T00:00:00Z',
                'generator':
                current_app.config.get('GENERATOR')
            })

        expected = {
            '@context':
            'http://www.w3.org/ns/anno.jsonld',
            'id':
            url_for('api.collections',
                    collection_id=collection.id,
                    page=current_page),
            'type':
            'AnnotationPage',
            'startIndex':
            0,
            'items':
            items,
            'partOf': {
                'id': url_for('api.collections', collection_id=collection.id),
                'total': len(annotations),
                'type': ['BasicContainer', 'AnnotationCollection'],
                'created': '1984-11-19T00:00:00Z',
                'generated': '1984-11-19T00:00:00Z'
            },
            'next':
            url_for('api.collections',
                    collection_id=collection.id,
                    page=current_page + 1),
            'prev':
            url_for('api.collections',
                    collection_id=collection.id,
                    page=current_page - 1),
        }

        endpoint = u'/annotations/{}/'.format(collection.id)
        res = self.app_get_json_ld(endpoint + '?page={}'.format(current_page))
        data = json.loads(res.data.decode('utf8'))
        assert_dict_equal(data, expected)
    def test_default_container_with_multiple_pages(self):
        """Test Collection with default container and multiple pages."""
        collection = CollectionFactory()
        n_pages = 3
        per_page = current_app.config.get('ANNOTATIONS_PER_PAGE')
        last_page = n_pages - 1
        annotations = AnnotationFactory.create_batch(per_page * n_pages,
                                                     collection=collection)

        items = []
        for anno in annotations[:per_page]:
            items.append({
                'id':
                url_for('api.annotations',
                        collection_id=collection.id,
                        annotation_id=anno.id),
                'type':
                'Annotation',
                'body':
                anno.data['body'],
                'target':
                anno.data['target'],
                'created':
                '1984-11-19T00:00:00Z',
                'generated':
                '1984-11-19T00:00:00Z',
                'generator':
                current_app.config.get('GENERATOR')
            })

        expected = {
            '@context':
            'http://www.w3.org/ns/anno.jsonld',
            'id':
            url_for('api.collections', collection_id=collection.id),
            'type':
            collection.data['type'],
            'created':
            '1984-11-19T00:00:00Z',
            'generated':
            '1984-11-19T00:00:00Z',
            'total':
            len(annotations),
            'first': {
                'id':
                url_for('api.collections', collection_id=collection.id,
                        page=0),
                'type':
                'AnnotationPage',
                'startIndex':
                0,
                'items':
                items,
                'next':
                url_for('api.collections', collection_id=collection.id,
                        page=1),
            },
            'last':
            url_for('api.collections',
                    collection_id=collection.id,
                    page=last_page)
        }

        endpoint = u'/annotations/{}/'.format(collection.id)
        res = self.app_get_json_ld(endpoint)
        data = json.loads(res.data.decode('utf8'))
        assert_dict_equal(data, expected)
Esempio n. 6
0
 def test_offset_slice_items(self):
     """Test offset slice of items returned."""
     collection = CollectionFactory()
     annotations = AnnotationFactory.create_batch(4, collection=collection)
     out = self.api_base._slice_items(collection.annotations, 2, 1)
     assert_equal(out, annotations[2:])