Skip to content

Commit

Permalink
Check behaviour with collections prior to activation
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed May 26, 2017
1 parent 1eb4eb8 commit b1eada0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ addons:
packages:
- oracle-java8-set-default
- elasticsearch
postgresql: "9.5"
services:
- elasticsearch
env:
Expand Down
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pytest-capturelog
mock
unittest2
webtest
kinto[postgresql]
10 changes: 10 additions & 0 deletions kinto_elasticsearch/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,27 @@ def search_view(request, **kwargs):
indexer = request.registry.indexer
try:
results = indexer.search(bucket_id, collection_id, **kwargs)

except elasticsearch.NotFoundError as e:
# If plugin was enabled after the creation of the collection.
indexer.create_index(bucket_id, collection_id)
results = indexer.search(bucket_id, collection_id, **kwargs)

except elasticsearch.RequestError as e:
# Malformed query.
message = e.info["error"]["reason"]
details = e.info["error"]["root_cause"][0]
response = http_error(httpexceptions.HTTPBadRequest(),
errno=ERRORS.INVALID_PARAMETERS,
message=message,
details=details)
raise response

except elasticsearch.ElasticsearchException as e:
# General failure.
logger.exception("Index query failed.")
results = {}

return results


Expand Down
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ def get_app_settings(cls, extras=None):
config = configparser.ConfigParser()
config.read(ini_path)
settings = dict(config.items('app:main'))
if extras:
settings.update(extras)
return settings
33 changes: 33 additions & 0 deletions tests/test_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,39 @@ def test_returns_false_if_connection_fails(self):
assert not resp.json["elasticsearch"]


class PostActivation(BaseWebTest, unittest.TestCase):

@classmethod
def get_app_settings(cls, extras=None):
settings = super().get_app_settings(extras)
settings['storage_backend'] = 'kinto.core.storage.postgresql'
settings['storage_url'] = 'postgres://postgres:postgres@localhost:5432/postgres'
settings['permission_backend'] = 'kinto.core.permission.postgresql'
settings['permission_url'] = settings['storage_url']
return settings

def setUp(self):
app = self.make_app(settings={"kinto.includes": ""})
capabilities = app.get("/").json["capabilities"]
assert "elasticsearch" not in capabilities

app.put("/buckets/bid", headers=self.headers)
app.put("/buckets/bid/collections/cid", headers=self.headers)
app.post_json("/buckets/bid/collections/cid/records",
{"data": {"before": "indexing"}},
headers=self.headers)

def test_search_does_not_fail_after_plugin_activation(self):
resp = self.app.get("/buckets/bid/collections/cid/records",
headers=self.headers)
assert len(resp.json["data"]) == 1

resp = self.app.get("/buckets/bid/collections/cid/search",
headers=self.headers)
results = resp.json
assert len(results["hits"]["hits"]) == 0


class RecordIndexing(BaseWebTest, unittest.TestCase):

def setUp(self):
Expand Down

0 comments on commit b1eada0

Please sign in to comment.