Example #1
0
 def test_annotation_collection_can_generate_an_annotation_collection_json(
         self):
     collection = AnnotationCollection(self.collection)
     annotation = Annotation(copy.copy(examples["vincent"]))
     collection.add_annotation(annotation.id)
     collection_json = collection.to_json()
     self.assertEqual(collection_json["id"], collection.id)
     self.assertEqual(collection_json["total"], 1)
     self.assertEqual(collection_json["items"][0], annotation.id)
Example #2
0
 def setUp(self):
     self.collection = AnnotationCollection(
         copy.copy(example_collections["empty_collection"]))
     self.annotations = [
         Annotation(copy.copy(examples["vincent"])),
         Annotation(copy.copy(examples["theo"]))
     ]
     self.label = "Some collection"
     self.base_url = "http://localhost:3000/api/annotations"
Example #3
0
 def test_annotation_collection_can_remove_valid_annotation(self):
     collection = AnnotationCollection(self.collection)
     annotation = Annotation(copy.copy(examples["vincent"]))
     collection.add_annotation(annotation.id)
     self.assertEqual(collection.has_annotation(annotation.id), True)
     collection.remove_annotation(annotation.id)
     self.assertEqual(collection.has_annotation(annotation.id), False)
 def create_collection_es(self, collection_data, params):
     # check if collection is valid, add id and timestamp
     collection = AnnotationCollection(collection_data)
     # if collection already has ID, check if it already exists in the index
     if "id" in collection_data:
         self.should_not_exist(collection_data['id'],
                               collection_data['type'])
     # add permissions for access (see) and update (edit)
     permissions.add_permissions(collection, params)
     # index collection
     self.add_to_index(collection.to_json(), collection.type)
     # set index needs refresh before next GET
     self.set_index_needs_refresh()
     # return collection to caller
     return collection.to_clean_json(params)
 def get_objects_from_hits(self, hits):
     objects = []
     for hit in hits:
         if hit["_source"]["type"] == "Annotation":
             objects += [Annotation(hit["_source"])]
         elif hit["_source"]["type"] == "AnnotationCollection":
             objects += [AnnotationCollection(hit["_source"])]
 def get_collections_es(self, params):
     # check index is up to date, refresh if needed
     self.check_index_is_fresh()
     response = self.get_from_index_by_filters(
         params, annotation_type="AnnotationCollection")
     collections = [
         AnnotationCollection(hit["_source"])
         for hit in response["hits"]["hits"]
     ]
     return {
         "total":
         response["hits"]["total"],
         "collections":
         [collection.to_clean_json(params) for collection in collections]
     }
 def update_collection_es(self, collection_json):
     self.should_exist(collection_json["id"], "AnnotationCollection")
     collection = AnnotationCollection(
         self.get_from_index_by_id(collection_json["id"],
                                   "AnnotationCollection"))
     collection.update(collection_json)
     self.update_in_index(collection.to_json(), "AnnotationCollection")
     # set index needs refresh before next GET
     self.set_index_needs_refresh()
     return collection.to_json()
 def get_from_index_if_allowed(self,
                               annotation_id,
                               username,
                               action,
                               annotation_type="_all"):
     # check index is up to date, refresh if needed
     self.check_index_is_fresh()
     # check that annotation exists (and is not deleted)
     self.should_exist(annotation_id, annotation_type)
     # get original annotation json
     annotation_json = self.get_from_index_by_id(annotation_id,
                                                 annotation_type)
     annotation = Annotation(annotation_json) if annotation_json[
         "type"] == "Annotation" else AnnotationCollection(annotation_json)
     # check if user has appropriate permissions
     if not permissions.is_allowed_action(username, action, annotation):
         raise PermissionError(
             message="Unauthorized access - no permission to {a} annotation"
             .format(a=action))
     return annotation
Example #9
0
class TestAnnotationContainer(unittest.TestCase):
    def setUp(self):
        self.collection = AnnotationCollection(
            copy.copy(example_collections["empty_collection"]))
        self.annotations = [
            Annotation(copy.copy(examples["vincent"])),
            Annotation(copy.copy(examples["theo"]))
        ]
        self.label = "Some collection"
        self.base_url = "http://*****:*****@context"])
        self.assertTrue("http://www.w3.org/ns/anno.jsonld", view["@context"])
        self.assertEqual(view["total"], 0)
        self.assertTrue("first" not in view.keys())

    def test_container_can_be_initialized_with_annotations(self):
        container = AnnotationContainer(self.base_url, self.annotations)
        self.assertEqual(container.page_size, 100)
        self.assertEqual(container.num_pages, 1)

    def test_container_can_be_initialized_with_annotations_as_json(self):
        annotations_as_json = [anno.data for anno in self.annotations]
        container = AnnotationContainer(self.base_url, annotations_as_json)
        self.assertEqual(container.page_size, 100)
        self.assertEqual(container.num_pages, 1)

    def test_non_empty_container_view_has_first_and_last_page_references(self):
        container = AnnotationContainer(self.base_url, self.annotations)
        view = container.view()
        self.assertEqual(view["total"], 2)
        self.assertEqual(type(view["first"]), str)
        self.assertEqual(type(view["last"]), str)

    def test_container_calculates_page_numbers_correctly(self):
        container = AnnotationContainer(self.base_url,
                                        self.annotations,
                                        page_size=1)
        view = container.view()
        last_url = container.update_url(self.base_url, {"iris": 1, "page": 1})
        self.assertEqual(view["last"], last_url)
        container = AnnotationContainer(self.base_url,
                                        self.annotations,
                                        page_size=2)
        view = container.view()
        last_url = container.update_url(self.base_url, {"iris": 1, "page": 0})
        self.assertEqual(view["last"], last_url)

    def test_container_can_generate_pages(self):
        container = AnnotationContainer(self.base_url,
                                        self.annotations,
                                        page_size=1)
        view = container.view_page(page=0)
        self.assertEqual(view["@context"], "http://www.w3.org/ns/anno.jsonld")
        self.assertEqual(
            view["id"],
            container.update_url(self.base_url, {
                "iris": 1,
                "page": 0
            }))
        self.assertEqual(view["type"], "AnnotationPage")
        self.assertEqual(view["partOf"]["id"], container.base_url)
        self.assertEqual(view["startIndex"], 0)
        self.assertEqual(
            view["next"],
            container.update_url(self.base_url, {
                "iris": 1,
                "page": 1
            }))
        self.assertEqual(len(view["items"]), 1)

    def test_container_generate_page_referencing(self):
        annotations = [
            Annotation(copy.copy(examples["vincent"])),
            Annotation(copy.copy(examples["theo"])),
            Annotation(copy.copy(examples["brothers"]))
        ]
        container = AnnotationContainer(self.base_url,
                                        annotations,
                                        page_size=1)
        view0 = container.view_page(page=0)
        view1 = container.view_page(page=1)
        view2 = container.view_page(page=2)
        self.assertEqual(view0["next"], view1["id"])
        self.assertEqual(view0["next"], view2["prev"])
        self.assertEqual(view1["prev"], view0["id"])
        self.assertEqual(view1["next"], view2["id"])
        self.assertEqual(view2["prev"], view1["id"])
        items = view0["items"] + view1["items"] + view2["items"]
        for anno in annotations:
            self.assertTrue(anno.id in items)

    def test_container_view_can_show_first_page_as_iris(self):
        anno_ids = [anno.id for anno in self.annotations]
        container = AnnotationContainer(self.base_url,
                                        self.annotations,
                                        view="PreferContainedIRIs",
                                        page_size=1)
        view = container.view()
        self.assertEqual(view["first"]["id"],
                         container.update_url(container.base_url, {"page": 0}))
        self.assertEqual(view["first"]["next"],
                         container.update_url(container.base_url, {"page": 1}))
        self.assertEqual(len(view["first"]["items"]), 1)
        self.assertTrue(view["first"]["items"][0] in anno_ids)

    def test_container_view_can_show_first_page_as_descriptions(self):
        anno = self.annotations[0]
        container = AnnotationContainer(self.base_url, [anno],
                                        view="PreferContainedDescriptions")
        view = container.view()
        item = view["first"]["items"][0]
        for key in item.keys():
            self.assertTrue(key in anno.data.keys())
            self.assertEqual(item[key], anno.data[key])
Example #10
0
 def test_annotation_collection_can_retrieve_annotation(self):
     collection = AnnotationCollection(self.collection)
     annotation = Annotation(copy.copy(examples["vincent"]))
     collection.add_annotation(annotation.id)
     self.assertTrue(collection.has_annotation(annotation.id))
Example #11
0
 def test_annotation_collection_can_add_valid_annotation(self):
     collection = AnnotationCollection(self.collection)
     annotation = Annotation(copy.copy(examples["vincent"]))
     collection.add_annotation(annotation.id)
     self.assertEqual(collection.size(), 1)
Example #12
0
 def test_annotation_collection_can_be_initialized(self):
     collection = AnnotationCollection(self.collection)
     self.assertEqual(collection.list_annotations(), [])
     self.assertEqual(collection.label, self.collection["label"])
     self.assertNotEqual(collection.id, None)