Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/BE-39: Backend Unittests [1] #382

Merged
merged 10 commits into from
Dec 25, 2022
2 changes: 2 additions & 0 deletions App/annotations/annotations/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,5 @@

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'
TEST_OUTPUT_DIR = 'test-output/'
286 changes: 282 additions & 4 deletions App/annotations/api/test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.test import TestCase
from django.test import TestCase, RequestFactory
from faker import Faker
from .models import *
from .serializers import AnnotationSerializer, AnnotationBodySerializer, AnnotationTargetSerializer, FragmentSelectorSerializer, SelectorSerializer

from .views import *
from .utils import utils
"""
{
"@context": "http://www.w3.org/ns/anno.jsonld",
Expand All @@ -25,7 +26,11 @@
"""
class TestAnnotations(TestCase):
def setUp(self):
self.faker = Faker()
print("TestAnnotation:setUp_:begin")
self.faker = Faker()
self.factory = RequestFactory()
# do something
print("TestAnnotation:setUp_:end")

def test_image_annotation_target_serializer(self):
typeImage = create_or_return_type("image")
Expand Down Expand Up @@ -78,7 +83,280 @@ def test_helpers(self):
self.assertEqual(type1.type.value, "Image")
self.assertEqual(motivation2, -1)
self.assertEqual(motivation1.motivation.value, "Commenting")


# POST image annotation test
def test_create_image_annotation(self):
source = "http://34.125.134.88/artitems/14"
bodytype= "TextualBody"
selectortype = "FragmentSelector"
bodyvalue = self.faker.pystr(min_chars = 10)
targetvalue = self.faker.pystr(min_chars = 10)
creator = self.faker.pyint()
data = {
"@context": "http://www.w3.org/ns/anno.jsonld",
"id": "#218d01ff-f077-4cc3-992d-1c81c426e51b",
"type": "Annotation",
"creator" : creator,
"body": [{
"type": bodytype,
"value": bodyvalue
}],
"target": {
"source": source,
"selector": {
"type": selectortype,
"conformsTo": "http://www.w3.org/TR/media-frags/",
"value": targetvalue
}
}
}

request = self.factory.post('annotations/', data, content_type='application/json')
response = annotate(request)

self.assertEqual(response.status_code, 201)
expected_body = {
"value": bodyvalue,
"type": "Text",
"format": "text/plain",
"purpose": "Commenting"
}
actual_body = response.data['body'][0] # it returns a list
actual_body.pop('created')
actual_body.pop('id')

self.assertEqual(actual_body, expected_body)

expected_target = {
"source": source,
"type": "Image",
"selector": {
"value": targetvalue,
"type": "FragmentSelector",
"conformsTo": "http://www.w3.org/TR/media-frags/"
}
}
actual_target = response.data['target']
actual_target.pop('id')
self.assertEqual(actual_target, expected_target)
self.assertEqual(response.data['creator'], creator)
self.assertEqual(response.data['@context'], "http://www.w3.org/ns/anno.jsonld")

# POST text annotation test
def test_create_text_annotation(self):
source = "https://cmpe451-development.s3.amazonaws.com/artitem/artitem-1.png"
bodytype= "TextualBody"
bodyvalue = self.faker.pystr(min_chars = 10)
creator = self.faker.pyint()
exact = self.faker.paragraph(nb_sentences=3)
start = self.faker.pyint()
end = start + self.faker.pyint()
data = {
"id": "#2395b9aa-64f0-46a7-a561-90f67ebb2193",
"body": [
{
"value": bodyvalue,
"type": bodytype,
}
],
"type": "Annotation",
"target": {
"source": source,
"selector": [
{
"exact": exact,
"type": "TextQuoteSelector"
},
{
"start": start,
"end": end,
"type": "TextPositionSelector"
}
]
},
"creator": creator,
"@context": "http://www.w3.org/ns/anno.jsonld"
}

request = self.factory.post('annotations/', data, content_type='application/json')
response = annotate(request)

self.assertEqual(response.status_code, 201)
expected_body = {
"value": bodyvalue,
"type": "Text",
"format": "text/plain",
"purpose": "Commenting"
}
actual_body = response.data['body'][0] # it returns a list
actual_body.pop('created')
actual_body.pop('id')

self.assertEqual(actual_body, expected_body)

expected_target = {
"source": source,
"type": "Image",
"selector": [
{
"exact": exact,
"type": "TextQuoteSelector"
},
{
"start": start,
"end": end,
"type": "TextPositionSelector"
}
]
}
actual_target = response.data['target']
actual_target.pop('id')
self.assertEqual(actual_target, expected_target)
self.assertEqual(response.data['creator'], creator)
self.assertEqual(response.data['@context'], "http://www.w3.org/ns/anno.jsonld")

# PUT edit annotation test
def test_edit_annotation(self):
creator = self.faker.pyint()
img_annotation = utils.image_annotation(creator)

updbody = self.faker.pystr(min_chars = 10)
value = self.faker.pystr(min_chars = 10)

data = {
"@context": "http://www.w3.org/ns/anno.jsonld",
"id": "#218d01ff-f077-4cc3-992d-1c81c426e51b",
"type": "Annotation",
"creator" : creator,
"body": [{
"type": "TextualBody",
"value": updbody
}],
"target": {
"source": img_annotation['target']['source'],
"selector": {
"type": "FragmentSelector",
"conformsTo": "http://www.w3.org/TR/media-frags/",
"value": value
}
}
}
request = self.factory.put('annotations/', data, content_type='application/json')
response = annotate(request)

self.assertEqual(response.status_code, 200)
self.assertEqual(updbody, response.data['body'][0]['value'])
self.assertEqual(value, response.data['target']['selector']['value'])

def __inc(self, annotationID, annotations):
return any([annotationID == ann['id'] for ann in annotations])

# ALL annotations
def test_get_image_annotations1(self):
creator = self.faker.pyint()
img_annotation = utils.image_annotation(creator)

# get all annotations
request = self.factory.get('annotations/', content_type='application/json')
response = annotate(request)
self.assertEqual(response.status_code, 200)
self.assertTrue(self.__inc(img_annotation['id'], response.data))

# ALL image annotations
def test_get_image_annotations2(self):
creator = self.faker.pyint()
img_annotation = utils.image_annotation(creator)
# get all image annotations
request = self.factory.get('annotations/image/', content_type='application/json')
response = get_image_annotations(request)
self.assertEqual(response.status_code, 200)
self.assertTrue(self.__inc(img_annotation['id'], response.data))

# ALL image annotations on an art item
def test_get_image_annotations3(self):
creator = self.faker.pyint()
img_annotation = utils.image_annotation(creator)
# get all image annotations on an art item
request = self.factory.get('annotations/image/artitems/', content_type='application/json')
response = get_image_annotation_by_artitem_id(request, int(img_annotation['target']['source'].split('-')[-1].split('.')[0]))
self.assertEqual(response.status_code, 200)
self.assertTrue(self.__inc(img_annotation['id'], response.data))

# ALL image annotations of a user
def test_get_image_annotations4(self):
creator = self.faker.pyint()
img_annotation = utils.image_annotation(creator)
# get all image annotations of a user
request = self.factory.get('annotations/image/users/', content_type='application/json')
response = get_image_annotation_by_user_id(request, creator)
self.assertEqual(response.status_code, 200)
self.assertTrue(self.__inc(img_annotation['id'], response.data))

# ALL image annotations of a user on an art item
def test_get_image_annotations5(self):
creator = self.faker.pyint()
img_annotation = utils.image_annotation(creator)
# get all image annotations of a user
request = self.factory.get('annotations/image/users/artitems', content_type='application/json')
response = get_image_annotation_by_artitem_user_id(request, creator, int(img_annotation['target']['source'].split('-')[-1].split('.')[0]))
self.assertEqual(response.status_code, 200)
self.assertTrue(self.__inc(img_annotation['id'], response.data))


# ALL annotations
def test_get_text_annotations1(self):
creator = self.faker.pyint()
img_annotation = utils.text_annotation(creator)

# get all annotations
request = self.factory.get('annotations/', content_type='application/json')
response = annotate(request)
self.assertEqual(response.status_code, 200)
self.assertTrue(self.__inc(img_annotation['id'], response.data))

# ALL text annotations
def test_get_text_annotations2(self):
creator = self.faker.pyint()
img_annotation = utils.text_annotation(creator)
# get all image annotations
request = self.factory.get('annotations/text/', content_type='application/json')
response = get_text_annotations(request)
self.assertEqual(response.status_code, 200)
self.assertTrue(self.__inc(img_annotation['id'], response.data))

# ALL text annotations on an art item
def test_get_text_annotations3(self):
creator = self.faker.pyint()
img_annotation = utils.text_annotation(creator)
# get all text annotations on an art item
request = self.factory.get('annotations/text/artitems/', content_type='application/json')
response = get_text_annotations_by_artitem_id(request, int(img_annotation['target']['source'].split('-')[-1].split('.')[0]))
self.assertEqual(response.status_code, 200)
self.assertTrue(self.__inc(img_annotation['id'], response.data))

# ALL text annotations of a user
def test_get_text_annotations4(self):
creator = self.faker.pyint()
img_annotation = utils.text_annotation(creator)
# get all image annotations of a user
request = self.factory.get('annotations/text/users/', content_type='application/json')
response = get_text_annotations_by_userid(request, creator)
self.assertEqual(response.status_code, 200)
self.assertTrue(self.__inc(img_annotation['id'], response.data))

# ALL text annotations of a user on an art item
def test_get_text_annotations5(self):
creator = self.faker.pyint()
img_annotation = utils.text_annotation(creator)
# get all image annotations of a user
request = self.factory.get('annotations/text/users/artitems', content_type='application/json')
response = get_text_annotation_by_artitem_user_id(request, creator, int(img_annotation['target']['source'].split('-')[-1].split('.')[0]))
self.assertEqual(response.status_code, 200)
self.assertTrue(self.__inc(img_annotation['id'], response.data))

# cleaning
def tearDown(self):
pass
print("TestAnnotation:tearDown_:begin")
print("TestAnnotation:tearDown_:end")

83 changes: 83 additions & 0 deletions App/annotations/api/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from django.test import TestCase, RequestFactory
from faker import Faker
from .views import annotate

# utils for unit tests
class Utils:
def __init__(self):
self.faker = Faker()
self.factory = RequestFactory()


def text_annotation(self, creator):
bodyid = "http://34.125.134.88/body1"
source = "https://cmpe451-development.s3.amazonaws.com/artitem/artitem-1.png"
bodytype= "TextualBody"
bodyvalue = self.faker.pystr(min_chars = 10)
exact = self.faker.paragraph(nb_sentences=3)
start = self.faker.pyint()
end = start + self.faker.pyint()
data = {
"id": "#2395b9aa-64f0-46a7-a561-90f67ebb2193",
"body": [
{
"id": bodyid,
"value": bodyvalue,
"type": bodytype,
}
],
"type": "Annotation",
"target": {
"source": source,
"selector": [
{
"exact": exact,
"type": "TextQuoteSelector"
},
{
"start": start,
"end": end,
"type": "TextPositionSelector"
}
]
},
"creator": creator,
"@context": "http://www.w3.org/ns/anno.jsonld"
}

request = self.factory.post('annotations/', data, content_type='application/json')
response = annotate(request)
return response.data

def image_annotation(self, creator):
bodyid = "http://34.125.134.88/body1"
source = "https://cmpe451-development.s3.amazonaws.com/artitem/artitem-1.png"
bodytype= "TextualBody"
selectortype = "FragmentSelector"
bodyvalue = self.faker.pystr(min_chars = 10)
targetvalue = self.faker.pystr(min_chars = 10)
data = {
"@context": "http://www.w3.org/ns/anno.jsonld",
"id": "#218d01ff-f077-4cc3-992d-1c81c426e51b",
"type": "Annotation",
"creator" : creator,
"body": [{
"id": bodyid,
"type": bodytype,
"value": bodyvalue
}],
"target": {
"source": source,
"selector": {
"type": selectortype,
"conformsTo": "http://www.w3.org/TR/media-frags/",
"value": targetvalue
}
}
}

request = self.factory.post('annotations/', data, content_type='application/json')
response = annotate(request)
return response.data

utils = Utils()
Loading