From 7db4ea73f2ef54031ebd0abb45c41ac71f77a705 Mon Sep 17 00:00:00 2001 From: Roland Date: Wed, 2 Nov 2022 15:12:48 -0600 Subject: [PATCH] Archive alternatives must have a URL. --- modules/archive/models.py | 1 + modules/archive/test/test_api.py | 8 +++----- modules/archive/test/test_lib.py | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/modules/archive/models.py b/modules/archive/models.py index 17041046..b652654e 100644 --- a/modules/archive/models.py +++ b/modules/archive/models.py @@ -137,6 +137,7 @@ class Archive(Base, ModelHelper): alternatives = list(session.query(Archive).filter( Archive.id != self.id, Archive.url == self.url, + Archive.url != None, ).order_by(Archive.archive_datetime)) return alternatives diff --git a/modules/archive/test/test_api.py b/modules/archive/test/test_api.py index 5fdd8d92..46c47880 100644 --- a/modules/archive/test/test_api.py +++ b/modules/archive/test/test_api.py @@ -106,9 +106,7 @@ def test_archives_search_no_query(test_session, archive_factory, test_client): def test_archive_and_domain_crud(test_session, test_client, archive_factory): - """ - Getting an Archive returns it's File. Testing deleting Archives. - """ + """Getting an Archive returns it's File. Testing deleting Archives.""" # Can get empty results. request, response = test_client.get(f'/api/archive/1') assert response.status_code == HTTPStatus.NOT_FOUND @@ -116,8 +114,8 @@ def test_archive_and_domain_crud(test_session, test_client, archive_factory): assert response.status_code == HTTPStatus.OK assert response.json['domains'] == [] - archive1 = archive_factory(domain='example.com') - archive2 = archive_factory(domain='example.com') + archive1 = archive_factory(domain='example.com', url='https://example.com/1') + archive2 = archive_factory(domain='example.com', url='https://example.com/1') test_session.commit() # Archive1 has Archive2 as alternative. diff --git a/modules/archive/test/test_lib.py b/modules/archive/test/test_lib.py index e9d2b180..f0e04829 100644 --- a/modules/archive/test/test_lib.py +++ b/modules/archive/test/test_lib.py @@ -686,3 +686,30 @@ async def test_archive_download_index(test_session, test_directory, image_file): and archive.readability_json_file.associated \ and archive.readability_file.associated \ and archive.screenshot_file.associated, 'Archive files must be associated' + + +def test_archive_alternatives(test_session, archive_factory): + """Archive's that share a URL are alternatives. + + Archive's with an empty URL are not associated.""" + archive1 = archive_factory(url='https://example.com/1') + archive2 = archive_factory(url='https://example.com/1') + archive3 = archive_factory(url='https://example.com/2') + archive4 = archive_factory() + archive5 = archive_factory() + + assert archive1 in archive2.alternatives, 'archive1 is an alternative for archive2' + assert archive2 in archive1.alternatives, 'archive2 is an alternative for archive2' + assert archive3 not in archive1.alternatives and archive3 not in archive2.alternatives, 'archive3 is unique' + assert not archive3.alternatives, 'archive3 has no alternatives' + assert not archive4.alternatives, 'archive4 has no alternatives' + assert not archive5.alternatives, 'archive5 has no alternatives' + + test_session.commit() + + assert archive1 in archive2.alternatives, 'archive1 is an alternative for archive2' + assert archive2 in archive1.alternatives, 'archive2 is an alternative for archive2' + assert archive3 not in archive1.alternatives and archive3 not in archive2.alternatives, 'archive3 is unique' + assert not archive3.alternatives, 'archive3 has no alternatives' + assert not archive4.alternatives, 'archive4 has no alternatives' + assert not archive5.alternatives, 'archive5 has no alternatives'