web/ldt/text/tests.py
changeset 9 22ab430e9b64
child 10 000f3ca19eaa
equal deleted inserted replaced
8:5bb249eefdd1 9:22ab430e9b64
       
     1 #encoding:UTF-8
       
     2 
       
     3 """ Run these tests with 'python manage.py test text'  """
       
     4 
       
     5 from django.test import TestCase
       
     6 import unittest
       
     7 import lxml.etree
       
     8 from ldt.text.models import *
       
     9 from ldt.core.models import Owner
       
    10 from views import *
       
    11 import base64
       
    12 import uuid
       
    13 import tempfile
       
    14 import datetime
       
    15 from django.contrib.auth.models import *
       
    16 from django.conf import settings
       
    17 from django.test.client import Client
       
    18 from ldt.text import VERSION_STR
       
    19 
       
    20 
       
    21 # This test creates an annotation and checks that:
       
    22 # 1. the annotation was created in the database (by trying to access it through a 'get')
       
    23 # 2. the returned xml contains correct data
       
    24 class CreateTest(unittest.TestCase):
       
    25     def setUp(self):
       
    26         self.content = base64.urlsafe_b64encode('<iri><text-annotation><id>f2c1d1fa-629d-4520-a3d2-955b4f2582c0</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>')
       
    27         self.c = Client()
       
    28         self.annot = Annotation(external_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')
       
    29         self.annot.save()
       
    30     def tearDown(self):
       
    31         annotlist=Annotation.objects.all()
       
    32         for annot in annotlist:
       
    33             annot.delete()
       
    34 
       
    35     def test_create_annotation(self):
       
    36         response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':self.content})
       
    37         self.annot1 = lxml.etree.fromstring(response.content)
       
    38         self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],"f2c1d1fa-629d-4520-a3d2-955b4f2582c0")
       
    39         self.assertEqual(self.annot1.xpath("/iri/text-annotation/content")[0].tag,"content")
       
    40         self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[0],"tag1")
       
    41         self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/text/text()")[0],u"texte selectionne lors de la creation de l\'annotation")
       
    42         self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()")[0],"2010-09-06 12:33:53.417550")
       
    43         annot = Annotation.objects.get(external_id="f2c1d1fa-629d-4520-a3d2-955b4f2582c0")
       
    44         self.assertEqual(annot.uri, "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168")
       
    45         
       
    46 #    def test_error_create(self):
       
    47 #        content = base64.urlsafe_b64encode('<iri><text-annotation><id>d2c1d1fa-629d-4520-a3d2-955b4f2582c0</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>')
       
    48 #        response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':content})
       
    49 #        #annot2 = create_annotation(content)
       
    50 #        self.assertEqual(response.status_code, '409')
       
    51 
       
    52 
       
    53 # This test creates an annotation, then gets it, and checks that the returned xml contains correct data
       
    54 class GetTest(unittest.TestCase):
       
    55     def setUp(self):
       
    56         self.annotation = Annotation(external_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-06 12:33:53.417550", update_date="2010-09-06 12:33:53.420459")
       
    57         self.annotation.save()
       
    58         self.c = Client()
       
    59     def tearDown(self):
       
    60         annotlist=Annotation.objects.all()
       
    61         for annot in annotlist:
       
    62             annot.delete()
       
    63       
       
    64     def test_get_annotation(self):
       
    65         response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
    66         self.annot1 = lxml.etree.fromstring(response.content)
       
    67         self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],self.annotation.external_id)
       
    68         self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[1], self.annotation.tags[1])
       
    69         self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/color/text()")[0],self.annotation.color)
       
    70         self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()")[0], str(self.annotation.creation_date))
       
    71 
       
    72 #    def test_error_get(self):
       
    73 #        response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'2'})
       
    74 #        #response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'2'})
       
    75 #        #annot3 = get_annotation('d2c1d1fa-629d-4520-a3d2-955b4f2582c0')
       
    76 #        #resp = response.status_code
       
    77 #        self.assertEqual(response.status_code,'404')
       
    78         
       
    79         
       
    80 class FilterTest(unittest.TestCase):
       
    81     def setUp(self):
       
    82         self.annotation = Annotation(external_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")
       
    83         self.annotation.save()
       
    84         self.annotation2 = Annotation(external_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")
       
    85         self.annotation2.save()
       
    86         self.annotation3 = Annotation(external_id="m2c1d1fa-629d-4520-a3d2-955b4f2582c0", title="titre3", text="texte3", color="#CCCCCC", uri="http://blabla", creator="wakimd")
       
    87         self.annotation3.save() 
       
    88         self.c = Client()       
       
    89     def tearDown(self):
       
    90         annotlist=Annotation.objects.all()
       
    91         for annot in annotlist:
       
    92             annot.delete()
       
    93         
       
    94     def test_filter_annotation_creator_limit(self):
       
    95         user = 'wakimd'
       
    96         uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
       
    97         limit= 1
       
    98         response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'creator':user,'limit':limit})
       
    99         doc = lxml.etree.fromstring(response.content)
       
   100         cpt = 0
       
   101         for elem in doc.xpath("/iri/text-annotation"):
       
   102             cpt = cpt + 1
       
   103         if limit is not None:
       
   104             self.assertEqual(cpt,limit)
       
   105         for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"):
       
   106             self.assertEqual(elem,user)
       
   107         for elem in doc.xpath("/iri/text-annotation/uri/text()"):
       
   108             self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml")
       
   109         
       
   110     def test_filter_annotation_uri(self):
       
   111         uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
       
   112         response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri})
       
   113         doc = lxml.etree.fromstring(response.content)
       
   114         for elem in doc.xpath("/iri/text-annotation/uri/text()"):
       
   115             self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml")
       
   116             
       
   117     def test_filter_annotation_filter(self):
       
   118         uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
       
   119         filter = 'lors'
       
   120         limit = None
       
   121         response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'filter':'lors'})
       
   122         doc = lxml.etree.fromstring(response.content)
       
   123         for elem in doc.xpath("/iri/text-annotation/content/text/text()"):
       
   124             self.assertTrue('lors' in elem)  
       
   125         for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"):
       
   126             self.assertEqual(elem,user)          
       
   127 
       
   128 
       
   129 # This test creates an annotation, then deletes it, and checks that:
       
   130 # 1. the annotation doesn't exist anymore in the database (by trying to access it through a 'get')
       
   131 # 2. the returned xml contains no data
       
   132 class DeleteTest(unittest.TestCase):
       
   133     def setUp(self):
       
   134         self.annotation = Annotation(external_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")
       
   135         self.annotation.save()
       
   136         self.c = Client()    
       
   137     def tearDown(self):
       
   138         annotlist=Annotation.objects.all()
       
   139         for annot in annotlist:
       
   140             annot.delete()
       
   141     
       
   142     def test_delete_annotation(self):
       
   143         response = self.c.post('/api/'+ VERSION_STR +'/text/delete/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
   144         #response2 = self.c.get('/ldt/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
   145         doc = lxml.etree.fromstring(response.content)
       
   146         self.assertEqual(doc.xpath("/iri/text-annotation/id/text()"),[])
       
   147         self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()"), [])
       
   148         self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()"),[])
       
   149         self.assertEqual(doc.xpath("/iri/text-annotation/meta/creator/text()"),[])
       
   150         #self.assertEqual(response2.status_code, '404')   
       
   151 
       
   152 #    def test_error_delete(self):
       
   153 #        response = self.c.post('/api/'+ VERSION_STR +'/text/ldt/delete/', {'id':'1'})
       
   154 #        #annot4 = delete_annotation('f2c1d1fa-629d-4520-a3d2-955b4f2582c0')
       
   155 #        self.assertEqual(response.status_code,'404')
       
   156         
       
   157 
       
   158 # This test creates an annotation, then updates it with new content, and checks that the returned xml contains the updated data
       
   159 class UpdateTest(unittest.TestCase):
       
   160     def setUp(self):
       
   161         self.annotation = Annotation(external_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")
       
   162         self.annotation.save()
       
   163         self.c = Client()
       
   164     def tearDown(self):
       
   165         annotlist=Annotation.objects.all()
       
   166         for annot in annotlist:
       
   167             annot.delete()
       
   168             
       
   169     def test_update_annotation(self):
       
   170         content = base64.urlsafe_b64encode('<iri><text-annotation><id></id><uri></uri><tags><tag>tag1</tag><tag>tag2new</tag><tag>tag3</tag></tags><content><color>#DDDDDD</color><description><![CDATA[texte de description update]]></description><title></title><text><![CDATA[texte selectionne a nouveau lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>80cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created></created><creator></creator><creator-id></creator-id><modified>2010-11-06 12:33:53.420459</modified></meta></text-annotation></iri>')
       
   171         response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
   172         doc = lxml.etree.fromstring(response.content)
       
   173         self.assertEqual(doc.xpath("/iri/text-annotation/id/text()")[0],"d2c1d1fa-629d-4520-a3d2-955b4f2582c0")
       
   174         self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()")[1], "mytag")
       
   175         self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()")[0],"#DDDDDD")
       
   176         
       
   177 #    def test_error_update(self):
       
   178 #        content = base64.urlsafe_b64encode('<iri><text-annotation><id>d2c1d1fa-629d-4520-a3d2-955b4f2582c0</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>')
       
   179 #        response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'1'})
       
   180 #        #annot5=update_annotation()
       
   181 #        self.assertEqual(response.status_code,'404')
       
   182 #               
       
   183