Browse Source

Improve measure_member_progress method

pull/1619/head
Hironsan 2 years ago
parent
commit
1d29c56540
2 changed files with 22 additions and 10 deletions
  1. 14
      backend/api/managers.py
  2. 18
      backend/api/tests/test_models.py

14
backend/api/managers.py

@ -103,11 +103,17 @@ class ExampleStateManager(Manager):
queryset = self.filter(example_id__in=examples) queryset = self.filter(example_id__in=examples)
return queryset.distinct().values('example').count() 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)\ done_count = self.filter(example_id__in=examples)\
.values('confirmed_by__username')\ .values('confirmed_by__username')\
.annotate(total=Count('confirmed_by')) .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

18
backend/api/tests/test_models.py

@ -194,18 +194,24 @@ class TestExampleState(TestCase):
self.assertEqual(done, 1) self.assertEqual(done, 1)
def test_initial_user(self): 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): def test_user_count_after_confirmation(self):
mommy.make('ExampleState', example=self.example, confirmed_by=self.project.users[0]) 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): def test_user_count_after_multiple_user_confirmation(self):
user1 = self.project.users[0] user1 = self.project.users[0]
user2 = self.project.users[1] user2 = self.project.users[1]
mommy.make('ExampleState', example=self.example, confirmed_by=user1) mommy.make('ExampleState', example=self.example, confirmed_by=user1)
mommy.make('ExampleState', example=self.example, confirmed_by=user2) 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})
Loading…
Cancel
Save