forked from mirrors/bookwyrm
Activitstreams tests
This commit is contained in:
parent
8fc23ba184
commit
224dc4100a
2 changed files with 81 additions and 2 deletions
|
@ -1,6 +1,9 @@
|
||||||
""" testing activitystreams """
|
""" testing activitystreams """
|
||||||
|
from datetime import datetime
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
from bookwyrm import activitystreams, models
|
from bookwyrm import activitystreams, models
|
||||||
|
|
||||||
|
|
||||||
|
@ -51,13 +54,63 @@ class Activitystreams(TestCase):
|
||||||
"""the abstract base class for stream objects"""
|
"""the abstract base class for stream objects"""
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.test_stream.stream_id(self.local_user),
|
self.test_stream.stream_id(self.local_user),
|
||||||
"{}-test".format(self.local_user.id),
|
f"{self.local_user.id}-test",
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.test_stream.unread_id(self.local_user),
|
self.test_stream.unread_id(self.local_user),
|
||||||
"{}-test-unread".format(self.local_user.id),
|
f"{self.local_user.id}-test-unread",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_unread_by_status_type_id(self, *_):
|
||||||
|
"""stream for status type"""
|
||||||
|
self.assertEqual(
|
||||||
|
self.test_stream.unread_by_status_type_id(self.local_user),
|
||||||
|
f"{self.local_user.id}-test-unread-by-type",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_get_rank(self, *_):
|
||||||
|
"""sort order"""
|
||||||
|
date = datetime(2022, 1, 28, 0, 0, tzinfo=timezone.utc)
|
||||||
|
status = models.Status.objects.create(
|
||||||
|
user=self.remote_user,
|
||||||
|
content="hi",
|
||||||
|
privacy="direct",
|
||||||
|
published_date=date,
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
str(self.test_stream.get_rank(status)),
|
||||||
|
"1643328000.0",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_get_activity_stream(self, *_):
|
||||||
|
"""load statuses"""
|
||||||
|
status = models.Status.objects.create(
|
||||||
|
user=self.remote_user,
|
||||||
|
content="hi",
|
||||||
|
privacy="direct",
|
||||||
|
)
|
||||||
|
status2 = models.Comment.objects.create(
|
||||||
|
user=self.remote_user,
|
||||||
|
content="hi",
|
||||||
|
privacy="direct",
|
||||||
|
book=self.book,
|
||||||
|
)
|
||||||
|
models.Comment.objects.create(
|
||||||
|
user=self.remote_user,
|
||||||
|
content="hi",
|
||||||
|
privacy="direct",
|
||||||
|
book=self.book,
|
||||||
|
)
|
||||||
|
with patch("bookwyrm.activitystreams.r.set"), patch(
|
||||||
|
"bookwyrm.activitystreams.r.delete"
|
||||||
|
), patch("bookwyrm.activitystreams.ActivityStream.get_store") as redis_mock:
|
||||||
|
redis_mock.return_value = [status.id, status2.id]
|
||||||
|
result = self.test_stream.get_activity_stream(self.local_user)
|
||||||
|
self.assertEqual(result.count(), 2)
|
||||||
|
self.assertEqual(result.first(), status)
|
||||||
|
self.assertEqual(result.last(), status2)
|
||||||
|
self.assertIsInstance(result.last(), models.Comment)
|
||||||
|
|
||||||
def test_abstractstream_get_audience(self, *_):
|
def test_abstractstream_get_audience(self, *_):
|
||||||
"""get a list of users that should see a status"""
|
"""get a list of users that should see a status"""
|
||||||
status = models.Status.objects.create(
|
status = models.Status.objects.create(
|
||||||
|
|
|
@ -52,3 +52,29 @@ class Activitystreams(TestCase):
|
||||||
# yes book, yes audience
|
# yes book, yes audience
|
||||||
result = activitystreams.BooksStream().get_statuses_for_user(self.local_user)
|
result = activitystreams.BooksStream().get_statuses_for_user(self.local_user)
|
||||||
self.assertEqual(list(result), [status])
|
self.assertEqual(list(result), [status])
|
||||||
|
|
||||||
|
def test_book_statuses(self, *_):
|
||||||
|
"""statuses about a book"""
|
||||||
|
alt_book = models.Edition.objects.create(
|
||||||
|
title="hi", parent_work=self.book.parent_work
|
||||||
|
)
|
||||||
|
status = models.Status.objects.create(
|
||||||
|
user=self.local_user, content="hi", privacy="public"
|
||||||
|
)
|
||||||
|
status = models.Comment.objects.create(
|
||||||
|
user=self.remote_user, content="hi", privacy="public", book=alt_book
|
||||||
|
)
|
||||||
|
models.ShelfBook.objects.create(
|
||||||
|
user=self.local_user,
|
||||||
|
shelf=self.local_user.shelf_set.first(),
|
||||||
|
book=self.book,
|
||||||
|
)
|
||||||
|
with patch(
|
||||||
|
"bookwyrm.activitystreams.BooksStream.bulk_add_objects_to_store"
|
||||||
|
) as redis_mock:
|
||||||
|
activitystreams.BooksStream().add_book_statuses(self.local_user, self.book)
|
||||||
|
args = redis_mock.call_args[0]
|
||||||
|
queryset = args[0]
|
||||||
|
self.assertEqual(queryset.count(), 1)
|
||||||
|
self.assertTrue(status in queryset)
|
||||||
|
self.assertEqual(args[1], f"{self.local_user.id}-books")
|
||||||
|
|
Loading…
Reference in a new issue