Skip to content

Commit a735037

Browse files
bartonipfilak-sap
authored andcommitted
Allow method to be specified in EntityModifyRequest
Signed-off-by: Jakub Filak <[email protected]>
1 parent 90016c2 commit a735037

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

pyodata/v2/service.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,13 +512,18 @@ class EntityModifyRequest(ODataHttpRequest):
512512
Call execute() to send the update-request to the OData service
513513
and get the modified entity."""
514514

515-
def __init__(self, url, connection, handler, entity_set, entity_key):
515+
def __init__(self, url, connection, handler, entity_set, entity_key, method="PATCH"):
516516
super(EntityModifyRequest, self).__init__(url, connection, handler)
517517
self._logger = logging.getLogger(LOGGER_NAME)
518518
self._entity_set = entity_set
519519
self._entity_type = entity_set.entity_type
520520
self._entity_key = entity_key
521521

522+
if method.upper() not in ["PATCH", "PUT"]:
523+
raise ValueError("Method must be either PATCH or PUT")
524+
525+
self._method = method
526+
522527
self._values = {}
523528

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

532537
def get_method(self):
533538
# pylint: disable=no-self-use
534-
return 'PATCH'
539+
return self._method
535540

536541
def get_body(self):
537542
# pylint: disable=no-self-use
@@ -1152,7 +1157,7 @@ def create_entity_handler(response):
11521157
return EntityCreateRequest(self._service.url, self._service.connection, create_entity_handler, self._entity_set,
11531158
self.last_segment)
11541159

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

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

11721177
return EntityModifyRequest(self._service.url, self._service.connection, update_entity_handler, self._entity_set,
1173-
entity_key)
1178+
entity_key, method=method)
11741179

11751180
def delete_entity(self, key: EntityKey = None, **kwargs):
11761181
"""Delete the entity"""

tests/test_service_v2.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,66 @@ def test_update_entity_with_entity_key(service):
658658
assert query.get_path() == "TemperatureMeasurements(Sensor='sensor1',Date=datetime'2017-12-24T18:00:00')"
659659

660660

661+
def test_update_entity_with_put_method_specified(service):
662+
"""Make sure the method update_entity handles correctly when PUT method is specified"""
663+
664+
# pylint: disable=redefined-outer-name
665+
666+
667+
key = EntityKey(
668+
service.schema.entity_type('TemperatureMeasurement'),
669+
Sensor='sensor1',
670+
Date=datetime.datetime(2017, 12, 24, 18, 0))
671+
672+
query = service.entity_sets.TemperatureMeasurements.update_entity(key, method="PUT")
673+
assert query.get_method() == "PUT"
674+
675+
676+
def test_update_entity_with_patch_method_specified(service):
677+
"""Make sure the method update_entity handles correctly when PATCH method is specified"""
678+
679+
# pylint: disable=redefined-outer-name
680+
681+
682+
key = EntityKey(
683+
service.schema.entity_type('TemperatureMeasurement'),
684+
Sensor='sensor1',
685+
Date=datetime.datetime(2017, 12, 24, 18, 0))
686+
687+
query = service.entity_sets.TemperatureMeasurements.update_entity(key, method="PATCH")
688+
assert query.get_method() == "PATCH"
689+
690+
691+
def test_update_entity_with_no_method_specified(service):
692+
"""Make sure the method update_entity handles correctly when no method is specified"""
693+
694+
# pylint: disable=redefined-outer-name
695+
696+
697+
key = EntityKey(
698+
service.schema.entity_type('TemperatureMeasurement'),
699+
Sensor='sensor1',
700+
Date=datetime.datetime(2017, 12, 24, 18, 0))
701+
702+
query = service.entity_sets.TemperatureMeasurements.update_entity(key)
703+
assert query.get_method() == "PATCH"
704+
705+
706+
def test_update_entity_with_wrong_method_specified(service):
707+
"""Make sure the method update_entity raises ValueError when wrong method is specified"""
708+
709+
# pylint: disable=redefined-outer-name
710+
711+
712+
key = EntityKey(
713+
service.schema.entity_type('TemperatureMeasurement'),
714+
Sensor='sensor1',
715+
Date=datetime.datetime(2017, 12, 24, 18, 0))
716+
717+
with pytest.raises(ValueError):
718+
service.entity_sets.TemperatureMeasurements.update_entity(key, method="DELETE")
719+
720+
661721
def test_get_entity_with_entity_key_and_other_params(service):
662722
"""Make sure the method update_entity handles correctly the parameter key which is EntityKey"""
663723

0 commit comments

Comments
 (0)