Skip to content

Commit

Permalink
Merge pull request #83 from GoogleCloudPlatform/webtestify
Browse files Browse the repository at this point in the history
Switching app engine tests to webtest and consolidating testing utils…
  • Loading branch information
Jonathan Wayne Parrott committed Sep 14, 2015
2 parents d1a01eb + dbd03b5 commit 9e059fe
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 100 deletions.
32 changes: 8 additions & 24 deletions appengine/bigquery/tests/test_appengine_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,20 @@
import re

from apiclient.http import HttpMock

from appengine.bigquery import main

import mock

import tests

import webapp2
import webtest


class TestAuthSample(tests.DatastoreTestbedCase, tests.CloudBaseTest):
class TestAuthSample(tests.AppEngineTestbedCase):

def setUp(self):
tests.DatastoreTestbedCase.setUp(self)
tests.CloudBaseTest.setUp(self)

self.testbed.init_user_stub()

def loginUser(self, email='user@example.com', id='123', is_admin=False):
self.testbed.setup_env(
user_email=email,
user_id=id,
user_is_admin='1' if is_admin else '0',
overwrite=True)
super(TestAuthSample, self).setUp()
self.app = webtest.TestApp(main.app)

def test_anonymous_get(self):
request = webapp2.Request.blank('/')
response = request.get_response(main.app)
response = self.app.get('/')

# Should redirect to login
self.assertEqual(response.status_int, 302)
Expand All @@ -53,8 +39,7 @@ def test_anonymous_get(self):
def test_loggedin_get(self):
self.loginUser()

request = webapp2.Request.blank('/')
response = request.get_response(main.app)
response = self.app.get('/')

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

request = webapp2.Request.blank('/')

mock_http = HttpMock(
os.path.join(self.resource_path, 'datasets-list.json'),
{'status': '200'})

with mock.patch.object(main.decorator, 'http', return_value=mock_http):
original_projectid = main.PROJECTID
try:
main.PROJECTID = self.constants['projectId']
response = request.get_response(main.app)
response = self.app.get('/')
finally:
main.PROJECTID = original_projectid

Expand Down
41 changes: 12 additions & 29 deletions appengine/images/tests/test_guestbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,36 @@
# from the app main.py
from appengine.images import main
import mock
from tests import DatastoreTestbedCase
from tests import AppEngineTestbedCase
import webtest

import webapp2


class TestHandlers(DatastoreTestbedCase):
class TestHandlers(AppEngineTestbedCase):
def setUp(self):
super(TestHandlers, self).setUp()

# Workaround for other tests clobbering our Greeting model.
reload(main)

self.app = webtest.TestApp(main.app)

def test_get(self):
main.Greeting(
parent=main.guestbook_key('default_guestbook'),
author='123',
content='abc'
).put()

# Build a request object passing the URI path to be tested.
# You can also pass headers, query arguments etc.
request = webapp2.Request.blank('/')
# Get a response for that request.
response = request.get_response(main.app)
response = self.app.get('/')

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

@mock.patch('appengine.images.main.images')
def test_post(self, mock_images):
mock_images.resize.return_value = 'asdf'
request = webapp2.Request.blank(
'/sign',
POST={'content': 'asdf'},
)
response = request.get_response(main.app)

response = self.app.post('/sign', {'content': 'asdf'})
mock_images.resize.assert_called_once_with(mock.ANY, 32, 32)

# Correct response is a redirect
Expand All @@ -66,30 +60,19 @@ def test_img(self):
greeting.avatar = b'123'
greeting.put()

request = webapp2.Request.blank(
'/img?img_id=%s' % greeting.key.urlsafe()
)
response = request.get_response(main.app)
response = self.app.get('/img?img_id=%s' % greeting.key.urlsafe())

self.assertEqual(response.status_int, 200)

def test_img_missing(self):
# Bogus image id, should get error
request = webapp2.Request.blank('/img?img_id=123')
response = request.get_response(main.app)

self.assertEqual(response.status_int, 500)
self.app.get('/img?img_id=123', status=500)

@mock.patch('appengine.images.main.images')
def test_post_and_get(self, mock_images):
mock_images.resize.return_value = 'asdf'
request = webapp2.Request.blank(
'/sign',
POST={'content': 'asdf'},
)
response = request.get_response(main.app)

request = webapp2.Request.blank('/')
response = request.get_response(main.app)
self.app.post('/sign', {'content': 'asdf'})
response = self.app.get('/')

self.assertEqual(response.status_int, 200)
13 changes: 5 additions & 8 deletions appengine/memcache/guestbook/tests/test_guestbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@

# from the app main.py
from appengine.memcache.guestbook import main
from tests import DatastoreTestbedCase
from tests import AppEngineTestbedCase
import webtest

import webapp2

class TestHandlers(AppEngineTestbedCase):

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

# Let's check if the response is correct.
self.assertEqual(response.status_int, 200)
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
"""Test classes for code snippet for modeling article."""

from appengine.ndb.modeling import contact_with_group_models as models

from google.appengine.ext import ndb

from tests import DatastoreTestbedCase
from tests import AppEngineTestbedCase


class ContactTestCase(DatastoreTestbedCase):
class ContactTestCase(AppEngineTestbedCase):
"""A test case for the Contact model with groups."""
def setUp(self):
"""Creates 3 contacts and 1 group.
Expand Down
5 changes: 2 additions & 3 deletions appengine/ndb/modeling/tests/test_keyproperty_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
import unittest

from appengine.ndb.modeling import keyproperty_models as models
from tests import AppEngineTestbedCase

from tests import DatastoreTestbedCase


class ContactTestCase(DatastoreTestbedCase):
class ContactTestCase(AppEngineTestbedCase):
"""A test case for the Contact model class with KeyProperty."""
NAME = 'Takashi Matsuo'

Expand Down
5 changes: 2 additions & 3 deletions appengine/ndb/modeling/tests/test_naive_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
"""Test classes for code snippet for modeling article."""

from appengine.ndb.modeling import naive_models as models
from tests import AppEngineTestbedCase

from tests import DatastoreTestbedCase


class ContactTestCase(DatastoreTestbedCase):
class ContactTestCase(AppEngineTestbedCase):
"""A test case for the naive Contact model classe."""
NAME = 'Takashi Matsuo'

Expand Down
6 changes: 2 additions & 4 deletions appengine/ndb/modeling/tests/test_parent_child_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
"""Test classes for code snippet for modeling article."""

from appengine.ndb.modeling import parent_child_models as models

from google.appengine.ext import ndb

from tests import DatastoreTestbedCase
from tests import AppEngineTestbedCase


class ContactTestCase(DatastoreTestbedCase):
class ContactTestCase(AppEngineTestbedCase):
"""A test case for the Contact model class with KeyProperty."""
NAME = 'Takashi Matsuo'

Expand Down
6 changes: 2 additions & 4 deletions appengine/ndb/modeling/tests/test_relation_model_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
"""Test classes for code snippet for modeling article."""

from appengine.ndb.modeling import relation_model_models as models

from google.appengine.ext import ndb

from tests import DatastoreTestbedCase
from tests import AppEngineTestbedCase


class ContactTestCase(DatastoreTestbedCase):
class ContactTestCase(AppEngineTestbedCase):
"""A test case for the Contact model with relationship model."""
def setUp(self):
"""Creates 1 contact and 1 company.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
"""Test classes for code snippet for modeling article."""

from appengine.ndb.modeling import structured_property_models as models
from tests import AppEngineTestbedCase

from tests import DatastoreTestbedCase


class ContactTestCase(DatastoreTestbedCase):
class ContactTestCase(AppEngineTestbedCase):
"""A test case for the Contact model with StructuredProperty."""
def setUp(self):
"""Creates one Contact entity with 2 phone numbers."""
Expand Down
15 changes: 5 additions & 10 deletions appengine/ndb/overview/tests/test_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@

# from the app main.py
from appengine.ndb.overview import main
from tests import AppEngineTestbedCase
import webtest

from tests import DatastoreTestbedCase

import webapp2


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

# Let's check if the response is correct.
self.assertEqual(response.status_int, 200)
6 changes: 2 additions & 4 deletions appengine/ndb/transactions/tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@

# from the app main.py
from appengine.ndb.transactions import main
from tests import AppEngineTestbedCase

from tests import DatastoreTestbedCase


class TestHandlers(DatastoreTestbedCase):
class TestHandlers(AppEngineTestbedCase):
def setUp(self):
super(TestHandlers, self).setUp()
self.testbed.init_taskqueue_stub()
main.app.config['TESTING'] = True
self.app = main.app.test_client()

Expand Down
4 changes: 2 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
#

from .utils import (
AppEngineTestbedCase,
BUCKET_NAME_ENV,
capture_stdout,
CloudBaseTest,
DatastoreTestbedCase,
mock_input_answers,
PROJECT_ID_ENV,
RESOURCE_PATH)


__all__ = [
'AppEngineTestbedCase',
'BUCKET_NAME_ENV',
'capture_stdout',
'CloudBaseTest',
'DatastoreTestbedCase',
'mock_input_answers',
'PROJECT_ID_ENV',
'RESOURCE_PATH'
Expand Down
18 changes: 16 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ def tearDown(self):
os.environ['SERVER_SOFTWARE'] = self._server_software_org


class DatastoreTestbedCase(unittest.TestCase):
class AppEngineTestbedCase(CloudBaseTest):
"""A base test case for common setup/teardown tasks for test."""
def setUp(self):
super(AppEngineTestbedCase, self).setUp()

if not APPENGINE_AVAILABLE:
raise SkipTest()

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

# Setup remaining stubs.
self.testbed.init_user_stub()
self.testbed.init_taskqueue_stub()

def tearDown(self):
super(AppEngineTestbedCase, self).tearDown()
self.testbed.deactivate()

def loginUser(self, email='user@example.com', id='123', is_admin=False):
self.testbed.setup_env(
user_email=email,
user_id=id,
user_is_admin='1' if is_admin else '0',
overwrite=True)


@contextlib.contextmanager
def capture_stdout():
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ deps =
mock
nose
coverage
webtest
nose-exclude
coverargs =
--with-coverage
Expand Down

0 comments on commit 9e059fe

Please sign in to comment.