Skip to content

Commit 1b215c1

Browse files
committed
service: allow MERGE method for Update Entity
Closes #103
1 parent e4c5319 commit 1b215c1

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
## [Unreleased]
88

99
### Added
10-
- Specify PATCH or PUT method for EntityUpdateRequest - Barton Ip
10+
- Specify PATCH, PUT, or MERGE method for EntityUpdateRequest - Barton Ip
1111
- Add a Service wide configuration (e.g. http.update\_method) - Jakub Filak
1212
- <, <=, >, >= operators on GetEntitySetFilter - Barton Ip
1313

pyodata/v2/service.py

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

515+
ALLOWED_HTTP_METHODS = ['PATCH', 'PUT', 'MERGE']
516+
515517
def __init__(self, url, connection, handler, entity_set, entity_key, method="PATCH"):
516518
super(EntityModifyRequest, self).__init__(url, connection, handler)
517519
self._logger = logging.getLogger(LOGGER_NAME)
518520
self._entity_set = entity_set
519521
self._entity_type = entity_set.entity_type
520522
self._entity_key = entity_key
521523

522-
if method.upper() not in ["PATCH", "PUT"]:
523-
raise ValueError("Method must be either PATCH or PUT")
524-
525-
self._method = method
524+
self._method = method.upper()
525+
if self._method not in EntityModifyRequest.ALLOWED_HTTP_METHODS:
526+
raise ValueError('The value "{}" is not on the list of allowed Entity Update HTTP Methods: {}'
527+
.format(method, ', '.join(EntityModifyRequest.ALLOWED_HTTP_METHODS)))
526528

527529
self._values = {}
528530

tests/test_service_v2.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,20 @@ def test_update_entity_with_patch_method_specified(service):
687687
query = service.entity_sets.TemperatureMeasurements.update_entity(key, method="PATCH")
688688
assert query.get_method() == "PATCH"
689689

690+
def test_update_entity_with_merge_method_specified(service):
691+
"""Make sure the method update_entity handles correctly when MERGE method is specified"""
692+
693+
# pylint: disable=redefined-outer-name
694+
695+
696+
key = EntityKey(
697+
service.schema.entity_type('TemperatureMeasurement'),
698+
Sensor='sensor1',
699+
Date=datetime.datetime(2017, 12, 24, 18, 0))
700+
701+
query = service.entity_sets.TemperatureMeasurements.update_entity(key, method='merge')
702+
assert query.get_method() == 'MERGE'
703+
690704

691705
def test_update_entity_with_no_method_specified(service):
692706
"""Make sure the method update_entity handles correctly when no method is specified"""
@@ -730,8 +744,10 @@ def test_update_entity_with_wrong_method_specified(service):
730744
Sensor='sensor1',
731745
Date=datetime.datetime(2017, 12, 24, 18, 0))
732746

733-
with pytest.raises(ValueError):
734-
service.entity_sets.TemperatureMeasurements.update_entity(key, method="DELETE")
747+
with pytest.raises(ValueError) as caught_ex:
748+
service.entity_sets.TemperatureMeasurements.update_entity(key, method='DELETE')
749+
750+
assert str(caught_ex.value).startswith('The value "DELETE" is not on the list of allowed Entity Update HTTP Methods: PATCH, PUT, MERGE')
735751

736752

737753
def test_get_entity_with_entity_key_and_other_params(service):

0 commit comments

Comments
 (0)