moviewyrm/bookwyrm/tests/test_suggested_users.py

239 lines
8.8 KiB
Python
Raw Normal View History

2021-04-26 18:09:24 +00:00
""" testing user follow suggestions """
from collections import namedtuple
from unittest.mock import patch
2021-08-03 20:27:32 +00:00
from django.db.models import Q
2021-04-26 18:09:24 +00:00
from django.test import TestCase
from bookwyrm import models
2021-08-03 19:48:44 +00:00
from bookwyrm.suggested_users import suggested_users, get_annotated_users
2021-04-26 18:09:24 +00:00
2021-11-12 17:17:00 +00:00
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
2021-09-06 20:53:49 +00:00
@patch("bookwyrm.activitystreams.add_status_task.delay")
2021-08-03 20:27:32 +00:00
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
2021-09-06 21:50:33 +00:00
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
2021-12-09 21:01:50 +00:00
@patch("bookwyrm.lists_stream.populate_lists_task.delay")
2021-09-06 23:59:58 +00:00
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
2021-08-03 20:27:32 +00:00
@patch("bookwyrm.suggested_users.rerank_user_task.delay")
2021-08-03 23:21:29 +00:00
@patch("bookwyrm.suggested_users.remove_user_task.delay")
2021-04-26 18:09:24 +00:00
class SuggestedUsers(TestCase):
"""using redis to build activity streams"""
def setUp(self):
"""use a test csv"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
2021-12-09 21:01:50 +00:00
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
2021-05-28 00:29:24 +00:00
self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "password", local=True, localname="mouse"
)
2021-04-26 18:09:24 +00:00
2021-05-28 00:29:24 +00:00
def test_get_rank(self, *_):
2021-04-26 18:09:24 +00:00
"""a float that reflects both the mutuals count and shared books"""
Mock = namedtuple("AnnotatedUserMock", ("mutuals", "shared_books"))
annotated_user_mock = Mock(3, 27)
rank = suggested_users.get_rank(annotated_user_mock)
self.assertEqual(rank, 3) # 3.9642857142857144)
2021-04-26 18:09:24 +00:00
def test_store_id_from_obj(self, *_):
"""redis key generation by user obj"""
2021-04-26 18:09:24 +00:00
self.assertEqual(
suggested_users.store_id(self.local_user),
f"{self.local_user.id}-suggestions",
)
def test_store_id_from_id(self, *_):
"""redis key generation by user id"""
self.assertEqual(
suggested_users.store_id(self.local_user.id),
f"{self.local_user.id}-suggestions",
2021-04-26 18:09:24 +00:00
)
def test_get_counts_from_rank(self, *_):
"""reverse the rank computation to get the mutuals and shared books counts"""
counts = suggested_users.get_counts_from_rank(3.9642857142857144)
self.assertEqual(counts["mutuals"], 3)
# self.assertEqual(counts["shared_books"], 27)
2021-04-26 18:09:24 +00:00
def test_get_objects_for_store(self, *_):
"""list of people to follow for a given user"""
mutual_user = models.User.objects.create_user(
"rat", "rat@local.rat", "password", local=True, localname="rat"
)
suggestable_user = models.User.objects.create_user(
"nutria",
"nutria@nutria.nutria",
"password",
local=True,
localname="nutria",
discoverable=True,
)
# you follow rat
mutual_user.followers.add(self.local_user)
# rat follows the suggested user
suggestable_user.followers.add(mutual_user)
results = suggested_users.get_objects_for_store(
f"{self.local_user.id}-suggestions"
2021-04-26 18:09:24 +00:00
)
self.assertEqual(results.count(), 1)
match = results.first()
self.assertEqual(match.id, suggestable_user.id)
self.assertEqual(match.mutuals, 1)
2021-05-28 00:29:24 +00:00
def test_get_stores_for_object(self, *_):
"""possible follows"""
mutual_user = models.User.objects.create_user(
"rat", "rat@local.rat", "password", local=True, localname="rat"
)
suggestable_user = models.User.objects.create_user(
"nutria",
"nutria@nutria.nutria",
"password",
local=True,
localname="nutria",
discoverable=True,
)
2021-05-28 00:29:24 +00:00
# you follow rat
mutual_user.followers.add(self.local_user)
# rat follows the suggested user
suggestable_user.followers.add(mutual_user)
results = suggested_users.get_stores_for_object(self.local_user)
self.assertEqual(len(results), 1)
self.assertEqual(results[0], f"{suggestable_user.id}-suggestions")
def test_get_users_for_object(self, *_):
"""given a user, who might want to follow them"""
mutual_user = models.User.objects.create_user(
"rat", "rat@local.rat", "password", local=True, localname="rat"
)
suggestable_user = models.User.objects.create_user(
"nutria",
"nutria@nutria.nutria",
"password",
local=True,
localname="nutria",
discoverable=True,
)
# you follow rat
mutual_user.followers.add(self.local_user)
# rat follows the suggested user
suggestable_user.followers.add(mutual_user)
results = suggested_users.get_users_for_object(self.local_user)
self.assertEqual(len(results), 1)
self.assertEqual(results[0], suggestable_user)
def test_rerank_user_suggestions(self, *_):
"""does it call the populate store function correctly"""
with patch(
"bookwyrm.suggested_users.SuggestedUsers.populate_store"
) as store_mock:
suggested_users.rerank_user_suggestions(self.local_user)
args = store_mock.call_args[0]
self.assertEqual(args[0], f"{self.local_user.id}-suggestions")
def test_get_suggestions(self, *_):
"""load from store"""
with patch("bookwyrm.suggested_users.SuggestedUsers.get_store") as mock:
mock.return_value = [(self.local_user.id, 7.9)]
results = suggested_users.get_suggestions(self.local_user)
self.assertEqual(results[0], self.local_user)
self.assertEqual(results[0].mutuals, 7)
2021-08-03 19:48:44 +00:00
def test_get_annotated_users(self, *_):
"""list of people you might know"""
user_1 = models.User.objects.create_user(
"nutria@local.com",
"nutria@nutria.com",
"nutriaword",
local=True,
localname="nutria",
discoverable=True,
)
user_2 = models.User.objects.create_user(
"fish@local.com",
"fish@fish.com",
"fishword",
local=True,
localname="fish",
2021-08-03 23:21:29 +00:00
)
work = models.Work.objects.create(title="Test Work")
book = models.Edition.objects.create(
title="Test Book",
remote_id="https://example.com/book/1",
parent_work=work,
2021-08-03 19:48:44 +00:00
)
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-08-03 19:48:44 +00:00
# 1 shared follow
self.local_user.following.add(user_2)
user_1.followers.add(user_2)
# 1 shared book
models.ShelfBook.objects.create(
user=self.local_user,
2021-08-03 23:21:29 +00:00
book=book,
2021-08-03 19:48:44 +00:00
shelf=self.local_user.shelf_set.first(),
)
models.ShelfBook.objects.create(
2021-08-03 23:21:29 +00:00
user=user_1, book=book, shelf=user_1.shelf_set.first()
2021-08-03 19:48:44 +00:00
)
2021-08-03 20:27:32 +00:00
result = get_annotated_users(self.local_user)
2021-08-03 23:21:29 +00:00
self.assertEqual(result.count(), 1)
2021-08-03 19:48:44 +00:00
self.assertTrue(user_1 in result)
self.assertFalse(user_2 in result)
user_1_annotated = result.get(id=user_1.id)
self.assertEqual(user_1_annotated.mutuals, 1)
# self.assertEqual(user_1_annotated.shared_books, 1)
2021-08-03 19:48:44 +00:00
def test_get_annotated_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(
f"{i}@local.com",
f"{i}@nutria.com",
2021-08-03 19:48:44 +00:00
"password",
local=True,
localname=i,
)
user.following.add(user_1)
user.followers.add(self.local_user)
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-08-03 19:48:44 +00:00
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 = get_annotated_users(
self.local_user,
~Q(id=self.local_user.id),
~Q(followers=self.local_user),
)
user_1_annotated = result.get(id=user_1.id)
self.assertEqual(user_1_annotated.mutuals, 3)