| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Unit tests for model.py.""" | 6 """Unit tests for model.py.""" |
| 7 | 7 |
| 8 import json | 8 import json |
| 9 import logging | 9 import logging |
| 10 import os | 10 import os |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 j = set | 64 j = set |
| 65 # k is not a type so it's not serialized. | 65 # k is not a type so it's not serialized. |
| 66 k = 23 | 66 k = 23 |
| 67 | 67 |
| 68 | 68 |
| 69 class TypeOrDict(PersistentMixIn): | 69 class TypeOrDict(PersistentMixIn): |
| 70 # Accepts a Basic or a dict. | 70 # Accepts a Basic or a dict. |
| 71 l = (Basic, dict) | 71 l = (Basic, dict) |
| 72 | 72 |
| 73 | 73 |
| 74 class StrDisallowed(PersistentMixIn): | 74 class StrAllowed(PersistentMixIn): |
| 75 m = str | 75 m = str |
| 76 | 76 |
| 77 | 77 |
| 78 def marshall(data): | 78 def marshall(data): |
| 79 """JSON encodes then decodes to make sure the data has passed through JSON | 79 """JSON encodes then decodes to make sure the data has passed through JSON |
| 80 type reduction. | 80 type reduction. |
| 81 """ | 81 """ |
| 82 return json.loads(json.dumps(data)) | 82 return json.loads(json.dumps(data)) |
| 83 | 83 |
| 84 | 84 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 data = TypeOrDict(l={'foo': u'bar'}) | 221 data = TypeOrDict(l={'foo': u'bar'}) |
| 222 self._check(data, TypeOrDict, l={'foo': u'bar'}) | 222 self._check(data, TypeOrDict, l={'foo': u'bar'}) |
| 223 expected = { | 223 expected = { |
| 224 'l': { | 224 'l': { |
| 225 'foo': 'bar', | 225 'foo': 'bar', |
| 226 }, | 226 }, |
| 227 TYPE_FLAG: 'TypeOrDict', | 227 TYPE_FLAG: 'TypeOrDict', |
| 228 } | 228 } |
| 229 self.assertEqual(expected, data.as_dict()) | 229 self.assertEqual(expected, data.as_dict()) |
| 230 | 230 |
| 231 def testStrDisallowed(self): | 231 def testStrAllowed(self): |
| 232 self.assertRaises(TypeError, StrDisallowed) | 232 data = StrAllowed(m='foo') |
| 233 expected = { |
| 234 'm': 'foo', |
| 235 TYPE_FLAG: 'StrAllowed', |
| 236 } |
| 237 self.assertEqual(expected, data.as_dict()) |
| 233 | 238 |
| 234 | 239 |
| 235 class Deserialize(Base): | 240 class Deserialize(Base): |
| 236 def testNotFound(self): | 241 def testNotFound(self): |
| 237 data = { TYPE_FLAG: 'DoesNotExists' } | 242 data = { TYPE_FLAG: 'DoesNotExists' } |
| 238 self.assertRaises(KeyError, PersistentMixIn.from_dict, marshall(data)) | 243 self.assertRaises(KeyError, PersistentMixIn.from_dict, marshall(data)) |
| 239 | 244 |
| 240 def testEmpty(self): | 245 def testEmpty(self): |
| 241 data = { } | 246 data = { } |
| 242 self.assertRaises(KeyError, PersistentMixIn.from_dict, marshall(data)) | 247 self.assertRaises(KeyError, PersistentMixIn.from_dict, marshall(data)) |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 def testTypeOrDict_dict(self): | 375 def testTypeOrDict_dict(self): |
| 371 data = { | 376 data = { |
| 372 'l': { | 377 'l': { |
| 373 'foo': 'bar', | 378 'foo': 'bar', |
| 374 }, | 379 }, |
| 375 TYPE_FLAG: 'TypeOrDict', | 380 TYPE_FLAG: 'TypeOrDict', |
| 376 } | 381 } |
| 377 actual = PersistentMixIn.from_dict(marshall(data)) | 382 actual = PersistentMixIn.from_dict(marshall(data)) |
| 378 self._check(actual, TypeOrDict, l={'foo': 'bar'}) | 383 self._check(actual, TypeOrDict, l={'foo': 'bar'}) |
| 379 | 384 |
| 380 def testStrDisallowed(self): | 385 def testStrAllowed(self): |
| 381 data = { | 386 data = { |
| 382 TYPE_FLAG: 'StrDisallowed', | 387 'm': 'bar', |
| 388 TYPE_FLAG: 'StrAllowed', |
| 383 } | 389 } |
| 384 self.assertRaises(TypeError, PersistentMixIn.from_dict, marshall(data)) | 390 actual = PersistentMixIn.from_dict(marshall(StrAllowed(m='bar'))) |
| 391 self._check(actual, StrAllowed, m='bar') |
| 385 | 392 |
| 386 | 393 |
| 387 class Mutable(object): | 394 class Mutable(object): |
| 388 def __init__(self, x): | 395 def __init__(self, x): |
| 389 self.x = x | 396 self.x = x |
| 390 | 397 |
| 391 @immutable | 398 @immutable |
| 392 def func_call_immutable_deep(self): | 399 def func_call_immutable_deep(self): |
| 393 return self.func_call_immutable() | 400 return self.func_call_immutable() |
| 394 | 401 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 self.assertEqual(3, obj.property_y) | 500 self.assertEqual(3, obj.property_y) |
| 494 self.assertEqual(3, obj.func_call_property()) | 501 self.assertEqual(3, obj.func_call_property()) |
| 495 self.assertEqual(2, obj.x) | 502 self.assertEqual(2, obj.x) |
| 496 | 503 |
| 497 | 504 |
| 498 if __name__ == '__main__': | 505 if __name__ == '__main__': |
| 499 logging.basicConfig( | 506 logging.basicConfig( |
| 500 level=logging.DEBUG if '-v' in sys.argv else logging.WARNING, | 507 level=logging.DEBUG if '-v' in sys.argv else logging.WARNING, |
| 501 format='%(levelname)5s %(module)15s(%(lineno)3d): %(message)s') | 508 format='%(levelname)5s %(module)15s(%(lineno)3d): %(message)s') |
| 502 unittest.main() | 509 unittest.main() |
| OLD | NEW |