15
15
# from the app main.py
16
16
from appengine .images import main
17
17
import mock
18
- from tests import DatastoreTestbedCase
18
+ from tests import AppEngineTestbedCase
19
+ import webtest
19
20
20
- import webapp2
21
21
22
-
23
- class TestHandlers (DatastoreTestbedCase ):
22
+ class TestHandlers (AppEngineTestbedCase ):
24
23
def setUp (self ):
25
24
super (TestHandlers , self ).setUp ()
26
25
27
26
# Workaround for other tests clobbering our Greeting model.
28
27
reload (main )
29
28
29
+ self .app = webtest .TestApp (main .app )
30
+
30
31
def test_get (self ):
31
32
main .Greeting (
32
33
parent = main .guestbook_key ('default_guestbook' ),
33
34
author = '123' ,
34
35
content = 'abc'
35
36
).put ()
36
37
37
- # Build a request object passing the URI path to be tested.
38
- # You can also pass headers, query arguments etc.
39
- request = webapp2 .Request .blank ('/' )
40
- # Get a response for that request.
41
- response = request .get_response (main .app )
38
+ response = self .app .get ('/' )
42
39
43
40
# Let's check if the response is correct.
44
41
self .assertEqual (response .status_int , 200 )
45
42
46
43
@mock .patch ('appengine.images.main.images' )
47
44
def test_post (self , mock_images ):
48
45
mock_images .resize .return_value = 'asdf'
49
- request = webapp2 .Request .blank (
50
- '/sign' ,
51
- POST = {'content' : 'asdf' },
52
- )
53
- response = request .get_response (main .app )
46
+
47
+ response = self .app .post ('/sign' , {'content' : 'asdf' })
54
48
mock_images .resize .assert_called_once_with (mock .ANY , 32 , 32 )
55
49
56
50
# Correct response is a redirect
@@ -66,30 +60,19 @@ def test_img(self):
66
60
greeting .avatar = b'123'
67
61
greeting .put ()
68
62
69
- request = webapp2 .Request .blank (
70
- '/img?img_id=%s' % greeting .key .urlsafe ()
71
- )
72
- response = request .get_response (main .app )
63
+ response = self .app .get ('/img?img_id=%s' % greeting .key .urlsafe ())
73
64
74
65
self .assertEqual (response .status_int , 200 )
75
66
76
67
def test_img_missing (self ):
77
68
# Bogus image id, should get error
78
- request = webapp2 .Request .blank ('/img?img_id=123' )
79
- response = request .get_response (main .app )
80
-
81
- self .assertEqual (response .status_int , 500 )
69
+ self .app .get ('/img?img_id=123' , status = 500 )
82
70
83
71
@mock .patch ('appengine.images.main.images' )
84
72
def test_post_and_get (self , mock_images ):
85
73
mock_images .resize .return_value = 'asdf'
86
- request = webapp2 .Request .blank (
87
- '/sign' ,
88
- POST = {'content' : 'asdf' },
89
- )
90
- response = request .get_response (main .app )
91
74
92
- request = webapp2 . Request . blank ('/' )
93
- response = request . get_response ( main . app )
75
+ self . app . post ('/sign' , { 'content' : 'asdf' } )
76
+ response = self . app . get ( '/' )
94
77
95
78
self .assertEqual (response .status_int , 200 )
0 commit comments