How to use the pyodata.v2.service.EntityKey function in pyodata

To help you get started, we’ve selected a few pyodata examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github SAP / python-pyodata / tests / test_service_v2.py View on Github external
def test_get_entity_with_entity_key_and_other_params(service):
    """Make sure the method get_entity handles correctly the parameter key which is EntityKey"""

    # pylint: disable=redefined-outer-name

    key = EntityKey(
        service.schema.entity_type('TemperatureMeasurement'),
        Sensor='sensor1',
        Date=datetime.datetime(2017, 12, 24, 18, 0))

    query = service.entity_sets.TemperatureMeasurements.get_entity(key=key, Foo='Bar')
    assert query.get_path() == "TemperatureMeasurements(Sensor='sensor1',Date=datetime'2017-12-24T18:00:00')"
github SAP / python-pyodata / tests / test_service_v2.py View on Github external
def test_delete_entity_http_error(service):
    """Check if error is raisen when deleting unknown entity"""

    responses.add(responses.DELETE, f"{service.url}/Employees(ID=23)", status=404)
    key = EntityKey(service.schema.entity_type('Employee'), ID=23)
    request = service.entity_sets.Employees.delete_entity(key=key)

    assert isinstance(request, pyodata.v2.service.EntityDeleteRequest)

    with pytest.raises(HttpError) as caught_ex:
        request.execute()

    assert str(caught_ex.value).startswith('HTTP POST for Entity delete')
    assert caught_ex.value.response.status_code == 404
github SAP / python-pyodata / tests / test_service_v2.py View on Github external
def test_entity_key_simple_valid(service):
    """Test valid single value for simple key"""

    # pylint: disable=redefined-outer-name

    key = EntityKey(
        service.schema.entity_type('MasterEntity'),
        '1')

    assert key.to_key_string() == "('1')"
github SAP / python-pyodata / tests / test_service_v2.py View on Github external
def test_get_entity_with_entity_key(service):
    """Make sure the method get_entity handles correctly the parameter key which is EntityKey"""

    # pylint: disable=redefined-outer-name


    key = EntityKey(
        service.schema.entity_type('TemperatureMeasurement'),
        Sensor='sensor1',
        Date=datetime.datetime(2017, 12, 24, 18, 0))

    query = service.entity_sets.TemperatureMeasurements.get_entity(key)
    assert query.get_path() == "TemperatureMeasurements(Sensor='sensor1',Date=datetime'2017-12-24T18:00:00')"
github SAP / python-pyodata / tests / test_service_v2.py View on Github external
def test_entity_key_complex_single_value(service):
    """Test rejection of single value for complex key"""

    with pytest.raises(PyODataException) as e_info:
        EntityKey(
            service.schema.entity_type('TemperatureMeasurement'),
            1)

    assert str(e_info.value).startswith('Key of entity type')
github SAP / python-pyodata / tests / test_service_v2.py View on Github external
def test_delete_entity_with_key(service):
    """Check deleting of entity with key"""

    responses.add(responses.DELETE, f"{service.url}/Employees(ID=23)", status=204)
    key = EntityKey(service.schema.entity_type('Employee'), ID=23)
    request = service.entity_sets.Employees.delete_entity(key=key)

    assert isinstance(request, pyodata.v2.service.EntityDeleteRequest)
    assert request.execute() is None
github SAP / python-pyodata / pyodata / v2 / service.py View on Github external
def get_entity_handler(response):
            """Gets entity from HTTP response"""

            if response.status_code != requests.codes.ok:
                raise HttpError('HTTP GET for Entity {0} failed with status code {1}'
                                .format(self._name, response.status_code), response)

            entity = response.json()['d']

            return EntityProxy(self._service, self._entity_set, self._entity_set.entity_type, entity)

        if key is not None and isinstance(key, EntityKey):
            entity_key = key
        else:
            entity_key = EntityKey(self._entity_set.entity_type, key, **args)

        self._logger.info('Getting entity %s for key %s and args %s', self._entity_set.entity_type.name, key, args)

        return EntityGetRequest(get_entity_handler, entity_key, self)
github SAP / python-pyodata / pyodata / v2 / service.py View on Github external
for entity in proprties[prop.name]['results']:
                                self._cache[prop.name].append(EntityProxy(service, None, prop_etype, entity))
                    else:
                        raise PyODataException('Unknown multiplicity {0} of association role {1}'
                                               .format(prop.to_role.multiplicity, prop.to_role.name))

        # build entity key if not provided
        if self._entity_key is None:
            # try to build key from available property values
            try:
                # if key seems to be simple (consists of single property)
                if len(self._key_props) == 1:
                    self._entity_key = EntityKey(entity_type, self._cache[self._key_props[0].name])
                else:
                    # build complex key
                    self._entity_key = EntityKey(entity_type, **self._cache)
            except KeyError:
                pass
            except PyODataException:
                pass
github SAP / python-pyodata / pyodata / v2 / service.py View on Github external
self._entity_type.name, ', '.join([prop.name for prop in self._key])))

            # get single key property and format key string
            key_prop = self._key[0]
            args[key_prop.name] = single_key

            self._type = EntityKey.TYPE_SINGLE

            self._logger.debug(('Detected single property key, adding pair %s->%s to key'
                                'properties'), key_prop.name, single_key)
        else:
            for key_prop in self._key:
                if key_prop.name not in args:
                    raise PyODataException('Missing value for key property {}'.format(key_prop.name))

            self._type = EntityKey.TYPE_COMPLEX
github SAP / python-pyodata / pyodata / v2 / service.py View on Github external
def to_key_string_without_parentheses(self):
        """Gets the string representation of the key without parentheses"""

        if self._type == EntityKey.TYPE_SINGLE:
            # first property is the key property
            key_prop = self._key[0]
            return key_prop.typ.traits.to_literal(self._proprties[key_prop.name])

        key_pairs = []
        for key_prop in self._key:
            # if key_prop.name not in self.__dict__['_cache']:
            #    raise RuntimeError('Entity key is not complete, missing value of property: {0}'.format(key_prop.name))

            key_pairs.append(
                '{0}={1}'.format(key_prop.name, key_prop.typ.traits.to_literal(self._proprties[key_prop.name])))

        return ','.join(key_pairs)