13 import tempfile |
13 import tempfile |
14 import datetime |
14 import datetime |
15 from django.contrib.auth.models import * |
15 from django.contrib.auth.models import * |
16 from django.conf import settings |
16 from django.conf import settings |
17 from django.test.client import Client |
17 from django.test.client import Client |
|
18 from ldt.ldt_utils import VERSION_STR |
18 |
19 |
19 |
20 |
20 |
21 |
21 # This test creates an annotation and checks that: |
22 # 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 # 1. the annotation was created in the database (by trying to access it through a 'get') |
31 annotlist=Annotation.objects.all() |
32 annotlist=Annotation.objects.all() |
32 for annot in annotlist: |
33 for annot in annotlist: |
33 annot.delete() |
34 annot.delete() |
34 |
35 |
35 def test_create_annotation(self): |
36 def test_create_annotation(self): |
36 response = self.c.post('/ldt/create/', {'content':self.content}) |
37 response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':self.content}) |
37 self.annot1 = lxml.etree.fromstring(response.content) |
38 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/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/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/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/content/text/text()")[0],u"texte selectionne lors de la creation de l\'annotation") |
43 annot = Annotation.objects.get(id="f2c1d1fa-629d-4520-a3d2-955b4f2582c0") |
44 annot = Annotation.objects.get(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 self.assertEqual(annot.uri, "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168") |
45 |
46 |
46 # def test_error_create(self): |
47 # 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 # 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('/ldt/create/', {'content':content}) |
49 # response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':content}) |
49 # #annot2 = create_annotation(content) |
50 # #annot2 = create_annotation(content) |
50 # self.assertEqual(response.status_code, '409') |
51 # self.assertEqual(response.status_code, '409') |
51 |
52 |
52 |
53 |
53 # This test creates an annotation, then gets it, and checks that the returned xml contains correct data |
54 # This test creates an annotation, then gets it, and checks that the returned xml contains correct data |
60 annotlist=Annotation.objects.all() |
61 annotlist=Annotation.objects.all() |
61 for annot in annotlist: |
62 for annot in annotlist: |
62 annot.delete() |
63 annot.delete() |
63 |
64 |
64 def test_get_annotation(self): |
65 def test_get_annotation(self): |
65 response = self.c.get('/ldt/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) |
66 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.annot1 = lxml.etree.fromstring(response.content) |
67 #self.annot1 = get_annotation("d2c1d1fa-629d-4520-a3d2-955b4f2582c0") |
68 #self.annot1 = get_annotation("d2c1d1fa-629d-4520-a3d2-955b4f2582c0") |
68 self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],self.annotation.id) |
69 self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],self.annotation.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/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/content/color/text()")[0],self.annotation.color) |
71 #self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()"), self.annotation.creation_date) |
72 #self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()"), self.annotation.creation_date) |
72 |
73 |
73 # def test_error_get(self): |
74 # def test_error_get(self): |
74 # response = self.c.get('/ldt/get/', {'id':'2'}) |
75 # response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'2'}) |
75 # #response = self.c.get('/ldt/get/', {'id':'2'}) |
76 # #response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'2'}) |
76 # #annot3 = get_annotation('d2c1d1fa-629d-4520-a3d2-955b4f2582c0') |
77 # #annot3 = get_annotation('d2c1d1fa-629d-4520-a3d2-955b4f2582c0') |
77 # #resp = response.status_code |
78 # #resp = response.status_code |
78 # self.assertEqual(response.status_code,'404') |
79 # self.assertEqual(response.status_code,'404') |
79 |
80 |
80 |
81 |
94 |
95 |
95 def test_filter_annotation_creator_limit(self): |
96 def test_filter_annotation_creator_limit(self): |
96 user = 'wakimd' |
97 user = 'wakimd' |
97 uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168" |
98 uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168" |
98 limit= 1 |
99 limit= 1 |
99 response = self.c.get('/ldt/filter/', {'uri':uri,'creator':user,'limit':limit}) |
100 response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'creator':user,'limit':limit}) |
100 doc = lxml.etree.fromstring(response.content) |
101 doc = lxml.etree.fromstring(response.content) |
101 #doc = filter_annotation(uri,None,limit,user) |
102 #doc = filter_annotation(uri,None,limit,user) |
102 cpt = 0 |
103 cpt = 0 |
103 for elem in doc.xpath("/iri/text-annotation"): |
104 for elem in doc.xpath("/iri/text-annotation"): |
104 cpt = cpt + 1 |
105 cpt = cpt + 1 |
109 for elem in doc.xpath("/iri/text-annotation/uri/text()"): |
110 for elem in doc.xpath("/iri/text-annotation/uri/text()"): |
110 self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml") |
111 self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml") |
111 |
112 |
112 def test_filter_annotation_uri(self): |
113 def test_filter_annotation_uri(self): |
113 uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168" |
114 uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168" |
114 response = self.c.get('/ldt/filter/', {'uri':uri}) |
115 response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri}) |
115 doc = lxml.etree.fromstring(response.content) |
116 doc = lxml.etree.fromstring(response.content) |
116 #doc = filter_annotation(uri,None,limit,None) |
117 #doc = filter_annotation(uri,None,limit,None) |
117 for elem in doc.xpath("/iri/text-annotation/uri/text()"): |
118 for elem in doc.xpath("/iri/text-annotation/uri/text()"): |
118 self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml") |
119 self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml") |
119 |
120 |
120 def test_filter_annotation_filter(self): |
121 def test_filter_annotation_filter(self): |
121 uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168" |
122 uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168" |
122 filter = 'lors' |
123 filter = 'lors' |
123 limit = None |
124 limit = None |
124 response = self.c.get('/ldt/filter/', {'uri':uri,'filter':'lors'}) |
125 response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'filter':'lors'}) |
125 doc = lxml.etree.fromstring(response.content) |
126 doc = lxml.etree.fromstring(response.content) |
126 #doc = filter_annotation(uri,filter,limit,user) |
127 #doc = filter_annotation(uri,filter,limit,user) |
127 for elem in doc.xpath("/iri/text-annotation/content/text/text()"): |
128 for elem in doc.xpath("/iri/text-annotation/content/text/text()"): |
128 self.assertTrue('lors' in elem) |
129 self.assertTrue('lors' in elem) |
129 for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"): |
130 for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"): |
142 annotlist=Annotation.objects.all() |
143 annotlist=Annotation.objects.all() |
143 for annot in annotlist: |
144 for annot in annotlist: |
144 annot.delete() |
145 annot.delete() |
145 |
146 |
146 def test_delete_annotation(self): |
147 def test_delete_annotation(self): |
147 response = self.c.post('/ldt/delete/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) |
148 response = self.c.post('/api/'+ VERSION_STR +'/text/delete/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) |
148 #response2 = self.c.get('/ldt/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) |
149 #response2 = self.c.get('/ldt/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) |
149 doc = lxml.etree.fromstring(response.content) |
150 doc = lxml.etree.fromstring(response.content) |
150 #doc=delete_annotation("d2c1d1fa-629d-4520-a3d2-955b4f2582c0") |
151 #doc=delete_annotation("d2c1d1fa-629d-4520-a3d2-955b4f2582c0") |
151 self.assertEqual(doc.xpath("/iri/text-annotation/id/text()"),[]) |
152 self.assertEqual(doc.xpath("/iri/text-annotation/id/text()"),[]) |
152 self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()"), []) |
153 self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()"), []) |
153 self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()"),[]) |
154 self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()"),[]) |
154 self.assertEqual(doc.xpath("/iri/text-annotation/meta/creator/text()"),[]) |
155 self.assertEqual(doc.xpath("/iri/text-annotation/meta/creator/text()"),[]) |
155 #self.assertEqual(response2.status_code, '404') |
156 #self.assertEqual(response2.status_code, '404') |
156 |
157 |
157 # def test_error_delete(self): |
158 # def test_error_delete(self): |
158 # response = self.c.post('/ldt/delete/', {'id':'1'}) |
159 # response = self.c.post('/api/'+ VERSION_STR +'/text/ldt/delete/', {'id':'1'}) |
159 # #annot4 = delete_annotation('f2c1d1fa-629d-4520-a3d2-955b4f2582c0') |
160 # #annot4 = delete_annotation('f2c1d1fa-629d-4520-a3d2-955b4f2582c0') |
160 # self.assertEqual(response.status_code,'404') |
161 # self.assertEqual(response.status_code,'404') |
161 |
162 |
162 |
163 |
163 # This test creates an annotation, then updates it with new content, and checks that the returned xml contains the updated data |
164 # This test creates an annotation, then updates it with new content, and checks that the returned xml contains the updated data |
171 for annot in annotlist: |
172 for annot in annotlist: |
172 annot.delete() |
173 annot.delete() |
173 |
174 |
174 def test_update_annotation(self): |
175 def test_update_annotation(self): |
175 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>') |
176 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>') |
176 response = self.c.post('/ldt/update/', {'content':content,'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) |
177 response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) |
177 doc = lxml.etree.fromstring(response.content) |
178 doc = lxml.etree.fromstring(response.content) |
178 #doc = update_annotation(content,'d2c1d1fa-629d-4520-a3d2-955b4f2582c0') |
179 #doc = update_annotation(content,'d2c1d1fa-629d-4520-a3d2-955b4f2582c0') |
179 self.assertEqual(doc.xpath("/iri/text-annotation/id/text()")[0],"d2c1d1fa-629d-4520-a3d2-955b4f2582c0") |
180 self.assertEqual(doc.xpath("/iri/text-annotation/id/text()")[0],"d2c1d1fa-629d-4520-a3d2-955b4f2582c0") |
180 self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()")[1], "mytag") |
181 self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()")[1], "mytag") |
181 self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()")[0],"#DDDDDD") |
182 self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()")[0],"#DDDDDD") |
182 |
183 |
183 # def test_error_update(self): |
184 # def test_error_update(self): |
184 # 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>') |
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>') |
185 # response = self.c.post('/update/', {'content':content,'id':'1'}) |
186 # response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'1'}) |
186 # #annot5=update_annotation() |
187 # #annot5=update_annotation() |
187 # self.assertEqual(response.status_code,'404') |
188 # self.assertEqual(response.status_code,'404') |
188 # |
189 # |
189 |
190 |