
as part of trying to simplify the core elasticRecheck, refactor the query creation into a separate set of query_builder routines. This takes away some of the duplication between the queries, and attempts to add documentation to the uses for each of them. add elasticRecheck fake pyelasticsearch testing build basic fixtures for unit testing that let us fake out the interaction to pyelasticsearch. This uses the json samples added for previous testing as the return results should an inbound query match one of the queries we know about. If the query is unknown to us, return an empty result set. Unit testing for both cases included going all the way from the top level Classifier class. Change-Id: I0d23b649274b31e8f281aaac588c4c6113a11a47
57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import fixtures
|
|
import json
|
|
import yaml
|
|
|
|
import elastic_recheck.tests
|
|
|
|
|
|
def load_empty():
|
|
with open("elastic_recheck/tests/unit/samples/no-results.json") as f:
|
|
return json.load(f)
|
|
|
|
|
|
def load_by_bug(bug):
|
|
with open("elastic_recheck/tests/unit/samples/bug-%s.json" % bug) as f:
|
|
return json.load(f)
|
|
|
|
|
|
class FakeES(object):
|
|
"""A fake elastic search interface.
|
|
|
|
This provides a stub of the elastic search interface, so we can return
|
|
fake results based on the samples we've already collected to use for
|
|
other unit tests. It does this by buiding a reverse mapping from our
|
|
queries.yaml file, and grabbing the results we'd find for known bugs.
|
|
"""
|
|
def __init__(self, url):
|
|
self._yaml = yaml.load(open('elastic_recheck/tests/unit/queries.yaml').read())
|
|
self._queries = {}
|
|
for item in self._yaml:
|
|
self._queries[item['query'].rstrip()] = item['bug']
|
|
|
|
def search(self, query, **kwargs):
|
|
qstring = query['query']['query_string']['query']
|
|
if qstring in self._queries:
|
|
return load_by_bug(self._queries[qstring])
|
|
return load_empty()
|
|
|
|
|
|
class UnitTestCase(elastic_recheck.tests.TestCase):
|
|
def setUp(self):
|
|
super(UnitTestCase, self).setUp()
|
|
|
|
self.useFixture(fixtures.MonkeyPatch('pyelasticsearch.ElasticSearch',
|
|
FakeES))
|