doc/book/devrepo/testing.rst
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 06 Jun 2016 21:17:33 +0200
changeset 11347 b4dcfd734686
parent 10829 550c2d27339f
child 12792 e2cdb1be6bd9
permissions -rw-r--r--
[server] some pep8 in rql2sql
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
.. -*- coding: utf-8 -*-
5266
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
     2
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
Tests
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     4
=====
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
Unit tests
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
----------
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     8
6301
d9d6bdd814ba imported patch doc_test_commit.diff
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6243
diff changeset
     9
The *CubicWeb* framework provides the
d9d6bdd814ba imported patch doc_test_commit.diff
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6243
diff changeset
    10
:class:`cubicweb.devtools.testlib.CubicWebTC` test base class .
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    11
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    12
Tests shall be put into the mycube/test directory. Additional test
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    13
data shall go into mycube/test/data.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    14
6301
d9d6bdd814ba imported patch doc_test_commit.diff
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6243
diff changeset
    15
It is much advised to write tests concerning entities methods,
d9d6bdd814ba imported patch doc_test_commit.diff
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6243
diff changeset
    16
actions, hooks and operations, security. The
d9d6bdd814ba imported patch doc_test_commit.diff
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6243
diff changeset
    17
:class:`~cubicweb.devtools.testlib.CubicWebTC` base class has
d9d6bdd814ba imported patch doc_test_commit.diff
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6243
diff changeset
    18
convenience methods to help test all of this.
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    19
6301
d9d6bdd814ba imported patch doc_test_commit.diff
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6243
diff changeset
    20
In the realm of views, automatic tests check that views are valid
9257
ce338133c92c remove cw 3.9 bw compat (bw compat other than the interface -> adapter changes)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8483
diff changeset
    21
XHTML. See :ref:`automatic_views_tests` for details.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    22
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    23
Most unit tests need a live database to work against. This is achieved
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    24
by CubicWeb using automatically sqlite (bundled with Python, see
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    25
http://docs.python.org/library/sqlite3.html) as a backend.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    26
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    27
The database is stored in the mycube/test/tmpdb,
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
    28
mycube/test/tmpdb-template files. If it does not (yet) exist, it will
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
    29
be built automatically when the test suite starts.
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    30
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    31
.. warning::
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    32
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    33
  Whenever the schema changes (new entities, attributes, relations)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    34
  one must delete these two files. Changes concerned only with entity
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    35
  or relation type properties (constraints, cardinalities,
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    36
  permissions) and generally dealt with using the
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
    37
  `sync_schema_props_perms()` function of the migration environment do
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
    38
  not need a database regeneration step.
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    39
6344
0cb0d8d83e4c [book] add a link in hooks section to test, talk about create_entity in tests section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6301
diff changeset
    40
.. _hook_test:
0cb0d8d83e4c [book] add a link in hooks section to test, talk about create_entity in tests section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6301
diff changeset
    41
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    42
Unit test by example
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    43
````````````````````
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    44
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    45
We start with an example extracted from the keyword cube (available
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    46
from http://www.cubicweb.org/project/cubicweb-keyword).
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    47
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    48
.. sourcecode:: python
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    49
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    50
    from cubicweb.devtools.testlib import CubicWebTC
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    51
    from cubicweb import ValidationError
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    52
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    53
    class ClassificationHooksTC(CubicWebTC):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    54
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    55
        def setup_database(self):
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    56
            with self.admin_access.repo_cnx() as cnx:
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    57
                group_etype = cnx.find('CWEType', name='CWGroup').one()
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    58
                c1 = cnx.create_entity('Classification', name=u'classif1',
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    59
                                       classifies=group_etype)
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    60
                user_etype = cnx.find('CWEType', name='CWUser').one()
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    61
                c2 = cnx.create_entity('Classification', name=u'classif2',
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    62
                                       classifies=user_etype)
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    63
                self.kw1eid = cnx.create_entity('Keyword', name=u'kwgroup', included_in=c1).eid
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    64
                cnx.commit()
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    65
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    66
        def test_cannot_create_cycles(self):
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    67
            with self.admin_access.repo_cnx() as cnx:
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    68
                kw1 = cnx.entity_from_eid(self.kw1eid)
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    69
                # direct obvious cycle
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    70
                with self.assertRaises(ValidationError):
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    71
                    kw1.cw_set(subkeyword_of=kw1)
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    72
                cnx.rollback()
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    73
                # testing indirect cycles
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    74
                kw3 = cnx.execute('INSERT Keyword SK: SK name "kwgroup2", SK included_in C, '
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    75
                                  'SK subkeyword_of K WHERE C name "classif1", K eid %(k)s'
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    76
                                  {'k': kw1}).get_entity(0,0)
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    77
                kw3.cw_set(reverse_subkeyword_of=kw1)
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    78
                self.assertRaises(ValidationError, cnx.commit)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
6301
d9d6bdd814ba imported patch doc_test_commit.diff
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6243
diff changeset
    80
The test class defines a :meth:`setup_database` method which populates the
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    81
database with initial data. Each test of the class runs with this
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    82
pre-populated database.
5186
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
    83
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    84
The test case itself checks that an Operation does its job of
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    85
preventing cycles amongst Keyword entities.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    86
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    87
The `create_entity` method of connection (or request) objects allows
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
    88
to create an entity. You can link this entity to other entities, by
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    89
specifying as argument, the relation name, and the entity to link, as
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    90
value. In the above example, the `Classification` entity is linked to
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    91
a `CWEtype` via the relation `classifies`. Conversely, if you are
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    92
creating a `CWEtype` entity, you can link it to a `Classification`
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    93
entity, by adding `reverse_classifies` as argument.
6344
0cb0d8d83e4c [book] add a link in hooks section to test, talk about create_entity in tests section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6301
diff changeset
    94
6301
d9d6bdd814ba imported patch doc_test_commit.diff
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6243
diff changeset
    95
.. note::
d9d6bdd814ba imported patch doc_test_commit.diff
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6243
diff changeset
    96
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    97
   the :meth:`commit` method is not called automatically. You have to
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
    98
   call it explicitly if needed (notably to test operations). It is a
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
    99
   good practice to regenerate entities with :meth:`entity_from_eid`
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   100
   after a commit to avoid request cache effects.
6301
d9d6bdd814ba imported patch doc_test_commit.diff
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6243
diff changeset
   101
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   102
You can see an example of security tests in the
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   103
:ref:`adv_tuto_security`.
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   104
5266
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   105
It is possible to have these tests run continuously using `apycot`_.
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   106
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   107
.. _apycot: http://www.cubicweb.org/project/apycot
5186
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
   108
6592
4dad0ec0a44f [book] FAQ: reorganize content, improve NoSelectableObject section, add database
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6471
diff changeset
   109
.. _securitytest:
4dad0ec0a44f [book] FAQ: reorganize content, improve NoSelectableObject section, add database
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6471
diff changeset
   110
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   111
Managing connections or users
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   112
+++++++++++++++++++++++++++++
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   113
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   114
Since unit tests are done with the SQLITE backend and this does not
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   115
support multiple connections at a time, you must be careful when
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   116
simulating security, changing users.
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   117
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   118
By default, tests run with a user with admin privileges. Connections
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   119
using these credentials are accessible through the `admin_access` object
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   120
of the test classes.
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   121
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   122
The `repo_cnx()` method returns a connection object that can be used as a
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   123
context manager:
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   124
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   125
.. sourcecode:: python
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   126
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   127
   # admin_access is a pre-cooked session wrapping object
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   128
   # it is built with:
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   129
   # self.admin_access = self.new_access('admin')
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   130
   with self.admin_access.repo_cnx() as cnx:
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   131
       cnx.execute(...)
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   132
       self.create_user(cnx, login='user1')
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   133
       cnx.commit()
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   134
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   135
   user1access = self.new_access('user1')
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   136
   with user1access.web_request() as req:
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   137
       req.execute(...)
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   138
       req.cnx.commit()
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   139
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   140
On exit of the context manager, a rollback is issued, which releases
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   141
the connection. Don't forget to issue the `cnx.commit()` calls!
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   142
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   143
.. warning::
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   144
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   145
   Do not use references kept to the entities created with a
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   146
   connection from another one!
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   147
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   148
Email notifications tests
6065
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   149
`````````````````````````
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   150
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   151
When running tests, potentially generated e-mails are not really sent
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   152
but are found in the list `MAILBOX` of module
6301
d9d6bdd814ba imported patch doc_test_commit.diff
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6243
diff changeset
   153
:mod:`cubicweb.devtools.testlib`.
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   154
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   155
You can test your notifications by analyzing the contents of this list, which
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   156
contains objects with two attributes:
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   157
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   158
* `recipients`, the list of recipients
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   159
* `msg`, email.Message object
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   160
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   161
Let us look at a simple example from the ``blog`` cube.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   162
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   163
.. sourcecode:: python
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   164
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   165
    from cubicweb.devtools.testlib import CubicWebTC, MAILBOX
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   166
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   167
    class BlogTestsCubicWebTC(CubicWebTC):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   168
        """test blog specific behaviours"""
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   169
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   170
        def test_notifications(self):
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   171
            with self.admin_access.web_request() as req:
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   172
                cubicweb_blog = req.create_entity('Blog', title=u'cubicweb',
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   173
                                    description=u'cubicweb is beautiful')
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   174
                blog_entry_1 = req.create_entity('BlogEntry', title=u'hop',
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   175
                                                 content=u'cubicweb hop')
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   176
                blog_entry_1.cw_set(entry_of=cubicweb_blog)
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   177
                blog_entry_2 = req.create_entity('BlogEntry', title=u'yes',
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   178
                                                 content=u'cubicweb yes')
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   179
                blog_entry_2.cw_set(entry_of=cubicweb_blog)
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   180
                self.assertEqual(len(MAILBOX), 0)
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   181
                req.cnx.commit()
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   182
                self.assertEqual(len(MAILBOX), 2)
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   183
                mail = MAILBOX[0]
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   184
                self.assertEqual(mail.subject, '[data] hop')
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   185
                mail = MAILBOX[1]
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   186
                self.assertEqual(mail.subject, '[data] yes')
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   187
6243
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   188
Visible actions tests
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   189
`````````````````````
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   190
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   191
It is easy to write unit tests to test actions which are visible to
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   192
a user or to a category of users. Let's take an example in the
6243
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   193
`conference cube`_.
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   194
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   195
.. _`conference cube`: http://www.cubicweb.org/project/cubicweb-conference
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   196
.. sourcecode:: python
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   197
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   198
    class ConferenceActionsTC(CubicWebTC):
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   199
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   200
        def setup_database(self):
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   201
            with self.admin_access.repo_cnx() as cnx:
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   202
                self.confeid = cnx.create_entity('Conference',
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   203
                                                 title=u'my conf',
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   204
                                                 url_id=u'conf',
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   205
                                                 start_on=date(2010, 1, 27),
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   206
                                                 end_on = date(2010, 1, 29),
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   207
                                                 call_open=True,
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   208
                                                 reverse_is_chair_at=chair,
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   209
                                                 reverse_is_reviewer_at=reviewer).eid
6243
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   210
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   211
        def test_admin(self):
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   212
            with self.admin_access.web_request() as req:
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   213
                rset = req.find('Conference').one()
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   214
                self.assertListEqual(self.pactions(req, rset),
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   215
                                      [('workflow', workflow.WorkflowActions),
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   216
                                       ('edit', confactions.ModifyAction),
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   217
                                       ('managepermission', actions.ManagePermissionsAction),
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   218
                                       ('addrelated', actions.AddRelatedActions),
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   219
                                       ('delete', actions.DeleteAction),
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   220
                                       ('generate_badge_action', badges.GenerateBadgeAction),
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   221
                                       ('addtalkinconf', confactions.AddTalkInConferenceAction)
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   222
                                       ])
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   223
                self.assertListEqual(self.action_submenu(req, rset, 'addrelated'),
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   224
                                      [(u'add Track in_conf Conference object',
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   225
                                        u'http://testing.fr/cubicweb/add/Track'
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   226
                                        u'?__linkto=in_conf%%3A%(conf)s%%3Asubject&'
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   227
                                        u'__redirectpath=conference%%2Fconf&'
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   228
                                        u'__redirectvid=' % {'conf': self.confeid}),
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   229
                                       ])
6243
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   230
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   231
You just have to execute a rql query corresponding to the view you want to test,
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   232
and to compare the result of
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   233
:meth:`~cubicweb.devtools.testlib.CubicWebTC.pactions` with the list of actions
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   234
that must be visible in the interface. This is a list of tuples. The first
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   235
element is the action's `__regid__`, the second the action's class.
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   236
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   237
To test actions in a submenu, you just have to test the result of
6243
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   238
:meth:`~cubicweb.devtools.testlib.CubicWebTC.action_submenu` method. The last
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   239
parameter of the method is the action's category. The result is a list of
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   240
tuples. The first element is the action's title, and the second element the
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   241
action's url.
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   242
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   243
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   244
.. _automatic_views_tests:
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   245
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   246
Automatic views testing
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   247
-----------------------
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   248
6242
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   249
This is done automatically with the :class:`cubicweb.devtools.testlib.AutomaticWebTest`
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   250
class. At cube creation time, the mycube/test/test_mycube.py file
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   251
contains such a test. The code here has to be uncommented to be
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   252
usable, without further modification.
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   253
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   254
The ``auto_populate`` method uses a smart algorithm to create
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   255
pseudo-random data in the database, thus enabling the views to be
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   256
invoked and tested.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   257
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   258
Depending on the schema, hooks and operations constraints, it is not
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   259
always possible for the automatic auto_populate to proceed.
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   260
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   261
It is possible of course to completely redefine auto_populate. A
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   262
lighter solution is to give hints (fill some class attributes) about
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   263
what entities and relations have to be skipped by the auto_populate
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   264
mechanism. These are:
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   265
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   266
* `no_auto_populate`, may contain a list of entity types to skip
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   267
* `ignored_relations`, may contain a list of relation types to skip
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   268
* `application_rql`, may contain a list of rql expressions that
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   269
  auto_populate cannot guess by itself; these must yield resultsets
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   270
  against which views may be selected.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   271
6242
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   272
.. warning::
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   273
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   274
  Take care to not let the imported `AutomaticWebTest` in your test module
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   275
  namespace, else both your subclass *and* this parent class will be run.
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   276
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6825
diff changeset
   277
Cache heavy database setup
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6825
diff changeset
   278
-------------------------------
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6825
diff changeset
   279
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   280
Some test suites require a complex setup of the database that takes
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   281
seconds (or even minutes) to complete. Doing the whole setup for each
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   282
individual test makes the whole run very slow. The ``CubicWebTC``
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   283
class offer a simple way to prepare a specific database once for
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   284
multiple tests. The `test_db_id` class attribute of your
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   285
``CubicWebTC`` subclass must be set to a unique identifier and the
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   286
:meth:`pre_setup_database` class method must build the cached content. As
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   287
the :meth:`pre_setup_database` method is not garanteed to be called
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   288
every time a test method is run, you must not set any class attribute
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   289
to be used during test *there*. Databases for each `test_db_id` are
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   290
automatically created if not already in cache. Clearing the cache is
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   291
up to the user. Cache files are found in the :file:`data/database`
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   292
subdirectory of your test directory.
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6825
diff changeset
   293
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6825
diff changeset
   294
.. warning::
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6825
diff changeset
   295
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   296
  Take care to always have the same :meth:`pre_setup_database`
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   297
  function for all classes with a given `test_db_id` otherwise your
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   298
  tests will have unpredictable results depending on the first
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   299
  encountered one.
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   300
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6825
diff changeset
   301
6167
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   302
Testing on a real-life database
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   303
-------------------------------
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   304
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   305
The ``CubicWebTC`` class uses the `cubicweb.devtools.ApptestConfiguration`
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   306
configuration class to setup its testing environment (database driver,
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   307
user password, application home, and so on). The `cubicweb.devtools`
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   308
module also provides a `RealDatabaseConfiguration`
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   309
class that will read a regular cubicweb sources file to fetch all
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   310
this information but will also prevent the database to be initalized
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   311
and reset between tests.
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   312
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   313
For a test class to use a specific configuration, you have to set
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   314
the `_config` class attribute on the class as in:
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   315
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   316
.. sourcecode:: python
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   317
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   318
    from cubicweb.devtools import RealDatabaseConfiguration
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   319
    from cubicweb.devtools.testlib import CubicWebTC
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   320
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   321
    class BlogRealDatabaseTC(CubicWebTC):
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   322
        _config = RealDatabaseConfiguration('blog',
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   323
                                            sourcefile='/path/to/realdb_sources')
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   324
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   325
        def test_blog_rss(self):
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   326
            with self.admin_access.web_request() as req:
10829
550c2d27339f fix typos and indentation in docstrings/docs
Jérôme Roy <jerome.roy@logilab.fr>
parents: 10495
diff changeset
   327
                rset = req.execute('Any B ORDERBY D DESC WHERE B is BlogEntry, '
550c2d27339f fix typos and indentation in docstrings/docs
Jérôme Roy <jerome.roy@logilab.fr>
parents: 10495
diff changeset
   328
                                   'B created_by U, U login "logilab", B creation_date D')
550c2d27339f fix typos and indentation in docstrings/docs
Jérôme Roy <jerome.roy@logilab.fr>
parents: 10495
diff changeset
   329
                self.view('rss', rset, req=req)
6167
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   330
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   331
6065
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   332
Testing with other cubes
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   333
------------------------
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   334
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   335
Sometimes a small component cannot be tested all by itself, so one
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   336
needs to specify other cubes to be used as part of the the unit test
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   337
suite. This is handled by the ``bootstrap_cubes`` file located under
6471
6e0642346f80 [book] small note about test/data/schema.py
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6344
diff changeset
   338
``mycube/test/data``. One example from the `preview` cube::
6065
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   339
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   340
 card, file, preview
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   341
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   342
The format is:
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   343
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   344
* possibly several empy lines or lines starting with ``#`` (comment lines)
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   345
* one line containing a comma-separated list of cube names.
6065
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   346
6471
6e0642346f80 [book] small note about test/data/schema.py
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6344
diff changeset
   347
It is also possible to add a ``schema.py`` file in
6e0642346f80 [book] small note about test/data/schema.py
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6344
diff changeset
   348
``mycube/test/data``, which will be used by the testing framework,
6e0642346f80 [book] small note about test/data/schema.py
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6344
diff changeset
   349
therefore making new entity types and relations available to the
6e0642346f80 [book] small note about test/data/schema.py
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6344
diff changeset
   350
tests. 
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   351
6731
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   352
Literate programming
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   353
--------------------
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   354
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   355
CubicWeb provides some literate programming capabilities. The :ref:`cubicweb-ctl`
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   356
`shell` command accepts different file formats. If your file ends with `.txt`
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   357
or `.rst`, the file will be parsed by :mod:`doctest.testfile` with CubicWeb's
6731
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   358
:ref:`migration` API enabled in it.
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   359
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   360
Create a `scenario.txt` file in the `test/` directory and fill with some content.
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   361
Refer to the :mod:`doctest.testfile` `documentation`_.
6731
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   362
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   363
.. _documentation: http://docs.python.org/library/doctest.html
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   364
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   365
Then, you can run it directly by::
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   366
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   367
    $ cubicweb-ctl shell <cube_instance> test/scenario.txt
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   368
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   369
When your scenario file is ready, put it in a new test case to be able to run
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   370
it automatically.
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   371
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   372
.. sourcecode:: python
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   373
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   374
      from os.path import dirname, join
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   375
      from logilab.common.testlib import unittest_main
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   376
      from cubicweb.devtools.testlib import CubicWebTC
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   377
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   378
      class AcceptanceTC(CubicWebTC):
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   379
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   380
              def test_scenario(self):
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   381
                      self.assertDocTestFile(join(dirname(__file__), 'scenario.txt'))
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   382
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   383
      if __name__ == '__main__':
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   384
              unittest_main()
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   385
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   386
Skipping a scenario
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   387
```````````````````
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   388
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   389
If you want to set up initial conditions that you can't put in your unit test
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   390
case, you have to use a :exc:`KeyboardInterrupt` exception only because of the
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   391
way :mod:`doctest` module will catch all the exceptions internally.
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   392
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   393
    >>> if condition_not_met:
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   394
    ...     raise KeyboardInterrupt('please, check your fixture.')
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   395
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   396
Passing paramaters
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   397
``````````````````
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   398
Using extra arguments to parametrize your scenario is possible by prepending them
6731
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   399
by double dashes.
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   400
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   401
Please refer to the `cubicweb-ctl shell --help` usage.
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   402
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   403
.. important::
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   404
    Your scenario file must be utf-8 encoded.
3ea1f7a6311c [book] Notes about literate testing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6592
diff changeset
   405
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   406
Test APIS
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   407
---------
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   408
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   409
Using Pytest
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   410
````````````
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   411
5266
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   412
The `pytest` utility (shipping with `logilab-common`_, which is a
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   413
mandatory dependency of CubicWeb) extends the Python unittest
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   414
functionality and is the preferred way to run the CubicWeb test
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   415
suites. Bare unittests also work the usual way.
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   416
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   417
.. _logilab-common: http://www.logilab.org/project/logilab-common
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   418
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   419
To use it, you may:
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   420
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   421
* just launch `pytest` in your cube to execute all tests (it will
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   422
  discover them automatically)
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   423
* launch `pytest unittest_foo.py` to execute one test file
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   424
* launch `pytest unittest_foo.py bar` to execute all test methods and
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   425
  all test cases whose name contains `bar`
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   426
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   427
Additionally, the `-x` option tells pytest to exit at the first error
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   428
or failure. The `-i` option tells pytest to drop into pdb whenever an
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   429
exception occurs in a test.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   430
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   431
When the `-x` option has been used and the run stopped on a test, it
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   432
is possible, after having fixed the test, to relaunch pytest with the
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   433
`-R` option to tell it to start testing again from where it previously
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   434
failed.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   435
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   436
Using the `TestCase` base class
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   437
```````````````````````````````
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   438
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   439
The base class of CubicWebTC is logilab.common.testlib.TestCase, which
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   440
provides a lot of convenient assertion methods.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   441
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   442
.. autoclass:: logilab.common.testlib.TestCase
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   443
   :members:
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   444
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   445
CubicWebTC API
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   446
``````````````
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   447
.. autoclass:: cubicweb.devtools.testlib.CubicWebTC
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   448
   :members:
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   449
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   450
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   451
What you need to know about request and session
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   452
-----------------------------------------------
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   453
10495
5bd914ebf3ae [doc] fix warnings/errors in doc build
Julien Cristau <julien.cristau@logilab.fr>
parents: 10491
diff changeset
   454
.. image:: ../../images/request_session.png
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   455
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   456
First, remember to think that some code run on a client side, some
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   457
other on the repository side. More precisely:
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   458
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   459
* client side: web interface, raw repoapi connection (cubicweb-ctl shell for
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   460
  instance);
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   461
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   462
* repository side: RQL query execution, that may trigger hooks and operation.
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   463
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   464
The client interacts with the repository through a repoapi connection.
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   465
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   466
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   467
.. note::
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   468
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   469
   These distinctions are going to disappear in cubicweb 3.21 (if not
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   470
   before).
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   471
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   472
A repoapi connection is tied to a session in the repository. The connection and
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   473
request objects are inaccessible from repository code / the session object is
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   474
inaccessible from client code (theoretically at least).
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   475
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   476
The web interface provides a request class.  That `request` object provides
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   477
access to all cubicweb resources, eg:
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   478
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   479
* the registry (which itself provides access to the schema and the
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   480
  configuration);
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   481
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   482
* an underlying repoapi connection (when using req.execute, you actually call the
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   483
  repoapi);
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   484
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   485
* other specific resources depending on the client type (url generation according
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   486
  to base url, form parameters, etc.).
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   487
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   488
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   489
A `session` provides an api similar to a request regarding RQL execution and
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   490
access to global resources (registry and all), but also has the following
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   491
responsibilities:
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   492
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   493
* handle transaction data, that will live during the time of a single
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   494
  transaction. This includes the database connections that will be used to
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   495
  execute RQL queries.
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   496
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   497
* handle persistent data that may be used across different (web) requests
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   498
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   499
* security and hooks control (not possible through a request)
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   500
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   501
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   502
The `_cw` attribute
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   503
```````````````````
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   504
The `_cw` attribute available on every application object provides access to all
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   505
cubicweb resources, i.e.:
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   506
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   507
- For code running on the client side (eg web interface view), `_cw` is a request
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   508
  instance.
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   509
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   510
- For code running on the repository side (hooks and operation), `_cw` is a
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   511
  Connection or Session instance.
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   512
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   513
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   514
Beware some views may be called with a session (e.g. notifications) or with a
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   515
request.
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   516
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   517
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   518
Request, session and transaction
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   519
````````````````````````````````
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   520
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   521
In the web interface, an HTTP request is handled by a single request, which will
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   522
be thrown away once the response is sent.
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   523
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   524
The web publisher handles the transaction:
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   525
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   526
* commit / rollback is done automatically
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   527
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   528
* you should not commit / rollback explicitly, except if you really
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   529
  need it
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   530
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   531
Let's detail the process:
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   532
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   533
1. an incoming RQL query comes from a client to the web stack
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   534
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   535
2. the web stack opens an authenticated database connection for the
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   536
   request, which is associated to a user session
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   537
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   538
3. the query is executed (through the repository connection)
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   539
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   540
4. this query may trigger hooks. Hooks and operations may execute some rql queries
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   541
   through `cnx.execute`.
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   542
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9257
diff changeset
   543
5. the repository gets the result of the query in 1. If it was a RQL read query,
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   544
   the database connection is released. If it was a write query, the connection
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   545
   is then tied to the session until the transaction is commited or rolled back.
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   546
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   547
6. results are sent back to the client
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   548
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   549
This implies several things:
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   550
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   551
* when using a request, or code executed in hooks, this database
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   552
  connection handling is totally transparent
8168
707466abe9cc [book] add doc about request, session and connection management you need to know when writing tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   553
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   554
* however, take care when writing tests: you are usually faking /
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   555
  testing both the server and the client side, so you have to decide
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   556
  when to use RepoAccess.client_cnx or RepoAccess.repo_cnx. Ask
9880
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   557
  yourself "where will the code I want to test be running, client or
9adf36ce805e [doc/book] spelling fixes in "testing" chapter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9878
diff changeset
   558
  repository side?". The response is usually: use a repo (since the
9878
f3936f64bd98 [doc/book] update examples, using the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9864
diff changeset
   559
  "client connection" concept is going away in a couple of releases).