Skip to content

Commit

Permalink
Merge pull request #33 from jeffmendoza/tests
Browse files Browse the repository at this point in the history
Add test for some samples.
  • Loading branch information
Jonathan Wayne Parrott committed May 22, 2015
2 parents a2cd34b + 16bccd0 commit f17f951
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 94 deletions.
Empty file.
31 changes: 31 additions & 0 deletions appengine/memcache/guestbook/tests/test_guestbook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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

import webapp2


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)

# Let's check if the response is correct.
self.assertEqual(response.status_int, 200)
42 changes: 0 additions & 42 deletions datastore/ndb/modeling/tests/test_base.py

This file was deleted.

11 changes: 2 additions & 9 deletions datastore/ndb/modeling/tests/test_contact_with_group_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@

"""Test classes for code snippet for modeling article."""


import unittest

from datastore.ndb.modeling import contact_with_group_models as models

from google.appengine.ext import ndb

import test_base
from tests import DatastoreTestbedCase


class ContactTestCase(test_base.TestCase):
class ContactTestCase(DatastoreTestbedCase):
"""A test case for the Contact model with groups."""
def setUp(self):
"""Creates 3 contacts and 1 group.
Expand Down Expand Up @@ -57,7 +54,3 @@ def test_groups(self):
# How about 'members' property?
friend_list = friends.members.fetch()
self.assertEqual(len(friend_list), 1)


if __name__ == '__main__':
unittest.main()
9 changes: 2 additions & 7 deletions datastore/ndb/modeling/tests/test_keyproperty_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@

"""Test classes for code snippet for modeling article."""


import unittest

from datastore.ndb.modeling import keyproperty_models as models

import test_base
from tests import DatastoreTestbedCase


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

Expand Down Expand Up @@ -50,7 +49,3 @@ def test_fails(self):
numbers = contact.phone_numbers.fetch()
self.assertEqual(1, len(numbers))
# [END failing_test]


if __name__ == '__main__':
unittest.main()
11 changes: 2 additions & 9 deletions datastore/ndb/modeling/tests/test_naive_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@

"""Test classes for code snippet for modeling article."""


import unittest

from datastore.ndb.modeling import naive_models as models

import test_base
from tests import DatastoreTestbedCase


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

Expand All @@ -36,7 +33,3 @@ def test_basic(self):
"""Test for getting a NaiveContact entity."""
contact = self.contact_key.get()
self.assertEqual(contact.name, self.NAME)


if __name__ == '__main__':
unittest.main()
11 changes: 2 additions & 9 deletions datastore/ndb/modeling/tests/test_parent_child_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@

"""Test classes for code snippet for modeling article."""


import unittest

from datastore.ndb.modeling import parent_child_models as models

from google.appengine.ext import ndb

import test_base
from tests import DatastoreTestbedCase


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

Expand Down Expand Up @@ -83,7 +80,3 @@ def test_phone_numbers(self):
models.PhoneNumber.phone_type == 'mobile')
entities = query.fetch()
self.assertEqual(0, len(entities))


if __name__ == '__main__':
unittest.main()
11 changes: 2 additions & 9 deletions datastore/ndb/modeling/tests/test_relation_model_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@

"""Test classes for code snippet for modeling article."""


import unittest

from datastore.ndb.modeling import relation_model_models as models

from google.appengine.ext import ndb

import test_base
from tests import DatastoreTestbedCase


class ContactTestCase(test_base.TestCase):
class ContactTestCase(DatastoreTestbedCase):
"""A test case for the Contact model with relationship model."""
def setUp(self):
"""Creates 1 contact and 1 company.
Expand Down Expand Up @@ -60,7 +57,3 @@ def test_relationship(self):
title='president').put()
# get the list of companies that Mary belongs to
self.assertEqual(len(mary.companies), 2)


if __name__ == '__main__':
unittest.main()
11 changes: 2 additions & 9 deletions datastore/ndb/modeling/tests/test_structured_property_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@

"""Test classes for code snippet for modeling article."""


import unittest

from datastore.ndb.modeling import structured_property_models as models

import test_base
from tests import DatastoreTestbedCase


class ContactTestCase(test_base.TestCase):
class ContactTestCase(DatastoreTestbedCase):
"""A test case for the Contact model with StructuredProperty."""
def setUp(self):
"""Creates one Contact entity with 2 phone numbers."""
Expand Down Expand Up @@ -67,7 +64,3 @@ def test_phone_numbers(self):
self.assertEqual(len(scott.phone_numbers), 1)
self.assertEqual(scott.phone_numbers[0].phone_type, 'home')
self.assertEqual(scott.phone_numbers[0].number, '(650) 555 - 2200')


if __name__ == '__main__':
unittest.main()
Empty file.
32 changes: 32 additions & 0 deletions datastore/ndb/overview/tests/test_overview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# from the app main.py
from datastore.ndb.overview import main

from tests import DatastoreTestbedCase

import webapp2


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)

# Let's check if the response is correct.
self.assertEqual(response.status_int, 200)
Empty file.
64 changes: 64 additions & 0 deletions datastore/ndb/transactions/tests/test_transactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# from the app main.py
from datastore.ndb.transactions import main

from tests import DatastoreTestbedCase


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

def test_hello(self):
rv = self.app.get('/')
self.assertIn('Permenant note page', rv.data)
self.assertEqual(rv.status, '200 OK')

def test_post(self):
rv = self.app.post('/add', data=dict(
note_title='Title',
note_text='Text'
), follow_redirects=True)
self.assertEqual(rv.status, '200 OK')

def test_post2(self):
rv = self.app.post('/add', data=dict(
note_title='Title2',
note_text='Text'
), follow_redirects=True)
self.assertEqual(rv.status, '200 OK')

def test_post3(self):
rv = self.app.post('/add', data=dict(
note_title='Title3',
note_text='Text'
), follow_redirects=True)
self.assertEqual(rv.status, '200 OK')

def test_there(self):
rv = self.app.post('/add', data=dict(
note_title='Title',
note_text='New'
), follow_redirects=True)
rv = self.app.post('/add', data=dict(
note_title='Title',
note_text='There'
), follow_redirects=True)
self.assertIn('Already there', rv.data)
self.assertEqual(rv.status, '200 OK')
23 changes: 23 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import unittest
import __builtin__

from google.appengine.datastore import datastore_stub_util
from google.appengine.ext import testbed

BUCKET_NAME_ENV = 'TEST_BUCKET_NAME'
PROJECT_ID_ENV = 'TEST_PROJECT_ID'
RESOURCE_PATH = os.path.join(
Expand Down Expand Up @@ -77,3 +80,23 @@ def setUp(self):

def tearDown(self):
os.environ['SERVER_SOFTWARE'] = self._server_software_org

class DatastoreTestbedCase(unittest.TestCase):
"""A base test case for common setup/teardown tasks for test."""
def setUp(self):
"""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
# use.
self.testbed.activate()
# Create a consistency policy that will simulate the High
# Replication consistency model.
self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(
probability=0)
# Initialize the datastore stub with this policy.
self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)
self.testbed.init_memcache_stub()

def tearDown(self):
self.testbed.deactivate()

0 comments on commit f17f951

Please sign in to comment.