from django.test import TestCase, RequestFactory
from .models import Book


class TestBookModel(TestCase):
    """."""
    def setUp(self):
        """."""
        Book.objects.create(title='book1', status='checkedin', year='1981', author='author1', date_added='1', last_borrowed='1')
        Book.objects.create(title='book2', status='checkedout', year='1981', author='author2', date_added='1', last_borrowed='1')
        Book.objects.create(title='book3', status='checkedin', year='1981', author='author3', date_added='1', last_borrowed='1')

    def test_book_titles(self):
        """."""
        one = Book.objects.get(title='book1')
        self.assertEqual(one.title, 'book1')

    def test_book_author(self):
        """."""
        books = Book.objects.all()
        self.assertEqual(books[2].author, 'author3')

    def test_book_checked(self):
        """."""
        two = Book.objects.get(title='book2')
        three = Book.objects.get(title='book3')
        self.assertEqual(two.status, 'checkedout')
        self.assertEqual(three.status, 'checkedin')

    def test_create_new_book(self):
        """."""
        new_book = Book.objects.create(title='book4', status='checkedin', year='1981', author='author4', date_added='1', last_borrowed='1')
        self.assertEqual(new_book.title, 'book4')
        self.assertEqual(new_book.author, 'author4')
        self.assertEqual(new_book.status, 'checkedin')
        self.assertEqual(new_book.year, '1981')


class TestBookViews(TestCase):
    """."""
    def setUp(self):
        self.request = RequestFactory()
        Book.objects.create(title='book1', status='checkedin', year='1981', author='author1', date_added='1', last_borrowed='1')
        Book.objects.create(title='book2', status='checkedout', year='1981', author='author2', date_added='1', last_borrowed='1')
        Book.objects.create(title='book3', status='checkedin', year='1981', author='author3', date_added='1', last_borrowed='1')

    def test_list_view_context(self):
        """."""
        from .views import book_list
        request = self.request.get('')
        response = book_list(request)
        self.assertIn(b'book1', response.content)

    def test_list_view_status(self):
        """."""
        from .views import book_list
        request = self.request.get('')
        response = book_list(request)
        self.assertEqual(response.status_code, 200)

    def test_detail_status_code_failure(self):
        """."""
        from .views import book_details
        from django.http import Http404
        request = self.request.get('')
        with self.assertRaises(Http404):
            book_details(request, '0')

    # def test_detail_view_context(self):
    #     """."""
    #     from .views import book_details
    #     request = self.request.get('')
    #     response = book_details(request, self.note.id)
    #     self.assertIn(b'book1', response.content)
    
from django.test import TestCase, RequestFactory
from .models import Book


class TestBookModel(TestCase):
    """."""
    def setUp(self):
        """."""
        Book.objects.create(title='book1', status='checkedin', year='1981', author='author1', date_added='1', last_borrowed='1')
        Book.objects.create(title='book2', status='checkedout', year='1981', author='author2', date_added='1', last_borrowed='1')
        Book.objects.create(title='book3', status='checkedin', year='1981', author='author3', date_added='1', last_borrowed='1')

    def test_book_titles(self):
        """."""
        one = Book.objects.get(title='book1')
        self.assertEqual(one.title, 'book1')

    def test_book_author(self):
        """."""
        books = Book.objects.all()
        self.assertEqual(books[2].author, 'author3')

    def test_book_checked(self):
        """."""
        two = Book.objects.get(title='book2')
        three = Book.objects.get(title='book3')
        self.assertEqual(two.status, 'checkedout')
        self.assertEqual(three.status, 'checkedin')

    def test_create_new_book(self):
        """."""
        new_book = Book.objects.create(title='book4', status='checkedin', year='1981', author='author4', date_added='1', last_borrowed='1')
        self.assertEqual(new_book.title, 'book4')
        self.assertEqual(new_book.author, 'author4')
        self.assertEqual(new_book.status, 'checkedin')
        self.assertEqual(new_book.year, '1981')


class TestBookViews(TestCase):
    """."""
    def setUp(self):
        self.request = RequestFactory()
        Book.objects.create(title='book1', status='checkedin', year='1981', author='author1', date_added='1', last_borrowed='1')
        Book.objects.create(title='book2', status='checkedout', year='1981', author='author2', date_added='1', last_borrowed='1')
        Book.objects.create(title='book3', status='checkedin', year='1981', author='author3', date_added='1', last_borrowed='1')

    def test_list_view_context(self):
        """."""
        from .views import book_list
        request = self.request.get('')
        response = book_list(request)
        self.assertIn(b'book1', response.content)

    def test_list_view_status(self):
        """."""
        from .views import book_list
        request = self.request.get('')
        response = book_list(request)
        self.assertEqual(response.status_code, 200)

    def test_detail_status_code_failure(self):
        """."""
        from .views import book_details
        from django.http import Http404
        request = self.request.get('')
        with self.assertRaises(Http404):
            book_details(request, '0')

    # def test_detail_view_context(self):
    #     """."""
    #     from .views import book_details
    #     request = self.request.get('')
    #     response = book_details(request, self.note.id)
    #     self.assertIn(b'book1', response.content)
    
. . . . . . . . . . # def test_detail_view_context(self): #     """.""" #     from .views import book_details #     request = self.request.get('') #     response = book_details(request, self.note.id) #     self.assertIn(b'book1', response.content)