Skip to content

Commit

Permalink
Add solution for dates comparision in PATCH operations and related test
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Aug 24, 2017
1 parent 8ee4b32 commit d578263
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/plone/restapi/deserializer/dxcontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ def __call__(self, validate_all=False, data=None): # noqa: ignore=C901
'message': e.doc(), 'field': name, 'error': e})
else:
field_data[name] = value
if value != dm.get():
try:
if value != dm.get():
dm.set(value)
modified = True
except TypeError:
# Most probably due to offset-naive and offset
# aware objects, set the value as they most likely
# are not the same
dm.set(value)
modified = True

Expand Down
53 changes: 46 additions & 7 deletions src/plone/restapi/tests/test_content_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
from plone.restapi.testing import PLONE_RESTAPI_DX_FUNCTIONAL_TESTING
from plone.restapi.testing import RelativeSession
from DateTime import DateTime
from datetime import datetime
from datetime import timedelta

import datetime
import requests
import transaction
import unittest
import pytz


class TestContentPatch(unittest.TestCase):
Expand Down Expand Up @@ -89,10 +91,10 @@ def test_patch_document_returns_401_unauthorized(self):
self.assertEqual(401, response.status_code)

def test_patch_feed_event_with_get_contents(self):
start_date = DateTime(datetime.datetime.today() +
datetime.timedelta(days=1)).ISO8601()
end_date = DateTime(datetime.datetime.today() +
datetime.timedelta(days=1, hours=1)).ISO8601()
start_date = DateTime(datetime.today() +
timedelta(days=1)).ISO8601()
end_date = DateTime(datetime.today() +
timedelta(days=1, hours=1)).ISO8601()
response = self.api_session.post(
'/',
json={
Expand All @@ -108,8 +110,8 @@ def test_patch_feed_event_with_get_contents(self):

response = response.json()
event_id = response['id']
two_days_ahead = DateTime(datetime.datetime.today() +
datetime.timedelta(days=2))
two_days_ahead = DateTime(datetime.today() +
timedelta(days=2))
response = self.api_session.patch(
'/{}'.format(event_id),
json={
Expand All @@ -131,3 +133,40 @@ def test_patch_feed_event_with_get_contents(self):
DateTime(response['end']).hour(),
two_days_ahead.hour()
)

def test_patch_document_with_expires(self):
response = requests.patch(
self.portal.doc1.absolute_url(),
headers={'Accept': 'application/json'},
auth=(SITE_OWNER_NAME, SITE_OWNER_PASSWORD),
json={
"expires": datetime(
2013, 1, 1, 10, 0).isoformat()
}
)

self.assertEqual(204, response.status_code)

response = requests.patch(
self.portal.doc1.absolute_url(),
headers={'Accept': 'application/json'},
auth=(SITE_OWNER_NAME, SITE_OWNER_PASSWORD),
json={
"expires": datetime(
2013, 1, 1, 10, 0).isoformat()
}
)

self.assertEqual(204, response.status_code)

response = requests.patch(
self.portal.doc1.absolute_url(),
headers={'Accept': 'application/json'},
auth=(SITE_OWNER_NAME, SITE_OWNER_PASSWORD),
json={
"expires": pytz.timezone('Europe/Berlin').localize(datetime(
2013, 1, 1, 10, 0)).isoformat()
}
)

self.assertEqual(204, response.status_code)

0 comments on commit d578263

Please sign in to comment.