def test_update_book(self): john = User.objects.create_user(username='******', first_name='fjohn', last_name='ljohn') self.assertEqual(john, User.objects.get(id=john.id)) url = reverse('library:user-detail', kwargs={'username': john.username}) with open('library/test_img.jpg', 'rb') as fp: book = Book( author='John Smith', title='Python for kids', description='This book about python', published_year=2013, user=john, ) book.poster.save('image_name', fp) book.save() john.books.add(book) self.assertEqual(1, john.books.all().count()) self.assertEqual(book.slug, 'python-for-kids') book = john.books.first() # url = book.get_absolute_url() url = reverse('library:book-update', kwargs={ 'id': book.id, 'slug': book.slug, }) self.assertEqual(url, f'/books/{book.id}/python-for-kids/') # Data to update book with open('library/test_img.jpg', 'rb') as file: new_data = { 'user': john.id, 'author': 'new author', 'title': 'Python not for kids', 'description': 'This book about python', 'published_year': '2013', 'poster': file, # 'slug': 'python-not-for-kids', # 'created': timezone.now(), } # POST: update book with new data resp = self.client.post(url, new_data) self.assertEqual(resp.status_code, 302) book.refresh_from_db() self.assertEqual(book.title, 'Python not for kids') self.assertEqual(book.slug, 'python-not-for-kids') self.assertEqual(book.author, 'new author')