[book] add a link in hooks section to test, talk about create_entity in tests section
--- 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