diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2020-07-24 13:00:04 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2020-07-30 18:28:03 -0700 |
commit | 1f6f9c0e251f45f220dd48242d48fa61922fe55f (patch) | |
tree | 41a85d22339279c6bc29661e3541f2696d580f23 /python/tests | |
parent | 86c150edadad347df31393742c2de091ff21da83 (diff) | |
download | fatcat-1f6f9c0e251f45f220dd48242d48fa61922fe55f.tar.gz fatcat-1f6f9c0e251f45f220dd48242d48fa61922fe55f.zip |
refactor coverage tests/mocks
Diffstat (limited to 'python/tests')
-rw-r--r-- | python/tests/fixtures.py | 42 | ||||
-rw-r--r-- | python/tests/subentity_state.py | 19 | ||||
-rw-r--r-- | python/tests/web_coverage.py | 38 | ||||
-rw-r--r-- | python/tests/web_entity_views.py | 19 | ||||
-rw-r--r-- | python/tests/web_search.py | 1 |
5 files changed, 80 insertions, 39 deletions
diff --git a/python/tests/fixtures.py b/python/tests/fixtures.py index 3263f243..d71ac21d 100644 --- a/python/tests/fixtures.py +++ b/python/tests/fixtures.py @@ -8,13 +8,53 @@ import fatcat_openapi_client from fatcat_openapi_client import * from fatcat_tools import authenticated_api + +ES_CONTAINER_STATS_RESP = { + 'timed_out': False, + 'aggregations': { + 'container_stats': {'buckets': { + 'is_preserved': {'doc_count': 461939}, + 'in_kbart': {'doc_count': 461939}, + 'in_web': {'doc_count': 2797}, + }}, + 'preservation': { + 'buckets': [ + {'key': 'bright', 'doc_count': 444}, + {'key': 'dark', 'doc_count': 111}, + ], + 'sum_other_doc_count': 0, + }, + 'release_type': { + 'buckets': [ + {'key': 'article-journal', 'doc_count': 456}, + {'key': 'book', 'doc_count': 123}, + ], + 'sum_other_doc_count': 0, + }, + }, + 'hits': {'total': 461939, 'hits': [], 'max_score': 0.0}, + '_shards': {'successful': 5, 'total': 5, 'skipped': 0, 'failed': 0}, + 'took': 50 +} + +# TODO: this should not be empty +ES_CONTAINER_RANDOM_RESP = { + 'timed_out': False, + 'hits': {'total': 461939, 'hits': [], 'max_score': 0.0}, + '_shards': {'successful': 5, 'total': 5, 'skipped': 0, 'failed': 0}, + 'took': 50 +} + @pytest.fixture -def full_app(): +def full_app(mocker): load_dotenv(dotenv_path="./example.env") fatcat_web.app.testing = True fatcat_web.app.debug = False fatcat_web.app.config['WTF_CSRF_ENABLED'] = False + + # mock out ES client requests, so they at least fail fast fatcat_web.app.es_client = elasticsearch.Elasticsearch("mockbackend") + mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') return fatcat_web.app @pytest.fixture diff --git a/python/tests/subentity_state.py b/python/tests/subentity_state.py index e03fa99e..2843176d 100644 --- a/python/tests/subentity_state.py +++ b/python/tests/subentity_state.py @@ -1,4 +1,6 @@ +import json + from fatcat_openapi_client import * from fixtures import * @@ -17,7 +19,7 @@ Current set of such references: => work -> release """ -def test_relation_states(api, app): +def test_relation_states(api, app, mocker): j1 = ContainerEntity(name="test journal") j2 = ContainerEntity(name="another test journal") @@ -28,6 +30,12 @@ def test_relation_states(api, app): f2 = FileEntity(md5="0000000000000000ad9d7b45268be409") # WIP container + # these are basic ES stats for the container view pages + es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') + es_raw.side_effect = [ + (200, {}, json.dumps(ES_CONTAINER_STATS_RESP)), + (200, {}, json.dumps(ES_CONTAINER_RANDOM_RESP)), + ] eg = quick_eg(api) j2 = api.get_container(api.create_container(eg.editgroup_id, j2).ident) rv = app.get('/container/{}'.format(j2.ident)) @@ -129,7 +137,14 @@ def test_relation_states(api, app): assert rv.status_code == 200 -def test_app_entity_states(api, app): +def test_app_entity_states(api, app, mocker): + + # these are basic ES stats for the container view pages + es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') + es_raw.side_effect = [ + (200, {}, json.dumps(ES_CONTAINER_STATS_RESP)), + (200, {}, json.dumps(ES_CONTAINER_RANDOM_RESP)), + ] j1 = ContainerEntity(name="test journal") j2 = ContainerEntity(name="another test journal") diff --git a/python/tests/web_coverage.py b/python/tests/web_coverage.py index c6fba427..41b04892 100644 --- a/python/tests/web_coverage.py +++ b/python/tests/web_coverage.py @@ -9,34 +9,6 @@ from fixtures import * def test_container_coverage(app, mocker): elastic_resp1 = { - 'timed_out': False, - 'aggregations': { - 'container_stats': {'buckets': { - 'is_preserved': {'doc_count': 461939}, - 'in_kbart': {'doc_count': 461939}, - 'in_web': {'doc_count': 2797}, - }}, - 'preservation': { - 'buckets': [ - {'key': 'bright', 'doc_count': 444}, - {'key': 'dark', 'doc_count': 111}, - ], - 'sum_other_doc_count': 0, - }, - 'release_type': { - 'buckets': [ - {'key': 'article-journal', 'doc_count': 456}, - {'key': 'book', 'doc_count': 123}, - ], - 'sum_other_doc_count': 0, - }, - }, - 'hits': {'total': 461939, 'hits': [], 'max_score': 0.0}, - '_shards': {'successful': 5, 'total': 5, 'skipped': 0, 'failed': 0}, - 'took': 50 - } - - elastic_resp2 = { 'took': 294, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, @@ -58,7 +30,7 @@ def test_container_coverage(app, mocker): }, } - elastic_resp3 = { + elastic_resp2 = { 'took': 294, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, @@ -77,9 +49,9 @@ def test_container_coverage(app, mocker): es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') es_raw.side_effect = [ # status - (200, {}, json.dumps(elastic_resp1)), + (200, {}, json.dumps(ES_CONTAINER_STATS_RESP)), # type preservation histogram - (200, {}, json.dumps(elastic_resp3)), + (200, {}, json.dumps(elastic_resp2)), ] rv = app.get('/container/aaaaaaaaaaaaaeiraaaaaaaaam/coverage') @@ -87,14 +59,14 @@ def test_container_coverage(app, mocker): es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') es_raw.side_effect = [ - (200, {}, json.dumps(elastic_resp2)), + (200, {}, json.dumps(elastic_resp1)), ] rv = app.get('/container/aaaaaaaaaaaaaeiraaaaaaaaam/ia_coverage_years.json') assert rv.status_code == 200 es_raw.side_effect = [ - (200, {}, json.dumps(elastic_resp2)), + (200, {}, json.dumps(elastic_resp1)), ] rv = app.get('/container/aaaaaaaaaaaaaeiraaaaaaaaam/ia_coverage_years.svg') diff --git a/python/tests/web_entity_views.py b/python/tests/web_entity_views.py index c1cbdc29..b01bd815 100644 --- a/python/tests/web_entity_views.py +++ b/python/tests/web_entity_views.py @@ -1,4 +1,5 @@ +import json from fixtures import * from fatcat_web.forms import ReleaseEntityForm, FileEntityForm, ContainerEntityForm @@ -25,7 +26,14 @@ REALISTIC_DEMO_ENTITIES = { } -def test_entity_basics(app): +def test_entity_basics(app, mocker): + + es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') + # these are basic ES stats for the container view pages + es_raw.side_effect = [ + (200, {}, json.dumps(ES_CONTAINER_STATS_RESP)), + (200, {}, json.dumps(ES_CONTAINER_RANDOM_RESP)), + ] for entity_type, (ident, revision) in DUMMY_DEMO_ENTITIES.items(): # good requests @@ -132,7 +140,14 @@ def test_lookups(app): assert rv.status_code == 404 -def test_web_container(app): +def test_web_container(app, mocker): + + es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') + # these are basic ES stats for the container view pages + es_raw.side_effect = [ + (200, {}, json.dumps(ES_CONTAINER_STATS_RESP)), + (200, {}, json.dumps(ES_CONTAINER_RANDOM_RESP)), + ] rv = app.get('/container/aaaaaaaaaaaaaeiraaaaaaaaai') assert rv.status_code == 200 diff --git a/python/tests/web_search.py b/python/tests/web_search.py index ef736969..e3d19878 100644 --- a/python/tests/web_search.py +++ b/python/tests/web_search.py @@ -137,7 +137,6 @@ def test_container_stats(app, mocker): (200, {}, json.dumps(elastic_resp)), ] rv = app.get('/container/issnl/1234-5678/stats.json') - #print(rv.json) assert rv.status_code == 200 rv = app.get('/container/aaaaaaaaaaaaaeiraaaaaaaaam/stats.json') |