Ejemplo n.º 1
0
    def test_null(self):
        from google.protobuf import struct_pb2
        from gcloud.datastore._generated import entity_pb2

        pb = entity_pb2.Value(null_value=struct_pb2.NULL_VALUE)
        result = self._callFUT(pb)
        self.assertIsNone(result)
Ejemplo n.º 2
0
    def test_single(self):
        from gcloud.datastore._generated import entity_pb2

        value_pb = entity_pb2.Value()
        value_pb.meaning = meaning = 22
        value_pb.string_value = u'hi'
        result = self._callFUT(value_pb)
        self.assertEqual(meaning, result)
Ejemplo n.º 3
0
    def test_key(self):
        from gcloud.datastore._generated import entity_pb2
        from gcloud.datastore.key import Key

        pb = entity_pb2.Value()
        expected = Key('KIND', 1234, project='PROJECT').to_protobuf()
        pb.key_value.CopyFrom(expected)
        found = self._callFUT(pb)
        self.assertEqual(found.to_protobuf(), expected)
Ejemplo n.º 4
0
    def test_empty_list_value(self):
        from gcloud.datastore._generated import entity_pb2

        value_pb = entity_pb2.Value()
        value_pb.list_value.add()
        value_pb.list_value.pop()

        result = self._callFUT(value_pb, is_list=True)
        self.assertEqual(None, result)
Ejemplo n.º 5
0
    def test_list(self):
        from gcloud.datastore._generated import entity_pb2

        pb = entity_pb2.Value()
        list_pb = pb.list_value
        item_pb = list_pb.add()
        item_pb.string_value = 'Foo'
        item_pb = list_pb.add()
        item_pb.string_value = 'Bar'
        items = self._callFUT(pb)
        self.assertEqual(items, ['Foo', 'Bar'])
Ejemplo n.º 6
0
    def test_datetime(self):
        import calendar
        import datetime
        from gcloud._helpers import UTC
        from gcloud.datastore._generated 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._callFUT(pb), utc)
Ejemplo n.º 7
0
    def test_geo_point(self):
        from google.type import latlng_pb2
        from gcloud.datastore._generated import entity_pb2
        from gcloud.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._callFUT(pb)
        self.assertIsInstance(result, GeoPoint)
        self.assertEqual(result.latitude, lat)
        self.assertEqual(result.longitude, lng)
Ejemplo n.º 8
0
    def test_list_value_partially_unset(self):
        from gcloud.datastore._generated import entity_pb2

        value_pb = entity_pb2.Value()
        meaning1 = 9
        sub_value_pb1 = value_pb.list_value.add()
        sub_value_pb2 = value_pb.list_value.add()

        sub_value_pb1.meaning = meaning1
        sub_value_pb1.string_value = u'hi'
        sub_value_pb2.string_value = u'bye'

        with self.assertRaises(ValueError):
            self._callFUT(value_pb, is_list=True)
Ejemplo n.º 9
0
    def test_array_value_meaning_partially_unset(self):
        from gcloud.datastore._generated 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._callFUT(value_pb, is_list=True)
        self.assertEqual(result, [meaning1, None])
Ejemplo n.º 10
0
    def test_list_value(self):
        from gcloud.datastore._generated import entity_pb2

        value_pb = entity_pb2.Value()
        meaning = 9
        sub_value_pb1 = value_pb.list_value.add()
        sub_value_pb2 = value_pb.list_value.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._callFUT(value_pb, is_list=True)
        self.assertEqual(meaning, result)
Ejemplo n.º 11
0
    def test_entity(self):
        from gcloud.datastore._generated import entity_pb2
        from gcloud.datastore.entity import Entity
        from gcloud.datastore.helpers import _new_value_pb

        pb = entity_pb2.Value()
        entity_pb = pb.entity_value
        entity_pb.key.path_element.add(kind='KIND')
        entity_pb.key.partition_id.dataset_id = 'PROJECT'

        value_pb = _new_value_pb(entity_pb, 'foo')
        value_pb.string_value = 'Foo'
        entity = self._callFUT(pb)
        self.assertTrue(isinstance(entity, Entity))
        self.assertEqual(entity['foo'], 'Foo')
Ejemplo n.º 12
0
    def test_array_value_disagreeing(self):
        from gcloud.datastore._generated import entity_pb2

        value_pb = entity_pb2.Value()
        meaning1 = 9
        meaning2 = 10
        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_pb2.meaning = meaning2
        sub_value_pb1.string_value = u'hi'
        sub_value_pb2.string_value = u'bye'

        with self.assertRaises(ValueError):
            self._callFUT(value_pb, is_list=True)
Ejemplo n.º 13
0
    def test_unknown(self):
        from gcloud.datastore._generated import entity_pb2

        pb = entity_pb2.Value()
        with self.assertRaises(ValueError):
            self._callFUT(pb)
Ejemplo n.º 14
0
    def test_no_meaning(self):
        from gcloud.datastore._generated import entity_pb2

        value_pb = entity_pb2.Value()
        result = self._callFUT(value_pb)
        self.assertEqual(result, None)
Ejemplo n.º 15
0
 def _makePB(self):
     from gcloud.datastore._generated import entity_pb2
     return entity_pb2.Value()
Ejemplo n.º 16
0
    def test_unknown(self):
        from gcloud.datastore._generated import entity_pb2

        pb = entity_pb2.Value()
        self.assertEqual(self._callFUT(pb), None)
Ejemplo n.º 17
0
    def _makePB(self, attr_name, value):
        from gcloud.datastore._generated import entity_pb2

        pb = entity_pb2.Value()
        setattr(pb, attr_name, value)
        return pb