|
| 1 | +from datetime import datetime |
| 2 | +import time |
| 3 | + |
| 4 | +import unittest2 |
| 5 | + |
| 6 | +from gcloud.datastore import helpers |
| 7 | +from gcloud.datastore.key import Key |
| 8 | + |
| 9 | + |
| 10 | +class TestHelpers(unittest2.TestCase): |
| 11 | + |
| 12 | + def test_get_protobuf_attribute(self): |
| 13 | + mapping = ( |
| 14 | + (str(), 'string_value'), |
| 15 | + (unicode(), 'string_value'), |
| 16 | + (int(), 'integer_value'), |
| 17 | + (long(), 'integer_value'), |
| 18 | + (float(), 'double_value'), |
| 19 | + (bool(), 'boolean_value'), |
| 20 | + (datetime.now(), 'timestamp_microseconds_value'), |
| 21 | + (Key(), 'key_value'), |
| 22 | + ) |
| 23 | + |
| 24 | + for test_value, expected_name in mapping: |
| 25 | + actual_name, _ = helpers.get_protobuf_attribute_and_value(test_value) |
| 26 | + self.assertEqual(expected_name, actual_name, |
| 27 | + 'Test value "%s" expected %s, got %s' % ( |
| 28 | + test_value, expected_name, actual_name)) |
| 29 | + |
| 30 | + def test_get_protobuf_value(self): |
| 31 | + now = datetime.now() |
| 32 | + |
| 33 | + mapping = ( |
| 34 | + (str('string'), 'string'), |
| 35 | + (unicode('string'), 'string'), |
| 36 | + (int(), int()), |
| 37 | + (long(), int()), |
| 38 | + (float(), float()), |
| 39 | + (bool(), bool()), |
| 40 | + (now, time.mktime(now.timetuple())), |
| 41 | + (Key(), Key().to_protobuf()), |
| 42 | + ) |
| 43 | + |
| 44 | + for test_value, expected_value in mapping: |
| 45 | + _, actual_value = helpers.get_protobuf_attribute_and_value(test_value) |
| 46 | + self.assertEqual(expected_value, actual_value, |
| 47 | + 'Test value "%s" expected %s, got %s.' % ( |
| 48 | + test_value, expected_value, actual_value)) |
0 commit comments