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 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)
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)
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])