import unittest
import final_proj as final
import json as js
import sqlite3

class TestFinalProject(unittest.TestCase):
    def test_Book(self):
        b1 = final.Book(title = "The Alchemist", author = "Paulo Coelho", rating = "3.86", desc = "description placeholder", genre = "classics")
        self.assertEqual(b1.title, "The Alchemist")
        self.assertEqual(b1.author, "Paulo Coelho")
        self.assertEqual(b1.rating, "3.86")
        self.assertEqual(b1.description, "description placeholder")
        self.assertEqual(b1.genre, "classics")
    def test_get_genre_most_read(self):
        final.init_db()
        rom = final.get_books_genre_most_read('romance')
        self.assertEqual(20,len(rom))
        self.assertEqual(rom[0].title, 'The Queen of Nothing')
        self.assertEqual(rom[0].author, 'Holly Black')
        self.assertEqual(rom[0].rating, '4.41')
        self.assertEqual(str(rom[0]), 'The Queen of Nothing by Holly Black (4.41)')
        conn = sqlite3.connect('final_proj.db')
        cur = conn.cursor()
        cur.execute('SELECT * FROM Authors')
        response = cur.fetchall()
        #print(response)
        self.assertLess(len(response), 20)
        self.assertGreater(len(response), 0)

        final.init_db()
        ya = final.get_books_genre_most_read('young-adult')
        self.assertEqual(20,len(ya))
        self.assertEqual(ya[1].title, 'Turtles All the Way Down')
        self.assertEqual(ya[1].author, 'John Green')
        self.assertEqual(ya[1].rating, '3.98')
        self.assertEqual(str(ya[1]), 'Turtles All the Way Down by John Green (3.98)')
        conn = sqlite3.connect('final_proj.db')
        cur = conn.cursor()
        cur.execute('SELECT * FROM Authors')
        response = cur.fetchall()
        #print(response)
        self.assertLess(len(response), 20)
        self.assertGreater(len(response), 0)

        final.init_db()
        cl = final.get_books_genre_most_read('classics', plot = True)
        self.assertEqual(10,len(cl))
        self.assertEqual(cl[1].title, "The Handmaid's Tale")
        self.assertEqual(cl[1].author, 'Margaret Atwood')
        self.assertEqual(cl[1].rating, '4.10')
        self.assertEqual(str(cl[1]), "The Handmaid's Tale by Margaret Atwood (4.10)")
        conn = sqlite3.connect('final_proj.db')
        cur = conn.cursor()
        cur.execute('SELECT * FROM Authors')
        response = cur.fetchall()
        #print(response)
        self.assertLessEqual(len(response), 20)
        self.assertGreater(len(response), 0)
    def test_get_genre_popular(self):
        final.init_db()
        f = final.get_books_genre_popular('fiction')
        self.assertEqual(20,len(f))
        self.assertEqual(f[0].title, 'To Kill a Mockingbird')
        self.assertEqual(f[0].author, 'Harper Lee')
        self.assertEqual(f[0].rating, '4.27')
        self.assertEqual(str(f[0]), 'To Kill a Mockingbird by Harper Lee (4.27)')
        conn = sqlite3.connect('final_proj.db')
        cur = conn.cursor()
        cur.execute('SELECT * FROM Authors')
        response = cur.fetchall()
        #print(response)
        self.assertLessEqual(len(response), 20)
        self.assertGreater(len(response), 0)

unittest.main(verbosity = 2)