Skip to content

Commit

Permalink
Moving app engine samples to py.test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Wayne Parrott committed Feb 22, 2016
1 parent 11cbbfd commit 6990555
Show file tree
Hide file tree
Showing 25 changed files with 466 additions and 576 deletions.
16 changes: 5 additions & 11 deletions appengine/app_identity/signing/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@
# limitations under the License.

import main
from testing import AppEngineTest
import webtest


class TestAppIdentityHandler(AppEngineTest):
def setUp(self):
super(TestAppIdentityHandler, self).setUp()

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

def test_get(self):
response = self.app.get('/')
self.assertEqual(response.status_int, 200)
self.assertTrue('Verified: True' in response.text)
def test_app(testbed):
app = webtest.TestApp(main.app)
response = app.get('/')
assert response.status_int == 200
assert 'Verified: True' in response.text
63 changes: 32 additions & 31 deletions appengine/bigquery/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,48 @@
from apiclient.http import HttpMock
import main
import mock
import testing
import pytest
import webtest


class TestAuthSample(testing.AppEngineTest):
@pytest.fixture
def app(cloud_config, testbed):
main.PROJECTID = cloud_config.GCLOUD_PROJECT
return webtest.TestApp(main.app)

def setUp(self):
super(TestAuthSample, self).setUp()
self.app = webtest.TestApp(main.app)
main.PROJECTID = self.config.GCLOUD_PROJECT

def test_anonymous_get(self):
response = self.app.get('/')
def test_anonymous(app):
response = app.get('/')

# Should redirect to login
self.assertEqual(response.status_int, 302)
self.assertRegexpMatches(response.headers['Location'],
r'.*accounts.*Login.*')
# Should redirect to login
assert response.status_int == 302
assert re.search(r'.*accounts.*Login.*', response.headers['Location'])

def test_loggedin_get(self):
self.login_user()

response = self.app.get('/')
def test_loggedin(app, login):
login()

# Should redirect to login
self.assertEqual(response.status_int, 302)
self.assertRegexpMatches(response.headers['Location'], r'.*oauth2.*')
response = app.get('/')

@mock.patch.object(main.decorator, 'has_credentials', return_value=True)
def test_oauthed_get(self, *args):
self.login_user()
# Should redirect to oauth2
assert response.status_int == 302
assert re.search(r'.*oauth2.*', response.headers['Location'])

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

with mock.patch.object(main.decorator, 'http', return_value=mock_http):
response = self.app.get('/')
def test_oauthed(resource, app, login):
login()

# Should make the api call
self.assertEqual(response.status_int, 200)
self.assertRegexpMatches(
response.body,
re.compile(r'.*datasets.*datasetReference.*etag.*', re.DOTALL))
mock_http = HttpMock(
resource('datasets-list.json'),
{'status': '200'})

with mock.patch.object(main.decorator, 'http', return_value=mock_http):
with mock.patch.object(
main.decorator, 'has_credentials', return_value=True):
response = app.get('/')

# Should make the api call
assert response.status_int == 200
assert re.search(
re.compile(r'.*datasets.*datasetReference.*etag.*', re.DOTALL),
response.body)
15 changes: 5 additions & 10 deletions appengine/blobstore/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@
# limitations under the License.

import main
import testing
import webtest


class TestBlobstoreSample(testing.AppEngineTest):
def test_app(testbed, login):
app = webtest.TestApp(main.app)

def setUp(self):
super(TestBlobstoreSample, self).setUp()
self.app = webtest.TestApp(main.app)
login()
response = app.get('/')

def test_form(self):
self.login_user()
response = self.app.get('/')

self.assertTrue('/_ah/upload' in response)
assert '/_ah/upload' in response
26 changes: 11 additions & 15 deletions appengine/cloudsql/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,20 @@

import os
import re
from unittest.case import SkipTest

import main
import testing
import pytest
import webtest


class TestMySQLSample(testing.AppEngineTest):
@pytest.mark.skipif(
not os.path.exists('/var/run/mysqld/mysqld.sock'),
reason='MySQL server not available.')
def test_app():
app = webtest.TestApp(main.app)
response = app.get('/')

def setUp(self):
if not os.path.exists('/var/run/mysqld/mysqld.sock'):
raise SkipTest('No MySQL server found.')
super(TestMySQLSample, self).setUp()
self.app = webtest.TestApp(main.app)

def test_get(self):
response = self.app.get('/')
self.assertEqual(response.status_int, 200)
self.assertRegexpMatches(
response.body,
re.compile(r'.*version.*', re.DOTALL))
assert response.status_int == 200
assert re.search(
re.compile(r'.*version.*', re.DOTALL),
response.body)
26 changes: 26 additions & 0 deletions appengine/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import testing.appengine


Expand All @@ -7,3 +8,28 @@ def pytest_configure(config):

def pytest_runtest_call(item):
testing.appengine.import_appengine_config()


@pytest.yield_fixture
def testbed():
testbed = testing.appengine.setup_testbed()
yield testbed
testbed.deactivate()


@pytest.fixture
def login(testbed):
def _login(email='[email protected]', id='123', is_admin=False):
testbed.setup_env(
user_email=email,
user_id=id,
user_is_admin='1' if is_admin else '0',
overwrite=True)
return _login


@pytest.fixture
def run_tasks(testbed):
def _run_tasks(app):
testing.appengine.run_tasks(testbed, app)
return _run_tasks
78 changes: 41 additions & 37 deletions appengine/images/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,60 +14,64 @@

import main
import mock
from testing import AppEngineTest
import pytest
import webtest


class TestHandlers(AppEngineTest):
def setUp(self):
super(TestHandlers, self).setUp()
self.app = webtest.TestApp(main.app)
@pytest.fixture
def app(testbed):
return webtest.TestApp(main.app)

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

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

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

@mock.patch('main.images')
def test_post(self, mock_images):
# Let's check if the response is correct.
assert response.status_int == 200


def test_post(app):
with mock.patch('main.images') as mock_images:
mock_images.resize.return_value = 'asdf'

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

# Correct response is a redirect
self.assertEqual(response.status_int, 302)
assert response.status_int == 302


def test_img(app):
greeting = main.Greeting(
parent=main.guestbook_key('default_guestbook'),
id=123
)
greeting.author = 'asdf'
greeting.content = 'asdf'
greeting.avatar = b'123'
greeting.put()

response = app.get('/img?img_id=%s' % greeting.key.urlsafe())

def test_img(self):
greeting = main.Greeting(
parent=main.guestbook_key('default_guestbook'),
id=123
)
greeting.author = 'asdf'
greeting.content = 'asdf'
greeting.avatar = b'123'
greeting.put()
assert response.status_int == 200

response = self.app.get('/img?img_id=%s' % greeting.key.urlsafe())

self.assertEqual(response.status_int, 200)
def test_img_missing(app):
# Bogus image id, should get error
app.get('/img?img_id=123', status=500)

def test_img_missing(self):
# Bogus image id, should get error
self.app.get('/img?img_id=123', status=500)

@mock.patch('main.images')
def test_post_and_get(self, mock_images):
def test_post_and_get(app):
with mock.patch('main.images') as mock_images:
mock_images.resize.return_value = 'asdf'

self.app.post('/sign', {'content': 'asdf'})
response = self.app.get('/')
app.post('/sign', {'content': 'asdf'})
response = app.get('/')

self.assertEqual(response.status_int, 200)
assert response.status_int == 200
18 changes: 6 additions & 12 deletions appengine/logging/reading_logs/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@
# limitations under the License.

import main
from testing import AppEngineTest
import webtest


class TestReadingLogs(AppEngineTest):
def setUp(self):
super(TestReadingLogs, self).setUp()

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

def test_get(self):
response = self.app.get('/')
self.assertEqual(response.status_int, 200)
self.assertTrue('No log entries found' in response.text)
self.assertTrue('More' not in response.text)
def test_app(testbed):
app = webtest.TestApp(main.app)
response = app.get('/')
assert response.status_int == 200
assert 'No log entries found' in response.text
assert 'More' not in response.text
16 changes: 5 additions & 11 deletions appengine/logging/writing_logs/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@
# limitations under the License.

import main
from testing import AppEngineTest
import webtest


class TestWritingLogs(AppEngineTest):
def setUp(self):
super(TestWritingLogs, self).setUp()

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

def test_get(self):
response = self.app.get('/')
self.assertEqual(response.status_int, 200)
self.assertTrue('Logging example' in response.text)
def test_app(testbed):
app = webtest.TestApp(main.app)
response = app.get('/')
assert response.status_int == 200
assert 'Logging example' in response.text
Loading

0 comments on commit 6990555

Please sign in to comment.