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