Fixing archive deleting.

This commit is contained in:
Roland 2021-12-31 22:35:23 -07:00
parent fb1f1c6926
commit 3cb2aaf6cf
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
10 changed files with 202 additions and 111 deletions

View file

@ -1,16 +1,15 @@
import json
import pathlib
from datetime import datetime
from http import HTTPStatus
import mock
from modules.archive.lib import new_archive, get_or_create_domain_and_url, get_urls, get_url_count, delete_url, \
_refresh_archives, get_new_archive_files
from modules.archive.lib import new_archive, get_or_create_domain_and_url, get_urls, get_url_count, _refresh_archives, \
get_new_archive_files, delete_archive
from modules.archive.models import Archive, URL, Domain
from wrolpi.common import get_media_directory
from wrolpi.db import get_db_session
from wrolpi.errors import InvalidDomain, UnknownURL
from wrolpi.errors import InvalidDomain
from wrolpi.media_path import MediaPath
from wrolpi.root_api import CustomJSONEncoder
from wrolpi.test.common import TestAPI, wrap_test_db, wrap_media_directory
@ -209,37 +208,6 @@ class TestArchive(TestAPI):
new_archive('https://example.org', sync=True)
self.assertEqual(get_url_count('example.org'), 1)
@wrap_test_db
def test_delete_url(self):
with mock.patch('modules.archive.lib.request_archive', make_fake_request_archive()):
archive = new_archive('https://example.com', sync=True)
with get_db_session() as session:
urls = session.query(URL).all()
self.assertEqual(len(urls), 1)
archives = session.query(Archive).all()
self.assertEqual(len(archives), 1)
self.assertIsNotNone(archive.singlefile_path)
singlefile_path = archive.singlefile_path.path
self.assertTrue(singlefile_path.is_file())
url_id = archive.url.id
# Delete the URL, all archives and all files.
delete_url(url_id)
self.assertFalse(singlefile_path.exists())
# Can't delete the same URL twice.
self.assertRaises(UnknownURL, delete_url, url_id)
with get_db_session() as session:
urls = session.query(URL).all()
self.assertEqual(len(urls), 0)
# Bad ID
self.assertRaises(UnknownURL, delete_url, 123)
@wrap_test_db
def test_get_title_from_html(self):
with wrap_media_directory():
@ -373,7 +341,7 @@ 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_fills_contents(test_session, archive_directory, archive_factory, test_client):
def test_refresh_archives_fills_contents(test_session, archive_factory, test_client):
"""
Refreshing archives fills in any missing contents.
"""
@ -405,3 +373,25 @@ def test_refresh_archives_fills_contents(test_session, archive_directory, archiv
assert archive2.contents
assert archive3.contents
assert not archive4.contents
def test_delete_archive(test_session, archive_factory):
archive1 = archive_factory('example.com', 'https://example.com/1')
archive2 = archive_factory('example.com', 'https://example.com/1')
archive3 = archive_factory('example.com', 'https://example.com/1')
url = test_session.query(URL).one()
assert url.latest == archive3
# Delete the oldest.
delete_archive(archive1.id)
# Delete the latest.
delete_archive(archive3.id)
# Delete the last archive. The URL and Domain should also be deleted.
delete_archive(archive2.id)
url = test_session.query(URL).one_or_none()
assert url is None
domain = test_session.query(Domain).one_or_none()
assert domain is None