From 75d84da4bc79ab12f4e0935897a836d5b6bbe0c2 Mon Sep 17 00:00:00 2001 From: Luiz Guilherme Pais dos Santos Date: Fri, 10 Oct 2014 14:10:23 -0300 Subject: [PATCH 1/4] Fix typo. --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 814a975..5a25456 100644 --- a/README.rst +++ b/README.rst @@ -73,9 +73,9 @@ Basic example, only re-defining fixtures: :: ESTester tests -------------- -In order to run ESTester tests, make sure you have ElasticSearch installed locally and it is up :: +In order to run ESTester tests, make sure you have ElasticSearch installed locally and it is up make setup - make test + make tests Compatibility From 108bf86378209ed98530eb2ac2b066f5b87ad296 Mon Sep 17 00:00:00 2001 From: Luiz Guilherme Pais dos Santos Date: Fri, 10 Oct 2014 14:11:48 -0300 Subject: [PATCH 2/4] Fix typo. --- README.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 5a25456..9eb23e2 100644 --- a/README.rst +++ b/README.rst @@ -73,7 +73,8 @@ Basic example, only re-defining fixtures: :: ESTester tests -------------- -In order to run ESTester tests, make sure you have ElasticSearch installed locally and it is up +In order to run ESTester tests, make sure you have ElasticSearch installed locally and it is up :: + make setup make tests From d9881a74e330a11bdc9c414d2673efedee63b770 Mon Sep 17 00:00:00 2001 From: "Luiz Guilherme P. Santos" Date: Fri, 10 Oct 2014 16:54:30 -0300 Subject: [PATCH 3/4] Use elasticsearch-py to access elasticsearch instead of requests. --- estester/__init__.py | 92 ++++++++++--------------------------- requirements.txt | 3 +- tests/test_querytestcase.py | 14 +++--- 3 files changed, 32 insertions(+), 77 deletions(-) diff --git a/estester/__init__.py b/estester/__init__.py index 84a89bd..cef1ef0 100644 --- a/estester/__init__.py +++ b/estester/__init__.py @@ -1,9 +1,6 @@ -import json -import time import unittest -import urllib -import requests +from elasticsearch import Elasticsearch __author__ = "Tatiana Al-Chueyr Pereira Martins" @@ -63,6 +60,7 @@ class ElasticSearchQueryTestCase(ExtendedTestCase): index = "sample.test" # must be lower case reset_index = True # warning: if this is True, index will be cleared up host = "http://0.0.0.0:9200/" + es = Elasticsearch(host) mappings = {} proxies = {} fixtures = [] @@ -107,15 +105,13 @@ def create_index(self): (i) http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/ configuring-analyzers.html """ - url = "{0}{1}/" - url = url.format(self.host, self.index) + data = {} if self.mappings: data["mappings"] = self.mappings if self.settings: data["settings"] = self.settings - json_data = json.dumps(data) - response = requests.put(url, proxies=self.proxies, data=json_data) + self.es.indices.create(index=self.index, body=data) def load_fixtures(self): """ @@ -146,18 +142,10 @@ def load_fixtures(self): body: json with fields of values of document """ for doc in self.fixtures: - doc_type = urllib.quote_plus(doc["type"]) - doc_id = urllib.quote_plus(doc["id"]) - doc_body = doc["body"] - url = "{0}{1}/{2}/{3}" - url = url.format(self.host, self.index, doc_type, doc_id) - response = requests.put( - url, - data=json.dumps(doc_body), - proxies=self.proxies) - if not response.status_code in [200, 201]: - raise ElasticSearchException(response.text) - time.sleep(self.timeout) + self.es.index( + index=self.index, doc_type=doc["type"], + id=doc["id"], body=doc["body"], refresh=True + ) # http://0.0.0.0:9200/sample.test/_search def delete_index(self): @@ -165,33 +153,22 @@ def delete_index(self): Deletes test index. Uses class attribute: index: name of the index to be deleted """ - url = "{0}{1}/".format(self.host, self.index) - requests.delete(url, proxies=self.proxies) + self.es.indices.delete(index=self.index, ignore=404) def search(self, query=None): """ Run a search (JSON) and returns the JSON response. """ - url = "{0}{1}/_search".format(self.host, self.index) query = {} if query is None else query - response = requests.post( - url, - data=json.dumps(query), - proxies=self.proxies) - return json.loads(response.text) + response = self.es.search(index=self.index, body=query) + return response def tokenize(self, text, analyzer): """ Run on text and returns a dict containing the tokens. """ - url = "{0}{1}/_analyze".format(self.host, self.index) - if analyzer != "default": - url += "?analyzer={0}".format(analyzer) - response = requests.post( - url, - data=json.dumps(text), - proxies=self.proxies) - return json.loads(response.text) + response = self.es.indices.analyze(index=self.index, body=text, analyzer=analyzer) + return response class MultipleIndexesQueryTestCase(ElasticSearchQueryTestCase): @@ -266,16 +243,13 @@ def create_index(self, index_name="", settings="", mappings=""): (i) http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/ configuring-analyzers.html """ - url = "{0}{1}/" - index = index_name or self.index - url = url.format(self.host, index) + data = {} if self.mappings: data["mappings"] = mappings or self.mappings if self.settings: data["settings"] = settings or self.settings - json_data = json.dumps(data) - response = requests.put(url, proxies=self.proxies, data=json_data) + self.es.indices.create(index=self.index, body=data) def load_fixtures(self, index_name="", fixtures=""): """ @@ -308,18 +282,10 @@ def load_fixtures(self, index_name="", fixtures=""): index = index_name or self.index fixtures = fixtures or self.fixtures for doc in fixtures: - doc_type = urllib.quote_plus(doc["type"]) - doc_id = urllib.quote_plus(doc["id"]) - doc_body = doc["body"] - url = "{0}{1}/{2}/{3}" - url = url.format(self.host, index, doc_type, doc_id) - response = requests.put( - url, - data=json.dumps(doc_body), - proxies=self.proxies) - if not response.status_code in [200, 201]: - raise ElasticSearchException(response.text) - time.sleep(self.timeout) + self.es.index( + index=index, doc_type=doc["type"], + id=doc["id"], body=doc["body"], refresh=True + ) # http://0.0.0.0:9200/sample.test/_search def delete_index(self, index_name=""): @@ -327,30 +293,20 @@ def delete_index(self, index_name=""): Deletes test index. Uses class attribute: index: name of the index to be deleted """ - index = index_name or self.index - url = "{0}{1}/".format(self.host, self.index) - requests.delete(url, proxies=self.proxies) + self.es.indices.delete(index=self.index, ignore=404) def search(self, query=None): """ Run a search (JSON) and returns the JSON response. """ - url = "{0}/_search".format(self.host) query = {} if query is None else query - response = requests.post( - url, - data=json.dumps(query), - proxies=self.proxies) - return json.loads(response.text) + response = self.es.search(body=query) + return response def search_in_index(self, index, query=None): """ Run a search (JSON) and returns the JSON response. """ - url = "{0}/{1}/_search".format(self.host, index) query = {} if query is None else query - response = requests.post( - url, - data=json.dumps(query), - proxies=self.proxies) - return json.loads(response.text) + response = self.es.search(index=index, body=query) + return response diff --git a/requirements.txt b/requirements.txt index 4d2e648..53a68bb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -requests==2.0.0 \ No newline at end of file +requests==2.0.0 +elasticsearch==1.2.0 \ No newline at end of file diff --git a/tests/test_querytestcase.py b/tests/test_querytestcase.py index 173b47c..666583d 100644 --- a/tests/test_querytestcase.py +++ b/tests/test_querytestcase.py @@ -1,5 +1,4 @@ import unittest -from mock import patch from estester import ElasticSearchQueryTestCase, ExtendedTestCase,\ MultipleIndexesQueryTestCase @@ -104,7 +103,7 @@ def test_search_all_indexes(self): def test_search_one_index_that_has_item(self): query = { "query": { - "text": { + "match": { "name": "Agnessa" } } @@ -117,7 +116,7 @@ def test_search_one_index_that_has_item(self): def test_search_one_index_that_doesnt_have_item(self): query = { "query": { - "text": { + "match": { "name": "Agnessa" } } @@ -144,7 +143,6 @@ class SimpleQueryTestCase(ElasticSearchQueryTestCase): def test_search_by_nothing_returns_two_results(self): response = self.search() - expected = {u"name": u"Nina Fox"} self.assertEqual(response["hits"]["total"], 2) self.assertEqual(response["hits"]["hits"][0]["_id"], u"1") self.assertEqual(response["hits"]["hits"][1]["_id"], u"2") @@ -159,13 +157,13 @@ def test_search_by_nina_returns_one_result(self): def test_tokenize_with_default_analyzer(self): response = self.tokenize("Nothing to declare", "default") items_list = response["tokens"] - self.assertEqual(len(items_list), 2) + self.assertEqual(len(items_list), 3) tokens = [item["token"] for item in items_list] - self.assertEqual(sorted(tokens), ["declare", "nothing"]) + self.assertEqual(sorted(tokens), ["declare", "nothing", "to"]) - def test_tokenize_with_default_analyzer(self): + def test_tokenize_with_whitespace_analyzer(self): response = self.tokenize("Nothing to declare", "whitespace") items_list = response["tokens"] self.assertEqual(len(items_list), 3) tokens = [item["token"] for item in items_list] - self.assertEqual(sorted(tokens), ['"Nothing', 'declare"', "to"]) + self.assertEqual(sorted(tokens), ["Nothing", "declare", "to"]) From b359e0f0569b1f7e4c683a056b29a975ecf26660 Mon Sep 17 00:00:00 2001 From: "Luiz Guilherme P. Santos" Date: Fri, 10 Oct 2014 16:56:58 -0300 Subject: [PATCH 4/4] Adjust project requirements. --- requirements.txt | 1 - setup.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 53a68bb..10e6b7c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1 @@ -requests==2.0.0 elasticsearch==1.2.0 \ No newline at end of file diff --git a/setup.py b/setup.py index 4973186..aace9ea 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ download_url = 'http://pypi.python.org/pypi/ESTester', description=u"Utilities for testing ElasticSearch queries", include_package_data=True, - install_requires=["requests>=2.0.0"], + install_requires=["elasticsearch>=1.2.0"], license="GNU GPLv2", long_description=README, packages=find_packages(),