def test_null(self): from google.protobuf import struct_pb2 from google.cloud.proto.datastore.v1 import entity_pb2 pb = entity_pb2.Value(null_value=struct_pb2.NULL_VALUE) result = self._call_fut(pb) self.assertIsNone(result)
def test_single(self): from google.cloud.proto.datastore.v1 import entity_pb2 value_pb = entity_pb2.Value() value_pb.meaning = meaning = 22 value_pb.string_value = u'hi' result = self._call_fut(value_pb) self.assertEqual(meaning, result)
def test_empty_array_value(self): from google.cloud.proto.datastore.v1 import entity_pb2 value_pb = entity_pb2.Value() value_pb.array_value.values.add() value_pb.array_value.values.pop() result = self._call_fut(value_pb, is_list=True) self.assertEqual(None, result)
def test_key(self): from google.cloud.proto.datastore.v1 import entity_pb2 from google.cloud.datastore.key import Key pb = entity_pb2.Value() expected = Key('KIND', 1234, project='PROJECT').to_protobuf() pb.key_value.CopyFrom(expected) found = self._call_fut(pb) self.assertEqual(found.to_protobuf(), expected)
def test_array(self): from google.cloud.proto.datastore.v1 import entity_pb2 pb = entity_pb2.Value() array_pb = pb.array_value.values item_pb = array_pb.add() item_pb.string_value = 'Foo' item_pb = array_pb.add() item_pb.string_value = 'Bar' items = self._call_fut(pb) self.assertEqual(items, ['Foo', 'Bar'])
def test_datetime(self): import calendar import datetime from google.cloud._helpers import UTC from google.cloud.proto.datastore.v1 import entity_pb2 micros = 4375 utc = datetime.datetime(2014, 9, 16, 10, 19, 32, micros, UTC) pb = entity_pb2.Value() pb.timestamp_value.seconds = calendar.timegm(utc.timetuple()) pb.timestamp_value.nanos = 1000 * micros self.assertEqual(self._call_fut(pb), utc)
def test_geo_point(self): from google.type import latlng_pb2 from google.cloud.proto.datastore.v1 import entity_pb2 from google.cloud.datastore.helpers import GeoPoint lat = -3.14 lng = 13.37 geo_pt_pb = latlng_pb2.LatLng(latitude=lat, longitude=lng) pb = entity_pb2.Value(geo_point_value=geo_pt_pb) result = self._call_fut(pb) self.assertIsInstance(result, GeoPoint) self.assertEqual(result.latitude, lat) self.assertEqual(result.longitude, lng)
def test_dict_to_entity(self): from google.cloud.proto.datastore.v1 import entity_pb2 from google.cloud.datastore.entity import Entity entity = Entity() entity['a'] = {'b': u'c'} entity_pb = self._call_fut(entity) expected_pb = entity_pb2.Entity( properties={ 'a': entity_pb2.Value( entity_value=entity_pb2.Entity( properties={ 'b': entity_pb2.Value( string_value='c', ), }, ), ), }, ) self.assertEqual(entity_pb, expected_pb)
def test_array_value_meaning_partially_unset(self): from google.cloud.proto.datastore.v1 import entity_pb2 value_pb = entity_pb2.Value() meaning1 = 9 sub_value_pb1 = value_pb.array_value.values.add() sub_value_pb2 = value_pb.array_value.values.add() sub_value_pb1.meaning = meaning1 sub_value_pb1.string_value = u'hi' sub_value_pb2.string_value = u'bye' result = self._call_fut(value_pb, is_list=True) self.assertEqual(result, [meaning1, None])
def test_array_value(self): from google.cloud.proto.datastore.v1 import entity_pb2 value_pb = entity_pb2.Value() meaning = 9 sub_value_pb1 = value_pb.array_value.values.add() sub_value_pb2 = value_pb.array_value.values.add() sub_value_pb1.meaning = sub_value_pb2.meaning = meaning sub_value_pb1.string_value = u'hi' sub_value_pb2.string_value = u'bye' result = self._call_fut(value_pb, is_list=True) self.assertEqual(meaning, result)
def test_entity(self): from google.cloud.proto.datastore.v1 import entity_pb2 from google.cloud.datastore.entity import Entity from google.cloud.datastore.helpers import _new_value_pb pb = entity_pb2.Value() entity_pb = pb.entity_value entity_pb.key.path.add(kind='KIND') entity_pb.key.partition_id.project_id = 'PROJECT' value_pb = _new_value_pb(entity_pb, 'foo') value_pb.string_value = 'Foo' entity = self._call_fut(pb) self.assertIsInstance(entity, Entity) self.assertEqual(entity['foo'], 'Foo')
def test_dict_to_entity_recursive(self): from google.cloud.proto.datastore.v1 import entity_pb2 from google.cloud.datastore.entity import Entity entity = Entity() entity['a'] = { 'b': { 'c': { 'd': 1.25, }, 'e': True, }, 'f': 10, } entity_pb = self._call_fut(entity) b_entity_pb = entity_pb2.Entity( properties={ 'c': entity_pb2.Value( entity_value=entity_pb2.Entity( properties={ 'd': entity_pb2.Value( double_value=1.25, ), }, ), ), 'e': entity_pb2.Value(boolean_value=True), } ) expected_pb = entity_pb2.Entity( properties={ 'a': entity_pb2.Value( entity_value=entity_pb2.Entity( properties={ 'b': entity_pb2.Value( entity_value=b_entity_pb, ), 'f': entity_pb2.Value( integer_value=10, ), }, ), ), }, ) self.assertEqual(entity_pb, expected_pb)
def test_no_meaning(self): from google.cloud.proto.datastore.v1 import entity_pb2 value_pb = entity_pb2.Value() result = self._call_fut(value_pb) self.assertIsNone(result)
def _makePB(self): from google.cloud.proto.datastore.v1 import entity_pb2 return entity_pb2.Value()
def test_unknown(self): from google.cloud.proto.datastore.v1 import entity_pb2 pb = entity_pb2.Value() with self.assertRaises(ValueError): self._call_fut(pb)
def _makePB(self, attr_name, value): from google.cloud.proto.datastore.v1 import entity_pb2 pb = entity_pb2.Value() setattr(pb, attr_name, value) return pb