Skip to content

Commit

Permalink
DRY on unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffmendoza committed May 22, 2015
1 parent 31db9a9 commit 55bc2c0
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 164 deletions.
26 changes: 2 additions & 24 deletions appengine/memcache/guestbook/tests/test_guestbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

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

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

import webapp2


class TestHandlers(unittest.TestCase):
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()

class TestHandlers(DatastoreTestbed):
def test_hello(self):
# Build a request object passing the URI path to be tested.
# You can also pass headers, query arguments etc.
Expand Down
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 DatastoreTestbed


class ContactTestCase(test_base.TestCase):
class ContactTestCase(DatastoreTestbed):
"""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 DatastoreTestbed


class ContactTestCase(test_base.TestCase):
class ContactTestCase(DatastoreTestbed):
"""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 DatastoreTestbed


class ContactTestCase(test_base.TestCase):
class ContactTestCase(DatastoreTestbed):
"""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 DatastoreTestbed


class ContactTestCase(test_base.TestCase):
class ContactTestCase(DatastoreTestbed):
"""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 DatastoreTestbed


class ContactTestCase(test_base.TestCase):
class ContactTestCase(DatastoreTestbed):
"""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 DatastoreTestbed


class ContactTestCase(test_base.TestCase):
class ContactTestCase(DatastoreTestbed):
"""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()
25 changes: 2 additions & 23 deletions datastore/ndb/overview/tests/test_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

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

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

import webapp2


class TestHandlers(unittest.TestCase):
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()

class TestHandlers(DatastoreTestbed):
def test_hello(self):
# Build a request object passing the URI path to be tested.
# You can also pass headers, query arguments etc.
Expand Down
27 changes: 4 additions & 23 deletions datastore/ndb/transactions/tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

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

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

from tests import DatastoreTestbed


class TestHandlers(unittest.TestCase):
class TestHandlers(DatastoreTestbed):
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()

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

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

def test_hello(self):
rv = self.app.get('/')
self.assertIn('Permenant note page', rv.data)
Expand Down
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 DatastoreTestbed(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 55bc2c0

Please sign in to comment.