From 1d29c565406552149ef83862f5dcd65c9355a268 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Wed, 5 Jan 2022 15:13:16 +0900 Subject: [PATCH] Improve measure_member_progress method --- backend/api/managers.py | 14 ++++++++++---- backend/api/tests/test_models.py | 18 ++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/backend/api/managers.py b/backend/api/managers.py index 4a41c9e0..6f9683fd 100644 --- a/backend/api/managers.py +++ b/backend/api/managers.py @@ -103,11 +103,17 @@ class ExampleStateManager(Manager): queryset = self.filter(example_id__in=examples) return queryset.distinct().values('example').count() - def count_user(self, examples): + def measure_member_progress(self, examples, members): done_count = self.filter(example_id__in=examples)\ .values('confirmed_by__username')\ .annotate(total=Count('confirmed_by')) - return { - obj['confirmed_by__username']: obj['total'] - for obj in done_count + response = { + 'total': examples.count(), + 'progress': { + obj['confirmed_by__username']: obj['total'] for obj in done_count + } } + for member in members: + if member.username not in response['progress']: + response['progress'][member.username] = 0 + return response diff --git a/backend/api/tests/test_models.py b/backend/api/tests/test_models.py index 739e5aac..7ea712c1 100644 --- a/backend/api/tests/test_models.py +++ b/backend/api/tests/test_models.py @@ -194,18 +194,24 @@ class TestExampleState(TestCase): self.assertEqual(done, 1) def test_initial_user(self): - user_count = ExampleState.objects.count_user(self.examples) - self.assertEqual(user_count, {}) + progress = ExampleState.objects.measure_member_progress(self.examples, self.project.users) + expected_progress = {user.username: 0 for user in self.project.users} + self.assertEqual(progress, {'total': 2, 'progress': expected_progress}) def test_user_count_after_confirmation(self): mommy.make('ExampleState', example=self.example, confirmed_by=self.project.users[0]) - user_count = ExampleState.objects.count_user(self.examples) - self.assertEqual(user_count, {self.project.users[0].username: 1}) + progress = ExampleState.objects.measure_member_progress(self.examples, self.project.users) + expected_progress = {user.username: 0 for user in self.project.users} + expected_progress[self.project.users[0].username] = 1 + self.assertEqual(progress, {'total': 2, 'progress': expected_progress}) def test_user_count_after_multiple_user_confirmation(self): user1 = self.project.users[0] user2 = self.project.users[1] mommy.make('ExampleState', example=self.example, confirmed_by=user1) mommy.make('ExampleState', example=self.example, confirmed_by=user2) - user_count = ExampleState.objects.count_user(self.examples) - self.assertEqual(user_count, {user1.username: 1, user2.username: 1}) + progress = ExampleState.objects.measure_member_progress(self.examples, self.project.users) + expected_progress = {user.username: 0 for user in self.project.users} + expected_progress[user1.username] = 1 + expected_progress[user2.username] = 1 + self.assertEqual(progress, {'total': 2, 'progress': expected_progress})