Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from google.appengine.api import datastore_errors | 5 from google.appengine.api import datastore_errors |
| 6 from google.appengine.ext import ndb | 6 from google.appengine.ext import ndb |
| 7 | 7 |
| 8 from testing_utils import testing | 8 from testing_utils import testing |
| 9 | 9 |
| 10 from model.versioned_model import VersionedModel | 10 from model.versioned_model import VersionedModel |
| 11 | 11 |
| 12 | 12 |
| 13 class _Entity(VersionedModel): | 13 class _SingularEntity(VersionedModel): |
| 14 value = ndb.IntegerProperty(indexed=False) | 14 value = ndb.IntegerProperty(indexed=False) |
| 15 | 15 |
| 16 | 16 |
| 17 class _MultipleEntity(VersionedModel): | |
| 18 | |
| 19 @staticmethod | |
| 20 def Create(string_id): | |
| 21 return _MultipleEntity(key=ndb.Key('_MultipleEntity', string_id)) | |
| 22 | |
| 23 value = ndb.IntegerProperty(indexed=False) | |
| 24 | |
| 25 | |
| 17 class VersionedModelTest(testing.AppengineTestCase): | 26 class VersionedModelTest(testing.AppengineTestCase): |
| 18 | 27 |
| 19 def testGetRootModel(self): | 28 def testGetRootModel(self): |
| 20 root_model_class = _Entity._GetRootModel() | 29 root_model_class = _SingularEntity._GetRootModel() |
| 21 self.assertEqual('_EntityRoot', root_model_class._get_kind()) | 30 self.assertEqual('_SingularEntityRoot', root_model_class._get_kind()) |
| 22 self.assertTrue(issubclass(root_model_class, ndb.Model)) | 31 self.assertTrue(issubclass(root_model_class, ndb.Model)) |
| 23 self.assertEqual(3, root_model_class(current=3).current) | 32 self.assertEqual(3, root_model_class(current=3).current) |
| 24 | 33 |
| 25 def testDefaultVersionIsZero(self): | 34 def testDefaultVersionIsZero(self): |
| 26 entity = _Entity() | 35 self.assertEqual(0, _SingularEntity().version) |
| 27 self.assertEqual(0, entity.version) | 36 self.assertEqual(0, _MultipleEntity().version) |
| 28 | 37 self.assertEqual(0, _MultipleEntity().Create('m1').version) |
| 29 def testGetMostRecentVersionWhenNoData(self): | 38 |
| 30 entity = _Entity.GetVersion() | 39 def testRootId(self): |
| 31 self.assertIsNone(entity) | 40 self.assertIsNone(_MultipleEntity()._root_id) |
| 32 | 41 self.assertEqual( |
| 33 def testGetMostRecentVersionWhenDataExists(self): | 42 1, _MultipleEntity(key=ndb.Key('_MultipleEntity', 1))._root_id) |
| 34 root_key = ndb.Key('_EntityRoot', 1) | 43 self.assertEqual( |
| 35 _Entity._GetRootModel()(key=root_key, current=2).put() | 44 'm1', _MultipleEntity(key=ndb.Key('_MultipleEntity', 'm1'))._root_id) |
| 36 _Entity(key=ndb.Key('_Entity', 1, parent=root_key), value=1).put() | 45 self.assertEqual( |
| 37 _Entity(key=ndb.Key('_Entity', 2, parent=root_key), value=2).put() | 46 'm1', |
| 38 | 47 _MultipleEntity( |
| 39 entity = _Entity.GetVersion() | 48 key=ndb.Key( |
| 49 '_MultipleEntityRoot', 'm1', '_MultipleEntity', 1))._root_id) | |
| 50 | |
| 51 def testGetMostRecentVersionWhenDataExistsForSingularEntity(self): | |
| 52 root_key = ndb.Key('_SingularEntityRoot', 1) | |
| 53 _SingularEntity._GetRootModel()(key=root_key, current=2).put() | |
| 54 _SingularEntity( | |
| 55 key=ndb.Key('_SingularEntity', 1, parent=root_key), value=1).put() | |
| 56 _SingularEntity( | |
| 57 key=ndb.Key('_SingularEntity', 2, parent=root_key), value=2).put() | |
| 58 | |
| 59 entity = _SingularEntity.GetVersion() | |
| 40 self.assertEqual(2, entity.version) | 60 self.assertEqual(2, entity.version) |
| 41 self.assertEqual(2, entity.value) | 61 self.assertEqual(2, entity.value) |
| 42 | 62 |
| 43 def testGetNextVersionWhenDataExists(self): | 63 def testGetMostRecentVersionWhenNoData(self): |
| 44 root_key = ndb.Key('_EntityRoot', 1) | 64 self.assertIsNone(_SingularEntity.GetVersion()) |
| 45 _Entity._GetRootModel()(key=root_key, current=2).put() | 65 self.assertIsNone(_MultipleEntity.Create('m1').GetVersion()) |
| 46 _Entity(key=ndb.Key('_Entity', 1, parent=root_key), value=1).put() | 66 |
| 47 _Entity(key=ndb.Key('_Entity', 2, parent=root_key), value=2).put() | 67 def testCreateMultipleEntity(self): |
| 48 | 68 # Tests creating multiple versioned entities of the same model type. |
| 49 entity = _Entity.GetVersion(2) | 69 multiple_entity_1 = _MultipleEntity.Create('m1') |
| 70 multiple_entity_1.value = 1 | |
| 71 multiple_entity_1.Save() | |
| 72 | |
| 73 multiple_entity_2 = _MultipleEntity.Create('m2') | |
| 74 multiple_entity_2.value = 2 | |
| 75 multiple_entity_2.Save() | |
| 76 | |
| 77 self.assertEqual(multiple_entity_1.version, 1) | |
| 78 self.assertEqual(multiple_entity_1.value, 1) | |
| 79 self.assertEqual(multiple_entity_2.version, 1) | |
| 80 self.assertEqual(multiple_entity_2.value, 2) | |
| 81 | |
| 82 def testUpdateMultipleEntity(self): | |
| 83 multiple_entity = _MultipleEntity.Create('m1') | |
| 84 multiple_entity.value = 1 | |
| 85 multiple_entity.Save() | |
| 86 multiple_entity.value = 3 | |
| 87 multiple_entity.Save() | |
| 88 | |
| 89 self.assertEqual(multiple_entity.version, 2) | |
| 90 self.assertEqual(multiple_entity.value, 3) | |
| 91 | |
| 92 def testGetNextVersionWhenDataExistsForSingularEntity(self): | |
| 93 root_key = ndb.Key('_SingularEntityRoot', 1) | |
| 94 _SingularEntity._GetRootModel()(key=root_key, current=2).put() | |
| 95 _SingularEntity( | |
| 96 key=ndb.Key('_SingularEntity', 1, parent=root_key), value=1).put() | |
| 97 _SingularEntity( | |
| 98 key=ndb.Key('_SingularEntity', 2, parent=root_key), value=2).put() | |
| 99 | |
| 100 entity = _SingularEntity.GetVersion(version=2) | |
| 50 self.assertEqual(2, entity.version) | 101 self.assertEqual(2, entity.version) |
| 51 self.assertEqual(2, entity.value) | 102 self.assertEqual(2, entity.value) |
| 52 self.assertIsNone(_Entity.GetVersion(0)) | 103 self.assertIsNone(_SingularEntity.GetVersion(version=0)) |
| 53 self.assertIsNone(_Entity.GetVersion(3)) | 104 self.assertIsNone(_SingularEntity.GetVersion(version=3)) |
| 54 | 105 |
| 55 def testGetLatestVersionNumber(self): | 106 def testGetLatestVersionNumberForSingularEntity(self): |
| 56 root_key = ndb.Key('_EntityRoot', 1) | 107 root_key = ndb.Key('_SingularEntityRoot', 1) |
| 57 _Entity._GetRootModel()(key=root_key, current=1).put() | 108 _SingularEntity._GetRootModel()(key=root_key, current=1).put() |
| 58 _Entity(key=ndb.Key('_Entity', 1, parent=root_key), value=2).put() | 109 _SingularEntity( |
| 59 | 110 key=ndb.Key('_SingularEntity', 1, parent=root_key), value=2).put() |
| 60 self.assertEqual(1, _Entity.GetLatestVersionNumber()) | 111 |
| 112 self.assertEqual(1, _SingularEntity.GetLatestVersionNumber()) | |
| 113 | |
| 114 def testGetVersionForMultipleEntity(self): | |
| 115 entity = _MultipleEntity.Create('m1') | |
| 116 entity.value = 1 | |
| 117 entity.Save() | |
| 118 entity.value = 2 | |
| 119 entity.Save() | |
| 120 self.assertEqual(2, _MultipleEntity.GetVersion('m1').version) | |
| 121 self.assertEqual(2, _MultipleEntity.GetVersion('m1').value) | |
| 122 self.assertIsNone(_MultipleEntity.GetVersion('m1', version=0)) | |
| 123 self.assertIsNone(_MultipleEntity.GetVersion('m1', version=3)) | |
| 61 | 124 |
| 62 def testGetLatestVersionNumberWhenNoRecordYet(self): | 125 def testGetLatestVersionNumberWhenNoRecordYet(self): |
| 63 self.assertEqual(-1, _Entity.GetLatestVersionNumber()) | 126 self.assertEqual(-1, _SingularEntity.GetLatestVersionNumber()) |
| 64 | 127 self.assertEqual(-1, _MultipleEntity.GetLatestVersionNumber('m1')) |
| 65 def testSaveNewVersion(self): | 128 |
| 66 entity = _Entity() | 129 def testSaveNewVersionForSingularEntity(self): |
| 130 entity = _SingularEntity() | |
| 67 entity.value = 1 | 131 entity.value = 1 |
| 68 key = entity.Save() | 132 key, saved = entity.Save() |
| 69 | 133 |
| 70 expected_key = ndb.Key('_EntityRoot', 1, '_Entity', 1) | 134 expected_key = ndb.Key('_SingularEntityRoot', 1, '_SingularEntity', 1) |
| 71 self.assertEqual(expected_key, key) | 135 self.assertEqual(expected_key, key) |
| 72 | 136 self.assertTrue(saved) |
| 73 entity = _Entity.GetVersion() | 137 |
| 138 entity = _SingularEntity.GetVersion() | |
| 74 self.assertEqual(1, entity.version) | 139 self.assertEqual(1, entity.version) |
| 75 self.assertEqual(1, entity.value) | 140 self.assertEqual(1, entity.value) |
| 76 | 141 |
| 77 def testSaveNewVersionAlreadyExist(self): | 142 def testSaveNewVersionForMultipleEntity(self): |
| 143 entity = _MultipleEntity.Create('m1') | |
| 144 entity.value = 1 | |
| 145 key, saved = entity.Save() | |
| 146 | |
| 147 expected_key = ndb.Key( | |
| 148 '_MultipleEntityRoot', 'm1', '_MultipleEntity', 1) | |
| 149 self.assertEqual(expected_key, key) | |
| 150 self.assertTrue(saved) | |
| 151 | |
| 152 def testSaveNewVersionAlreadyExistsTryNextVersion(self): | |
| 78 original_ndb_transaction = ndb.transaction | 153 original_ndb_transaction = ndb.transaction |
| 79 | 154 |
| 80 def MockNdbTransaction(func, **options): | 155 def MockNdbTransaction(func, **options): |
| 81 _Entity(key=ndb.Key('_EntityRoot', 1, '_Entity', 1), value=1).put() | 156 _SingularEntity( |
| 82 _Entity(key=ndb.Key('_EntityRoot', 1, '_Entity', 2), value=2).put() | 157 key=ndb.Key( |
| 158 '_SingularEntityRoot', 1, '_SingularEntity', 1), value=1).put() | |
| 159 _SingularEntity( | |
| 160 key=ndb.Key( | |
| 161 '_SingularEntityRoot', 1, '_SingularEntity', 2), value=2).put() | |
| 83 return original_ndb_transaction(func, **options) | 162 return original_ndb_transaction(func, **options) |
| 84 self.mock(ndb, 'transaction', MockNdbTransaction) | 163 self.mock(ndb, 'transaction', MockNdbTransaction) |
| 85 | 164 |
| 86 entity = _Entity() | 165 entity = _SingularEntity() |
| 87 key = entity.Save() | 166 key, saved = entity.Save() |
| 88 expected_key = ndb.Key('_EntityRoot', 1, '_Entity', 3) | 167 expected_key = ndb.Key('_SingularEntityRoot', 1, '_SingularEntity', 3) |
| 89 self.assertEqual(expected_key, key) | 168 self.assertEqual(expected_key, key) |
| 169 self.assertTrue(saved) | |
| 170 | |
| 171 def testSaveDataNewVersionAlreadyExistsNotTryNextVersion(self): | |
| 172 # When the call to SaveData() starts, another transaction had just beat this | |
|
stgao
2016/09/23 16:45:20
SaveData() is an internal implementation in Versio
lijeffrey
2016/09/23 19:56:09
This test is to cover the case that a save conflic
| |
| 173 # one to it. The expected result is the saved version from the first | |
| 174 # transaction. | |
| 175 original_ndb_transaction = ndb.transaction | |
| 176 | |
| 177 def MockNdbTransaction(func, **options): | |
| 178 _SingularEntity( | |
| 179 key=ndb.Key( | |
| 180 '_SingularEntityRoot', 1, '_SingularEntity', 1), value=1).put() | |
| 181 return original_ndb_transaction(func, **options) | |
| 182 self.mock(ndb, 'transaction', MockNdbTransaction) | |
| 183 | |
| 184 entity = _SingularEntity() | |
| 185 key, saved = entity.Save(retry_on_conflict=False) | |
| 186 expected_key = ndb.Key('_SingularEntityRoot', 1, '_SingularEntity', 1) | |
| 187 self.assertEqual(expected_key, key) | |
| 188 self.assertFalse(saved) | |
| 189 | |
| 190 def testSaveNewVersionAlreadyExistsNotTryNextVersion(self): | |
| 191 # When the call to Save() starts, another transaction had just beat this | |
| 192 # one to it. The expected result is the saved version from the first | |
| 193 # transaction should be returned. | |
| 194 original_save = VersionedModel.Save | |
| 195 | |
| 196 def MockSave(*args, **kwargs): | |
|
stgao
2016/09/23 16:45:20
What we want to test is VersionedModel.Save, why d
lijeffrey
2016/09/23 19:56:09
This is to test the branch of the code that does t
| |
| 197 _SingularEntity( | |
| 198 key=ndb.Key( | |
| 199 '_SingularEntityRoot', 1, '_SingularEntity', 1), value=1).put() | |
| 200 return original_save(*args, **kwargs) | |
| 201 self.mock(VersionedModel, 'Save', MockSave) | |
| 202 | |
| 203 entity = _SingularEntity() | |
| 204 key, saved = entity.Save(retry_on_conflict=False) | |
| 205 expected_key = ndb.Key('_SingularEntityRoot', 1, '_SingularEntity', 1) | |
| 206 self.assertEqual(expected_key, key) | |
| 207 self.assertFalse(saved) | |
| 90 | 208 |
| 91 def testLikelyTransactionFailure(self): | 209 def testLikelyTransactionFailure(self): |
| 92 original_ndb_transaction = ndb.transaction | 210 original_ndb_transaction = ndb.transaction |
| 93 | 211 |
| 94 calls = [] | 212 calls = [] |
| 95 def MockNdbTransaction(func, **options): | 213 def MockNdbTransaction(func, **options): |
| 96 if len(calls) < 1: | 214 if len(calls) < 1: |
| 97 calls.append(1) | 215 calls.append(1) |
| 98 raise datastore_errors.Timeout() | 216 raise datastore_errors.Timeout() |
| 99 return original_ndb_transaction(func, **options) | 217 return original_ndb_transaction(func, **options) |
| 100 self.mock(ndb, 'transaction', MockNdbTransaction) | 218 self.mock(ndb, 'transaction', MockNdbTransaction) |
| 101 | 219 |
| 102 entity = _Entity() | 220 entity = _SingularEntity() |
| 103 key = entity.Save() | 221 key, saved = entity.Save() |
| 104 expected_key = ndb.Key('_EntityRoot', 1, '_Entity', 1) | 222 expected_key = ndb.Key('_SingularEntityRoot', 1, '_SingularEntity', 1) |
| 105 self.assertEqual(expected_key, key) | 223 self.assertEqual(expected_key, key) |
| 224 self.assertTrue(saved) | |
| 106 self.assertEqual([1], calls) | 225 self.assertEqual([1], calls) |
| 107 | 226 |
| 108 def testTransactionFailure(self): | 227 def testTransactionFailure(self): |
| 109 original_ndb_transaction = ndb.transaction | 228 original_ndb_transaction = ndb.transaction |
| 110 | 229 |
| 111 calls = [] | 230 calls = [] |
| 112 def MockNdbTransaction(func, **options): | 231 def MockNdbTransaction(func, **options): |
| 113 if len(calls) < 1: | 232 if len(calls) < 1: |
| 114 calls.append(1) | 233 calls.append(1) |
| 115 raise datastore_errors.BadRequestError() | 234 raise datastore_errors.BadRequestError() |
| 116 return original_ndb_transaction(func, **options) | 235 return original_ndb_transaction(func, **options) |
| 117 self.mock(ndb, 'transaction', MockNdbTransaction) | 236 self.mock(ndb, 'transaction', MockNdbTransaction) |
| 118 | 237 |
| 119 entity = _Entity() | 238 entity = _SingularEntity() |
| 120 key = entity.Save() | 239 key, saved = entity.Save() |
| 121 expected_key = ndb.Key('_EntityRoot', 1, '_Entity', 1) | 240 expected_key = ndb.Key('_SingularEntityRoot', 1, '_SingularEntity', 1) |
| 122 self.assertEqual(expected_key, key) | 241 self.assertEqual(expected_key, key) |
| 123 self.assertEqual([1], calls) | 242 self.assertEqual([1], calls) |
| 243 self.assertTrue(saved) | |
| OLD | NEW |