[CalendarServer-changes] [13567] CalendarServer/trunk/txdav/dps/test/test_json.py
source_changes at macosforge.org
source_changes at macosforge.org
Thu May 29 15:56:05 PDT 2014
Revision: 13567
http://trac.calendarserver.org//changeset/13567
Author: wsanchez at apple.com
Date: 2014-05-29 15:56:05 -0700 (Thu, 29 May 2014)
Log Message:
-----------
More serialization tests
Modified Paths:
--------------
CalendarServer/trunk/txdav/dps/test/test_json.py
Modified: CalendarServer/trunk/txdav/dps/test/test_json.py
===================================================================
--- CalendarServer/trunk/txdav/dps/test/test_json.py 2014-05-29 22:55:49 UTC (rev 13566)
+++ CalendarServer/trunk/txdav/dps/test/test_json.py 2014-05-29 22:56:05 UTC (rev 13567)
@@ -24,7 +24,7 @@
from ..json import (
matchExpressionAsJSON, compoundExpressionAsJSON,
- # expressionAsJSON, expressionAsJSONText,
+ expressionAsJSON, expressionAsJSONText,
matchExpressionFromJSON, # compoundExpressionFromJSON,
# expressionFromJSON, expressionFromJSONText,
from_json_text, # to_json_text,
@@ -40,53 +40,59 @@
Tests for serialization to JSON.
"""
- def test_matchExpressionAsJSON_basic(self):
+ def test_matchExpressionAsJSON_basic(
+ self, serialize=matchExpressionAsJSON
+ ):
"""
L{matchExpressionAsJSON} with default matching and flags.
"""
uid = u"Some UID"
expression = MatchExpression(FieldName.uid, uid)
- json = matchExpressionAsJSON(expression)
+ json = serialize(expression)
expected = {
- "type": "MatchExpression",
- "field": "uid",
- "match": "equals",
- "value": uid,
- "flags": "{}",
+ u"type": u"MatchExpression",
+ u"field": u"uid",
+ u"match": u"equals",
+ u"value": uid,
+ u"flags": u"{}",
}
self.assertEquals(json, expected)
- def test_matchExpressionAsJSON_types(self):
+ def test_matchExpressionAsJSON_types(
+ self, serialize=matchExpressionAsJSON
+ ):
"""
L{matchExpressionAsJSON} with various match types.
"""
uid = u"Some UID"
for matchType, matchText in (
- (MatchType.equals, "equals"),
- (MatchType.endsWith, "endsWith"),
- (MatchType.lessThanOrEqualTo, "lessThanOrEqualTo"),
+ (MatchType.equals, u"equals"),
+ (MatchType.endsWith, u"endsWith"),
+ (MatchType.lessThanOrEqualTo, u"lessThanOrEqualTo"),
):
expression = MatchExpression(
FieldName.uid, uid, matchType=matchType
)
- json = matchExpressionAsJSON(expression)
+ json = serialize(expression)
expected = {
- "type": "MatchExpression",
- "field": "uid",
- "match": matchText,
- "value": uid,
- "flags": "{}",
+ u"type": u"MatchExpression",
+ u"field": u"uid",
+ u"match": matchText,
+ u"value": uid,
+ u"flags": u"{}",
}
self.assertEquals(json, expected)
- def test_matchExpressionAsJSON_flags(self):
+ def test_matchExpressionAsJSON_flags(
+ self, serialize=matchExpressionAsJSON
+ ):
"""
L{matchExpressionAsJSON} with various flags.
"""
@@ -95,36 +101,38 @@
for flags, flagsText, in (
(
MatchFlags.none,
- "{}"
+ u"{}"
),
(
MatchFlags.NOT,
- "NOT"
+ u"NOT"
),
(
MatchFlags.caseInsensitive,
- "caseInsensitive"
+ u"caseInsensitive"
),
(
MatchFlags.NOT | MatchFlags.caseInsensitive,
- "{NOT,caseInsensitive}"
+ u"{NOT,caseInsensitive}"
),
):
expression = MatchExpression(FieldName.uid, uid, flags=flags)
- json = matchExpressionAsJSON(expression)
+ json = serialize(expression)
expected = {
- "type": "MatchExpression",
- "field": "uid",
- "match": "equals",
- "value": uid,
- "flags": flagsText,
+ u"type": u"MatchExpression",
+ u"field": u"uid",
+ u"match": u"equals",
+ u"value": uid,
+ u"flags": flagsText,
}
self.assertEquals(json, expected)
- def test_compoundExpressionAsJSON_expressions(self):
+ def test_compoundExpressionAsJSON_expressions(
+ self, serialize=compoundExpressionAsJSON
+ ):
"""
L{compoundExpressionAsJSON} with 0, 1 and 2 sub-expressions.
"""
@@ -142,35 +150,84 @@
json = compoundExpressionAsJSON(expression)
expected = {
- "type": "CompoundExpression",
- "expressions": subExpressionsText,
- "operand": "AND",
+ u"type": u"CompoundExpression",
+ u"expressions": subExpressionsText,
+ u"operand": u"AND",
}
self.assertEquals(json, expected)
- def test_compoundExpressionAsJSON_operands(self):
+ def test_compoundExpressionAsJSON_operands(
+ self, serialize=compoundExpressionAsJSON
+ ):
"""
L{compoundExpressionAsJSON} with different operands.
"""
for operand, operandText in (
- (Operand.AND, "AND"),
- (Operand.OR, "OR"),
+ (Operand.AND, u"AND"),
+ (Operand.OR, u"OR"),
):
expression = CompoundExpression((), operand)
json = compoundExpressionAsJSON(expression)
expected = {
- "type": "CompoundExpression",
- "expressions": [],
- "operand": operandText,
+ u"type": u"CompoundExpression",
+ u"expressions": [],
+ u"operand": operandText,
}
self.assertEquals(json, expected)
+ def test_expressionAsJSON_matchExpression(self):
+ """
+ L{expressionAsJSON} with match expression.
+ """
+ self.test_matchExpressionAsJSON_basic(expressionAsJSON)
+ self.test_matchExpressionAsJSON_types(expressionAsJSON)
+ self.test_matchExpressionAsJSON_flags(expressionAsJSON)
+
+ def test_expressionAsJSON_compoundExpression(self):
+ """
+ L{expressionAsJSON} with compound expression.
+ """
+ self.test_compoundExpressionAsJSON_expressions(expressionAsJSON)
+ self.test_compoundExpressionAsJSON_operands(expressionAsJSON)
+
+
+ def test_expressionAsJSON_unknown(self):
+ """
+ L{expressionAsJSON} with compound expression.
+ """
+ self.assertRaises(TypeError, expressionAsJSON, object())
+
+
+ def test_expressionAsJSONText(self):
+ """
+ L{expressionAsJSON} with compound expression.
+ """
+ uid = u"Some UID"
+ expression = MatchExpression(FieldName.uid, uid)
+ jsonText = expressionAsJSONText(expression)
+
+ expected = (
+ u"""
+ {{
+ "field": "uid",
+ "flags": "{{}}",
+ "match": "equals",
+ "value": "{uid}",
+ "type": "MatchExpression"
+ }}
+ """
+ ).replace(" ", "").replace("\n", "").format(uid=uid)
+
+ self.assertEquals(jsonText, expected)
+
+
+
class DeserializationTests(unittest.TestCase):
"""
Tests for deserialization from JSON.
@@ -187,7 +244,7 @@
service = self.service()
uid = u"Some UID"
jsonText = (
- """
+ u"""
{{
"type": "MatchExpression",
"field": "uid",
@@ -211,12 +268,12 @@
uid = u"Some UID"
for matchType, matchText in (
- (MatchType.equals, b"equals"),
- (MatchType.endsWith, b"endsWith"),
- (MatchType.lessThanOrEqualTo, b"lessThanOrEqualTo"),
+ (MatchType.equals, u"equals"),
+ (MatchType.endsWith, u"endsWith"),
+ (MatchType.lessThanOrEqualTo, u"lessThanOrEqualTo"),
):
jsonText = (
- """
+ u"""
{{
"type": "MatchExpression",
"field": "uid",
@@ -244,23 +301,23 @@
for flags, flagsText, in (
(
MatchFlags.none,
- "{}"
+ u"{}"
),
(
MatchFlags.NOT,
- "NOT"
+ u"NOT"
),
(
MatchFlags.caseInsensitive,
- "caseInsensitive"
+ u"caseInsensitive"
),
(
MatchFlags.NOT | MatchFlags.caseInsensitive,
- "{NOT,caseInsensitive}"
+ u"{NOT,caseInsensitive}"
),
):
jsonText = (
- """
+ u"""
{{
"type": "MatchExpression",
"field": "uid",
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140529/a32b284e/attachment-0001.html>
More information about the calendarserver-changes
mailing list