-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
54 lines (42 loc) · 1.92 KB
/
tests.py
File metadata and controls
54 lines (42 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import unittest
from app import app
from common import utils
class TestApi(unittest.TestCase):
def setUp(self):
app.config["TESTING"] = True
self.app = app.test_client()
def test_books_get(self):
""" Test that books api works"""
response = self.app.get("/books?rows=5")
self.assertEqual(response.status_code, 200)
self.assertIsNotNone(response.json.get("books"))
def test_books_bad_rows(self):
""" Test that we get error on negative rows"""
response = self.app.get("/books?rows=-5")
self.assertEqual(response.status_code, 404)
self.assertIsNone(response.json.get("books"))
def test_books_zero_rows(self):
""" Test that we get no books when row is 0"""
response = self.app.get("/books?rows=0")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json["books"]), 0)
def test_books_invalid_column(self):
""" Test that we get invalid column error when bad column is sent"""
response = self.app.post("/books", data={"badcolumn": 0})
self.assertEqual(response.status_code, 404)
self.assertIsNone(response.json.get("books"))
self.assertIn("invalid column", str(response.data).lower())
def test_books_filter(self):
""" Test that we get proper data through books api data filter"""
response = self.app.post("/books", data={"id": 1})
self.assertEqual(response.status_code, 200)
self.assertIsNotNone(response.json.get("books"))
self.assertEqual(response.json.get("books")[0]["id"], 1)
def test_utils(self):
""" Test that the utils module is working properly"""
books = utils.load_books()
self.assertNotEqual(len(books), 0) # books are not empty
books = utils.filter_books(books, {"author": "barbara parisi"})
self.assertIsNotNone(books)
if __name__ == "__main__":
unittest.main()