diff --git a/appengine/memcache/guestbook/tests/__init__.py b/appengine/memcache/guestbook/tests/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/appengine/memcache/guestbook/tests/test_guestbook.py b/appengine/memcache/guestbook/tests/test_guestbook.py new file mode 100644 index 00000000000..b9747929d07 --- /dev/null +++ b/appengine/memcache/guestbook/tests/test_guestbook.py @@ -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) diff --git a/datastore/ndb/modeling/tests/test_base.py b/datastore/ndb/modeling/tests/test_base.py deleted file mode 100644 index 267b2661f5a..00000000000 --- 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 68a4984ca92..c73bd964b93 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 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. @@ -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 78113189751..66940badef1 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 DatastoreTestbedCase -class ContactTestCase(test_base.TestCase): +class ContactTestCase(DatastoreTestbedCase): """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 e6fb51aa739..b62189f881a 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 DatastoreTestbedCase -class ContactTestCase(test_base.TestCase): +class ContactTestCase(DatastoreTestbedCase): """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 4f7b8864d1d..a8c7b718769 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 DatastoreTestbedCase -class ContactTestCase(test_base.TestCase): +class ContactTestCase(DatastoreTestbedCase): """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 8990dae6196..19b4b4486bc 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 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. @@ -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 8af106894d0..d894cb185d0 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 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.""" @@ -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/__init__.py b/datastore/ndb/overview/tests/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/datastore/ndb/overview/tests/test_overview.py b/datastore/ndb/overview/tests/test_overview.py new file mode 100644 index 00000000000..152b68dbc69 --- /dev/null +++ b/datastore/ndb/overview/tests/test_overview.py @@ -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) diff --git a/datastore/ndb/transactions/tests/__init__.py b/datastore/ndb/transactions/tests/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/datastore/ndb/transactions/tests/test_transactions.py b/datastore/ndb/transactions/tests/test_transactions.py new file mode 100644 index 00000000000..4b39afdbc7c --- /dev/null +++ b/datastore/ndb/transactions/tests/test_transactions.py @@ -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') diff --git a/tests/__init__.py b/tests/__init__.py index e006de633f0..6e173362057 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 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()