|
| 1 | +# Copyright 2015 Google Inc |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START imports] |
| 16 | +import unittest |
| 17 | +from google.appengine.api import memcache |
| 18 | +from google.appengine.ext import ndb |
| 19 | +from google.appengine.ext import testbed |
| 20 | +# [END imports] |
| 21 | + |
| 22 | + |
| 23 | +# [START datastore_example_1] |
| 24 | +class TestModel(ndb.Model): |
| 25 | + """A model class used for testing.""" |
| 26 | + number = ndb.IntegerProperty(default=42) |
| 27 | + text = ndb.StringProperty() |
| 28 | + |
| 29 | + |
| 30 | +class TestEntityGroupRoot(ndb.Model): |
| 31 | + """Entity group root""" |
| 32 | + pass |
| 33 | + |
| 34 | + |
| 35 | +def GetEntityViaMemcache(entity_key): |
| 36 | + """Get entity from memcache if available, from datastore if not.""" |
| 37 | + entity = memcache.get(entity_key) |
| 38 | + if entity is not None: |
| 39 | + return entity |
| 40 | + key = ndb.Key(urlsafe=entity_key) |
| 41 | + entity = key.get() |
| 42 | + if entity is not None: |
| 43 | + memcache.set(entity_key, entity) |
| 44 | + return entity |
| 45 | +# [END datastore_example_1] |
| 46 | + |
| 47 | + |
| 48 | +# [START datastore_example_test] |
| 49 | +class DatstoreTestCase(unittest.TestCase): |
| 50 | + |
| 51 | + def setUp(self): |
| 52 | + # First, create an instance of the Testbed class. |
| 53 | + self.testbed = testbed.Testbed() |
| 54 | + # Then activate the testbed, which prepares the service stubs for use. |
| 55 | + self.testbed.activate() |
| 56 | + # Next, declare which service stubs you want to use. |
| 57 | + self.testbed.init_datastore_v3_stub() |
| 58 | + self.testbed.init_memcache_stub() |
| 59 | + # Clear ndb's in-context cache between tests. |
| 60 | + # This prevents data from leaking between tests. |
| 61 | + # Alternatively, you could disable caching by |
| 62 | + # using ndb.get_context().set_cache_policy(False) |
| 63 | + ndb.get_context().clear_cache() |
| 64 | + |
| 65 | +# [END datastore_example_test] |
| 66 | + |
| 67 | + # [START datastore_example_teardown] |
| 68 | + def tearDown(self): |
| 69 | + self.testbed.deactivate() |
| 70 | + # [END datastore_example_teardown] |
| 71 | + |
| 72 | + # [START datastore_example_insert] |
| 73 | + def testInsertEntity(self): |
| 74 | + TestModel().put() |
| 75 | + self.assertEqual(1, len(TestModel.query().fetch(2))) |
| 76 | + # [END datastore_example_insert] |
| 77 | + |
| 78 | + # [START datastore_example_filter] |
| 79 | + def testFilterByNumber(self): |
| 80 | + root = TestEntityGroupRoot(id="root") |
| 81 | + TestModel(parent=root.key).put() |
| 82 | + TestModel(number=17, parent=root.key).put() |
| 83 | + query = TestModel.query(ancestor=root.key).filter(TestModel.number == 42) |
| 84 | + results = query.fetch(2) |
| 85 | + self.assertEqual(1, len(results)) |
| 86 | + self.assertEqual(42, results[0].number) |
| 87 | + # [END datastore_example_filter] |
| 88 | + |
| 89 | + # [START datastore_example_memcache] |
| 90 | + def testGetEntityViaMemcache(self): |
| 91 | + entity_key = TestModel(number=18).put().urlsafe() |
| 92 | + retrieved_entity = GetEntityViaMemcache(entity_key) |
| 93 | + self.assertNotEqual(None, retrieved_entity) |
| 94 | + self.assertEqual(18, retrieved_entity.number) |
| 95 | + # [END datastore_example_memcache] |
| 96 | + |
| 97 | + |
| 98 | +# [START HRD_example_1] |
| 99 | +from google.appengine.datastore import datastore_stub_util |
| 100 | + |
| 101 | + |
| 102 | +class HighReplicationTestCaseOne(unittest.TestCase): |
| 103 | + |
| 104 | + def setUp(self): |
| 105 | + # First, create an instance of the Testbed class. |
| 106 | + self.testbed = testbed.Testbed() |
| 107 | + # Then activate the testbed, which prepares the service stubs for use. |
| 108 | + self.testbed.activate() |
| 109 | + # Create a consistency policy that will simulate the High Replication |
| 110 | + # consistency model. |
| 111 | + self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy( |
| 112 | + probability=0) |
| 113 | + # Initialize the datastore stub with this policy. |
| 114 | + self.testbed.init_datastore_v3_stub(consistency_policy=self.policy) |
| 115 | + # Initialize memcache stub too, since ndb also uses memcache |
| 116 | + self.testbed.init_memcache_stub() |
| 117 | + # Clear in-context cache before each test. |
| 118 | + ndb.get_context().clear_cache() |
| 119 | + |
| 120 | + def tearDown(self): |
| 121 | + self.testbed.deactivate() |
| 122 | + |
| 123 | + def testEventuallyConsistentGlobalQueryResult(self): |
| 124 | + class TestModel(ndb.Model): |
| 125 | + pass |
| 126 | + |
| 127 | + user_key = ndb.Key('User', 'ryan') |
| 128 | + |
| 129 | + # Put two entities |
| 130 | + ndb.put_multi([ |
| 131 | + TestModel(parent=user_key), |
| 132 | + TestModel(parent=user_key) |
| 133 | + ]) |
| 134 | + |
| 135 | + # Global query doesn't see the data. |
| 136 | + self.assertEqual(0, TestModel.query().count(3)) |
| 137 | + # Ancestor query does see the data. |
| 138 | + self.assertEqual(2, TestModel.query(ancestor=user_key).count(3)) |
| 139 | +# [END HRD_example_1] |
| 140 | + |
| 141 | + # [START HRD_example_2] |
| 142 | + def testDeterministicOutcome(self): |
| 143 | + # 50% chance to apply. |
| 144 | + self.policy.SetProbability(.5) |
| 145 | + # Use the pseudo random sequence derived from seed=2. |
| 146 | + self.policy.SetSeed(2) |
| 147 | + |
| 148 | + class TestModel(ndb.Model): |
| 149 | + pass |
| 150 | + |
| 151 | + TestModel().put() |
| 152 | + |
| 153 | + self.assertEqual(0, TestModel.query().count(3)) |
| 154 | + self.assertEqual(0, TestModel.query().count(3)) |
| 155 | + # Will always be applied before the third query. |
| 156 | + self.assertEqual(1, TestModel.query().count(3)) |
| 157 | + # [END HRD_example_2] |
| 158 | + |
| 159 | + |
| 160 | +# [START main] |
| 161 | +if __name__ == '__main__': |
| 162 | + unittest.main() |
| 163 | +# [END main] |
0 commit comments