web/ldt/text/tests.py
changeset 24 9e19b7ae3780
parent 21 1a061f244254
equal deleted inserted replaced
23:65baf3ff7315 24:9e19b7ae3780
       
     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 urllib
       
    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 from django.db import transaction
       
    20 from django.contrib.auth.models import User
       
    21 import time
       
    22 import lucene
       
    23 from ldt.text import STORE, ANALYZER
       
    24 from ldt.text.utils import *
       
    25 
       
    26 
       
    27 # This test creates an annotation and checks that:
       
    28 # 1. the annotation was created in the database (by trying to access it through a 'get')
       
    29 # 2. the returned xml contains correct data
       
    30 class CreateTest(unittest.TestCase):
       
    31     def setUp(self):
       
    32         self.content = str('<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>')
       
    33         self.c = Client()
       
    34         self.annot = Annotation(external_id=u'd2c1d1fa-629d-4520-a3d2-955b4f2582c0', uri=u'http://iri.blabla', tags=u"tag1,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')
       
    35         self.annot.save()
       
    36     def tearDown(self):
       
    37         transaction.rollback()
       
    38         annotlist=Annotation.objects.all()
       
    39         for annot in annotlist:
       
    40             annot.delete()
       
    41 
       
    42     def test_create_annotation(self):
       
    43         response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':self.content})
       
    44         #self.assertEqual(response.content, "  ")
       
    45         self.annot1 = lxml.etree.fromstring(response.content)
       
    46         self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],"f2c1d1fa-629d-4520-a3d2-955b4f2582c0")
       
    47         self.assertEqual(self.annot1.xpath("/iri/text-annotation/content")[0].tag,"content")
       
    48         self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[0],u"tag1")
       
    49         self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/text/text()")[0],u"texte selectionne lors de la creation de l\'annotation")
       
    50         #self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()")[0],"2010-09-06 12:33:53.417550")
       
    51         response2 = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'f2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
    52         annot2 = lxml.etree.fromstring(response.content)
       
    53         self.assertEqual(annot2.xpath("/iri/text-annotation/uri/text()")[0], "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168")
       
    54         
       
    55     def test_error_create(self):
       
    56         content = '<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>'
       
    57         response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':content})
       
    58         self.assertEqual(response.status_code, 409)
       
    59 
       
    60 
       
    61 # This test creates an annotation, then gets it, and checks that the returned xml contains correct data
       
    62 class GetTest(unittest.TestCase):
       
    63     def setUp(self):
       
    64         self.annotation = Annotation(external_id="d2c1d1fa-629d-4520-a3d2-955b4f2582c0", tags=u"tag1 ,tag2 ,     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")
       
    65         self.annotation.save()
       
    66         self.c = Client()
       
    67     def tearDown(self):
       
    68         annotlist=Annotation.objects.all()
       
    69         for annot in annotlist:
       
    70             annot.delete()
       
    71       
       
    72     def test_get_annotation(self):
       
    73         response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
    74         print response
       
    75         self.annot1 = lxml.etree.fromstring(response.content)
       
    76         self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],self.annotation.external_id)
       
    77         self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[1], "tag2")
       
    78         self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/color/text()")[0],self.annotation.color)
       
    79         self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()")[0], str(self.annotation.creation_date))
       
    80 
       
    81     def test_error_get(self):
       
    82         response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'2'})
       
    83         self.assertEqual(response.status_code,404)
       
    84         
       
    85         
       
    86 class FilterTest(unittest.TestCase):
       
    87     def setUp(self):
       
    88         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")
       
    89         self.annotation.save()
       
    90         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")
       
    91         self.annotation2.save()
       
    92         self.annotation3 = Annotation(external_id="m2c1d1fa-629d-4520-a3d2-955b4f2582c0", title="titre3", text="texte3", color="#CCCCCC", uri="http://blabla", creator="wakimd")
       
    93         self.annotation3.save() 
       
    94         self.c = Client()       
       
    95     def tearDown(self):
       
    96         annotlist=Annotation.objects.all()
       
    97         for annot in annotlist:
       
    98             annot.delete()
       
    99         
       
   100     def test_filter_annotation_creator_limit(self):
       
   101         user = 'wakimd'
       
   102         uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
       
   103         limit= 1
       
   104         response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'creator':user,'limit':limit})
       
   105         doc = lxml.etree.fromstring(response.content)
       
   106         cpt = 0
       
   107         for elem in doc.xpath("/iri/text-annotation"):
       
   108             cpt = cpt + 1
       
   109         if limit is not None:
       
   110             self.assertEqual(cpt,limit)
       
   111         for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"):
       
   112             self.assertEqual(elem,user)
       
   113         for elem in doc.xpath("/iri/text-annotation/uri/text()"):
       
   114             self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml")
       
   115         
       
   116     def test_filter_annotation_uri(self):
       
   117         uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
       
   118         response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri})
       
   119         doc = lxml.etree.fromstring(response.content)
       
   120         for elem in doc.xpath("/iri/text-annotation/uri/text()"):
       
   121             self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml")
       
   122             
       
   123     def test_filter_annotation_filter(self):
       
   124         uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
       
   125         filter = 'lors'
       
   126         limit = None
       
   127         response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'filter':filter})
       
   128         doc = lxml.etree.fromstring(response.content)
       
   129         for elem in doc.xpath("/iri/text-annotation/content/text/text()"):
       
   130             self.assertTrue('lors' in elem)  
       
   131         #for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"):
       
   132         #    self.assertEqual(elem,user)          
       
   133 
       
   134 
       
   135 # This test creates an annotation, then deletes it, and checks that:
       
   136 # 1. the annotation doesn't exist anymore in the database (by trying to access it through a 'get')
       
   137 # 2. the returned xml contains no data
       
   138 class DeleteTest(unittest.TestCase):
       
   139     def setUp(self):
       
   140         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")
       
   141         self.annotation.save()
       
   142         self.c = Client()
       
   143     def tearDown(self):
       
   144         annotlist=Annotation.objects.all()
       
   145         for annot in annotlist:
       
   146             annot.delete()
       
   147         
       
   148     
       
   149     def test_delete_annotation(self):
       
   150         id = urllib.urlencode({'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
   151         f = urllib.urlopen("http://127.0.0.1:8000/api/1.0/text/delete/", id)
       
   152         response = self.c.post('/api/'+ VERSION_STR +'/text/delete/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
   153         response2 = self.c.get('/ldt/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
       
   154         doc = lxml.etree.fromstring(response.content)
       
   155         self.assertEqual(doc.xpath("/iri/text-annotation/id/text()"),[])
       
   156         self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()"), [])
       
   157         self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()"),[])
       
   158         self.assertEqual(doc.xpath("/iri/text-annotation/meta/creator/text()"),[])
       
   159         self.assertEqual(response2.status_code, 404)
       
   160 
       
   161 
       
   162     def test_error_delete(self):
       
   163         response = self.c.post('/api/'+ VERSION_STR +'/text/ldt/delete/', {'id':'1'})
       
   164         self.assertEqual(response.status_code,404)
       
   165         
       
   166 
       
   167 # This test creates an annotation, then updates it with new content, and checks that the returned xml contains the updated data
       
   168 class UpdateTest(unittest.TestCase):
       
   169     def setUp(self):
       
   170         self.annotation = Annotation(external_id="d2c1d1fa-629d-4520-a3d2-955b4f2582c0", tags=u"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")
       
   171         self.annotation.save()
       
   172         self.c = Client()
       
   173     def tearDown(self):
       
   174         annotlist=Annotation.objects.all()
       
   175         for annot in annotlist:
       
   176             annot.delete()
       
   177             
       
   178     def test_update_annotation(self):
       
   179         content = '<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>'
       
   180         response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})        
       
   181         doc = lxml.etree.fromstring(response.content)
       
   182         #self.assertEqual(lxml.etree.tostring(doc), " ")
       
   183         self.assertEqual(doc.xpath("/iri/text-annotation/id/text()")[0],"d2c1d1fa-629d-4520-a3d2-955b4f2582c0")
       
   184         self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()")[1], "tag2new")
       
   185         self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()")[0],"#DDDDDD")
       
   186         
       
   187     def test_error_update(self):
       
   188         content = '<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>'
       
   189         response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'1'})
       
   190         self.assertEqual(response.status_code,404)
       
   191                
       
   192 
       
   193 class OnServerGlobalTest(unittest.TestCase):
       
   194     def setUp(self):
       
   195         self.content = urllib.urlencode({'content':'<iri><text-annotation><id>mypersonnalid</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>'})
       
   196         self.content2 = urllib.urlencode({'content':'<iri><text-annotation><id>mypersonnalid2</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>#BBBBBB</color><description><![CDATA[texte de description2]]></description><title><![CDATA[titre de l\'annotation2]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation2]]></text></content><meta><contributor>wakimd</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>'})
       
   197         self.id = urllib.urlencode({"id":"mypersonnalid"})
       
   198         self.id2 = urllib.urlencode({"id":"mypersonnalid2"})
       
   199         self.uri = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"})
       
   200         self.filt1 = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168", "creator":"","limit":"","filter":""})
       
   201         self.filt2 = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168","creator":"wakimd","limit":"","filter":""})
       
   202         self.up = urllib.urlencode({'content':'<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>','id':'mypersonnalid'})
       
   203         self.LS = LdtSearch()
       
   204     
       
   205     def test_everything(self):
       
   206         creation = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/create/", self.content)
       
   207         creation2 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/create/", self.content2)
       
   208 
       
   209         res1 = self.LS.query("title","titre de l'annotation")
       
   210         self.assertEqual(len(res1),1)
       
   211         res2 = self.LS.query("title","titre de l'annotation2")
       
   212         self.assertEqual(len(res2),1)
       
   213 
       
   214         
       
   215         get = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/get/?%s" % self.id)
       
   216         
       
   217         update = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/update/", self.up)
       
   218         
       
   219         res3 = self.LS.query("abstract","texte de description update")
       
   220         self.assertEqual(len(res3),1)
       
   221         
       
   222         filt1 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/filter/?%s" % self.uri)
       
   223         filt2 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/filter/?uri=http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168?creator=wakimd")
       
   224 
       
   225         delete = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/delete/", self.id)
       
   226         delete = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/delete/", self.id2)
       
   227 
       
   228         res4 = self.LS.query("title","titre de l'annotation")
       
   229         self.assertEqual(len(res4),0)
       
   230         res5 = self.LS.query("title","titre de l'annotation2")
       
   231         self.assertEqual(len(res5),0)
       
   232 
       
   233         
       
   234