-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
124 lines (93 loc) · 2.89 KB
/
test.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import logging
import time
import vcr
from jinja2 import Template
import app
my_vcr = vcr.VCR(
cassette_library_dir="fixtures/vcr_cassettes",
decode_compressed_response=True,
record_mode="once",
)
app.app.config["TESTING"] = True
class FakeGhibli:
def __init__(self, films, people):
self.films = films
self.people = people
def get_films(self):
if isinstance(self.films, Exception):
raise self.films
else:
return self.films
def get_people(self):
if isinstance(self.people, Exception):
raise self.people
else:
return self.people
def test_get_films_with_people():
fg = FakeGhibli(
films=[{"url": "some_film", "title": "some_title"}],
people=[{"name": "some_name", "films": ["some_film"]}],
)
assert app.get_films_with_people(fg) == {
"some_film": {"title": "some_title", "people": {"some_name"}}
}
def test_unknown_film():
fg = FakeGhibli(films=[], people=[{"name": "some_name", "films": ["unknown_film"]}])
assert app.get_films_with_people(fg) == {} # Should not raise KeyError
def test_request_exception():
fg = FakeGhibli(films=app.requests.HTTPError(), people=[])
assert app.movies(fg) == ("Error connecting to Ghibli API", 500)
fg = FakeGhibli(films=[], people=app.requests.ConnectionError())
assert app.movies(fg) == ("Error connecting to Ghibli API", 500)
def test_html_template():
films = {"some_film": {"title": "some_title", "people": {"some_name"}}}
with open("templates/ghibli.html") as f:
template = Template(f.read())
html = """<!DOCTYPE html>
<html lang="en">
<head>
<title>Movies</title>
</head>
<body>
<h1>Movies</h1>
<ul>
<li>some_title: some_name</li>
</ul>
</body>
</html>"""
assert template.render(films=films) == html
@my_vcr.use_cassette("test_ghibli")
def test_ghibli():
films = app.ghibli.get_films()
assert len(films) == 20
people = app.ghibli.get_people()
assert len(people) == 31
def test_ghibli_should_cache():
class FakeResponse:
def raise_for_status(self):
pass
def json(self):
return []
class FakeSession:
def __init__(self):
self.n_requests = 0
def get(self, *args, **kwargs):
self.n_requests += 1
return FakeResponse()
s = FakeSession()
g = app.Ghibli(session=s)
g.CACHE_EXPIRE = app.timedelta(seconds=0.1)
g.get_films()
assert s.n_requests == 1
g.get_films() # Should use cache.
assert s.n_requests == 1
time.sleep(0.1)
g.get_films() # Should make a new request.
assert s.n_requests == 2
@my_vcr.use_cassette("test_ghibli")
def test_app_integration():
with app.app.test_client() as c:
r = c.get("/movies")
assert r.status_code == 200
assert r.data
print(r.data.decode())