1515# from the app main.py
1616from appengine .images import main
1717import mock
18- from tests import DatastoreTestbedCase
18+ from tests import AppEngineTestbedCase
19+ import webtest
1920
20- import webapp2
2121
22-
23- class TestHandlers (DatastoreTestbedCase ):
22+ class TestHandlers (AppEngineTestbedCase ):
2423 def setUp (self ):
2524 super (TestHandlers , self ).setUp ()
2625
2726 # Workaround for other tests clobbering our Greeting model.
2827 reload (main )
2928
29+ self .app = webtest .TestApp (main .app )
30+
3031 def test_get (self ):
3132 main .Greeting (
3233 parent = main .guestbook_key ('default_guestbook' ),
3334 author = '123' ,
3435 content = 'abc'
3536 ).put ()
3637
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 ('/' )
4239
4340 # Let's check if the response is correct.
4441 self .assertEqual (response .status_int , 200 )
4542
4643 @mock .patch ('appengine.images.main.images' )
4744 def test_post (self , mock_images ):
4845 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' })
5448 mock_images .resize .assert_called_once_with (mock .ANY , 32 , 32 )
5549
5650 # Correct response is a redirect
@@ -66,30 +60,19 @@ def test_img(self):
6660 greeting .avatar = b'123'
6761 greeting .put ()
6862
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 ())
7364
7465 self .assertEqual (response .status_int , 200 )
7566
7667 def test_img_missing (self ):
7768 # 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 )
8270
8371 @mock .patch ('appengine.images.main.images' )
8472 def test_post_and_get (self , mock_images ):
8573 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 )
9174
92- request = webapp2 . Request . blank ('/' )
93- response = request . get_response ( main . app )
75+ self . app . post ('/sign' , { 'content' : 'asdf' } )
76+ response = self . app . get ( '/' )
9477
9578 self .assertEqual (response .status_int , 200 )
0 commit comments