Skip to content

Commit 55bc2c0

Browse files
committed
DRY on unittests
1 parent 31db9a9 commit 55bc2c0

File tree

11 files changed

+43
-164
lines changed

11 files changed

+43
-164
lines changed

appengine/memcache/guestbook/tests/test_guestbook.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import unittest
16-
1715
# from the app main.py
1816
from appengine.memcache.guestbook import main
19-
20-
from google.appengine.datastore import datastore_stub_util
21-
from google.appengine.ext import testbed
17+
from tests import DatastoreTestbed
2218

2319
import webapp2
2420

2521

26-
class TestHandlers(unittest.TestCase):
27-
def setUp(self):
28-
"""Setup the datastore and memcache stub."""
29-
# First, create an instance of the Testbed class.
30-
self.testbed = testbed.Testbed()
31-
# Then activate the testbed, which prepares the service stubs for
32-
# use.
33-
self.testbed.activate()
34-
# Create a consistency policy that will simulate the High
35-
# Replication consistency model.
36-
self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(
37-
probability=0)
38-
# Initialize the datastore stub with this policy.
39-
self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)
40-
self.testbed.init_memcache_stub()
41-
42-
def tearDown(self):
43-
self.testbed.deactivate()
44-
22+
class TestHandlers(DatastoreTestbed):
4523
def test_hello(self):
4624
# Build a request object passing the URI path to be tested.
4725
# You can also pass headers, query arguments etc.

datastore/ndb/modeling/tests/test_base.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

datastore/ndb/modeling/tests/test_contact_with_group_models.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414

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

17-
18-
import unittest
19-
2017
from datastore.ndb.modeling import contact_with_group_models as models
2118

2219
from google.appengine.ext import ndb
2320

24-
import test_base
21+
from tests import DatastoreTestbed
2522

2623

27-
class ContactTestCase(test_base.TestCase):
24+
class ContactTestCase(DatastoreTestbed):
2825
"""A test case for the Contact model with groups."""
2926
def setUp(self):
3027
"""Creates 3 contacts and 1 group.
@@ -57,7 +54,3 @@ def test_groups(self):
5754
# How about 'members' property?
5855
friend_list = friends.members.fetch()
5956
self.assertEqual(len(friend_list), 1)
60-
61-
62-
if __name__ == '__main__':
63-
unittest.main()

datastore/ndb/modeling/tests/test_keyproperty_models.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414

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

17-
1817
import unittest
1918

2019
from datastore.ndb.modeling import keyproperty_models as models
2120

22-
import test_base
21+
from tests import DatastoreTestbed
2322

2423

25-
class ContactTestCase(test_base.TestCase):
24+
class ContactTestCase(DatastoreTestbed):
2625
"""A test case for the Contact model class with KeyProperty."""
2726
NAME = 'Takashi Matsuo'
2827

@@ -50,7 +49,3 @@ def test_fails(self):
5049
numbers = contact.phone_numbers.fetch()
5150
self.assertEqual(1, len(numbers))
5251
# [END failing_test]
53-
54-
55-
if __name__ == '__main__':
56-
unittest.main()

datastore/ndb/modeling/tests/test_naive_models.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@
1414

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

17-
18-
import unittest
19-
2017
from datastore.ndb.modeling import naive_models as models
2118

22-
import test_base
19+
from tests import DatastoreTestbed
2320

2421

25-
class ContactTestCase(test_base.TestCase):
22+
class ContactTestCase(DatastoreTestbed):
2623
"""A test case for the naive Contact model classe."""
2724
NAME = 'Takashi Matsuo'
2825

@@ -36,7 +33,3 @@ def test_basic(self):
3633
"""Test for getting a NaiveContact entity."""
3734
contact = self.contact_key.get()
3835
self.assertEqual(contact.name, self.NAME)
39-
40-
41-
if __name__ == '__main__':
42-
unittest.main()

datastore/ndb/modeling/tests/test_parent_child_models.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414

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

17-
18-
import unittest
19-
2017
from datastore.ndb.modeling import parent_child_models as models
2118

2219
from google.appengine.ext import ndb
2320

24-
import test_base
21+
from tests import DatastoreTestbed
2522

2623

27-
class ContactTestCase(test_base.TestCase):
24+
class ContactTestCase(DatastoreTestbed):
2825
"""A test case for the Contact model class with KeyProperty."""
2926
NAME = 'Takashi Matsuo'
3027

@@ -83,7 +80,3 @@ def test_phone_numbers(self):
8380
models.PhoneNumber.phone_type == 'mobile')
8481
entities = query.fetch()
8582
self.assertEqual(0, len(entities))
86-
87-
88-
if __name__ == '__main__':
89-
unittest.main()

datastore/ndb/modeling/tests/test_relation_model_models.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414

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

17-
18-
import unittest
19-
2017
from datastore.ndb.modeling import relation_model_models as models
2118

2219
from google.appengine.ext import ndb
2320

24-
import test_base
21+
from tests import DatastoreTestbed
2522

2623

27-
class ContactTestCase(test_base.TestCase):
24+
class ContactTestCase(DatastoreTestbed):
2825
"""A test case for the Contact model with relationship model."""
2926
def setUp(self):
3027
"""Creates 1 contact and 1 company.
@@ -60,7 +57,3 @@ def test_relationship(self):
6057
title='president').put()
6158
# get the list of companies that Mary belongs to
6259
self.assertEqual(len(mary.companies), 2)
63-
64-
65-
if __name__ == '__main__':
66-
unittest.main()

datastore/ndb/modeling/tests/test_structured_property_models.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@
1414

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

17-
18-
import unittest
19-
2017
from datastore.ndb.modeling import structured_property_models as models
2118

22-
import test_base
19+
from tests import DatastoreTestbed
2320

2421

25-
class ContactTestCase(test_base.TestCase):
22+
class ContactTestCase(DatastoreTestbed):
2623
"""A test case for the Contact model with StructuredProperty."""
2724
def setUp(self):
2825
"""Creates one Contact entity with 2 phone numbers."""
@@ -67,7 +64,3 @@ def test_phone_numbers(self):
6764
self.assertEqual(len(scott.phone_numbers), 1)
6865
self.assertEqual(scott.phone_numbers[0].phone_type, 'home')
6966
self.assertEqual(scott.phone_numbers[0].number, '(650) 555 - 2200')
70-
71-
72-
if __name__ == '__main__':
73-
unittest.main()

datastore/ndb/overview/tests/test_overview.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import unittest
16-
1715
# from the app main.py
1816
from datastore.ndb.overview import main
1917

20-
from google.appengine.datastore import datastore_stub_util
21-
from google.appengine.ext import testbed
18+
from tests import DatastoreTestbed
2219

2320
import webapp2
2421

2522

26-
class TestHandlers(unittest.TestCase):
27-
def setUp(self):
28-
"""Setup the datastore and memcache stub."""
29-
# First, create an instance of the Testbed class.
30-
self.testbed = testbed.Testbed()
31-
# Then activate the testbed, which prepares the service stubs for
32-
# use.
33-
self.testbed.activate()
34-
# Create a consistency policy that will simulate the High
35-
# Replication consistency model.
36-
self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(
37-
probability=0)
38-
# Initialize the datastore stub with this policy.
39-
self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)
40-
self.testbed.init_memcache_stub()
41-
42-
def tearDown(self):
43-
self.testbed.deactivate()
44-
23+
class TestHandlers(DatastoreTestbed):
4524
def test_hello(self):
4625
# Build a request object passing the URI path to be tested.
4726
# You can also pass headers, query arguments etc.

datastore/ndb/transactions/tests/test_transactions.py

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,19 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import unittest
16-
1715
# from the app main.py
1816
from datastore.ndb.transactions import main
1917

20-
from google.appengine.datastore import datastore_stub_util
21-
from google.appengine.ext import testbed
22-
18+
from tests import DatastoreTestbed
2319

2420

25-
class TestHandlers(unittest.TestCase):
21+
class TestHandlers(DatastoreTestbed):
2622
def setUp(self):
27-
"""Setup the datastore and memcache stub."""
28-
# First, create an instance of the Testbed class.
29-
self.testbed = testbed.Testbed()
30-
# Then activate the testbed, which prepares the service stubs for
31-
# use.
32-
self.testbed.activate()
33-
# Create a consistency policy that will simulate the High
34-
# Replication consistency model.
35-
self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(
36-
probability=0)
37-
# Initialize the datastore stub with this policy.
38-
self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)
39-
self.testbed.init_memcache_stub()
40-
23+
super(TestHandlers, self).setUp()
24+
self.testbed.init_taskqueue_stub()
4125
main.app.config['TESTING'] = True
4226
self.app = main.app.test_client()
4327

44-
def tearDown(self):
45-
self.testbed.deactivate()
46-
4728
def test_hello(self):
4829
rv = self.app.get('/')
4930
self.assertIn('Permenant note page', rv.data)

0 commit comments

Comments
 (0)