diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2020-07-02 12:12:11 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2020-07-24 10:07:48 -0700 |
commit | a1f14f5b5ce087cb4681d46817da2be0777e4220 (patch) | |
tree | 6f212219538ec0a794d69ae14a4c489f7d11227f /python | |
parent | b9ba24553e2e1de3c3ac0faeba59231ec512fa67 (diff) | |
download | fatcat-a1f14f5b5ce087cb4681d46817da2be0777e4220.tar.gz fatcat-a1f14f5b5ce087cb4681d46817da2be0777e4220.zip |
update web_search tests to mock ES client
Instead of using 'responses' mock of 'requests' library.
Tried using 'elasticmock' helper but it didn't work.
Diffstat (limited to 'python')
-rw-r--r-- | python/tests/fixtures.py | 4 | ||||
-rw-r--r-- | python/tests/web_search.py | 88 |
2 files changed, 47 insertions, 45 deletions
diff --git a/python/tests/fixtures.py b/python/tests/fixtures.py index 44c7be63..3263f243 100644 --- a/python/tests/fixtures.py +++ b/python/tests/fixtures.py @@ -1,9 +1,10 @@ import pytest from dotenv import load_dotenv +import elasticsearch + import fatcat_web import fatcat_openapi_client - from fatcat_openapi_client import * from fatcat_tools import authenticated_api @@ -13,6 +14,7 @@ def full_app(): fatcat_web.app.testing = True fatcat_web.app.debug = False fatcat_web.app.config['WTF_CSRF_ENABLED'] = False + fatcat_web.app.es_client = elasticsearch.Elasticsearch("mockbackend") return fatcat_web.app @pytest.fixture diff --git a/python/tests/web_search.py b/python/tests/web_search.py index b55b0fcf..07985e04 100644 --- a/python/tests/web_search.py +++ b/python/tests/web_search.py @@ -1,33 +1,36 @@ import json -import responses +import pytest +from fatcat_openapi_client.rest import ApiException from fixtures import * -@pytest.mark.skip -@responses.activate -def test_release_search(app): + +def test_release_search(app, mocker): with open('tests/files/elastic_release_search.json') as f: elastic_resp=json.loads(f.read()) - responses.add(responses.GET, 'http://localhost:9200/fatcat_release/_search', - json=elastic_resp, status=200) + es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') + es_raw.side_effect = [ + (200, {}, json.dumps(elastic_resp)), + ] rv = app.get('/release/search?q=blood') assert rv.status_code == 200 assert b"Showing" in rv.data assert b"Quantum Studies of Acetylene Adsorption on Ice Surface" in rv.data -@pytest.mark.skip -@responses.activate -def test_container_search(app): +def test_container_search(app, mocker): with open('tests/files/elastic_container_search.json') as f: elastic_resp=json.loads(f.read()) - responses.add(responses.GET, 'http://localhost:9200/fatcat_container/_search', - json=elastic_resp, status=200) + + es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') + es_raw.side_effect = [ + (200, {}, json.dumps(elastic_resp)), + ] rv = app.get('/container/search?q=blood') assert rv.status_code == 200 @@ -62,39 +65,35 @@ elastic_resp3 = { 'took': 0 } -@responses.activate -def test_stats(app): - - responses.add(responses.GET, - 'http://localhost:9200/fatcat_release/_search?request_cache=true', - json=elastic_resp1.copy(), status=200) - responses.add(responses.GET, - 'http://localhost:9200/fatcat_release/_search?request_cache=true', - json=elastic_resp2.copy(), status=200) - responses.add(responses.GET, - 'http://localhost:9200/fatcat_container/_search?request_cache=true', - json=elastic_resp3.copy(), status=200) +def test_stats(app, mocker): + + es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') + es_raw.side_effect = [ + (200, {}, json.dumps(elastic_resp1)), + (200, {}, json.dumps(elastic_resp2)), + (200, {}, json.dumps(elastic_resp3)), + ] + rv = app.get('/stats') assert rv.status_code == 200 - # TODO: robe these responses better - -@responses.activate -def test_stats_json(app): - - responses.add(responses.GET, - 'http://localhost:9200/fatcat_release/_search?request_cache=true', - json=elastic_resp1.copy(), status=200) - responses.add(responses.GET, - 'http://localhost:9200/fatcat_release/_search?request_cache=true', - json=elastic_resp2.copy(), status=200) - responses.add(responses.GET, - 'http://localhost:9200/fatcat_container/_search?request_cache=true', - json=elastic_resp3.copy(), status=200) + assert b"80,578,584" in rv.data + +def test_stats_json(app, mocker): + + es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') + es_raw.side_effect = [ + (200, {}, json.dumps(elastic_resp1)), + (200, {}, json.dumps(elastic_resp2)), + (200, {}, json.dumps(elastic_resp3)), + ] + rv = app.get('/stats.json') assert rv.status_code == 200 + assert rv.json['papers']['in_kbart'] == 51594200 + assert rv.json['release']['refs_total'] == 8031459 -@responses.activate -def test_container_stats(app): +@pytest.mark.skip +def test_container_stats(app, mocker): elastic_resp = { 'timed_out': False, @@ -108,9 +107,10 @@ def test_container_stats(app): 'took': 50 } - responses.add(responses.GET, - 'http://localhost:9200/fatcat_release/_search?request_cache=true', - json=elastic_resp, status=200) + es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') + es_raw.side_effect = [ + (200, {}, json.dumps(elastic_resp)), + ] rv = app.get('/container/issnl/1234-5678/stats.json') - assert rv.status_code == 200 - # TODO: probe this response better + #print(rv.json) + assert rv.status_code == 201 |