Skip to content

Commit

Permalink
Allow method to be specified in EntityModifyRequest
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Filak <jakub.filak@sap.com>
  • Loading branch information
bartonip authored and filak-sap committed Jun 26, 2020
1 parent 90016c2 commit a735037
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pyodata/v2/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,13 +512,18 @@ class EntityModifyRequest(ODataHttpRequest):
Call execute() to send the update-request to the OData service
and get the modified entity."""

def __init__(self, url, connection, handler, entity_set, entity_key):
def __init__(self, url, connection, handler, entity_set, entity_key, method="PATCH"):
super(EntityModifyRequest, self).__init__(url, connection, handler)
self._logger = logging.getLogger(LOGGER_NAME)
self._entity_set = entity_set
self._entity_type = entity_set.entity_type
self._entity_key = entity_key

if method.upper() not in ["PATCH", "PUT"]:
raise ValueError("Method must be either PATCH or PUT")

self._method = method

self._values = {}

# get all properties declared by entity type
Expand All @@ -531,7 +536,7 @@ def get_path(self):

def get_method(self):
# pylint: disable=no-self-use
return 'PATCH'
return self._method

def get_body(self):
# pylint: disable=no-self-use
Expand Down Expand Up @@ -1152,7 +1157,7 @@ def create_entity_handler(response):
return EntityCreateRequest(self._service.url, self._service.connection, create_entity_handler, self._entity_set,
self.last_segment)

def update_entity(self, key=None, **kwargs):
def update_entity(self, key=None, method="PATCH", **kwargs):
"""Updates an existing entity in the given entity-set."""

def update_entity_handler(response):
Expand All @@ -1170,7 +1175,7 @@ def update_entity_handler(response):
self._logger.info('Updating entity %s for key %s and args %s', self._entity_set.entity_type.name, key, kwargs)

return EntityModifyRequest(self._service.url, self._service.connection, update_entity_handler, self._entity_set,
entity_key)
entity_key, method=method)

def delete_entity(self, key: EntityKey = None, **kwargs):
"""Delete the entity"""
Expand Down
60 changes: 60 additions & 0 deletions tests/test_service_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,66 @@ def test_update_entity_with_entity_key(service):
assert query.get_path() == "TemperatureMeasurements(Sensor='sensor1',Date=datetime'2017-12-24T18:00:00')"


def test_update_entity_with_put_method_specified(service):
"""Make sure the method update_entity handles correctly when PUT method is specified"""

# 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.update_entity(key, method="PUT")
assert query.get_method() == "PUT"


def test_update_entity_with_patch_method_specified(service):
"""Make sure the method update_entity handles correctly when PATCH method is specified"""

# 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.update_entity(key, method="PATCH")
assert query.get_method() == "PATCH"


def test_update_entity_with_no_method_specified(service):
"""Make sure the method update_entity handles correctly when no method is specified"""

# 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.update_entity(key)
assert query.get_method() == "PATCH"


def test_update_entity_with_wrong_method_specified(service):
"""Make sure the method update_entity raises ValueError when wrong method is specified"""

# pylint: disable=redefined-outer-name


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

with pytest.raises(ValueError):
service.entity_sets.TemperatureMeasurements.update_entity(key, method="DELETE")


def test_get_entity_with_entity_key_and_other_params(service):
"""Make sure the method update_entity handles correctly the parameter key which is EntityKey"""

Expand Down

0 comments on commit a735037

Please sign in to comment.