Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

count method #1

Merged
merged 1 commit into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions elasticmock/fake_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,37 @@ def get_source(self, index, doc_type, id, params=None):
document = self.get(index=index, doc_type=doc_type, id=id, params=params)
return document.get('_source')

@query_params('_source', '_source_exclude', '_source_include',
'allow_no_indices', 'analyze_wildcard', 'analyzer', 'default_operator',
'df', 'expand_wildcards', 'explain', 'fielddata_fields', 'fields',
'from_', 'ignore_unavailable', 'lenient', 'lowercase_expanded_terms',
'preference', 'q', 'request_cache', 'routing', 'scroll', 'search_type',
'size', 'sort', 'stats', 'suggest_field', 'suggest_mode',
'suggest_size', 'suggest_text', 'terminate_after', 'timeout',
'track_scores', 'version')
def count(self, index=None, doc_type=None, body=None, params=None):
if index is not None and index not in self.__documents_dict:
raise NotFoundError(404, 'IndexMissingException[[{0}] missing]'.format(index))

searchable_indexes = [index] if index is not None else self.__documents_dict.keys()

i = 0
for searchable_index in searchable_indexes:
for document in self.__documents_dict[searchable_index]:
if doc_type is not None and document.get('_type') != doc_type:
continue
i += 1
result = {
'count': i,
'_shards': {
'successful': 1,
'failed': 0,
'total': 1
}
}

return result

@query_params('_source', '_source_exclude', '_source_include',
'allow_no_indices', 'analyze_wildcard', 'analyzer', 'default_operator',
'df', 'expand_wildcards', 'explain', 'fielddata_fields', 'fields',
Expand Down
9 changes: 8 additions & 1 deletion tests/test_elasticmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


class TestFakeElasticsearch(TestCase):

@elasticmock
def setUp(self):
self.es = elasticsearch.Elasticsearch(hosts=[{'host': 'localhost', 'port': 9200}])
Expand Down Expand Up @@ -94,6 +93,14 @@ def test_should_raise_notfounderror_when_search_for_unexistent_index(self):
with self.assertRaises(NotFoundError):
self.es.search(index=self.index_name)

def test_should_return_count_for_indexed_documents_on_index(self):
index_quantity = 0
for i in range(0, index_quantity):
self.es.index(index='index_{0}'.format(i), doc_type=self.doc_type, body={'data': 'test_{0}'.format(i)})

count = self.es.count()
self.assertEqual(index_quantity, count.get('count'))

def test_should_return_all_documents(self):
index_quantity = 10
for i in range(0, index_quantity):
Expand Down