[CalendarServer-changes] [12586] twext/trunk/twext/who/test/test_util.py

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:23:33 PDT 2014


Revision: 12586
          http://trac.calendarserver.org//changeset/12586
Author:   wsanchez at apple.com
Date:     2014-02-06 06:10:28 -0800 (Thu, 06 Feb 2014)
Log Message:
-----------
more container tests

Modified Paths:
--------------
    twext/trunk/twext/who/test/test_util.py

Modified: twext/trunk/twext/who/test/test_util.py
===================================================================
--- twext/trunk/twext/who/test/test_util.py	2014-02-06 13:37:02 UTC (rev 12585)
+++ twext/trunk/twext/who/test/test_util.py	2014-02-06 14:10:28 UTC (rev 12586)
@@ -22,7 +22,7 @@
 
 from twisted.trial import unittest
 from twisted.python.constants import (
-    Names, NamedConstant, Flags, FlagConstant,
+    Names, NamedConstant, Values, ValueConstant, Flags, FlagConstant
 )
 
 from ..idirectory import DirectoryServiceError
@@ -59,6 +59,27 @@
 
 
 
+class Statuses(Values):
+    OK = ValueConstant(200)
+    NOT_OK = ValueConstant(500)
+
+
+    @staticmethod
+    def isError(status):
+        return status.value < 500
+
+
+
+class MoreStatuses(Values):
+    MOOLAH = ValueConstant(402)
+
+
+    @staticmethod
+    def isError(status):
+        return status.value < 400
+
+
+
 class Switches(Flags):
     r = FlagConstant()
     g = FlagConstant()
@@ -98,6 +119,21 @@
         )
 
 
+    def test_constants_from_constantsContainers(self):
+        """
+        Initialize a container from other L{ConstantsContainer}s.
+        """
+        self.assertEquals(
+            set(
+                ConstantsContainer((
+                    ConstantsContainer((Tools,)),
+                    ConstantsContainer((MoreTools,)),
+                )).iterconstants()
+            ),
+            set(chain(Tools.iterconstants(), MoreTools.iterconstants())),
+        )
+
+
     def test_constants_from_iterables(self):
         """
         Initialize a container from iterables of constants.
@@ -114,7 +150,7 @@
 
     def test_conflictingClasses(self):
         """
-        A container can't contain two constants with the same name.
+        A container can't contain two constants with different types.
         """
         self.assertRaises(TypeError, ConstantsContainer, (Tools, Switches))
 
@@ -126,6 +162,13 @@
         self.assertRaises(ValueError, ConstantsContainer, (Tools, Instruments))
 
 
+    def test_notConstantClass(self):
+        """
+        A container can't contain random classes.
+        """
+        self.assertRaises(TypeError, ConstantsContainer, (self.__class__,))
+
+
     def test_attrs(self):
         """
         Constants are assessible via attributes.
@@ -159,12 +202,23 @@
         Static methods from source containers are accessible via attributes.
         """
         container = ConstantsContainer((Tools, MoreTools))
+
         self.assertTrue(container.isPounder(container.hammer))
         self.assertTrue(container.isPounder(container.mallet))
         self.assertFalse(container.isPounder(container.screwdriver))
         self.assertFalse(container.isPounder(container.saw))
 
 
+    def test_conflictingMethods(self):
+        """
+        A container can't contain two static methods with the same name.
+        """
+        self.assertRaises(
+            ValueError, ConstantsContainer, (Statuses, MoreStatuses)
+        )
+
+
+
     def test_lookupByName(self):
         """
         Constants are assessible via L{ConstantsContainer.lookupByName}.
@@ -195,7 +249,20 @@
         )
 
 
+    def test_lookupByValue(self):
+        """
+        Containers with L{ValueConstant}s are assessible via
+        L{ConstantsContainer.lookupByValue}.
+        """
+        container = ConstantsContainer((Statuses,))
 
+        self.assertEquals(container.lookupByValue(200), Statuses.OK)
+        self.assertEquals(container.lookupByValue(500), Statuses.NOT_OK)
+
+        self.assertRaises(ValueError, container.lookupByValue, 999)
+
+
+
 class UtilTest(unittest.TestCase):
     """
     Miscellaneous tests.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/d74da067/attachment.html>


More information about the calendarserver-changes mailing list