Adds tests for recent import average time

This commit is contained in:
Mouse Reeve 2022-11-10 12:27:48 -08:00
parent f0d3ceefa0
commit f80e2465ed
2 changed files with 46 additions and 9 deletions

View file

@ -1,4 +1,5 @@
""" test for app action functionality """
import datetime
import pathlib
from unittest.mock import patch
@ -106,3 +107,45 @@ class ImportViews(TestCase):
with patch("bookwyrm.models.import_job.import_item_task.delay") as mock:
views.retry_item(request, job.id, item.id)
self.assertEqual(mock.call_count, 1)
def test_get_average_import_time_no_imports(self):
"""Give people a sense of the timing"""
result = views.imports.import_data.get_average_import_time()
self.assertIsNone(result)
def test_get_average_import_time_no_imports_this_week(self):
"""Give people a sense of the timing"""
models.ImportJob.objects.create(
user=self.local_user,
created_date=datetime.datetime(2000, 1, 1),
updated_date=datetime.datetime(2001, 1, 1),
status="complete",
complete=True,
mappings={},
)
result = views.imports.import_data.get_average_import_time()
self.assertIsNone(result)
def test_get_average_import_time_with_data(self):
"""Now, with data"""
now = datetime.datetime.now()
two_hours_ago = now - datetime.timedelta(hours=2)
four_hours_ago = now - datetime.timedelta(hours=4)
models.ImportJob.objects.create(
user=self.local_user,
created_date=two_hours_ago,
updated_date=now,
status="complete",
complete=True,
mappings={},
)
models.ImportJob.objects.create(
user=self.local_user,
created_date=four_hours_ago,
updated_date=now,
status="complete",
complete=True,
mappings={},
)
result = views.imports.import_data.get_average_import_time()
self.assertEqual(result, 3 * 60 * 60)

View file

@ -44,11 +44,7 @@ class Import(View):
),
}
seconds = get_or_set(
"avg-import-time",
get_average_import_time,
timeout=86400
)
seconds = get_or_set("avg-import-time", get_average_import_time, timeout=86400)
if seconds > 60**2:
data["recent_avg_hours"] = seconds / (60**2)
elif seconds:
@ -98,9 +94,7 @@ def get_average_import_time() -> float:
"""Helper to figure out how long imports are taking (returns seconds)"""
last_week = timezone.now() - datetime.timedelta(days=7)
recent_avg = (
models.ImportJob.objects.filter(
created_date__gte=last_week, status="complete"
)
models.ImportJob.objects.filter(created_date__gte=last_week, status="complete")
.annotate(
runtime=ExpressionWrapper(
F("updated_date") - F("created_date"),