-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_main.py
70 lines (50 loc) · 2.25 KB
/
test_main.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
import unittest
from assertpy import assert_that
from flask import json
from mock import Mock
import main
from model.movies import Movies
class MainTest(unittest.TestCase):
def setUp(self):
self.a_movie_data = {"title": "Interstellar", "year": 2014, "director": "Christopher Nolan"}
main.app.config['TESTING'] = True
self.app = main.app.test_client()
def tearDown(self):
self.app.application.movies = Movies()
def test_hello(self):
rv = self.app.get('/')
assert "Hello, World!" in rv.data
def test_get_movie_nonexisting(self):
response = self.app.get('/movies/1')
assert response.status_code == 404
def test_get_movie_existing(self):
self.app.post('/movies/'
, data=json.dumps(self.a_movie_data)
, content_type='application/json')
response = self.app.get('/movies/1')
json_data = json.loads(response.data)
assert response.status_code == 200
assert json_data['title'] == "Interstellar"
def test_get_movie_existing_without_post(self):
self.app.application.movies.movies[1] = self.a_movie_data
response = self.app.get('/movies/1')
assert_that(response.status_code).is_equal_to(200)
def test_get_movie_existing_with_mock(self):
self.app.application.movies = Mock()
self.app.application.movies.get_movie = Mock(return_value=self.a_movie_data)
response = self.app.get('/movies/1')
assert_that(response.status_code).is_equal_to(200)
def test_create_new_movie(self):
response = self.app.post('/movies/'
, data=json.dumps(self.a_movie_data)
, content_type='application/json')
assert response.status_code == 200
def test_create_new_movie_with_mock(self):
self.app.application.movies = Mock()
self.app.application.movies.create_movie = Mock(return_value=self.a_movie_data)
self.app.post('/movies/'
, data=json.dumps(self.a_movie_data)
, content_type='application/json')
self.app.application.movies.create_movie.assert_called_once_with(self.a_movie_data)
if __name__ == '__main__':
unittest.main()