Commit d4005227 authored by Katia Saurfelt's avatar Katia Saurfelt
Browse files

[es6] fix tests of 0.7.XXX

parent 47268681d4b0
......@@ -3,11 +3,11 @@ from __future__ import print_function
import sys
import unittest
from io import StringIO
from itertools import repeat
from mock import patch
from elasticsearch_dsl.faceted_search import FacetedResponse
from elasticsearch_dsl.search import Search
from cubicweb.devtools import testlib
from cubicweb.cwconfig import CubicWebConfiguration
......@@ -30,6 +30,7 @@ class ExportElasticSearchTC(testlib.AutoPopulateTest):
CubicWebConfiguration.config_for = staticmethod(config_for)
self.config['elasticsearch-locations'] = 'http://nonexistant.elastic.search:9200'
self.config['index-name'] = 'unittest_index_name'
self.tested_etype = 'Person'
def to_test_etypes(self):
with self.admin_access.repo_cnx() as cnx:
......@@ -42,8 +43,10 @@ class ExportElasticSearchTC(testlib.AutoPopulateTest):
def test_indexable_types(self):
with self.admin_access.repo_cnx() as cnx:
self.assertNotEquals(
len(indexable_types(cnx.vreg.schema)), 0)
self.assertIn(self.tested_etype,
self.to_test_etypes())
self.assertIn(self.tested_etype,
indexable_types(cnx.vreg.schema))
@patch('elasticsearch.client.Elasticsearch.index', unsafe=True)
@patch('elasticsearch.client.Elasticsearch.bulk', unsafe=True)
......@@ -76,7 +79,7 @@ class ExportElasticSearchTC(testlib.AutoPopulateTest):
sys.stdout = sys.__stdout__
with self.admin_access.repo_cnx() as cnx:
self.assert_(cnx.execute('Any X WHERE X is %(etype)s' %
{'etype': indexable_types(cnx.vreg.schema)[0]}))
{'etype': self.tested_etype}))
# TODO - put this somewhere where it tests on the first get_connection
# create.assert_called_with(ignore=400,
# index='unittest_index_name',
......@@ -89,8 +92,8 @@ class ExportElasticSearchTC(testlib.AutoPopulateTest):
@patch('elasticsearch.client.Elasticsearch.index', unsafe=True)
def test_es_hooks_create(self, index, exists, create):
with self.admin_access.cnx() as cnx:
cnx.create_entity('BlogEntry', title=u'Article about stuff',
content=u'content herer')
cnx.create_entity(self.tested_etype, name=u'Jean Valjean',
age=14)
cnx.commit()
index.assert_called()
......@@ -99,11 +102,10 @@ class ExportElasticSearchTC(testlib.AutoPopulateTest):
@patch('elasticsearch.client.Elasticsearch.index', unsafe=True)
def test_es_hooks_modify(self, index, exists, create):
with self.admin_access.cnx() as cnx:
entity = cnx.create_entity('BlogEntry', title=u'Article about stuff',
content=u'content herer')
entity = cnx.create_entity(self.tested_etype, name=u'Jean Valjean')
cnx.commit()
index.reset_mock()
entity.cw_set(title=u'Different title')
entity.cw_set(name=u'Victor Hugo')
cnx.commit()
index.assert_called()
......@@ -120,17 +122,27 @@ def mock_execute_1(*args, **kwargs):
return mock_execute(1)
def mock_execute(nb_results):
result = {'_source': {'description': 'test',
'cwuri': 'http://example.org/123',
'eid': 123,
'title': 'test'},
'_type': 'BaseContent',
'_score': 1}
search = {'hits': {'hits': repeat(result, nb_results),
'total': nb_results
}}
return FacetedResponse(search, search)
def mock_execute(count):
def _result(i):
return {
'_source': {
'cwuri': 'http://example.org/{}'.format(i),
'eid': i,
'cw_etype': 'Person',
'name': 'Jean Valjean'
},
'_type': 'Person',
'_score': 1
}
search = Search(doc_type='_doc', index='unittest_index_name')
return FacetedResponse(search, {
'hits': {
'hits': [_result(i) for i in range(count)],
'total': count,
}
})
def mock_cnx(*args, **kwargs):
......
......@@ -28,7 +28,8 @@ class IndexHookTC(testlib.CubicWebTC):
es = indexer.get_connection()
self.assertTrue(es.index.called)
args, kwargs = es.index.call_args
for arg_name, expected_value in (('id', p.eid), ('doc_type', p.cw_etype)):
for arg_name, expected_value in (('id', p.eid),
('doc_type', '_doc')):
self.assertEqual(kwargs[arg_name], expected_value)
......
......@@ -34,7 +34,8 @@ class IFullTextIndexSerializableTC(testlib.CubicWebTC):
self.assertTrue(index.called)
args, kwargs = index.call_args
# blog title is a in custom_indexable_attributes
self.assertEqual(kwargs['doc_type'], 'Blog')
self.assertEqual(kwargs['doc_type'], '_doc')
self.assertEqual(kwargs['body']['cw_etype'], 'Blog')
self.assertEqual(kwargs['body']['title'], u'Blog')
index.reset_mock()
# create a BlogEntry
......@@ -44,7 +45,7 @@ class IFullTextIndexSerializableTC(testlib.CubicWebTC):
cnx.commit()
self.assertEqual(index.call_count, 2)
for args, kwargs in index.call_args_list:
if kwargs['doc_type'] == 'BlogEntry':
if kwargs['doc_type'] == '_doc':
break
else:
self.fail('index not called for the BlogEntry')
......@@ -61,7 +62,7 @@ class IFullTextIndexSerializableTC(testlib.CubicWebTC):
self.assertTrue(index.called)
args, kwargs = index.call_args
for arg_name, expected_value in (
('id', bentry.eid), ('doc_type', bentry.cw_etype)):
('id', bentry.eid), ('doc_type', '_doc')):
self.assertEqual(kwargs[arg_name], expected_value)
for arg_name, expected_value in (
('content', u'Le nouveau programme'),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment