Merge pull request #1562 from bookwyrm-social/suggested-users-tests

Adds a few more tests to the suggested users module
This commit is contained in:
Mouse Reeve 2021-10-22 10:39:11 -07:00 committed by GitHub
commit ac749d94b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -35,11 +35,18 @@ class SuggestedUsers(TestCase):
rank = suggested_users.get_rank(annotated_user_mock) rank = suggested_users.get_rank(annotated_user_mock)
self.assertEqual(rank, 3) # 3.9642857142857144) self.assertEqual(rank, 3) # 3.9642857142857144)
def test_store_id(self, *_): def test_store_id_from_obj(self, *_):
"""redis key generation""" """redis key generation by user obj"""
self.assertEqual( self.assertEqual(
suggested_users.store_id(self.local_user), suggested_users.store_id(self.local_user),
"{:d}-suggestions".format(self.local_user.id), 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",
) )
def test_get_counts_from_rank(self, *_): def test_get_counts_from_rank(self, *_):
@ -69,21 +76,74 @@ class SuggestedUsers(TestCase):
suggestable_user.followers.add(mutual_user) suggestable_user.followers.add(mutual_user)
results = suggested_users.get_objects_for_store( results = suggested_users.get_objects_for_store(
"{:d}-suggestions".format(self.local_user.id) f"{self.local_user.id}-suggestions"
) )
self.assertEqual(results.count(), 1) self.assertEqual(results.count(), 1)
match = results.first() match = results.first()
self.assertEqual(match.id, suggestable_user.id) self.assertEqual(match.id, suggestable_user.id)
self.assertEqual(match.mutuals, 1) self.assertEqual(match.mutuals, 1)
def test_create_user_signal(self, *_): def test_get_stores_for_object(self, *_):
"""build suggestions for new users""" """possible follows"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay") as mock: mutual_user = models.User.objects.create_user(
models.User.objects.create_user( "rat", "rat@local.rat", "password", local=True, localname="rat"
"nutria", "nutria@nu.tria", "password", local=True, localname="nutria" )
) suggestable_user = models.User.objects.create_user(
"nutria",
"nutria@nutria.nutria",
"password",
local=True,
localname="nutria",
discoverable=True,
)
self.assertEqual(mock.call_count, 1) # 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)
def test_get_annotated_users(self, *_): def test_get_annotated_users(self, *_):
"""list of people you might know""" """list of people you might know"""
@ -144,8 +204,8 @@ class SuggestedUsers(TestCase):
) )
for i in range(3): for i in range(3):
user = models.User.objects.create_user( user = models.User.objects.create_user(
"{:d}@local.com".format(i), f"{i}@local.com",
"{:d}@nutria.com".format(i), f"{i}@nutria.com",
"password", "password",
local=True, local=True,
localname=i, localname=i,
@ -175,3 +235,12 @@ class SuggestedUsers(TestCase):
) )
user_1_annotated = result.get(id=user_1.id) user_1_annotated = result.get(id=user_1.id)
self.assertEqual(user_1_annotated.mutuals, 3) self.assertEqual(user_1_annotated.mutuals, 3)
def test_create_user_signal(self, *_):
"""build suggestions for new users"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay") as mock:
models.User.objects.create_user(
"nutria", "nutria@nu.tria", "password", local=True, localname="nutria"
)
self.assertEqual(mock.call_count, 1)