Skip to content

Commit dbd03b5

Browse files
author
Jon Wayne Parrott
committed
Switching app engine tests to webtest and consolidating testing utils into AppEngineTestbedCase
1 parent d1a01eb commit dbd03b5

14 files changed

+63
-100
lines changed

appengine/bigquery/tests/test_appengine_auth.py

+8-24
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,20 @@
1616
import re
1717

1818
from apiclient.http import HttpMock
19-
2019
from appengine.bigquery import main
21-
2220
import mock
23-
2421
import tests
25-
26-
import webapp2
22+
import webtest
2723

2824

29-
class TestAuthSample(tests.DatastoreTestbedCase, tests.CloudBaseTest):
25+
class TestAuthSample(tests.AppEngineTestbedCase):
3026

3127
def setUp(self):
32-
tests.DatastoreTestbedCase.setUp(self)
33-
tests.CloudBaseTest.setUp(self)
34-
35-
self.testbed.init_user_stub()
36-
37-
def loginUser(self, email='user@example.com', id='123', is_admin=False):
38-
self.testbed.setup_env(
39-
user_email=email,
40-
user_id=id,
41-
user_is_admin='1' if is_admin else '0',
42-
overwrite=True)
28+
super(TestAuthSample, self).setUp()
29+
self.app = webtest.TestApp(main.app)
4330

4431
def test_anonymous_get(self):
45-
request = webapp2.Request.blank('/')
46-
response = request.get_response(main.app)
32+
response = self.app.get('/')
4733

4834
# Should redirect to login
4935
self.assertEqual(response.status_int, 302)
@@ -53,8 +39,7 @@ def test_anonymous_get(self):
5339
def test_loggedin_get(self):
5440
self.loginUser()
5541

56-
request = webapp2.Request.blank('/')
57-
response = request.get_response(main.app)
42+
response = self.app.get('/')
5843

5944
# Should redirect to login
6045
self.assertEqual(response.status_int, 302)
@@ -64,16 +49,15 @@ def test_loggedin_get(self):
6449
def test_oauthed_get(self, *args):
6550
self.loginUser()
6651

67-
request = webapp2.Request.blank('/')
68-
6952
mock_http = HttpMock(
7053
os.path.join(self.resource_path, 'datasets-list.json'),
7154
{'status': '200'})
55+
7256
with mock.patch.object(main.decorator, 'http', return_value=mock_http):
7357
original_projectid = main.PROJECTID
7458
try:
7559
main.PROJECTID = self.constants['projectId']
76-
response = request.get_response(main.app)
60+
response = self.app.get('/')
7761
finally:
7862
main.PROJECTID = original_projectid
7963

appengine/images/tests/test_guestbook.py

+12-29
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,36 @@
1515
# from the app main.py
1616
from appengine.images import main
1717
import 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)

appengine/memcache/guestbook/tests/test_guestbook.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@
1414

1515
# from the app main.py
1616
from appengine.memcache.guestbook import main
17-
from tests import DatastoreTestbedCase
17+
from tests import AppEngineTestbedCase
18+
import webtest
1819

19-
import webapp2
2020

21+
class TestHandlers(AppEngineTestbedCase):
2122

22-
class TestHandlers(DatastoreTestbedCase):
2323
def test_hello(self):
24-
# Build a request object passing the URI path to be tested.
25-
# You can also pass headers, query arguments etc.
26-
request = webapp2.Request.blank('/')
27-
# Get a response for that request.
28-
response = request.get_response(main.app)
24+
app = webtest.TestApp(main.app)
25+
response = app.get('/')
2926

3027
# Let's check if the response is correct.
3128
self.assertEqual(response.status_int, 200)

appengine/ndb/modeling/tests/test_contact_with_group_models.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
"""Test classes for code snippet for modeling article."""
1616

1717
from appengine.ndb.modeling import contact_with_group_models as models
18-
1918
from google.appengine.ext import ndb
20-
21-
from tests import DatastoreTestbedCase
19+
from tests import AppEngineTestbedCase
2220

2321

24-
class ContactTestCase(DatastoreTestbedCase):
22+
class ContactTestCase(AppEngineTestbedCase):
2523
"""A test case for the Contact model with groups."""
2624
def setUp(self):
2725
"""Creates 3 contacts and 1 group.

appengine/ndb/modeling/tests/test_keyproperty_models.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
import unittest
1818

1919
from appengine.ndb.modeling import keyproperty_models as models
20+
from tests import AppEngineTestbedCase
2021

21-
from tests import DatastoreTestbedCase
2222

23-
24-
class ContactTestCase(DatastoreTestbedCase):
23+
class ContactTestCase(AppEngineTestbedCase):
2524
"""A test case for the Contact model class with KeyProperty."""
2625
NAME = 'Takashi Matsuo'
2726

appengine/ndb/modeling/tests/test_naive_models.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
"""Test classes for code snippet for modeling article."""
1616

1717
from appengine.ndb.modeling import naive_models as models
18+
from tests import AppEngineTestbedCase
1819

19-
from tests import DatastoreTestbedCase
2020

21-
22-
class ContactTestCase(DatastoreTestbedCase):
21+
class ContactTestCase(AppEngineTestbedCase):
2322
"""A test case for the naive Contact model classe."""
2423
NAME = 'Takashi Matsuo'
2524

appengine/ndb/modeling/tests/test_parent_child_models.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
"""Test classes for code snippet for modeling article."""
1616

1717
from appengine.ndb.modeling import parent_child_models as models
18-
1918
from google.appengine.ext import ndb
20-
21-
from tests import DatastoreTestbedCase
19+
from tests import AppEngineTestbedCase
2220

2321

24-
class ContactTestCase(DatastoreTestbedCase):
22+
class ContactTestCase(AppEngineTestbedCase):
2523
"""A test case for the Contact model class with KeyProperty."""
2624
NAME = 'Takashi Matsuo'
2725

appengine/ndb/modeling/tests/test_relation_model_models.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
"""Test classes for code snippet for modeling article."""
1616

1717
from appengine.ndb.modeling import relation_model_models as models
18-
1918
from google.appengine.ext import ndb
20-
21-
from tests import DatastoreTestbedCase
19+
from tests import AppEngineTestbedCase
2220

2321

24-
class ContactTestCase(DatastoreTestbedCase):
22+
class ContactTestCase(AppEngineTestbedCase):
2523
"""A test case for the Contact model with relationship model."""
2624
def setUp(self):
2725
"""Creates 1 contact and 1 company.

appengine/ndb/modeling/tests/test_structured_property_models.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
"""Test classes for code snippet for modeling article."""
1616

1717
from appengine.ndb.modeling import structured_property_models as models
18+
from tests import AppEngineTestbedCase
1819

19-
from tests import DatastoreTestbedCase
2020

21-
22-
class ContactTestCase(DatastoreTestbedCase):
21+
class ContactTestCase(AppEngineTestbedCase):
2322
"""A test case for the Contact model with StructuredProperty."""
2423
def setUp(self):
2524
"""Creates one Contact entity with 2 phone numbers."""

appengine/ndb/overview/tests/test_overview.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,14 @@
1414

1515
# from the app main.py
1616
from appengine.ndb.overview import main
17+
from tests import AppEngineTestbedCase
18+
import webtest
1719

18-
from tests import DatastoreTestbedCase
1920

20-
import webapp2
21-
22-
23-
class TestHandlers(DatastoreTestbedCase):
21+
class TestHandlers(AppEngineTestbedCase):
2422
def test_hello(self):
25-
# Build a request object passing the URI path to be tested.
26-
# You can also pass headers, query arguments etc.
27-
request = webapp2.Request.blank('/')
28-
# Get a response for that request.
29-
response = request.get_response(main.app)
23+
app = webtest.TestApp(main.app)
24+
response = app.get('/')
3025

3126
# Let's check if the response is correct.
3227
self.assertEqual(response.status_int, 200)

appengine/ndb/transactions/tests/test_transactions.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414

1515
# from the app main.py
1616
from appengine.ndb.transactions import main
17+
from tests import AppEngineTestbedCase
1718

18-
from tests import DatastoreTestbedCase
1919

20-
21-
class TestHandlers(DatastoreTestbedCase):
20+
class TestHandlers(AppEngineTestbedCase):
2221
def setUp(self):
2322
super(TestHandlers, self).setUp()
24-
self.testbed.init_taskqueue_stub()
2523
main.app.config['TESTING'] = True
2624
self.app = main.app.test_client()
2725

tests/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313
#
1414

1515
from .utils import (
16+
AppEngineTestbedCase,
1617
BUCKET_NAME_ENV,
1718
capture_stdout,
1819
CloudBaseTest,
19-
DatastoreTestbedCase,
2020
mock_input_answers,
2121
PROJECT_ID_ENV,
2222
RESOURCE_PATH)
2323

2424

2525
__all__ = [
26+
'AppEngineTestbedCase',
2627
'BUCKET_NAME_ENV',
2728
'capture_stdout',
2829
'CloudBaseTest',
29-
'DatastoreTestbedCase',
3030
'mock_input_answers',
3131
'PROJECT_ID_ENV',
3232
'RESOURCE_PATH'

tests/utils.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,15 @@ def tearDown(self):
9696
os.environ['SERVER_SOFTWARE'] = self._server_software_org
9797

9898

99-
class DatastoreTestbedCase(unittest.TestCase):
99+
class AppEngineTestbedCase(CloudBaseTest):
100100
"""A base test case for common setup/teardown tasks for test."""
101101
def setUp(self):
102+
super(AppEngineTestbedCase, self).setUp()
103+
102104
if not APPENGINE_AVAILABLE:
103105
raise SkipTest()
104106

105-
"""Setup the datastore and memcache stub."""
107+
# Setup the datastore and memcache stub.
106108
# First, create an instance of the Testbed class.
107109
self.testbed = testbed.Testbed()
108110
# Then activate the testbed, which prepares the service stubs for
@@ -118,9 +120,21 @@ def setUp(self):
118120
consistency_policy=self.policy)
119121
self.testbed.init_memcache_stub()
120122

123+
# Setup remaining stubs.
124+
self.testbed.init_user_stub()
125+
self.testbed.init_taskqueue_stub()
126+
121127
def tearDown(self):
128+
super(AppEngineTestbedCase, self).tearDown()
122129
self.testbed.deactivate()
123130

131+
def loginUser(self, email='user@example.com', id='123', is_admin=False):
132+
self.testbed.setup_env(
133+
user_email=email,
134+
user_id=id,
135+
user_is_admin='1' if is_admin else '0',
136+
overwrite=True)
137+
124138

125139
@contextlib.contextmanager
126140
def capture_stdout():

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ deps =
1212
mock
1313
nose
1414
coverage
15+
webtest
1516
nose-exclude
1617
coverargs =
1718
--with-coverage

0 commit comments

Comments
 (0)