Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving mvm samples to use py.test #189

Merged
merged 1 commit into from
Feb 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions managed_vms/cloudsql/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
# limitations under the License.

import main
from testing import CloudTest


class CloudSqlTest(CloudTest):
def test_index():
main.db.create_all()

def test_index(self):
main.db.create_all()
main.app.testing = True
client = main.app.test_client()

main.app.testing = True
client = main.app.test_client()

r = client.get('/', environ_base={'REMOTE_ADDR': '127.0.0.1'})
self.assertEqual(r.status_code, 200)
self.assertTrue('127.0' in r.data.decode('utf-8'))
r = client.get('/', environ_base={'REMOTE_ADDR': '127.0.0.1'})
assert r.status_code == 200
assert '127.0' in r.data.decode('utf-8')
15 changes: 6 additions & 9 deletions managed_vms/datastore/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
# limitations under the License.

import main
from testing import CloudTest


class DatastoreTest(CloudTest):
def test_index():
main.app.testing = True
client = main.app.test_client()

def test_index(self):
main.app.testing = True
client = main.app.test_client()

r = client.get('/', environ_base={'REMOTE_ADDR': '127.0.0.1'})
self.assertEqual(r.status_code, 200)
self.assertTrue('127.0' in r.data.decode('utf-8'))
r = client.get('/', environ_base={'REMOTE_ADDR': '127.0.0.1'})
assert r.status_code == 200
assert '127.0' in r.data.decode('utf-8')
15 changes: 6 additions & 9 deletions managed_vms/disk/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
# limitations under the License.

import main
from testing import CloudTest


class DiskTest(CloudTest):
def test_index():
main.app.testing = True
client = main.app.test_client()

def test_index(self):
main.app.testing = True
client = main.app.test_client()

r = client.get('/', environ_base={'REMOTE_ADDR': '127.0.0.1'})
self.assertEqual(r.status_code, 200)
self.assertTrue('127.0' in r.data.decode('utf-8'))
r = client.get('/', environ_base={'REMOTE_ADDR': '127.0.0.1'})
assert r.status_code == 200
assert '127.0' in r.data.decode('utf-8')
25 changes: 10 additions & 15 deletions managed_vms/extending_runtime/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,16 @@
import os

import main
from nose.plugins.skip import SkipTest
from testing import CloudTest
import pytest


class ExtendingRuntimeTest(CloudTest):
@pytest.mark.skipif(
not os.path.exists('/usr/games/fortune'),
reason='Fortune executable is not installed.')
def test_index():
main.app.testing = True
client = main.app.test_client()

def test_index(self):
if not os.path.exists('/usr/games/fortune'):
raise SkipTest(
'Extending runtime test will only execute if fortune is'
'installed')

main.app.testing = True
client = main.app.test_client()

r = client.get('/')
self.assertEqual(r.status_code, 200)
self.assertTrue(len(r.data))
r = client.get('/')
assert r.status_code == 200
assert len(r.data)
15 changes: 6 additions & 9 deletions managed_vms/hello_world/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
# limitations under the License.

import main
from testing import CloudTest


class HelloWorldTest(CloudTest):
def test_index():
main.app.testing = True
client = main.app.test_client()

def test_index(self):
main.app.testing = True
client = main.app.test_client()

r = client.get('/')
self.assertEqual(r.status_code, 200)
self.assertTrue('Hello World' in r.data.decode('utf-8'))
r = client.get('/')
assert r.status_code == 200
assert 'Hello World' in r.data.decode('utf-8')
15 changes: 6 additions & 9 deletions managed_vms/hello_world_compat/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
# limitations under the License.

import main
from testing import CloudTest


class HelloWorldTest(CloudTest):
def test_index():
main.app.testing = True
client = main.app.test_client()

def test_index(self):
main.app.testing = True
client = main.app.test_client()

r = client.get('/')
self.assertEqual(r.status_code, 200)
self.assertTrue('Hello World' in r.data.decode('utf-8'))
r = client.get('/')
assert r.status_code == 200
assert 'Hello World' in r.data.decode('utf-8')
30 changes: 13 additions & 17 deletions managed_vms/memcache/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest.case import SkipTest

import main
from testing import CloudTest

import pytest

class MemcacheTest(CloudTest):

def test_index(self):
main.memcache_client.set('counter', 0)
def test_index():
main.memcache_client.set('counter', 0)

if main.memcache_client.get('counter') is None:
raise SkipTest('Memcache is unavailable.')
if main.memcache_client.get('counter') is None:
pytest.skip('Memcache is unavailable.')

main.app.testing = True
client = main.app.test_client()
main.app.testing = True
client = main.app.test_client()

r = client.get('/')
self.assertEqual(r.status_code, 200)
self.assertTrue('1' in r.data.decode('utf-8'))
r = client.get('/')
assert r.status_code == 200
assert '1' in r.data.decode('utf-8')

r = client.get('/')
self.assertEqual(r.status_code, 200)
self.assertTrue('2' in r.data.decode('utf-8'))
r = client.get('/')
assert r.status_code == 200
assert '2' in r.data.decode('utf-8')
95 changes: 49 additions & 46 deletions managed_vms/pubsub/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,52 @@
import os

import main
from testing import CloudTest


class PubSubTest(CloudTest):
def setUp(self):
super(PubSubTest, self).setUp()
main.app.testing = True
self.client = main.app.test_client()

def test_index(self):
r = self.client.get('/')
self.assertEqual(r.status_code, 200)

def test_post_index(self):
r = self.client.post('/', data={'payload': 'Test payload'})
self.assertEqual(r.status_code, 200)

def test_push_endpoint(self):
url = '/pubsub/push?token=' + os.environ['PUBSUB_VERIFICATION_TOKEN']

r = self.client.post(
url,
data=json.dumps({
"message": {
"data": base64.b64encode(
u'Test message'.encode('utf-8')
).decode('utf-8')
}
})
)

self.assertEqual(r.status_code, 200)

# Make sure the message is visible on the home page.
r = self.client.get('/')
self.assertEqual(r.status_code, 200)
self.assertTrue('Test message' in r.data.decode('utf-8'))

def test_push_endpoint_errors(self):
# no token
r = self.client.post('/pubsub/push')
self.assertEqual(r.status_code, 400)

# invalid token
r = self.client.post('/pubsub/push?token=bad')
self.assertEqual(r.status_code, 400)
import pytest


@pytest.fixture
def client():
main.app.testing = True
return main.app.test_client()


def test_index(client):
r = client.get('/')
assert r.status_code == 200


def test_post_index(client):
r = client.post('/', data={'payload': 'Test payload'})
assert r.status_code == 200


def test_push_endpoint(client):
url = '/pubsub/push?token=' + os.environ['PUBSUB_VERIFICATION_TOKEN']

r = client.post(
url,
data=json.dumps({
"message": {
"data": base64.b64encode(
u'Test message'.encode('utf-8')
).decode('utf-8')
}
})
)

assert r.status_code == 200

# Make sure the message is visible on the home page.
r = client.get('/')
assert r.status_code == 200
assert 'Test message' in r.data.decode('utf-8')


def test_push_endpoint_errors(client):
# no token
r = client.post('/pubsub/push')
assert r.status_code == 400

# invalid token
r = client.post('/pubsub/push?token=bad')
assert r.status_code == 400
17 changes: 7 additions & 10 deletions managed_vms/static_files/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
# limitations under the License.

import main
from testing import CloudTest


class StaticFilesTest(CloudTest):
def test_index():
main.app.testing = True
client = main.app.test_client()

def test_index(self):
main.app.testing = True
client = main.app.test_client()
r = client.get('/')
assert r.status_code == 200

r = client.get('/')
self.assertEqual(r.status_code, 200)

r = client.get('/static/main.css')
self.assertEqual(r.status_code, 200)
r = client.get('/static/main.css')
assert r.status_code == 200
49 changes: 25 additions & 24 deletions managed_vms/storage/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,37 @@
# limitations under the License.

import main
import pytest
import requests
from six import BytesIO
from testing import CloudTest


class StorageTest(CloudTest):
def setUp(self):
super(StorageTest, self).setUp()
main.app.testing = True
self.client = main.app.test_client()
@pytest.fixture
def client():
main.app.testing = True
return main.app.test_client()

def test_index(self):
r = self.client.get('/')
self.assertEqual(r.status_code, 200)

def test_upload(self):
# Upload a simple file
file_content = b"This is some test content."
def test_index(client):
r = client.get('/')
assert r.status_code == 200

r = self.client.post(
'/upload',
data={
'file': (BytesIO(file_content), 'example.txt')
}
)

self.assertEqual(r.status_code, 200)
def test_upload(client):
# Upload a simple file
file_content = b"This is some test content."

# The app should return the public cloud storage URL for the uploaded
# file. Download and verify it.
cloud_storage_url = r.data.decode('utf-8')
r = requests.get(cloud_storage_url)
self.assertEqual(r.text.encode('utf-8'), file_content)
r = client.post(
'/upload',
data={
'file': (BytesIO(file_content), 'example.txt')
}
)

assert r.status_code == 200

# The app should return the public cloud storage URL for the uploaded
# file. Download and verify it.
cloud_storage_url = r.data.decode('utf-8')
r = requests.get(cloud_storage_url)
assert r.text.encode('utf-8') == file_content