From c42719ee1ec535fe89759896e555ad9be0c577d1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 28 Mar 2021 10:32:58 -0700 Subject: [PATCH 1/2] Fixes wildly high similiarity metrics in directory --- bookwyrm/tests/views/test_helpers.py | 46 ++++++++++++++++++++++++++++ bookwyrm/views/helpers.py | 2 ++ 2 files changed, 48 insertions(+) diff --git a/bookwyrm/tests/views/test_helpers.py b/bookwyrm/tests/views/test_helpers.py index 7d6df948e..2d100f7bf 100644 --- a/bookwyrm/tests/views/test_helpers.py +++ b/bookwyrm/tests/views/test_helpers.py @@ -2,6 +2,7 @@ import json from unittest.mock import patch import pathlib +from django.db.models import Q from django.test import TestCase from django.test.client import RequestFactory import responses @@ -287,3 +288,48 @@ class ViewsHelpers(TestCase): remote_user_annotated = result.get(id=self.remote_user.id) self.assertEqual(remote_user_annotated.mutuals, 0) self.assertEqual(remote_user_annotated.shared_books, 0) + + def test_get_suggested_users_counts(self, _): + """ correct counting for multiple shared attributed """ + user_1 = models.User.objects.create_user( + "nutria@local.com", + "nutria@nutria.com", + "nutriaword", + local=True, + localname="nutria", + discoverable=True, + ) + for i in range(3): + user = models.User.objects.create_user( + "{:d}@local.com".format(i), + "{:d}@nutria.com".format(i), + "password", + local=True, + localname=i, + ) + user.followers.add(user_1) + user.followers.add(self.local_user) + + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): + for i in range(3): + book = models.Edition.objects.create( + title=i, + parent_work=models.Work.objects.create(title=i), + ) + models.ShelfBook.objects.create( + user=self.local_user, + book=book, + shelf=self.local_user.shelf_set.first(), + ) + models.ShelfBook.objects.create( + user=user_1, book=book, shelf=user_1.shelf_set.first() + ) + + result = views.helpers.get_suggested_users( + self.local_user, + ~Q(id=self.local_user.id), + ~Q(followers=self.local_user), + ) + self.assertEqual(result.count(), 2) + user_1_annotated = result.get(id=user_1.id) + self.assertEqual(user_1_annotated.mutuals, 3) diff --git a/bookwyrm/views/helpers.py b/bookwyrm/views/helpers.py index 2a5f9d105..2c2f96c3b 100644 --- a/bookwyrm/views/helpers.py +++ b/bookwyrm/views/helpers.py @@ -205,6 +205,7 @@ def get_suggested_users(user, *args, **kwargs): ~Q(id__in=user.following.all()), following__in=user.following.all(), ), + distinct=True ), shared_books=Count( "shelfbook", @@ -214,6 +215,7 @@ def get_suggested_users(user, *args, **kwargs): s.book.parent_work for s in user.shelfbook_set.all() ], ), + distinct=True ), ) ) From 6f99947cac3fe7fb9977fb70b88efcb87e4b7161 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 28 Mar 2021 10:45:46 -0700 Subject: [PATCH 2/2] Fixes python formatting --- bookwyrm/views/helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/views/helpers.py b/bookwyrm/views/helpers.py index 2c2f96c3b..52b5818f3 100644 --- a/bookwyrm/views/helpers.py +++ b/bookwyrm/views/helpers.py @@ -205,7 +205,7 @@ def get_suggested_users(user, *args, **kwargs): ~Q(id__in=user.following.all()), following__in=user.following.all(), ), - distinct=True + distinct=True, ), shared_books=Count( "shelfbook", @@ -215,7 +215,7 @@ def get_suggested_users(user, *args, **kwargs): s.book.parent_work for s in user.shelfbook_set.all() ], ), - distinct=True + distinct=True, ), ) )