[CalendarServer-changes] [13564] CalendarServer/trunk/txdav/dps
source_changes at macosforge.org
source_changes at macosforge.org
Thu May 29 15:36:41 PDT 2014
Revision: 13564
http://trac.calendarserver.org//changeset/13564
Author: wsanchez at apple.com
Date: 2014-05-29 15:36:41 -0700 (Thu, 29 May 2014)
Log Message:
-----------
Test match expression deserialization
Modified Paths:
--------------
CalendarServer/trunk/txdav/dps/json.py
CalendarServer/trunk/txdav/dps/test/test_json.py
Modified: CalendarServer/trunk/txdav/dps/json.py
===================================================================
--- CalendarServer/trunk/txdav/dps/json.py 2014-05-29 22:36:05 UTC (rev 13563)
+++ CalendarServer/trunk/txdav/dps/json.py 2014-05-29 22:36:41 UTC (rev 13564)
@@ -75,25 +75,31 @@
-def matchExpressionFromJSON(json):
+def matchExpressionFromJSON(service, json):
try:
- field_json = json["field"]
- value_json = json["value"]
- match_json = json["match"]
- flags_json = json["flags"]
+ jsonField = json["field"]
+ jsonValue = json["value"]
except KeyError as e:
raise ValueError(
"JSON match expression must have {!r} key.".format(e[0])
)
- raise NotImplementedError()
+ jsonMatch = json.get("match", "equals")
+ jsonFlags = json.get("flags", "{}")
- fieldName = NotImplemented, field_json # Need service...
- fieldValue = NotImplemented, value_json # Need to cast to correct value
- matchType = MatchType.lookupByName(match_json)
- flags = NotImplemented, flags_json # Need to handle composite flags
+ fieldName = service.fieldName.lookupByName(jsonField)
+ fieldValue = unicode(jsonValue)
+ matchType = MatchType.lookupByName(jsonMatch)
- MatchFlags # Shh, flakes
+ if jsonFlags == "{}":
+ flags = MatchFlags.none
+ elif jsonFlags.startswith("{") and jsonFlags.endswith("}"):
+ # Composite flags: "{A,B,C,...}"
+ flags = MatchFlags.none
+ for flag in jsonFlags[1:-1].split(","):
+ flags |= MatchFlags.lookupByName(flag)
+ else:
+ flags = MatchFlags.lookupByName(jsonFlags)
return MatchExpression(
fieldName, fieldValue,
Modified: CalendarServer/trunk/txdav/dps/test/test_json.py
===================================================================
--- CalendarServer/trunk/txdav/dps/test/test_json.py 2014-05-29 22:36:05 UTC (rev 13563)
+++ CalendarServer/trunk/txdav/dps/test/test_json.py 2014-05-29 22:36:41 UTC (rev 13564)
@@ -25,10 +25,12 @@
from ..json import (
matchExpressionAsJSON, compoundExpressionAsJSON,
# expressionAsJSON, expressionAsJSONText,
- # matchExpressionFromJSON, compoundExpressionFromJSON,
+ matchExpressionFromJSON, # compoundExpressionFromJSON,
# expressionFromJSON, expressionFromJSONText,
+ from_json_text, # to_json_text,
)
+from twext.who.test.test_xml import xmlService
from twisted.trial import unittest
@@ -38,6 +40,10 @@
Tests for serialization to JSON.
"""
+ def service(self, subClass=None, xmlData=None):
+ return xmlService(self.mktemp())
+
+
def test_matchExpressionAsJSON_basic(self):
"""
L{matchExpressionAsJSON} with default matching and flags.
@@ -64,9 +70,9 @@
uid = u"Some UID"
for matchType, matchText in (
- (MatchType.equals, b"equals"),
- (MatchType.endsWith, b"endsWith"),
- (MatchType.lessThanOrEqualTo, b"lessThanOrEqualTo"),
+ (MatchType.equals, "equals"),
+ (MatchType.endsWith, "endsWith"),
+ (MatchType.lessThanOrEqualTo, "lessThanOrEqualTo"),
):
expression = MatchExpression(
FieldName.uid, uid, matchType=matchType
@@ -74,8 +80,8 @@
json = matchExpressionAsJSON(expression)
expected = {
- "type": b"MatchExpression",
- "field": b"uid",
+ "type": "MatchExpression",
+ "field": "uid",
"match": matchText,
"value": uid,
"flags": "{}",
@@ -93,28 +99,28 @@
for flags, flagsText, in (
(
MatchFlags.none,
- b"{}"
+ "{}"
),
(
MatchFlags.NOT,
- b"NOT"
+ "NOT"
),
(
MatchFlags.caseInsensitive,
- b"caseInsensitive"
+ "caseInsensitive"
),
(
MatchFlags.NOT | MatchFlags.caseInsensitive,
- b"{NOT,caseInsensitive}"
+ "{NOT,caseInsensitive}"
),
):
expression = MatchExpression(FieldName.uid, uid, flags=flags)
json = matchExpressionAsJSON(expression)
expected = {
- "type": b"MatchExpression",
- "field": b"uid",
- "match": b"equals",
+ "type": "MatchExpression",
+ "field": "uid",
+ "match": "equals",
"value": uid,
"flags": flagsText,
}
@@ -153,8 +159,8 @@
L{compoundExpressionAsJSON} with different operands.
"""
for operand, operandText in (
- (Operand.AND, b"AND"),
- (Operand.OR, b"OR"),
+ (Operand.AND, "AND"),
+ (Operand.OR, "OR"),
):
expression = CompoundExpression((), operand)
json = compoundExpressionAsJSON(expression)
@@ -166,3 +172,101 @@
}
self.assertEquals(json, expected)
+
+
+ def test_matchExpressionFromJSON_basic(self):
+ """
+ L{test_matchExpressionFromJSON_basic} with default matching and flags.
+ """
+ service = self.service()
+ uid = u"Some UID"
+ jsonText = (
+ """
+ {{
+ "type": "MatchExpression",
+ "field": "uid",
+ "value": "{uid}"
+ }}
+ """
+ ).format(uid=uid)
+ json = from_json_text(jsonText)
+
+ expected = MatchExpression(FieldName.uid, uid)
+ expression = matchExpressionFromJSON(service, json)
+
+ self.assertEquals(expression, expected)
+
+
+ def test_matchExpressionFromJSON_types(self):
+ """
+ L{matchExpressionFromJSON} with various match types.
+ """
+ service = self.service()
+ uid = u"Some UID"
+
+ for matchType, matchText in (
+ (MatchType.equals, b"equals"),
+ (MatchType.endsWith, b"endsWith"),
+ (MatchType.lessThanOrEqualTo, b"lessThanOrEqualTo"),
+ ):
+ jsonText = (
+ """
+ {{
+ "type": "MatchExpression",
+ "field": "uid",
+ "match": "{matchType}",
+ "value": "{uid}",
+ "flags": "{{}}"
+ }}
+ """
+ ).format(uid=uid, matchType=matchText)
+ json = from_json_text(jsonText)
+
+ expected = MatchExpression(FieldName.uid, uid, matchType=matchType)
+ expression = matchExpressionFromJSON(service, json)
+
+ self.assertEquals(expression, expected)
+
+
+ def test_matchExpressionFromJSON_flags(self):
+ """
+ L{matchExpressionFromJSON} with various flags.
+ """
+ service = self.service()
+ uid = u"Some UID"
+
+ for flags, flagsText, in (
+ (
+ MatchFlags.none,
+ "{}"
+ ),
+ (
+ MatchFlags.NOT,
+ "NOT"
+ ),
+ (
+ MatchFlags.caseInsensitive,
+ "caseInsensitive"
+ ),
+ (
+ MatchFlags.NOT | MatchFlags.caseInsensitive,
+ "{NOT,caseInsensitive}"
+ ),
+ ):
+ jsonText = (
+ """
+ {{
+ "type": "MatchExpression",
+ "field": "uid",
+ "match": "equals",
+ "value": "{uid}",
+ "flags": "{flagsText}"
+ }}
+ """
+ ).format(uid=uid, flagsText=flagsText)
+ json = from_json_text(jsonText)
+
+ expected = MatchExpression(FieldName.uid, uid, flags=flags)
+ expression = matchExpressionFromJSON(service, json)
+
+ self.assertEquals(expression, expected)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140529/3aee8f7d/attachment.html>
More information about the calendarserver-changes
mailing list