|
9
|
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 * |
|
16
|
11 |
import urllib |
|
9
|
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): |
|
16
|
26 |
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>') |
|
9
|
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}) |
|
16
|
37 |
#self.assertEqual(response.content, " ") |
|
9
|
38 |
self.annot1 = lxml.etree.fromstring(response.content) |
|
|
39 |
self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],"f2c1d1fa-629d-4520-a3d2-955b4f2582c0") |
|
|
40 |
self.assertEqual(self.annot1.xpath("/iri/text-annotation/content")[0].tag,"content") |
|
|
41 |
self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[0],"tag1") |
|
|
42 |
self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/text/text()")[0],u"texte selectionne lors de la creation de l\'annotation") |
|
|
43 |
self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()")[0],"2010-09-06 12:33:53.417550") |
|
16
|
44 |
response2 = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'f2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) |
|
|
45 |
annot2 = lxml.etree.fromstring(response.content) |
|
|
46 |
self.assertEqual(annot2.xpath("/iri/text-annotation/uri/text()")[0], "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168") |
|
9
|
47 |
|
|
16
|
48 |
def test_error_create(self): |
|
|
49 |
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>' |
|
|
50 |
response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':content}) |
|
|
51 |
self.assertEqual(response.status_code, 409) |
|
9
|
52 |
|
|
|
53 |
|
|
|
54 |
# This test creates an annotation, then gets it, and checks that the returned xml contains correct data |
|
|
55 |
class GetTest(unittest.TestCase): |
|
|
56 |
def setUp(self): |
|
|
57 |
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") |
|
|
58 |
self.annotation.save() |
|
|
59 |
self.c = Client() |
|
|
60 |
def tearDown(self): |
|
|
61 |
annotlist=Annotation.objects.all() |
|
|
62 |
for annot in annotlist: |
|
|
63 |
annot.delete() |
|
|
64 |
|
|
|
65 |
def test_get_annotation(self): |
|
|
66 |
response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) |
|
|
67 |
self.annot1 = lxml.etree.fromstring(response.content) |
|
|
68 |
self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],self.annotation.external_id) |
|
|
69 |
self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[1], self.annotation.tags[1]) |
|
|
70 |
self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/color/text()")[0],self.annotation.color) |
|
|
71 |
self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()")[0], str(self.annotation.creation_date)) |
|
|
72 |
|
|
10
|
73 |
def test_error_get(self): |
|
|
74 |
response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'2'}) |
|
|
75 |
self.assertEqual(response.status_code,404) |
|
9
|
76 |
|
|
|
77 |
|
|
|
78 |
class FilterTest(unittest.TestCase): |
|
|
79 |
def setUp(self): |
|
|
80 |
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") |
|
|
81 |
self.annotation.save() |
|
|
82 |
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") |
|
|
83 |
self.annotation2.save() |
|
|
84 |
self.annotation3 = Annotation(external_id="m2c1d1fa-629d-4520-a3d2-955b4f2582c0", title="titre3", text="texte3", color="#CCCCCC", uri="http://blabla", creator="wakimd") |
|
|
85 |
self.annotation3.save() |
|
|
86 |
self.c = Client() |
|
|
87 |
def tearDown(self): |
|
|
88 |
annotlist=Annotation.objects.all() |
|
|
89 |
for annot in annotlist: |
|
|
90 |
annot.delete() |
|
|
91 |
|
|
|
92 |
def test_filter_annotation_creator_limit(self): |
|
|
93 |
user = 'wakimd' |
|
|
94 |
uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168" |
|
|
95 |
limit= 1 |
|
|
96 |
response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'creator':user,'limit':limit}) |
|
|
97 |
doc = lxml.etree.fromstring(response.content) |
|
|
98 |
cpt = 0 |
|
|
99 |
for elem in doc.xpath("/iri/text-annotation"): |
|
|
100 |
cpt = cpt + 1 |
|
|
101 |
if limit is not None: |
|
|
102 |
self.assertEqual(cpt,limit) |
|
|
103 |
for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"): |
|
|
104 |
self.assertEqual(elem,user) |
|
|
105 |
for elem in doc.xpath("/iri/text-annotation/uri/text()"): |
|
|
106 |
self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml") |
|
|
107 |
|
|
|
108 |
def test_filter_annotation_uri(self): |
|
|
109 |
uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168" |
|
|
110 |
response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri}) |
|
|
111 |
doc = lxml.etree.fromstring(response.content) |
|
|
112 |
for elem in doc.xpath("/iri/text-annotation/uri/text()"): |
|
|
113 |
self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml") |
|
|
114 |
|
|
|
115 |
def test_filter_annotation_filter(self): |
|
|
116 |
uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168" |
|
|
117 |
filter = 'lors' |
|
|
118 |
limit = None |
|
|
119 |
response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'filter':'lors'}) |
|
|
120 |
doc = lxml.etree.fromstring(response.content) |
|
|
121 |
for elem in doc.xpath("/iri/text-annotation/content/text/text()"): |
|
|
122 |
self.assertTrue('lors' in elem) |
|
|
123 |
for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"): |
|
|
124 |
self.assertEqual(elem,user) |
|
|
125 |
|
|
|
126 |
|
|
|
127 |
# This test creates an annotation, then deletes it, and checks that: |
|
|
128 |
# 1. the annotation doesn't exist anymore in the database (by trying to access it through a 'get') |
|
|
129 |
# 2. the returned xml contains no data |
|
|
130 |
class DeleteTest(unittest.TestCase): |
|
|
131 |
def setUp(self): |
|
|
132 |
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") |
|
|
133 |
self.annotation.save() |
|
|
134 |
self.c = Client() |
|
|
135 |
def tearDown(self): |
|
|
136 |
annotlist=Annotation.objects.all() |
|
|
137 |
for annot in annotlist: |
|
|
138 |
annot.delete() |
|
|
139 |
|
|
|
140 |
def test_delete_annotation(self): |
|
16
|
141 |
id = urllib.urlencode({'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) |
|
|
142 |
f = urllib.urlopen("http://127.0.0.1:8000/api/1.0/text/delete/", id) |
|
9
|
143 |
response = self.c.post('/api/'+ VERSION_STR +'/text/delete/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) |
|
10
|
144 |
response2 = self.c.get('/ldt/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) |
|
9
|
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()"),[]) |
|
10
|
150 |
self.assertEqual(response2.status_code, 404) |
|
9
|
151 |
|
|
10
|
152 |
def test_error_delete(self): |
|
|
153 |
response = self.c.post('/api/'+ VERSION_STR +'/text/ldt/delete/', {'id':'1'}) |
|
|
154 |
self.assertEqual(response.status_code,404) |
|
9
|
155 |
|
|
|
156 |
|
|
|
157 |
# This test creates an annotation, then updates it with new content, and checks that the returned xml contains the updated data |
|
|
158 |
class UpdateTest(unittest.TestCase): |
|
|
159 |
def setUp(self): |
|
|
160 |
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") |
|
|
161 |
self.annotation.save() |
|
|
162 |
self.c = Client() |
|
|
163 |
def tearDown(self): |
|
|
164 |
annotlist=Annotation.objects.all() |
|
|
165 |
for annot in annotlist: |
|
|
166 |
annot.delete() |
|
|
167 |
|
|
|
168 |
def test_update_annotation(self): |
|
16
|
169 |
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>' |
|
|
170 |
response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) |
|
9
|
171 |
doc = lxml.etree.fromstring(response.content) |
|
16
|
172 |
#self.assertEqual(lxml.etree.tostring(doc), " ") |
|
|
173 |
self.assertEqual(doc.xpath("/iri/text-annotation/id/text()")[0],"d2c1d1fa-629d-4520-a3d2-955b4f2582c0") |
|
9
|
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 |
|
|
10
|
177 |
def test_error_update(self): |
|
16
|
178 |
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>' |
|
10
|
179 |
response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'1'}) |
|
|
180 |
self.assertEqual(response.status_code,404) |
|
|
181 |
|
|
9
|
182 |
|
|
16
|
183 |
class OnServerGlobalTest(unittest.TestCase): |
|
|
184 |
def setUp(self): |
|
|
185 |
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>'}) |
|
|
186 |
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>'}) |
|
|
187 |
self.id = urllib.urlencode({"id":"mypersonnalid"}) |
|
|
188 |
self.id2 = urllib.urlencode({"id":"mypersonnalid2"}) |
|
|
189 |
self.uri = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"}) |
|
|
190 |
self.filt1 = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168", "creator":"","limit":"","filter":""}) |
|
|
191 |
self.filt2 = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168","creator":"wakimd","limit":"","filter":""}) |
|
|
192 |
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'}) |
|
|
193 |
|
|
|
194 |
def test_everything(self): |
|
|
195 |
creation = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/create/", self.content) |
|
|
196 |
creation2 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/create/", self.content2) |
|
|
197 |
|
|
|
198 |
get = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/get/?%s" % self.id) |
|
|
199 |
|
|
|
200 |
update = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/update/", self.up) |
|
|
201 |
|
|
|
202 |
filt1 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/filter/?%s", self.uri) |
|
|
203 |
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") |
|
|
204 |
tmp = open('debug.html','r+') |
|
|
205 |
tmp.write(filt2.read()) |
|
|
206 |
|
|
|
207 |
delete = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/delete/", self.id) |
|
|
208 |
delete = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/delete/", self.id2) |
|
|
209 |
|
|
|
210 |
|