Skip to content
Permalink
Browse files

Add test_queryStringFromExpression_recordType test and fix recordType…

… NOT match

git-svn-id: https://svn.calendarserver.org/repository/calendarserver/twext/trunk@13259 e27351fd-9f3e-4f54-a53b-843176b1656c
  • Loading branch information
bgaya committed Apr 11, 2014
1 parent 8775c24 commit cb78af26017a9641feb34827e3248a593cea660b
Showing with 146 additions and 5 deletions.
  1. +9 −4 twext/who/opendirectory/_service.py
  2. +137 −1 twext/who/opendirectory/test/test_service.py
@@ -279,8 +279,10 @@ def _queryStringAndRecordTypesFromCompoundExpression(self, expression):
for subExpression in expression.expressions:
queryToken, subExpRecordTypes = self._queryStringAndRecordTypesFromExpression(subExpression)
if subExpRecordTypes:
if expression.operand is Operand.AND or queryToken == u"!": # AND or NOR
if expression.operand is Operand.AND:
recordTypes |= subExpRecordTypes
elif queryToken == u"!": #NOR
recordTypes |= set([t.value for t in ODRecordType.iterconstants()]) - subExpRecordTypes
else:
raise QueryNotSupportedError(
"Record type matches must AND or NOR"
@@ -398,9 +400,6 @@ def _queryFromMatchExpression(self, expression, recordType=None):

flags = tuple(expression.flags)

if MatchFlags.NOT in flags:
raise NotImplementedError()

if MatchFlags.caseInsensitive in flags:
caseInsensitive = 0x100
else:
@@ -425,11 +424,17 @@ def _queryFromMatchExpression(self, expression, recordType=None):
recordTypes = ODRecordType.fromRecordType(
expression.fieldValue
).value
if MatchFlags.NOT in flags:
recordTypes = set([t.value for t in ODRecordType.iterconstants()]) - recordTypes

matchType = ODMatchType.any.value
queryAttribute = None
queryValue = None

else:
if MatchFlags.NOT in flags:
raise NotImplementedError()

if recordType is None:
recordTypes = [t.value for t in ODRecordType.iterconstants()]
else:
@@ -27,7 +27,7 @@
)
from .._constants import ODAttribute
from .._service import DirectoryService

from ...idirectory import QueryNotSupportedError


class OpenDirectoryServiceTestCase(unittest.TestCase):
@@ -225,3 +225,139 @@ def test_queryStringFromExpression(self):
u"(dsAttrTypeStandard:RealName=c))"
)
)


def test_queryStringFromExpression_recordType(self):
"""
Record type in expression
"""
service = DirectoryService()

# AND expression
expression = CompoundExpression(
[
MatchExpression(
service.fieldName.shortNames,
u"xyzzy",
matchType=MatchType.equals
),
MatchExpression(
service.fieldName.recordType,
service.recordType.group,
matchType=MatchType.equals
),
MatchExpression(
service.fieldName.recordType,
service.recordType.user,
matchType=MatchType.equals
),
],
Operand.AND
)
queryString, recordTypes = service._queryStringAndRecordTypesFromExpression(expression)
self.assertEquals(set(recordTypes), set([u"dsRecTypeStandard:Groups", u"dsRecTypeStandard:Users"]))
self.assertEquals(
queryString,
u"(dsAttrTypeStandard:RecordName=xyzzy)"
)

# AND subexpression
expression = CompoundExpression(
[
MatchExpression(
service.fieldName.shortNames,
u"xyzzy",
matchType=MatchType.equals
),
CompoundExpression(
[

MatchExpression(
service.fieldName.recordType,
service.recordType.group,
matchType=MatchType.equals
),
MatchExpression(
service.fieldName.recordType,
service.recordType.user,
matchType=MatchType.equals
),
],
Operand.AND
),
],
Operand.AND
)
queryString, recordTypes = service._queryStringAndRecordTypesFromExpression(expression)
self.assertEquals(set(recordTypes), set([u"dsRecTypeStandard:Groups", u"dsRecTypeStandard:Users"]))
self.assertEquals(
queryString,
u"(dsAttrTypeStandard:RecordName=xyzzy)"
)

# NOR expression
expression = CompoundExpression(
[
MatchExpression(
service.fieldName.shortNames,
u"xxxxx",
matchType=MatchType.equals
),
MatchExpression(
service.fieldName.shortNames,
u"yyyyy",
matchType=MatchType.equals
),
MatchExpression(
service.fieldName.recordType,
service.recordType.user,
matchType=MatchType.equals,
flags=MatchFlags.NOT
),
],
Operand.OR
)
queryString, recordTypes = service._queryStringAndRecordTypesFromExpression(expression)
self.assertEquals(set(recordTypes), set([u"dsRecTypeStandard:Groups"]))
self.assertEquals(
queryString,
u"("
u"|(dsAttrTypeStandard:RecordName=xxxxx)"
u"(dsAttrTypeStandard:RecordName=yyyyy)"
u")"
)

# Simple AND expression -> empty query string
expression = CompoundExpression(
[
MatchExpression(
service.fieldName.recordType,
service.recordType.user,
matchType=MatchType.equals
),
],
Operand.AND
)
queryString, recordTypes = service._queryStringAndRecordTypesFromExpression(expression)
self.assertEquals(set(recordTypes), set([u"dsRecTypeStandard:Users"]))
self.assertEquals(
queryString,
u""
)

# recordType OR expression raises QueryNotSupportedError
expression = CompoundExpression(
[
MatchExpression(
service.fieldName.recordType,
service.recordType.user,
matchType=MatchType.equals
),
],
Operand.OR
)
self.assertRaises(
QueryNotSupportedError,
service._queryStringAndRecordTypesFromExpression,
expression,
)

0 comments on commit cb78af2

Please sign in to comment.
You can’t perform that action at this time.