from django.contrib.auth import  get_user_model
from django.test import Client, TestCase
from django.urls import reverse

from .models import Book, Review


class BookTests(TestCase):

  def setUp(self):
    self.user = get_user_model().objects.create_user(
      username='testuser',
      email='testuser@joboadevs.com',
      password='test123@'
    )

    self.book = Book.objects.create(
      title='Machine Learning',
      author='J .A. Boamah',
      price='50.00'
    )

    self.review = Review.objects.create(
      book=self.book,
      author=self.user,
      review='Nice book',
    )

  def test_book_listing(self):
    self.assertEqual(f'{self.book.title}', 'Machine Learning')
    self.assertEqual(f'{self.book.author}', 'J .A. Boamah')
    self.assertEqual(f'{self.book.price}', '50.00')

  def test_book_list_view(self):
    response = self.client.get(reverse('book_list'))
    self.assertEqual(response.status_code, 200)
    self.assertContains(response, 'Machine Learning')
    self.assertTemplateUsed(response, 'books/book_list.html')
    
  def test_book_detail_view(self):
    response = self.client.get(self.book.get_absolute_url())
    no_response = self.client.get('/books/12345')
    self.assertEqual(response.status_code, 200)
    self.assertEqual(no_response.status_code, 404)
    self.assertContains(response, 'Machine Learning')
    self.assertTemplateUsed(response, 'books/book_detail.html')
    self.assertContains(response, 'Nice book') # review test

