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