Test for deleting authors

This commit is contained in:
Mouse Reeve 2021-03-07 15:14:57 -08:00
parent 79d9c493f7
commit 1eac2b9386
2 changed files with 23 additions and 1 deletions

View file

@ -114,11 +114,29 @@ class BookViews(TestCase):
view(request, self.book.id) view(request, self.book.id)
# the changes haven't been saved yet
self.book.refresh_from_db() self.book.refresh_from_db()
self.assertEqual(self.book.title, 'New Title') self.assertEqual(self.book.title, 'New Title')
self.assertEqual(self.book.authors.first().name, 'Sappho') self.assertEqual(self.book.authors.first().name, 'Sappho')
def test_edit_book_remove_author(self):
''' remove an author from a book '''
author = models.Author.objects.create(name='Sappho')
self.book.authors.add(author)
form = forms.EditionForm(instance=self.book)
view = views.EditBook.as_view()
self.local_user.groups.add(self.group)
form = forms.EditionForm(instance=self.book)
form.data['title'] = 'New Title'
form.data['last_edited_by'] = self.local_user.id
form.data['remove_authors'] = [author.id]
request = self.factory.post('', form.data)
request.user = self.local_user
view(request, self.book.id)
self.book.refresh_from_db()
self.assertEqual(self.book.title, 'New Title')
self.assertFalse(self.book.authors.exists())
def test_switch_edition(self): def test_switch_edition(self):
''' updates user's relationships to a book ''' ''' updates user's relationships to a book '''

View file

@ -206,6 +206,10 @@ class ConfirmEditBook(View):
name=request.POST.get('add_author')) name=request.POST.get('add_author'))
book.authors.add(author) book.authors.add(author)
remove_authors = request.POST.getlist('remove_authors')
for author_id in remove_authors:
book.authors.remove(author_id)
return redirect('/book/%s' % book.id) return redirect('/book/%s' % book.id)