#
# Copyright (c) 2014 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

"""
Tests for txdav.dps.json.
"""

from twext.who.idirectory import FieldName
from twext.who.expression import MatchExpression, MatchType, MatchFlags
from twext.who.expression import CompoundExpression, Operand

from ..json import (
    matchExpressionAsJSON, compoundExpressionAsJSON,
    # expressionAsJSON, expressionAsJSONText,
    # matchExpressionFromJSON, compoundExpressionFromJSON,
    # expressionFromJSON, expressionFromJSONText,
)

from twisted.trial import unittest



class SerializationTests(unittest.TestCase):
    """
    Tests for serialization to JSON.
    """

    def test_matchExpressionAsJSON_basic(self):
        """
        L{matchExpressionAsJSON} with default matching and flags.
        """
        uid = u"Some UID"
        expression = MatchExpression(FieldName.uid, uid)
        json = matchExpressionAsJSON(expression)

        expected = {
            "type": "MatchExpression",
            "field": "uid",
            "match": "equals",
            "value": uid,
            "flags": "{}",
        }

        self.assertEquals(json, expected)


    def test_matchExpressionAsJSON_types(self):
        """
        L{matchExpressionAsJSON} with various match types.
        """
        uid = u"Some UID"

        for matchType, matchText in (
            (MatchType.equals, b"equals"),
            (MatchType.endsWith, b"endsWith"),
            (MatchType.lessThanOrEqualTo, b"lessThanOrEqualTo"),
        ):
            expression = MatchExpression(
                FieldName.uid, uid, matchType=matchType
            )
            json = matchExpressionAsJSON(expression)

            expected = {
                "type": b"MatchExpression",
                "field": b"uid",
                "match": matchText,
                "value": uid,
                "flags": "{}",
            }

            self.assertEquals(json, expected)


    def test_matchExpressionAsJSON_flags(self):
        """
        L{matchExpressionAsJSON} with various flags.
        """
        uid = u"Some UID"

        for flags, flagsText, in (
            (
                MatchFlags.none,
                b"{}"
            ),
            (
                MatchFlags.NOT,
                b"NOT"
            ),
            (
                MatchFlags.caseInsensitive,
                b"caseInsensitive"
            ),
            (
                MatchFlags.NOT | MatchFlags.caseInsensitive,
                b"{NOT,caseInsensitive}"
            ),
        ):
            expression = MatchExpression(FieldName.uid, uid, flags=flags)
            json = matchExpressionAsJSON(expression)

            expected = {
                "type": b"MatchExpression",
                "field": b"uid",
                "match": b"equals",
                "value": uid,
                "flags": flagsText,
            }

            self.assertEquals(json, expected)


    def test_compoundExpressionAsJSON_expressions(self):
        """
        L{compoundExpressionAsJSON} with 0, 1 and 2 sub-expressions.
        """
        for uids in (
            (), (u"UID1",), (u"UID1", u"UID2"),
        ):
            subExpressions = [
                MatchExpression(FieldName.uid, uid) for uid in uids
            ]
            subExpressionsText = [
                matchExpressionAsJSON(e) for e in subExpressions
            ]

            expression = CompoundExpression(subExpressions, Operand.AND)
            json = compoundExpressionAsJSON(expression)

            expected = {
                'type': 'CompoundExpression',
                'expressions': subExpressionsText,
                'operand': 'AND',
            }

            self.assertEquals(json, expected)

@@ -4097,17 +4097,17 @@
     
-'
+%22
 type
-': '
+%22: %22
 Comp
@@ -4120,17 +4120,17 @@
 pression
-'
+%22
 ,%0A      
@@ -4139,17 +4139,17 @@
         
-'
+%22
 expressi
@@ -4151,17 +4151,17 @@
 ressions
-'
+%22
 : subExp
@@ -4194,24 +4194,603 @@
     
-'
+%22
 operand
-': 'AND'
+%22: %22AND%22,%0A            %7D%0A%0A            self.assertEquals(json, expected)%0A%0A%0A    def test_compoundExpressionAsJSON_operands(self):%0A        %22%22%22%0A        L%7BcompoundExpressionAsJSON%7D with different operands.%0A        %22%22%22%0A        for operand, operandText in (%0A            (Operand.AND, b%22AND%22),%0A            (Operand.OR, b%22OR%22),%0A        ):%0A            expression = CompoundExpression((), operand)%0A            json = compoundExpressionAsJSON(expression)%0A%0A            expected = %7B%0A                %22type%22: %22CompoundExpression%22,%0A                %22expressions%22: %5B%5D,%0A                %22operand%22: operandText
 ,%0A  

Add test_compoundExpressionAsJSON_operands