From a95cbd74c95a93f35476c25c7d1a0376fd2a5910 Mon Sep 17 00:00:00 2001 From: Roland Date: Fri, 31 Dec 2021 20:23:49 -0700 Subject: [PATCH] Splitting new archive test for thoroughness. --- modules/archive/conftest.py | 5 +-- modules/archive/test/test_api.py | 49 +++++++++++++++++++++++++++++ modules/archive/test/test_lib.py | 54 +++++++++----------------------- 3 files changed, 66 insertions(+), 42 deletions(-) create mode 100644 modules/archive/test/test_api.py diff --git a/modules/archive/conftest.py b/modules/archive/conftest.py index 8f6ed2d1..2f18abd1 100644 --- a/modules/archive/conftest.py +++ b/modules/archive/conftest.py @@ -26,8 +26,8 @@ def archive_factory(test_session, archive_directory): now = time_generator() - def _(domain: str = None, url: str = None) -> Archive: - title = str(uuid4()) + def _(domain: str = None, url: str = None, title: str = None, contents: str = None) -> Archive: + title = title or str(uuid4()) url = url or f'https://example.com/{title}' domain_dir = domain or title domain_dir = archive_directory / domain_dir @@ -74,6 +74,7 @@ def archive_factory(test_session, archive_directory): title=title, url_id=url_.id, domain_id=domain.id, + contents=contents, ) test_session.add(archive) test_session.commit() diff --git a/modules/archive/test/test_api.py b/modules/archive/test/test_api.py new file mode 100644 index 00000000..835c09ee --- /dev/null +++ b/modules/archive/test/test_api.py @@ -0,0 +1,49 @@ +import json +from http import HTTPStatus + + +def test_archives_search(test_session, archive_directory, archive_factory, test_client): + """ + Archives can be searched by their title and their contents. + """ + archive_factory('example.com', 'https://example.com/one', 'my archive', 'foo bar qux') + archive_factory('example.com', 'https://example.com/one', 'other archive', 'foo baz qux qux') + archive_factory('example.org', title='archive third', contents='baz qux qux qux') + archive_factory('example.org') # has no contents + + def check_results(data, ids): + request, response = test_client.post('/api/archive/search', content=json.dumps(data)) + assert response.status_code == HTTPStatus.OK, response.json + assert [i['id'] for i in response.json['archives']] == ids + + # 1 and 2 contain "foo". + data = {'search_str': 'foo'} + check_results(data, [1, 2]) + + # 2 and 3 contain "baz". + data = {'search_str': 'baz'} + check_results(data, [2, 3]) + + # 1 contains "bar". + data = {'search_str': 'bar'} + check_results(data, [1, ]) + + # No archives contain "huzzah" + data = {'search_str': 'huzzah'} + check_results(data, []) + + # Only 3 contains "baz" and is in domain "example.org" + data = {'search_str': 'baz', 'domain': 'example.org'} + check_results(data, [3, ]) + + # 1's title contains "my", this is ignored by Postgres. + data = {'search_str': 'my'} + check_results(data, []) + + # 3's title contains "third". + data = {'search_str': 'third'} + check_results(data, [3, ]) + + # All titles contain "qux", but they contain different amounts. They are ordered by the amount. + data = {'search_str': 'qux'} + check_results(data, [3, 2, 1]) diff --git a/modules/archive/test/test_lib.py b/modules/archive/test/test_lib.py index 3a2ff7c0..9c22aed4 100644 --- a/modules/archive/test/test_lib.py +++ b/modules/archive/test/test_lib.py @@ -373,14 +373,14 @@ def test_archive_refresh_deleted_archive(test_session, archive_directory, archiv check_counts(archive_count=0, url_count=0, domain_count=0) -def test_refresh_archives_search(test_session, archive_directory, archive_factory, test_client): +def test_refresh_archives_fills_contents(test_session, archive_directory, archive_factory, test_client): """ - Archives can be searched. An Archive missing `contents` will be filled using the readability txt file. + Refreshing archives fills in any missing contents. """ archive1 = archive_factory('example.com', 'https://example.com/one') archive2 = archive_factory('example.com', 'https://example.com/one') archive3 = archive_factory('example.org') - archive_factory('example.org') # has no contents + archive4 = archive_factory('example.org') # has no contents contents_title_archive = [ ('foo bar qux', 'my archive', archive1), @@ -393,41 +393,15 @@ def test_refresh_archives_search(test_session, archive_directory, archive_factor fh.write(contents) test_session.commit() + # Contents are empty. + assert not archive1.contents + assert not archive2.contents + assert not archive3.contents + assert not archive4.contents + + # Fill the contents. _refresh_archives() - - def check_results(data, ids): - request, response = test_client.post('/api/archive/search', content=json.dumps(data)) - assert response.status_code == HTTPStatus.OK, response.json - assert [i['id'] for i in response.json['archives']] == ids - - # 1 and 2 contain "foo". - data = {'search_str': 'foo'} - check_results(data, [1, 2]) - - # 2 and 3 contain "baz". - data = {'search_str': 'baz'} - check_results(data, [2, 3]) - - # 1 contains "bar". - data = {'search_str': 'bar'} - check_results(data, [1, ]) - - # No archives contain "huzzah" - data = {'search_str': 'huzzah'} - check_results(data, []) - - # Only 3 contains "baz" and is in domain "example.org" - data = {'search_str': 'baz', 'domain': 'example.org'} - check_results(data, [3, ]) - - # 1's title contains "my", this is ignored by Postgres. - data = {'search_str': 'my'} - check_results(data, []) - - # 3's title contains "third". - data = {'search_str': 'third'} - check_results(data, [3, ]) - - # All titles contain "qux", but they contain different amounts. They are ordered by the amount. - data = {'search_str': 'qux'} - check_results(data, [3, 2, 1]) + assert archive1.contents + assert archive2.contents + assert archive3.contents + assert not archive4.contents