diff -r 5bb249eefdd1 -r 22ab430e9b64 web/ldt/ldt_utils/tests.py
--- a/web/ldt/ldt_utils/tests.py Tue Nov 16 12:20:59 2010 +0100
+++ b/web/ldt/ldt_utils/tests.py Tue Nov 16 14:15:07 2010 +0100
@@ -1,190 +1,179 @@
-#encoding:UTF-8
+"""
+This file demonstrates two different styles of tests (one doctest and one
+unittest). These will both pass when you run "manage.py test".
-""" Run these tests with 'python manage.py test ldt_utils' """
+Replace these with more appropriate tests for your application.
+"""
from django.test import TestCase
import unittest
import lxml.etree
-from ldt.ldt_utils.models import *
+from models import Project, Content
from ldt.core.models import Owner
-from views import *
+from utils import LdtUtils, LdtSearch, create_ldt, create_empty_iri, copy_ldt
import base64
import uuid
import tempfile
-import datetime
from django.contrib.auth.models import *
+from views import get_attrib
from django.conf import settings
-from django.test.client import Client
-from ldt.ldt_utils import VERSION_STR
+
+
+
+class SimpleTest(TestCase):
+ def test_basic_addition(self):
+ """
+ Tests that 1 + 1 always equals 2.
+ """
+ self.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+
+>>> 1 + 1 == 2
+True
+"""}
-# This test creates an annotation and checks that:
-# 1. the annotation was created in the database (by trying to access it through a 'get')
-# 2. the returned xml contains correct data
-class CreateTest(unittest.TestCase):
+class UtilsTest(unittest.TestCase):
def setUp(self):
- self.content = base64.urlsafe_b64encode('f2c1d1fa-629d-4520-a3d2-955b4f2582c0 http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168 tag1 tag2 #AAAAAA oaubert 79cd0532-1dda-4130-b351-6a181130a7c9 2010-09-06 12:33:53.417550 oaubert 79cd0532-1dda-4130-b351-6a181130a7c9 2010-09-06 12:33:53.420459 ')
- self.c = Client()
- 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')
- self.annot.save()
+ self.user = Owner()
+ self.user.username = "toto"
+ self.LU = LdtUtils()
+
+ self.project = Project(title="titleproj1", owner=self.user)
+ self.project.ldt = ' CA: prof et admin conseil de classe Reprise de la figure precedente TC: prof et admin Conseil de classe Argumentation conseil de classe Reprise de la figure precedente Bout a bout 1 '
+ self.project.id = "11"
+ self.project.ldt_id = str(uuid.uuid1())
+ self.project.save()
+
+ self.projectcopy = Project(title="the2ndproject")
+ self.projectcopy.id="22"
+
def tearDown(self):
- annotlist=Annotation.objects.all()
- for annot in annotlist:
- annot.delete()
+ self.project.delete()
+ self.projectcopy.delete()
+ #self.cont1.delete()
+ #self.cont2.delete()
+
+ def test_generate_ldt(self):
+ self.cont1 = Content(iriurl="id1/iriurl1")
+ self.cont1.iri_id = "id1"
+ self.cont1.save()
+
+ self.cont2 = Content(iriurl="id2/iriurl2")
+ self.cont2.iri_id = "id2"
+ self.cont2.save()
+
+ self.project.contents.add(self.cont1,self.cont2)
+
+ f=tempfile.TemporaryFile(mode='r+')
+ self.LU.generateLdt(Content.objects.all(),f)
+ f.seek(0)
+ ldoc = lxml.etree.parse(f)
+ self.assertEqual(ldoc.xpath("/iri/displays/display/content")[9].get("id"),self.cont2.iri_id)
+ self.assertEqual(ldoc.xpath("/iri/medias/media")[8].get("id"), self.cont1.iri_id)
+ f.close()
+
+ def test_generate_init(self):
+ self.cont3 = Content(iriurl="id3/iriurl1")
+ self.cont3.iri_id = "id3"
+ self.cont3.save()
+
+ self.cont4 = Content(iriurl="id4/iriurl2")
+ self.cont4.iri_id = "id4"
+ self.cont4.save()
+
+ self.project.contents.add(self.cont3,self.cont4)
+ ldoc = self.LU.generateInit(None,None)
+ self.assertEqual(ldoc.xpath("/iri/files/init")[0].tag, "init")
+ self.assertEqual(ldoc.xpath("/iri/files/library")[0].tag, "library")
+ self.assertEqual(ldoc.xpath("/iri/files/init/file")[0].get("video"), settings.STREAM_URL)
- def test_create_annotation(self):
- response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':self.content})
- self.annot1 = lxml.etree.fromstring(response.content)
- self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],"f2c1d1fa-629d-4520-a3d2-955b4f2582c0")
- self.assertEqual(self.annot1.xpath("/iri/text-annotation/content")[0].tag,"content")
- self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[0],"tag1")
- self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/text/text()")[0],u"texte selectionne lors de la creation de l\'annotation")
- self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()")[0],"2010-09-06 12:33:53.417550")
- annot = Annotation.objects.get(id="f2c1d1fa-629d-4520-a3d2-955b4f2582c0")
- self.assertEqual(annot.uri, "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168")
+ def test_create_ldt(self):
+ self.cont5 = Content(iriurl="id5/iriurl1")
+ self.cont5.iri_id = "id5"
+ self.cont5.save()
+
+ self.cont6 = Content(iriurl="id6/iriurl2")
+ self.cont6.iri_id = "id6"
+ self.cont6.save()
+
+ self.project.contents.add(self.cont5,self.cont6)
+ self.project.ldt=""
+ create_ldt(self.project, self.user)
+ ldt = lxml.etree.fromstring(self.project.ldt)
+ self.assertEqual(ldt.xpath("/iri")[0].tag,"iri")
+ self.assertEqual(ldt.xpath("/iri/project")[0].get("title"), self.project.title)
+ self.assertEqual(ldt.xpath("/iri/medias/media")[0].get("src"), self.cont5.iri_url())
+ self.assertEqual(ldt.xpath("/iri/medias/media")[1].get("id"), self.cont6.iri_id)
+
+ def test_copy_ldt(self):
+ self.cont7 = Content(iriurl="id7/iriurl1")
+ self.cont7.iri_id = "id7"
+ self.cont7.save()
+
+ self.cont8 = Content(iriurl="id8/iriurl2")
+ self.cont8.iri_id = "id8"
+ self.cont8.save()
-# def test_error_create(self):
-# content = base64.urlsafe_b64encode('d2c1d1fa-629d-4520-a3d2-955b4f2582c0 http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168 tag1 tag2 #AAAAAA oaubert 79cd0532-1dda-4130-b351-6a181130a7c9 2010-09-06 12:33:53.417550 oaubert 79cd0532-1dda-4130-b351-6a181130a7c9 2010-09-06 12:33:53.420459 ')
-# response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':content})
-# #annot2 = create_annotation(content)
-# self.assertEqual(response.status_code, '409')
+ self.project.contents.add(self.cont7,self.cont8)
+ copy_ldt(self.project, self.projectcopy, self.user)
+ ldt1=lxml.etree.fromstring(self.project.ldt)
+ ldt2=lxml.etree.fromstring(self.projectcopy.ldt)
+ self.assertTrue(ldt1.xpath("/iri/project")[0].get("id")!= ldt2.xpath("/iri/project")[0].get("id"))
+ self.assertEqual(ldt1.xpath("/iri/medias/media")[0].get("id"),ldt2.xpath("/iri/medias/media")[0].get("id"))
+ self.assertEqual(ldt1.xpath("/iri/annotations/content/ensemble")[0].get("title"),ldt2.xpath("/iri/annotations/content/ensemble")[0].get("title"))
+ self.assertEqual(ldt1.xpath("/iri/annotations/content/ensemble/decoupage")[0].get("id"),ldt2.xpath("/iri/annotations/content/ensemble/decoupage")[0].get("id"))
+ self.assertEqual(ldt1.xpath("/iri/annotations/content/ensemble/decoupage/title")[1].text,ldt2.xpath("/iri/annotations/content/ensemble/decoupage/title")[1].text.strip("\n\t"))
+
+ def test_create_empty_iri(self):
+ self.cont9 = Content(iriurl="id9/iriurl1")
+ self.cont9.iri_id = "id9"
+ self.cont9.save()
+
+ self.cont10 = Content(iriurl="id10/iriurl2")
+ self.cont10.iri_id = "id10"
+ self.cont10.save()
+
+ self.project.contents.add(self.cont9,self.cont10)
+ tmp = tempfile.TemporaryFile(mode='r+')
+ create_empty_iri(tmp, self.cont9, "admin")
+ tmp.seek(0)
+ ldoc = lxml.etree.parse(tmp)
+ self.assertEqual(ldoc.xpath("/iri/head/meta")[0].get("content"), self.cont9.iri_id)
+ self.assertEqual(ldoc.xpath("/iri/body/medias/media/video")[0].get("id"), self.cont9.iri_id)
+ tmp.close()
+
-# This test creates an annotation, then gets it, and checks that the returned xml contains correct data
-class GetTest(unittest.TestCase):
+
+class ViewsTest(unittest.TestCase):
def setUp(self):
- 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")
- self.annotation.save()
- self.c = Client()
- def tearDown(self):
- annotlist=Annotation.objects.all()
- for annot in annotlist:
- annot.delete()
-
- def test_get_annotation(self):
- response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
- self.annot1 = lxml.etree.fromstring(response.content)
- #self.annot1 = get_annotation("d2c1d1fa-629d-4520-a3d2-955b4f2582c0")
- self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],self.annotation.id)
- self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[1], self.annotation.tags[1])
- self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/color/text()")[0],self.annotation.color)
- #self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()"), self.annotation.creation_date)
-
-# def test_error_get(self):
-# response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'2'})
-# #response = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'2'})
-# #annot3 = get_annotation('d2c1d1fa-629d-4520-a3d2-955b4f2582c0')
-# #resp = response.status_code
-# self.assertEqual(response.status_code,'404')
-
-
-class FilterTest(unittest.TestCase):
- def setUp(self):
- 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")
- self.annotation.save()
- 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")
- self.annotation2.save()
- self.annotation3 = Annotation(id="m2c1d1fa-629d-4520-a3d2-955b4f2582c0", title="titre3", text="texte3", color="#CCCCCC", uri="http://blabla", creator="wakimd")
- self.annotation3.save()
- self.c = Client()
- def tearDown(self):
- annotlist=Annotation.objects.all()
- for annot in annotlist:
- annot.delete()
-
- def test_filter_annotation_creator_limit(self):
- user = 'wakimd'
- uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
- limit= 1
- response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'creator':user,'limit':limit})
- doc = lxml.etree.fromstring(response.content)
- #doc = filter_annotation(uri,None,limit,user)
- cpt = 0
- for elem in doc.xpath("/iri/text-annotation"):
- cpt = cpt + 1
- if limit is not None:
- self.assertEqual(cpt,limit)
- for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"):
- self.assertEqual(elem,user)
- for elem in doc.xpath("/iri/text-annotation/uri/text()"):
- self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml")
+ self.project = Project()
+ self.project.id = "121"
+ self.project.save()
+ self.project.ldt = ' CA: prof et admin conseil de classe Reprise de la figure precedente TC: prof et admin Conseil de classe Argumentation conseil de classe Reprise de la figure precedente Bout a bout 1 '
+
+ self.cont1 = Content(iriurl="/laurentcantet_entrelesmurs/iriurl1")
+ self.cont1.iri_id = 'laurentcantet_entrelesmurs'
+ self.cont1.save()
+
+ self.cont2 = Content(iriurl="/content_notinldt/iriurl2")
+ self.cont2.iri_id = 'content_notinldt'
+ self.cont2.save()
- def test_filter_annotation_uri(self):
- uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
- response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri})
- doc = lxml.etree.fromstring(response.content)
- #doc = filter_annotation(uri,None,limit,None)
- for elem in doc.xpath("/iri/text-annotation/uri/text()"):
- self.assertEqual(elem[:57],"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml")
-
- def test_filter_annotation_filter(self):
- uri = "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"
- filter = 'lors'
- limit = None
- response = self.c.get('/api/'+ VERSION_STR +'/text/filter/', {'uri':uri,'filter':'lors'})
- doc = lxml.etree.fromstring(response.content)
- #doc = filter_annotation(uri,filter,limit,user)
- for elem in doc.xpath("/iri/text-annotation/content/text/text()"):
- self.assertTrue('lors' in elem)
- for elem in doc.xpath("/iri/text-annotation/meta/creator/text()"):
- self.assertEqual(elem,user)
+ self.project.contents.add(self.cont1, self.cont2)
-
-# This test creates an annotation, then deletes it, and checks that:
-# 1. the annotation doesn't exist anymore in the database (by trying to access it through a 'get')
-# 2. the returned xml contains no data
-class DeleteTest(unittest.TestCase):
- def setUp(self):
- 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")
- self.annotation.save()
- self.c = Client()
def tearDown(self):
- annotlist=Annotation.objects.all()
- for annot in annotlist:
- annot.delete()
-
- def test_delete_annotation(self):
- response = self.c.post('/api/'+ VERSION_STR +'/text/delete/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
- #response2 = self.c.get('/ldt/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
- doc = lxml.etree.fromstring(response.content)
- #doc=delete_annotation("d2c1d1fa-629d-4520-a3d2-955b4f2582c0")
- self.assertEqual(doc.xpath("/iri/text-annotation/id/text()"),[])
- self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()"), [])
- self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()"),[])
- self.assertEqual(doc.xpath("/iri/text-annotation/meta/creator/text()"),[])
- #self.assertEqual(response2.status_code, '404')
-
-# def test_error_delete(self):
-# response = self.c.post('/api/'+ VERSION_STR +'/text/ldt/delete/', {'id':'1'})
-# #annot4 = delete_annotation('f2c1d1fa-629d-4520-a3d2-955b4f2582c0')
-# self.assertEqual(response.status_code,'404')
-
+ self.project.delete()
+ ##self.cont1.delete()
+ ##self.cont2.delete()
-# This test creates an annotation, then updates it with new content, and checks that the returned xml contains the updated data
-class UpdateTest(unittest.TestCase):
- def setUp(self):
- 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")
- self.annotation.save()
- self.c = Client()
- def tearDown(self):
- annotlist=Annotation.objects.all()
- for annot in annotlist:
- annot.delete()
-
- def test_update_annotation(self):
- content = base64.urlsafe_b64encode('tag1 tag2new tag3 #DDDDDD oaubert 80cd0532-1dda-4130-b351-6a181130a7c9 2010-11-06 12:33:53.420459 ')
- response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
- doc = lxml.etree.fromstring(response.content)
- #doc = update_annotation(content,'d2c1d1fa-629d-4520-a3d2-955b4f2582c0')
- self.assertEqual(doc.xpath("/iri/text-annotation/id/text()")[0],"d2c1d1fa-629d-4520-a3d2-955b4f2582c0")
- self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()")[1], "mytag")
- self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()")[0],"#DDDDDD")
-
-# def test_error_update(self):
-# content = base64.urlsafe_b64encode('d2c1d1fa-629d-4520-a3d2-955b4f2582c0 http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168 tag1 tag2 #AAAAAA oaubert 79cd0532-1dda-4130-b351-6a181130a7c9 2010-09-06 12:33:53.417550 oaubert 79cd0532-1dda-4130-b351-6a181130a7c9 2010-09-06 12:33:53.420459 ')
-# response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'1'})
-# #annot5=update_annotation()
-# self.assertEqual(response.status_code,'404')
-#
-
+# def test_get_attrib(self):
+# get_attrib(self.project)
+# ldoc = lxml.etree.fromstring(self.project.ldt)
+# self.assertEqual(self.project.title, ldoc.xpath("/iri/project")[0].get("title"))
+# self.assertEqual(ldoc.xpath("/iri/medias/media")[0].get('id'), self.cont1.iri_id)
+# self.assertTrue(self.cont2.iri_id not in self.project.contents.all())