[CalendarServer-changes] [12589] twext/trunk/twext/who/test/test_directory.py
source_changes at macosforge.org
source_changes at macosforge.org
Wed Mar 12 11:21:31 PDT 2014
Revision: 12589
http://trac.calendarserver.org//changeset/12589
Author: wsanchez at apple.com
Date: 2014-02-06 10:32:15 -0800 (Thu, 06 Feb 2014)
Log Message:
-----------
Add tests for constant containers as valueType specialness.
Modified Paths:
--------------
twext/trunk/twext/who/test/test_directory.py
Modified: twext/trunk/twext/who/test/test_directory.py
===================================================================
--- twext/trunk/twext/who/test/test_directory.py 2014-02-06 17:52:49 UTC (rev 12588)
+++ twext/trunk/twext/who/test/test_directory.py 2014-02-06 18:32:15 UTC (rev 12589)
@@ -25,7 +25,9 @@
from zope.interface.verify import verifyObject, BrokenMethodImplementation
-from twisted.python.constants import Names, NamedConstant
+from twisted.python.constants import (
+ Names, NamedConstant, Values, ValueConstant, Flags, FlagConstant
+)
from twisted.trial import unittest
from twisted.internet.defer import inlineCallbacks
from twisted.internet.defer import succeed
@@ -730,6 +732,71 @@
)
+ def test_initWithContainerClassFieldType_valid(self):
+ """
+ If C{valueType} is L{Names}, L{Values} or L{Flags}, the expected type
+ is L{NamedConstant}, L{ValueConstant} or L{FlagConstant}, respectively.
+ Check that these can be used as fields.
+ """
+
+ class ConstantHavingDirectoryService(self.serviceClass):
+ fieldName = ConstantsContainer((
+ self.serviceClass.fieldName, ConstantHavingFieldName
+ ))
+
+ service = self.service(subClass=ConstantHavingDirectoryService)
+
+ baseFields = {
+ FieldName.uid: u"UID:sam",
+ FieldName.recordType: RecordType.user,
+ FieldName.shortNames: (u"sam",),
+ }
+
+ for fieldName, validValue in (
+ (service.fieldName.eyeColor, Color.blue),
+ (service.fieldName.language, Language.English),
+ (service.fieldName.access, Access.read),
+ ):
+ fields = baseFields.copy()
+ fields.update({fieldName: validValue})
+ record = self.makeRecord(fields=fields, service=service)
+ self.assertEquals(record.fields[fieldName], validValue)
+
+
+ def test_initWithContainerClassFieldType_invalid(self):
+ """
+ If C{valueType} is L{Names}, L{Values} or L{Flags}, the expected type
+ is L{NamedConstant}, L{ValueConstant} or L{FlagConstant}, respectively.
+ Check that other types raise.
+ """
+
+ class ConstantHavingDirectoryService(self.serviceClass):
+ fieldName = ConstantsContainer((
+ self.serviceClass.fieldName, ConstantHavingFieldName
+ ))
+
+ service = self.service(subClass=ConstantHavingDirectoryService)
+
+ baseFields = {
+ FieldName.uid: u"UID:sam",
+ FieldName.recordType: RecordType.user,
+ FieldName.shortNames: (u"sam",),
+ }
+
+ for fieldName, validValue in (
+ (service.fieldName.eyeColor, Color.blue),
+ (service.fieldName.language, Language.English),
+ (service.fieldName.access, Access.read),
+ ):
+ for invalidValue in (u"string", None, object()):
+ fields = baseFields.copy()
+ fields.update({fieldName: invalidValue})
+ self.assertRaises(
+ InvalidDirectoryRecordError,
+ self.makeRecord, fields=fields, service=service
+ )
+
+
def test_repr(self):
"""
L{DirectoryRecord.repr} returns the expected string.
@@ -1001,3 +1068,47 @@
Wacky operands.
"""
WHUH = NamedConstant()
+
+
+
+class Color(Names):
+ """
+ Some colors.
+ """
+ red = NamedConstant()
+ green = NamedConstant()
+ blue = NamedConstant()
+ black = NamedConstant()
+
+
+
+class Language(Values):
+ """
+ Some languages.
+ """
+ English = ValueConstant(u"en")
+ Spanish = ValueConstant(u"sp")
+
+
+
+class Access(Flags):
+ """
+ Some access types.
+ """
+ read = FlagConstant()
+ write = FlagConstant()
+
+
+
+class ConstantHavingFieldName(Names):
+ """
+ Field names with constants values.
+ """
+ eyeColor = NamedConstant()
+ eyeColor.valueType = Color
+
+ language = NamedConstant()
+ language.valueType = Language
+
+ access = NamedConstant()
+ access.valueType = Access
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/354475e9/attachment.html>
More information about the calendarserver-changes
mailing list