Splitting new archive test for thoroughness.

This commit is contained in:
Roland 2021-12-31 20:23:49 -07:00
parent 6e16ad9a23
commit a95cbd74c9
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
3 changed files with 66 additions and 42 deletions

View file

@ -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()

View file

@ -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])

View file

@ -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