diff -r 40eddcc3d063 -r 3a30d255c235 web/ldt/ldt_utils/tests.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/ldt/ldt_utils/tests.py Sun Nov 14 20:25:22 2010 +0100 @@ -0,0 +1,206 @@ +#encoding:UTF-8 + +""" Run these tests with 'python manage.py test ldt_utils' """ + +from django.test import TestCase +import unittest +import lxml.etree +from ldt.ldt_utils.models import * +from ldt.core.models import Owner +from views import * +import base64 +import uuid +import tempfile +import datetime +from django.contrib.auth.models import * +from django.conf import settings +from django.test.client import Client + + + +# This test creates an annotation and checks that: +# 1. the annotation was created in the database (by trying to access it through a 'get') +# 2. the returned xml contains correct data +class CreateTest(unittest.TestCase): + def setUp(self): + self.content = base64.urlsafe_b64encode('f2c1d1fa-629d-4520-a3d2-955b4f2582c0http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168tag1tag2#AAAAAA<![CDATA[titre de l�annotation]]>oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.417550oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.420459') + self.c = Client() + def tearDown(self): + annotlist=Annotation.objects.all() + for annot in annotlist: + annot.delete() + + def test_create_annotation(self): + response = self.c.post('/create/', {'content':self.content}) + self.annot1 = lxml.etree.fromstring(response.content) + #self.assertEqual(lxml.etree.tostring(self.annot1),"lala") + self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],"f2c1d1fa-629d-4520-a3d2-955b4f2582c0") + self.assertEqual(self.annot1.xpath("/iri/text-annotation/content")[0].tag,"content") + self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[0],"tag1") + self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/text/text()")[0],u"texte selectionne lors de la creation de l\'annotation") + self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()")[0],"2010-09-06 12:33:53.417550") + annot = Annotation.objects.get(id="f2c1d1fa-629d-4520-a3d2-955b4f2582c0") + self.assertEqual(annot.uri, "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168") + + +# This test creates an annotation, then gets it, and checks that the returned xml contains correct data +class GetTest(unittest.TestCase): + def setUp(self): + self.annotation = Annotation(id="d2c1d1fa-629d-4520-a3d2-955b4f2582c0", tags=[u"tag1",u"tag2",u"tag3"], title="titre de l\'annotation",text="texte selectionne lors de la creation de l\'annotation",color="#AAAAAA", creation_date="2010-09-06T12:33:53.417550", update_date="2010-09-06T12:33:53.420459") + self.annotation.save() + def tearDown(self): + annotlist=Annotation.objects.all() + for annot in annotlist: + annot.delete() + + def test_get_annotation(self): + response = self.c.get('/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) + self.annot1 = lxml.etree.fromstring(response.content) + #self.annot1 = get_annotation("d2c1d1fa-629d-4520-a3d2-955b4f2582c0") + #self.assertEqual(lxml.etree.tostring(self.annot1),"lala") + self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],self.annotation.id) + self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[1], self.annotation.tags[1]) + self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/color/text()")[0],self.annotation.color) + #self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()"), self.annotation.creation_date) + + +class FilterTest(unittest.TestCase): + def setUp(self): + self.annotation = Annotation(id="k2c1d1fa-629d-4520-a3d2-955b4f2582c0",title="titre de l\'annotation",text="texte selectionne lors de la creation de l\'annotation",color="#AAAAAA", uri="http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168", creator="wakimd") + self.annotation.save() + self.annotation2 = Annotation(id="l2c1d1fa-629d-4520-a3d2-955b4f2582c0",title="titre de l\'annotation2",text="texte selectionne lors de la creation de l\'annotation2",color="#BBBBBB", uri="http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168", creator="wakimd") + self.annotation2.save() + self.annotation3 = Annotation(id="m2c1d1fa-629d-4520-a3d2-955b4f2582c0", title="titre3", text="texte3", color="#CCCCCC", uri="http://blabla", creator="wakimd") + self.annotation3.save() + def tearDown(self): + annotlist=Annotation.objects.all() + for annot in annotlist: + annot.delete() + + def test_filter_annotation_creator(self): + user = 'wakimd' + uri = None + limit= 2 + doc = filter_annotation(uri,None,limit,user) + cpt = 0 + for elem in doc.xpath("/iri/text-annotation"): + cpt = cpt + 1 + #self.assertEqual(lxml.etree.tostring(doc),"lala") + if limit is not None: + self.assertEqual(cpt,limit) + for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"): + self.assertEqual(elem,user) + + def test_filter_annotation_uri(self): + uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168" + limit = None + doc = filter_annotation(uri,None,limit,None) + for elem in doc.xpath("/iri/text-annotation/uri/text()"): + self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml") + + def test_filter_annotation_filter(self): + user = 'wakimd' + uri = None + filter = 'lors' + limit = None + doc = filter_annotation(uri,filter,limit,user) + #self.assertEqual(lxml.etree.tostring(doc),"lala") + for elem in doc.xpath("/iri/text-annotation/content/text/text()"): + self.assertTrue('lors' in elem) + for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"): + self.assertEqual(elem,user) + + +# This test creates an annotation, then deletes it, and checks that: +# 1. the annotation doesn't exist anymore in the database (by trying to access it through a 'get') +# 2. the returned xml contains no data +class DeleteTest(unittest.TestCase): + def setUp(self): + self.annotation = Annotation(id="d2c1d1fa-629d-4520-a3d2-955b4f2582c0",title="titre de l\'annotation",text="texte selectionne lors de la creation de l\'annotation",color="#AAAAAA", creation_date="2010-09-06T12:33:53.417550", update_date="2010-09-06T12:33:53.420459") + self.annotation.save() + def tearDown(self): + annotlist=Annotation.objects.all() + for annot in annotlist: + annot.delete() + + def test_delete_annotation(self): + response = self.c.post('/delete/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) + response2 - self.c.get('/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) + doc = lxml.etree.fromstring(response.content) + #doc=delete_annotation("d2c1d1fa-629d-4520-a3d2-955b4f2582c0") + self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],'') + self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[1], '') + self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/color/text()")[0],'') + self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/creator/text()")[0],'') + self.assertEqual(response2.status_code, '404') + + +# This test creates an annotation, then updates it with new content, and checks that the returned xml contains the updated data +class UpdateTest(unittest.TestCase): + def setUp(self): + self.annotation = Annotation(id="d2c1d1fa-629d-4520-a3d2-955b4f2582c0", tags=['tag1','mytag'],title="titre de l\'annotation",text="texte selectionne lors de la creation de l\'annotation",color="#AAAAAA", creation_date="2010-09-06T12:33:53.417550", update_date="2010-09-06T12:33:53.420459") + self.annotation.save() + def tearDown(self): + annotlist=Annotation.objects.all() + for annot in annotlist: + annot.delete() + + def test_update_annotation(self): + content = base64.urlsafe_b64encode('tag1tag2newtag3#DDDDDDoaubert80cd0532-1dda-4130-b351-6a181130a7c92010-11-06 12:33:53.420459') + response = self.c.post('/update/', {'content':content,'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) + doc = lxml.etree.fromstring(response.content) + #doc = update_annotation(content,'d2c1d1fa-629d-4520-a3d2-955b4f2582c0') + #self.assertEqual(lxml.etree.tostring(doc),"lala") + self.assertEqual(doc.xpath("/iri/text-annotation/id/text()")[0],"d2c1d1fa-629d-4520-a3d2-955b4f2582c0") + self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()")[1], "mytag") + self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()")[0],"#DDDDDD") + + +#class AnnotTest(unittest.TestCase): +# def setUp(self): +# self.annot = Annotation(id=u'd2c1d1fa-629d-4520-a3d2-955b4f2582c0', uri=u'http://iri.blabla', tags=[u'tag1',u'tag2'], title=u'montitre', description=u'madesc', text=u'letexteselectionne', color=u'#AAAAAA', creator=u'wakimd', contributor=u'wakimd', creation_date=u'2010-09-06 12:33:53.417550', update_date=u'2010-09-06 12:33:53.417550') +# self.annot.save() +# def tearDown(self): +# annotlist=Annotation.objects.all() +# for annot in annotlist: +# annot.delete() +# +# def test_create_annotation(self): +# b = Annotation.objects.get(id=u'd2c1d1fa-629d-4520-a3d2-955b4f2582c0') +# self.assertEqual(b.title, 'montitre') +# + +# These tests check common error codes are raised when needed +class ErrorTest(unittest.TestCase): + def setUp(self): + self.annot = Annotation(id=u'd2c1d1fa-629d-4520-a3d2-955b4f2582c0', uri=u'http://iri.blabla', tags=[u'tag1',u'tag2'], title=u'montitre', description=u'madesc', text=u'letexteselectionne', color=u'#AAAAAA', creator=u'wakimd', contributor=u'wakimd', creation_date=u'2010-09-06 12:33:53.417550', update_date=u'2010-09-06 12:33:53.417550') + self.annot.save() + + def tearDown(self): + annotlist=Annotation.objects.all() + for annot in annotlist: + annot.delete() + + def test_error_create(self): + content = base64.urlsafe_b64encode('d2c1d1fa-629d-4520-a3d2-955b4f2582c0http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168tag1tag2#AAAAAA<![CDATA[titre de l�annotation]]>oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.417550oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.420459') + response = self.c.post('/create/', {'content':self.content}) + #annot2 = create_annotation(content) + self.assertEqual(response.status_code, '409') + + def test_error_get(self): + response = self.c.get('/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) + #annot3 = get_annotation('d2c1d1fa-629d-4520-a3d2-955b4f2582c0') + resp = response.status_code + self.assertEqual(response.status_code,'404') + + def test_error_delete(self): + response = self.c.post('/delete/', {'id':'f2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) + #annot4 = delete_annotation('f2c1d1fa-629d-4520-a3d2-955b4f2582c0') + self.assertEqual(response.status_code,'404') + + def test_error_update(self): + content = base64.urlsafe_b64encode('d2c1d1fa-629d-4520-a3d2-955b4f2582c0http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168tag1tag2#AAAAAA<![CDATA[titre de l�annotation]]>oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.417550oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.420459') + response = self.c.post('/update/', {'content':content,'id':'dfc1d1fa-629d-4520-a3d2-955b4f2582c0'}) + #annot5=update_annotation() + self.assertEqual(response.status_code,'404') +