diff --git a/appengine/memcache/guestbook/tests/test_guestbook.py b/appengine/memcache/guestbook/tests/test_guestbook.py index 0419cfd4b191..8d7acd74f7b5 100644 --- a/appengine/memcache/guestbook/tests/test_guestbook.py +++ b/appengine/memcache/guestbook/tests/test_guestbook.py @@ -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. diff --git a/datastore/ndb/modeling/tests/test_base.py b/datastore/ndb/modeling/tests/test_base.py deleted file mode 100644 index 267b2661f5ad..000000000000 --- a/datastore/ndb/modeling/tests/test_base.py +++ /dev/null @@ -1,42 +0,0 @@ -# Copyright 2014 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. - -"""Test classes for code snippet for modeling article.""" - - -import unittest - -from google.appengine.datastore import datastore_stub_util -from google.appengine.ext import testbed - - -class TestCase(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() diff --git a/datastore/ndb/modeling/tests/test_contact_with_group_models.py b/datastore/ndb/modeling/tests/test_contact_with_group_models.py index 68a4984ca923..1ce4227a0763 100644 --- a/datastore/ndb/modeling/tests/test_contact_with_group_models.py +++ b/datastore/ndb/modeling/tests/test_contact_with_group_models.py @@ -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. @@ -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() diff --git a/datastore/ndb/modeling/tests/test_keyproperty_models.py b/datastore/ndb/modeling/tests/test_keyproperty_models.py index 781131897513..a25b3f701d44 100644 --- a/datastore/ndb/modeling/tests/test_keyproperty_models.py +++ b/datastore/ndb/modeling/tests/test_keyproperty_models.py @@ -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' @@ -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() diff --git a/datastore/ndb/modeling/tests/test_naive_models.py b/datastore/ndb/modeling/tests/test_naive_models.py index e6fb51aa739a..87d676f5061c 100644 --- a/datastore/ndb/modeling/tests/test_naive_models.py +++ b/datastore/ndb/modeling/tests/test_naive_models.py @@ -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' @@ -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() diff --git a/datastore/ndb/modeling/tests/test_parent_child_models.py b/datastore/ndb/modeling/tests/test_parent_child_models.py index 4f7b8864d1de..93cc87e13fa2 100644 --- a/datastore/ndb/modeling/tests/test_parent_child_models.py +++ b/datastore/ndb/modeling/tests/test_parent_child_models.py @@ -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' @@ -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() diff --git a/datastore/ndb/modeling/tests/test_relation_model_models.py b/datastore/ndb/modeling/tests/test_relation_model_models.py index 8990dae61969..bcb60c1a482c 100644 --- a/datastore/ndb/modeling/tests/test_relation_model_models.py +++ b/datastore/ndb/modeling/tests/test_relation_model_models.py @@ -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. @@ -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() diff --git a/datastore/ndb/modeling/tests/test_structured_property_models.py b/datastore/ndb/modeling/tests/test_structured_property_models.py index 8af106894d03..4e7a67756972 100644 --- a/datastore/ndb/modeling/tests/test_structured_property_models.py +++ b/datastore/ndb/modeling/tests/test_structured_property_models.py @@ -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.""" @@ -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() diff --git a/datastore/ndb/overview/tests/test_overview.py b/datastore/ndb/overview/tests/test_overview.py index 1dc585325bdb..39d5ffe2e02d 100644 --- a/datastore/ndb/overview/tests/test_overview.py +++ b/datastore/ndb/overview/tests/test_overview.py @@ -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. diff --git a/datastore/ndb/transactions/tests/test_transactions.py b/datastore/ndb/transactions/tests/test_transactions.py index 9af3e4aaffce..c42b6e3771fe 100644 --- a/datastore/ndb/transactions/tests/test_transactions.py +++ b/datastore/ndb/transactions/tests/test_transactions.py @@ -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) diff --git a/tests/__init__.py b/tests/__init__.py index e006de633f0e..d4a3ae70b043 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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( @@ -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()