Adds a couple suggestions tests

This commit is contained in:
Mouse Reeve 2024-08-26 13:57:22 -07:00
parent 01d7088e28
commit 6b615d45b3
3 changed files with 56 additions and 5 deletions

View file

@ -162,6 +162,11 @@ class List(OrderedCollectionMixin, BookWyrmModel):
self.embed_key = uuid.uuid4() self.embed_key = uuid.uuid4()
update_fields = add_update_fields(update_fields, "embed_key") update_fields = add_update_fields(update_fields, "embed_key")
# ensure that suggestion lists have the right properties
if self.suggests_for:
self.privacy = "public"
self.curation = "open"
super().save(*args, update_fields=update_fields, **kwargs) super().save(*args, update_fields=update_fields, **kwargs)

View file

@ -51,6 +51,11 @@ class BookViews(TestCase):
remote_id="https://example.com/book/1", remote_id="https://example.com/book/1",
parent_work=cls.work, parent_work=cls.work,
) )
cls.another_book = models.Edition.objects.create(
title="Another Example Edition",
remote_id="https://example.com/book/1",
parent_work=models.Work.objects.create(title="Another Work"),
)
models.SiteSettings.objects.create() models.SiteSettings.objects.create()
@ -267,7 +272,7 @@ class BookViews(TestCase):
"""make sure the endposition is served as well""" """make sure the endposition is served as well"""
view = views.Book.as_view() view = views.Book.as_view()
_ = models.Quotation.objects.create( models.Quotation.objects.create(
user=self.local_user, user=self.local_user,
book=self.book, book=self.book,
content="hi", content="hi",
@ -290,6 +295,49 @@ class BookViews(TestCase):
result.context_data["statuses"].object_list[0].endposition, "13" result.context_data["statuses"].object_list[0].endposition, "13"
) )
def test_create_suggestion_list(self, *_):
"""start a suggestion list for a book"""
self.assertFalse(hasattr(self.book, "suggestion_list"))
view = views.create_suggestion_list
form = forms.SuggestionListForm()
form.data["user"] = self.local_user.id
form.data["suggests_for"] = self.book.id
request = self.factory.post("", form.data)
request.user = self.local_user
view(request, self.book.id)
self.book.refresh_from_db()
self.assertTrue(hasattr(self.book, "suggestion_list"))
suggestion_list = self.book.suggestion_list
self.assertEqual(suggestion_list.suggests_for, self.book)
self.assertEqual(suggestion_list.privacy, "public")
self.assertEqual(suggestion_list.curation, "open")
def test_book_add_suggestion(self, *_):
"""Add a book to the recommendation list"""
suggestion_list = models.List.objects.create(
suggests_for=self.book, user=self.local_user
)
view = views.book_add_suggestion
form = forms.ListItemForm()
form.data["user"] = self.local_user.id
form.data["book"] = self.another_book.id
form.data["book_list"] = suggestion_list.id
form.data["notes"] = "hello"
request = self.factory.post("", form.data)
request.user = self.local_user
view(request, self.book.id)
self.assertEqual(suggestion_list.listitem_set.count(), 1)
item = suggestion_list.listitem_set.first()
self.assertEqual(item.book, self.another_book)
self.assertEqual(item.user, self.local_user)
self.assertEqual(item.notes, "hello")
def _setup_cover_url(): def _setup_cover_url():
"""creates cover url mock""" """creates cover url mock"""

View file

@ -249,11 +249,9 @@ def create_suggestion_list(request, book_id):
if not form.is_valid(): if not form.is_valid():
return redirect("book", book.id) return redirect("book", book.id)
# saving in two steps means django uses the model's custom save functionality,
# which adds an embed key and fixes the privacy and curation settings
suggestion_list = form.save(request, commit=False) suggestion_list = form.save(request, commit=False)
# default values for the suggestion list
suggestion_list.privacy = "public"
suggestion_list.curation = "open"
suggestion_list.save() suggestion_list.save()
return redirect("book", book.id) return redirect("book", book.id)