def testEntityFromClientEntity(self): k = Key(['kind', 1234], project=self._PROJECT) exclude_from_indexes = ('efi1', 'efi2') e = Entity(k, exclude_from_indexes=exclude_from_indexes) e.set_properties({'efi1': 'value', 'property': 'value'}) efc = Entity.from_client_entity(e.to_client_entity()) self.assertEqual(e, efc)
def testEntityToClientEntity(self): k = Key(['kind', 1234], project=self._PROJECT) kc = k.to_client_key() exclude_from_indexes = ('efi1', 'efi2') e = Entity(k, exclude_from_indexes=exclude_from_indexes) e.set_properties({'efi1': 'value', 'property': 'value'}) ec = e.to_client_entity() self.assertEqual(kc, ec.key) self.assertSetEqual(set(exclude_from_indexes), ec.exclude_from_indexes) self.assertEqual('kind', ec.kind) self.assertEqual(1234, ec.id)
def get_ndb_model_from_beam_entity( beam_entity: beam_datastore_types.Entity) -> datastore_services.Model: """Returns an NDB model equivalent to the given Apache Beam entity. Args: beam_entity: beam_datastore_types.Entity. The Apache Beam entity. Returns: datastore_services.Model. The NDB model. """ ndb_key = get_ndb_key_from_beam_key(beam_entity.key) # We use private _lookup_model and _entity_from_ds_entity here because it # provides a functionality that we need and writing it ourselves would be # too complicated. ndb_model_class = get_model_class(ndb_key.kind()) # pylint: disable=protected-access return ndb_model._entity_from_ds_entity( # pylint: disable=protected-access beam_entity.to_client_entity(), model_class=ndb_model_class)
def testEntityToClientEntity(self): # Test conversion from Beam type to client type. k = Key(['kind', 1234], project=self._PROJECT) kc = k.to_client_key() exclude_from_indexes = ('datetime', 'key') e = Entity(k, exclude_from_indexes=exclude_from_indexes) properties = { 'datetime': datetime.datetime.utcnow(), 'key_ref': Key(['kind2', 1235]), 'bool': True, 'float': 1.21, 'int': 1337, 'unicode': 'text', 'bytes': b'bytes', 'geopoint': GeoPoint(0.123, 0.456), 'none': None, 'list': [1, 2, 3], 'entity': Entity(Key(['kind', 111])), 'dict': { 'property': 5 }, } e.set_properties(properties) ec = e.to_client_entity() self.assertEqual(kc, ec.key) self.assertSetEqual(set(exclude_from_indexes), ec.exclude_from_indexes) self.assertEqual('kind', ec.kind) self.assertEqual(1234, ec.id) for name, unconverted in properties.items(): converted = ec[name] if name == 'key_ref': self.assertNotIsInstance(converted, Key) self._assert_keys_equal(unconverted, converted, self._PROJECT) elif name == 'entity': self.assertNotIsInstance(converted, Entity) self.assertNotIsInstance(converted.key, Key) self._assert_keys_equal(unconverted.key, converted.key, self._PROJECT) else: self.assertEqual(unconverted, converted) # Test reverse conversion. entity_from_client_entity = Entity.from_client_entity(ec) self.assertEqual(e, entity_from_client_entity)