[test] unittest on hook

authorSamuel Trégouët <samuel.tregouet@logilab.fr>
changeset074421eb0f64
branchdefault
phasepublic
hiddenno
parent revision#e6392f5058c4 [pkg] make elasticsearch a new-layout-style cube
child revision#98d546d70af9 [hook] move delete es document in method and catch not found error
files modified by this revision
test/test_hooks.py
# HG changeset patch
# User Samuel Trégouët <samuel.tregouet@logilab.fr>
# Date 1489145623 -3600
# Fri Mar 10 12:33:43 2017 +0100
# Node ID 074421eb0f6474916981ecbb92a1f7f731c2db74
# Parent e6392f5058c418affb1b40649c9460ced29f1d61
[test] unittest on hook

diff --git a/test/test_hooks.py b/test/test_hooks.py
@@ -1,16 +1,45 @@
1  import unittest
2  import time
3 
4 +from mock import patch
5 +
6  from elasticsearch_dsl import Search
7 
8  from cubicweb.devtools import testlib
9 +from cubicweb.cwconfig import CubicWebConfiguration
10 
11  from cubicweb_elasticsearch.testutils import RealESTestMixin, BlogFTIAdapter
12  from cubicweb_elasticsearch.search_helpers import compose_search
13 
14 
15 +class IndexHookTC(testlib.CubicWebTC):
16 +
17 +    def setup_database(self):
18 +        super(IndexHookTC, self).setup_database()
19 +        self.orig_config_for = CubicWebConfiguration.config_for
20 +        config_for = lambda appid: self.config  # noqa
21 +        CubicWebConfiguration.config_for = staticmethod(config_for)
22 +        self.config['elasticsearch-locations'] = 'http://nonexistant.elastic.search:9200'
23 +        self.config['index-name'] = 'unittest_index_name'
24 +
25 +    @patch('elasticsearch.client.indices.IndicesClient.create')
26 +    @patch('elasticsearch.client.indices.IndicesClient.exists')
27 +    @patch('elasticsearch.client.Elasticsearch.index')
28 +    def test_index_after_create_entity(self, create, exists, index):
29 +        with self.admin_access.cnx() as cnx:
30 +            indexer = cnx.vreg['es'].select('indexer', cnx)
31 +            ce = cnx.create_entity
32 +            p = ce('Person', age=12, name=u'Jean')
33 +            cnx.commit()
34 +            es = indexer.get_connection()
35 +            self.assertTrue(es.index.called)
36 +            args, kwargs = es.index.call_args
37 +            for arg_name, expected_value in (('id', p.eid), ('doc_type', p.cw_etype)):
38 +                self.assertEqual(kwargs[arg_name], expected_value)
39 +
40 +
41  class ReindexOnRelationTests(RealESTestMixin, testlib.CubicWebTC):
42 
43      def test_es_hooks_modify_relation(self):
44          with self.admin_access.cnx() as cnx:
45              with self.temporary_appobjects(BlogFTIAdapter):