-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_api.py
85 lines (69 loc) · 2.72 KB
/
test_api.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
from unittest.mock import patch, Mock
import requests
from flask import Flask
from flask_testing import LiveServerTestCase
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from api.books_catalog_api import app
from api.models import create_tables
author_id = 1
class BooksCatalogAPITests(LiveServerTestCase):
def create_app(self):
app.config['TESTING'] = True
# Default port is 5000
app.config['LIVESERVER_PORT'] = 8943
# Default timeout is 5 seconds
app.config['LIVESERVER_TIMEOUT'] = 10
return app
def setUp(self):
engine = create_engine('sqlite:///:memory:')
self.db = scoped_session(sessionmaker(bind=engine, expire_on_commit=False))
create_tables(engine)
@patch("api.books_catalog_api.get_sqlalchemy_session")
def test_root_path(self, mock_db):
mock_db.__enter__.return_value = self.db
response = app.test_client().get("/")
self.assertEqual(response.status_code, 200)
def test_non_existing_path(self):
response = app.test_client().get("/non-existing")
self.assertEqual(response.status_code, 404)
@patch("api.books_catalog_api.get_sqlalchemy_session")
def test_create_author(self, mock_db):
mock_db.__enter__.return_value = self.db
response = app.test_client().post(
"/authors/",
json={
'first_name': "Damian",
'last_name': "Ziobro",
}
)
print(f"DAMIAN: {response.data}")
author_id = response.json.get('author_id')
self.assertEqual(response.status_code, 200)
@patch("api.books_catalog_api.get_sqlalchemy_session")
def test_get_all_books_return(self, mock_db):
mock_db.__enter__.return_value = self.db
response = app.test_client().get("/books/")
self.assertEqual(response.status_code, 200)
@patch("api.books_catalog_api.get_sqlalchemy_session")
def test_get_all_authors_return(self, mock_db):
mock_db.__enter__.return_value = self.db
response = app.test_client().get("/authors/")
self.assertEqual(response.status_code, 200)
#TODO - improve coverage
#@patch("api.books_catalog_api.get_sqlalchemy_session")
#def test_create_book(self, mock_db):
#mock_db.__enter__.return_value = self.db
#response = app.test_client().post(
#"/books/",
#json={
#'title': "Test title",
#'description': "test description",
#'author_id': 1,
#}
#)
#print(f"DAMIAN: {response.data}")
#self.assertEqual(response.status_code, 200)