web/ldt/ldt_utils/tests.py
changeset 1 3a30d255c235
child 2 59311c28454f
equal deleted inserted replaced
0:40eddcc3d063 1:3a30d255c235
       
     1 #encoding:UTF-8
       
     2 
       
     3 """ Run these tests with 'python manage.py test ldt_utils'  """
       
     4 
       
     5 from django.test import TestCase
       
     6 import unittest
       
     7 import lxml.etree
       
     8 from ldt.ldt_utils.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 
       
    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     def tearDown(self):
       
    29         annotlist=Annotation.objects.all()
       
    30         for annot in annotlist:
       
    31             annot.delete()
       
    32 
       
    33     def test_create_annotation(self):
       
    34         response = self.c.post('/create/', {'content':self.content})
       
    35         self.annot1 = lxml.etree.fromstring(response.content)
       
    36         #self.assertEqual(lxml.etree.tostring(self.annot1),"lala")
       
    37         self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],"f2c1d1fa-629d-4520-a3d2-955b4f2582c0")
       
    38         self.assertEqual(self.annot1.xpath("/iri/text-annotation/content")[0].tag,"content")
       
    39         self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[0],"tag1")
       
    40         self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/text/text()")[0],u"texte selectionne lors de la creation de l\'annotation")
       
    41         self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()")[0],"2010-09-06 12:33:53.417550")
       
    42         annot = Annotation.objects.get(id="f2c1d1fa-629d-4520-a3d2-955b4f2582c0")
       
    43         self.assertEqual(annot.uri, "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168")
       
    44 
       
    45 
       
    46 # This test creates an annotation, then gets it, and checks that the returned xml contains correct data
       
    47 class GetTest(unittest.TestCase):
       
    48     def setUp(self):
       
    49         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")
       
    50         self.annotation.save()
       
    51     def tearDown(self):
       
    52         annotlist=Annotation.objects.all()
       
    53         for annot in annotlist:
       
    54             annot.delete()
       
    55       
       
    56     def test_get_annotation(self):
       
    57         response = self.c.get('/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
    58         self.annot1 = lxml.etree.fromstring(response.content)
       
    59         #self.annot1 = get_annotation("d2c1d1fa-629d-4520-a3d2-955b4f2582c0")
       
    60         #self.assertEqual(lxml.etree.tostring(self.annot1),"lala")
       
    61         self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],self.annotation.id)
       
    62         self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[1], self.annotation.tags[1])
       
    63         self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/color/text()")[0],self.annotation.color)
       
    64         #self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()"), self.annotation.creation_date)
       
    65 
       
    66         
       
    67 class FilterTest(unittest.TestCase):
       
    68     def setUp(self):
       
    69         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")
       
    70         self.annotation.save()
       
    71         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")
       
    72         self.annotation2.save()
       
    73         self.annotation3 = Annotation(id="m2c1d1fa-629d-4520-a3d2-955b4f2582c0", title="titre3", text="texte3", color="#CCCCCC", uri="http://blabla", creator="wakimd")
       
    74         self.annotation3.save()        
       
    75     def tearDown(self):
       
    76         annotlist=Annotation.objects.all()
       
    77         for annot in annotlist:
       
    78             annot.delete()
       
    79         
       
    80     def test_filter_annotation_creator(self):
       
    81         user = 'wakimd'
       
    82         uri = None
       
    83         limit= 2
       
    84         doc = filter_annotation(uri,None,limit,user)
       
    85         cpt = 0
       
    86         for elem in doc.xpath("/iri/text-annotation"):
       
    87             cpt = cpt + 1
       
    88         #self.assertEqual(lxml.etree.tostring(doc),"lala")
       
    89         if limit is not None:
       
    90             self.assertEqual(cpt,limit)
       
    91         for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"):
       
    92             self.assertEqual(elem,user)
       
    93         
       
    94     def test_filter_annotation_uri(self):
       
    95         uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
       
    96         limit = None
       
    97         doc = filter_annotation(uri,None,limit,None)
       
    98         for elem in doc.xpath("/iri/text-annotation/uri/text()"):
       
    99             self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml")
       
   100             
       
   101     def test_filter_annotation_filter(self):
       
   102         user = 'wakimd'
       
   103         uri = None
       
   104         filter = 'lors'
       
   105         limit = None
       
   106         doc = filter_annotation(uri,filter,limit,user)
       
   107         #self.assertEqual(lxml.etree.tostring(doc),"lala")
       
   108         for elem in doc.xpath("/iri/text-annotation/content/text/text()"):
       
   109             self.assertTrue('lors' in elem)  
       
   110         for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"):
       
   111             self.assertEqual(elem,user)          
       
   112 
       
   113 
       
   114 # This test creates an annotation, then deletes it, and checks that:
       
   115 # 1. the annotation doesn't exist anymore in the database (by trying to access it through a 'get')
       
   116 # 2. the returned xml contains no data
       
   117 class DeleteTest(unittest.TestCase):
       
   118     def setUp(self):
       
   119         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")
       
   120         self.annotation.save()    
       
   121     def tearDown(self):
       
   122         annotlist=Annotation.objects.all()
       
   123         for annot in annotlist:
       
   124             annot.delete()
       
   125     
       
   126     def test_delete_annotation(self):
       
   127         response = self.c.post('/delete/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
   128         response2 - self.c.get('/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
   129         doc = lxml.etree.fromstring(response.content)
       
   130         #doc=delete_annotation("d2c1d1fa-629d-4520-a3d2-955b4f2582c0")
       
   131         self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],'')
       
   132         self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[1], '')
       
   133         self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/color/text()")[0],'')
       
   134         self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/creator/text()")[0],'')
       
   135         self.assertEqual(response2.status_code, '404')   
       
   136 
       
   137 
       
   138 # This test creates an annotation, then updates it with new content, and checks that the returned xml contains the updated data
       
   139 class UpdateTest(unittest.TestCase):
       
   140     def setUp(self):
       
   141         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")
       
   142         self.annotation.save()
       
   143     def tearDown(self):
       
   144         annotlist=Annotation.objects.all()
       
   145         for annot in annotlist:
       
   146             annot.delete()
       
   147             
       
   148     def test_update_annotation(self):
       
   149         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>')
       
   150         response = self.c.post('/update/', {'content':content,'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
   151         doc = lxml.etree.fromstring(response.content)
       
   152         #doc = update_annotation(content,'d2c1d1fa-629d-4520-a3d2-955b4f2582c0')
       
   153         #self.assertEqual(lxml.etree.tostring(doc),"lala")
       
   154         self.assertEqual(doc.xpath("/iri/text-annotation/id/text()")[0],"d2c1d1fa-629d-4520-a3d2-955b4f2582c0")
       
   155         self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()")[1], "mytag")
       
   156         self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()")[0],"#DDDDDD")
       
   157                
       
   158                 
       
   159 #class AnnotTest(unittest.TestCase):
       
   160 #    def setUp(self):
       
   161 #        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')
       
   162 #        self.annot.save()       
       
   163 #    def tearDown(self):
       
   164 #        annotlist=Annotation.objects.all()
       
   165 #        for annot in annotlist:
       
   166 #            annot.delete()
       
   167 #  
       
   168 #    def test_create_annotation(self):
       
   169 #        b = Annotation.objects.get(id=u'd2c1d1fa-629d-4520-a3d2-955b4f2582c0')
       
   170 #        self.assertEqual(b.title, 'montitre')
       
   171 #            
       
   172 
       
   173 # These tests check common error codes are raised when needed
       
   174 class ErrorTest(unittest.TestCase):
       
   175     def setUp(self):
       
   176         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')
       
   177         self.annot.save()
       
   178     
       
   179     def tearDown(self):
       
   180         annotlist=Annotation.objects.all()
       
   181         for annot in annotlist:
       
   182             annot.delete()
       
   183 
       
   184     def test_error_create(self):
       
   185         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>')
       
   186         response = self.c.post('/create/', {'content':self.content})
       
   187         #annot2 = create_annotation(content)
       
   188         self.assertEqual(response.status_code, '409')
       
   189         
       
   190     def test_error_get(self):
       
   191         response = self.c.get('/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
   192         #annot3 = get_annotation('d2c1d1fa-629d-4520-a3d2-955b4f2582c0')
       
   193         resp = response.status_code
       
   194         self.assertEqual(response.status_code,'404')
       
   195 
       
   196     def test_error_delete(self):
       
   197         response = self.c.post('/delete/', {'id':'f2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
   198         #annot4 = delete_annotation('f2c1d1fa-629d-4520-a3d2-955b4f2582c0')
       
   199         self.assertEqual(response.status_code,'404')
       
   200 
       
   201     def test_error_update(self):
       
   202         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>')
       
   203         response = self.c.post('/update/', {'content':content,'id':'dfc1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
   204         #annot5=update_annotation()
       
   205         self.assertEqual(response.status_code,'404')
       
   206