[book] add a link in hooks section to test, talk about create_entity in tests section stable
authorStephanie Marcu <stephanie.marcu@logilab.fr>
Fri, 24 Sep 2010 18:38:24 +0200
branchstable
changeset 6344 0cb0d8d83e4c
parent 6343 4814d44405fc
child 6345 1a7f4bfbf92b
[book] add a link in hooks section to test, talk about create_entity in tests section
doc/book/en/devrepo/repo/hooks.rst
doc/book/en/devrepo/testing.rst
--- a/doc/book/en/devrepo/repo/hooks.rst	Fri Sep 24 18:37:03 2010 +0200
+++ b/doc/book/en/devrepo/repo/hooks.rst	Fri Sep 24 18:38:24 2010 +0200
@@ -58,6 +58,10 @@
 implementing `precommit_event` and other standard methods (wholly
 described in :ref:`operations_api`).
 
+.. hint::
+
+   It is a good practice, to write unit tests for each hook. See an example in :ref:`hook_test`
+
 Events
 ------
 
@@ -241,6 +245,8 @@
 
 .. sourcecode:: python
 
+    from cubicweb.server.hook import Hook, Operation, match_rtype
+
     def check_cycle(self, session, eid, rtype, role='subject'):
         parents = set([eid])
         parent = session.entity_from_eid(eid)
@@ -300,7 +306,7 @@
    class CheckSubsidiaryCycleOp(Operation):
 
        def precommit_event(self):
-           for eid in self._cw.transaction_data['subsidiary_cycle_detection']:
+           for eid in self.session.transaction_data['subsidiary_cycle_detection']:
                check_cycle(self.session, eid, self.rtype)
 
 Here, we call set_operation with a session object, a specially forged
--- a/doc/book/en/devrepo/testing.rst	Fri Sep 24 18:37:03 2010 +0200
+++ b/doc/book/en/devrepo/testing.rst	Fri Sep 24 18:38:24 2010 +0200
@@ -42,6 +42,8 @@
   `sync_schema_props_perms()` fonction of the migration environment
   need not a database regeneration step.
 
+.. _hook_test:
+
 Unit test by example
 ````````````````````
 
@@ -85,6 +87,14 @@
 The test case itself checks that an Operation does it job of
 preventing cycles amongst Keyword entities.
 
+`create_entity` is a useful method, which easily allows to create an
+entity. You can link this entity to others entities, by specifying as
+argument, the relation name, and the entity to link, as value. In the
+above example, the `Classification` entity is linked to a `CWEtype`
+via the relation `classifies`. Conversely, if you are creating a
+`CWEtype` entity, you can link it to a `Classification` entity, by
+adding `reverse_classifies` as argument.
+
 .. note::
 
    :meth:`commit` method is not called automatically in test_XXX