ApprovalQueue: use a materialized table #240

Merged
Oleg-Komarov merged 4 commits from approval-queue-fix into main 2024-08-23 15:11:46 +02:00
2 changed files with 99 additions and 0 deletions
Showing only changes of commit c0b3fb3d02 - Show all commits

View File

@ -6,6 +6,7 @@ django-debug-toolbar==4.2.0
factory-boy==3.2.1 factory-boy==3.2.1
Faker==13.15.1 Faker==13.15.1
filelock==3.7.1 filelock==3.7.1
freezegun==1.5.1
identify==2.5.2 identify==2.5.2
mdgen==0.1.10 mdgen==0.1.10
nodeenv==1.7.0 nodeenv==1.7.0

View File

@ -1,7 +1,11 @@
from datetime import datetime, timedelta, timezone
from django.test import TestCase from django.test import TestCase
from django.shortcuts import reverse from django.shortcuts import reverse
from freezegun import freeze_time
from common.tests.factories.extensions import create_version from common.tests.factories.extensions import create_version
from common.tests.factories.users import UserFactory
from files.models import File from files.models import File
from reviewers.models import ApprovalActivity from reviewers.models import ApprovalActivity
@ -40,3 +44,97 @@ class CommentsViewTest(TestCase):
# Display only previews and version that are 'pending review' # Display only previews and version that are 'pending review'
# Approval requires manager role # Approval requires manager role
class QueueOrderTest(TestCase):
fixtures = ['licenses']
def test_order(self):
extensions = []
user = UserFactory()
for i in range(5):
version = create_version(status=File.STATUSES.AWAITING_REVIEW)
extension = version.extension
extensions.append(extension)
# to control the distance between timestamps
starting_datetime = datetime(2020, 12, 31, 23, 2, 3, tzinfo=timezone.utc)
delta_s = 0
with freeze_time(starting_datetime + timedelta(seconds=delta_s)):
ApprovalActivity(
type=ApprovalActivity.ActivityType.AWAITING_REVIEW,
user=user,
extension=extensions[0],
message='test comment',
).save()
delta_s += 1
with freeze_time(starting_datetime + timedelta(seconds=delta_s)):
ApprovalActivity(
type=ApprovalActivity.ActivityType.AWAITING_CHANGES,
user=user,
extension=extensions[1],
message='test comment',
).save()
delta_s += 1
with freeze_time(starting_datetime + timedelta(seconds=delta_s)):
ApprovalActivity(
type=ApprovalActivity.ActivityType.AWAITING_CHANGES,
user=user,
extension=extensions[2],
message='test comment',
).save()
apr = None
delta_s += 1
with freeze_time(starting_datetime + timedelta(seconds=delta_s)):
apr = ApprovalActivity(
date_created=starting_datetime + timedelta(seconds=delta_s),
type=ApprovalActivity.ActivityType.APPROVED,
user=user,
extension=extensions[3],
message='test comment',
)
apr.save()
delta_s += 1
with freeze_time(starting_datetime + timedelta(seconds=delta_s)):
ApprovalActivity(
date_created=starting_datetime + timedelta(seconds=delta_s),
type=ApprovalActivity.ActivityType.COMMENT,
user=user,
extension=extensions[3],
message='test comment',
).save()
delta_s += 1
with freeze_time(starting_datetime + timedelta(seconds=delta_s)):
ApprovalActivity(
date_created=starting_datetime + timedelta(seconds=delta_s),
type=ApprovalActivity.ActivityType.COMMENT,
user=user,
extension=extensions[4],
message='test comment',
).save()
response = self.client.get(reverse('reviewers:approval-queue'))
queue = response.context['object_list']
# extensions[4] doesn't have meaningful activity
self.assertEqual(len(queue), 4)
# ordered by category
self.assertEqual(queue[0].extension, extensions[0])
# and within category by recent timestamp
self.assertEqual(queue[1].extension, extensions[2])
self.assertEqual(queue[2].extension, extensions[1])
self.assertEqual(queue[3].extension, extensions[3])
# counts are correct
self.assertEqual(queue[0].activity_count, 1)
self.assertEqual(queue[1].activity_count, 1)
self.assertEqual(queue[2].activity_count, 1)
self.assertEqual(queue[3].activity_count, 2)
# latest_activity doesn't account for non-meaningful activity
self.assertEqual(queue[3].latest_activity, apr)