# HG changeset patch # User Sylvain Thénault # Date 1250859984 -7200 # Node ID 7c80f9631f8b8927b6bb1617f286da08a907a9ed # Parent 2daabf4c646e147e58e359533de0a4f92210814f# Parent e5bdf98be37fc897df05aece6225e871aac0dc61 merge diff -r 2daabf4c646e -r 7c80f9631f8b doc/book/en/development/testing/index.rst --- a/doc/book/en/development/testing/index.rst Fri Aug 21 14:57:36 2009 +0200 +++ b/doc/book/en/development/testing/index.rst Fri Aug 21 15:06:24 2009 +0200 @@ -16,14 +16,48 @@ * `EnvBasedTC`, to simulate a complete environment (web + repository) * `RepositoryBasedTC`, to simulate a repository environment only -Thos two classes almost have the same interface and offers numerous methods to -write tests rapidely and efficiently. +Those two classes almost have the same interface and offer numerous +methods to write tests rapidly and efficiently. XXX FILLME describe API In most of the cases, you will inherit `EnvBasedTC` to write Unittest or functional tests for your entities, views, hooks, etc... +Managing connections or users ++++++++++++++++++++++++++++++ + +Since unit tests are done with the SQLITE backend and this does not +support multiple connections at a time, you must be careful when +simulating security, changing users. + +By default, tests run with a user with admin privileges. This +user/connection must never be closed. +qwq +Before a self.login, one has to release the connection pool in use with a self.commit, self.rollback or self.close. + +When one is logged in as a normal user and wants to switch back to the admin user, one has to use self.restore_connection(). + +Usually it looks like this: + +.. sourcecode:: python + + # execute using default admin connection + self.execute(...) + # I want to login with another user, ensure to free admin connection pool + # (could have used rollback but not close here, we should never close defaut admin connection) + self.commit() + cnx = self.login('user') + # execute using user connection + self.execute(...) + # I want to login with another user or with admin user + self.commit(); cnx.close() + # restore admin connection, never use cnx = self.login('admin'), it will return + # the default admin connection and one may be tempted to close it + self.restore_connection() + +Take care of the references kept to the entities created with a connection or the other. + Email notifications tests ------------------------- diff -r 2daabf4c646e -r 7c80f9631f8b web/test/data/sample1.pdf Binary file web/test/data/sample1.pdf has changed diff -r 2daabf4c646e -r 7c80f9631f8b web/test/data/sample1.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/data/sample1.xml Fri Aug 21 15:06:24 2009 +0200 @@ -0,0 +1,138 @@ + + + + + ] > + + + + + + + +Comet 0.2.0 (unset title) + + + + + + + + + + + + + + + + + + + + + + +
+
+
+ + +
+
+ +
+
+ + \ No newline at end of file diff -r 2daabf4c646e -r 7c80f9631f8b web/test/unittest_pdf.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/unittest_pdf.py Fri Aug 21 15:06:24 2009 +0200 @@ -0,0 +1,34 @@ +from unittest import TestCase +import os.path as osp +from xml.etree.cElementTree import ElementTree, fromstring, tostring, dump + +from tempfile import NamedTemporaryFile +from subprocess import Popen as sub + +from cubicweb.utils import can_do_pdf_conversion + +from cubicweb.web.xhtml2fo import ReportTransformer + +DATADIR = osp.join(osp.dirname(__file__), 'data') + +class PDFTC(TestCase): + + def test_xhtml_to_fop_to_pdf(self): + if not can_do_pdf_conversion(): + self.skip('dependencies not available : check pysixt and fop') + xmltree = ElementTree() + xmltree.parse(osp.join(DATADIR, 'sample1.xml')) + foptree = ReportTransformer(u'contentmain').transform(xmltree) + # next + foptmp = NamedTemporaryFile() + foptree.write(foptmp) + foptmp.flush() + pdftmp = NamedTemporaryFile() + fopproc = sub(['/usr/bin/fop', foptmp.name, pdftmp.name]) + fopproc.wait() + del foptmp + pdftmp.seek(0) # a bit superstitious + reference = open(osp.join(DATADIR, 'sample1.pdf'), 'r').read() + output = pdftmp.read() + # XXX almost equals due to ID, creation date, so it seems to fail + self.assertTextEquals(output, reference)