[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/securitypolicy/ Changed API of security maps to have getCell and queryCell.

Stephan Richter srichter at cosmos.phy.tufts.edu
Fri Jul 9 00:08:38 EDT 2004


Log message for revision 26275:
Changed API of security maps to have getCell and queryCell.

Wrote tests for security map.

Updated code that used the security map interface.



-=-
Modified: Zope3/trunk/src/zope/app/securitypolicy/browser/tests/rolepermissionmanager.py
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/browser/tests/rolepermissionmanager.py	2004-07-09 04:06:47 UTC (rev 26274)
+++ Zope3/trunk/src/zope/app/securitypolicy/browser/tests/rolepermissionmanager.py	2004-07-09 04:08:38 UTC (rev 26275)
@@ -76,7 +76,7 @@
         '''See interface IRolePermissionMap'''
         rp = self._getRolePermissions()
         if rp:
-            return rp.getCell(permission_id, role_id)
+            return rp.queryCell(permission_id, role_id)
         else:
             return Unset
 

Modified: Zope3/trunk/src/zope/app/securitypolicy/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/interfaces.py	2004-07-09 04:06:47 UTC (rev 26274)
+++ Zope3/trunk/src/zope/app/securitypolicy/interfaces.py	2004-07-09 04:08:38 UTC (rev 26275)
@@ -22,23 +22,31 @@
     """Security map to hold matrix-like relationships."""
 
     def addCell(rowentry, colentry, value):
-        " add a cell "
+        """Add a cell"""
 
     def delCell(rowentry, colentry):
-        " delete a cell "
+        """Delete a cell"""
 
-    # XXX queryCell / getCell ?
-    def getCell(rowentry, colentry, default=None):
-        " return the value of a cell by row, entry "
+    def queryCell(rowentry, colentry, default=None):
+        """Return the value of a cell by row, entry
 
+        Return `default`, if the cell is not found.
+        """
+
+    def getCell(rowentry, colentry):
+        """Return the value of a cell by row, entry
+
+        Raise an error, if the cell is not found.
+        """
+        
     def getRow(rowentry):
-        " return a list of (colentry, value) tuples from a row "
+        """Return a list of (colentry, value) tuples from a row"""
 
     def getCol(colentry):
-        " return a list of (rowentry, value) tuples from a col "
+        """Return a list of (rowentry, value) tuples from a col"""
 
     def getAllCells():
-        " return a list of (rowentry, colentry, value) "
+        """Return a list of (rowentry, colentry, value)"""
 
 class IRole(Interface):
     """A role object."""

Modified: Zope3/trunk/src/zope/app/securitypolicy/principalpermission.py
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/principalpermission.py	2004-07-09 04:06:47 UTC (rev 26274)
+++ Zope3/trunk/src/zope/app/securitypolicy/principalpermission.py	2004-07-09 04:08:38 UTC (rev 26275)
@@ -76,7 +76,7 @@
         ''' See the interface IPrincipalPermissionManager '''
         pp = self._getPrincipalPermissions()
         if pp:
-            return pp.getCell(permission_id, principal_id, default=Unset)
+            return pp.queryCell(permission_id, principal_id, default=Unset)
         return []
 
     def getPrincipalsAndPermissions(self):

Modified: Zope3/trunk/src/zope/app/securitypolicy/principalrole.py
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/principalrole.py	2004-07-09 04:06:47 UTC (rev 26274)
+++ Zope3/trunk/src/zope/app/securitypolicy/principalrole.py	2004-07-09 04:08:38 UTC (rev 26275)
@@ -74,7 +74,7 @@
         ''' See the interface IPrincipalRoleManager '''
         pp = self._getPrincipalRoles()
         if pp:
-            return pp.getCell(role_id, principal_id, default=Unset)
+            return pp.queryCell(role_id, principal_id, default=Unset)
         return Unset
 
     def getPrincipalsAndRoles(self):
@@ -142,7 +142,7 @@
 
     def getSetting(self, role_id, principal_id):
         ''' See the interface IPrincipalRoleMap '''
-        return self.getCell(role_id, principal_id, default=Unset)
+        return self.queryCell(role_id, principal_id, default=Unset)
 
     def getPrincipalsAndRoles(self):
         ''' See the interface IPrincipalRoleMap '''

Modified: Zope3/trunk/src/zope/app/securitypolicy/rolepermission.py
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/rolepermission.py	2004-07-09 04:06:47 UTC (rev 26274)
+++ Zope3/trunk/src/zope/app/securitypolicy/rolepermission.py	2004-07-09 04:08:38 UTC (rev 26275)
@@ -96,7 +96,7 @@
         '''See interface IRolePermissionMap'''
         rp = self._getRolePermissions()
         if rp:
-            return rp.getCell(permission_id, role_id)
+            return rp.queryCell(permission_id, role_id)
         else:
             return Unset
 
@@ -198,7 +198,7 @@
 
     def getSetting(self, permission_id, role_id):
         '''See interface IRolePermissionMap'''
-        return self.getCell(permission_id, role_id)
+        return self.queryCell(permission_id, role_id)
 
     def getRolesAndPermissions(self):
         '''See interface IRolePermissionMap'''

Modified: Zope3/trunk/src/zope/app/securitypolicy/securitymap.py
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/securitymap.py	2004-07-09 04:06:47 UTC (rev 26274)
+++ Zope3/trunk/src/zope/app/securitypolicy/securitymap.py	2004-07-09 04:08:38 UTC (rev 26275)
@@ -11,8 +11,10 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-""" Generic two-dimensional array type """
+"""Generic two-dimensional array type (in context of security)
 
+$Id$
+"""
 from persistent import Persistent
 from persistent.dict import PersistentDict
 from zope.interface import implements
@@ -33,6 +35,7 @@
         return {}
 
     def addCell(self, rowentry, colentry, value):
+        """See ISecurityMap"""
         # setdefault may get expensive if an empty mapping is
         # expensive to create, for PersistentDict for instance.
         row = self._byrow.setdefault(rowentry, self._empty_mapping())
@@ -46,6 +49,7 @@
             pass
 
     def delCell(self, rowentry, colentry):
+        """See ISecurityMap"""
         row = self._byrow.get(rowentry)
         if row and (colentry in row):
             del self._byrow[rowentry][colentry]
@@ -55,28 +59,36 @@
         except AttributeError:
             pass
 
-    def getCell(self, rowentry, colentry, default=None):
-        " return the value of a cell by row, entry "
+    def queryCell(self, rowentry, colentry, default=None):
+        """See ISecurityMap"""
         row = self._byrow.get(rowentry)
         if row: return row.get(colentry, default)
         else: return default
 
+    def getCell(self, rowentry, colentry):
+        """See ISecurityMap"""
+        marker = object()
+        cell = self.queryCell(rowentry, colentry, marker)
+        if cell is marker:
+            raise KeyError('Not a valid row and column pair.')
+        return cell
+
     def getRow(self, rowentry):
-        " return a list of (colentry, value) tuples from a row "
+        """See ISecurityMap"""
         row = self._byrow.get(rowentry)
         if row:
             return row.items()
         else: return []
 
     def getCol(self, colentry):
-        " return a list of (rowentry, value) tuples from a col "
+        """See ISecurityMap"""
         col = self._bycol.get(colentry)
         if col:
             return col.items()
         else: return []
 
     def getAllCells(self):
-        " return a list of (rowentry, colentry, value) "
+        """See ISecurityMap"""
         try:
             return self._v_cells
         except AttributeError:

Modified: Zope3/trunk/src/zope/app/securitypolicy/tests/test_securitymap.py
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/tests/test_securitymap.py	2004-07-09 04:06:47 UTC (rev 26274)
+++ Zope3/trunk/src/zope/app/securitypolicy/tests/test_securitymap.py	2004-07-09 04:08:38 UTC (rev 26275)
@@ -11,7 +11,8 @@
 # FOR A PARTICULAR PURPOSE.
 #
 #############################################################################
-"""
+"""Test ISecurityMap implementations
+
 $Id$
 """
 import unittest
@@ -25,14 +26,125 @@
     def testInterface(self):
         verifyClass(ISecurityMap, SecurityMap)
 
-    # XXX Test the map. Grrrrr.
+    def _getSecurityMap(self):
+        return SecurityMap()
 
+    def test_addCell(self):
+        map = self._getSecurityMap()
+        map.addCell(0, 0, 'aa')
+        self.assertEqual(map._byrow[0][0], 'aa')
+        self.assertEqual(map._bycol[0][0], 'aa')
+
+        map.addCell(1, 0, 'ba')
+        self.assertEqual(map._byrow[1][0], 'ba')
+        self.assertEqual(map._bycol[0][1], 'ba')
+
+        map.addCell(5, 3, 'fd')
+        self.assertEqual(map._byrow[5][3], 'fd')
+        self.assertEqual(map._bycol[3][5], 'fd')
+
+    def test_addCell_noninteger(self):
+        map = self._getSecurityMap()
+        map.addCell(0.3, 0.4, 'entry')
+        self.assertEqual(map._byrow[0.3][0.4], 'entry')
+        self.assertEqual(map._bycol[0.4][0.3], 'entry')
+
+        marker = object()
+        map.addCell('a', 'b', marker)
+        self.assertEqual(map._byrow['a']['b'], marker)
+        self.assertEqual(map._bycol['b']['a'], marker)
+        
+    def test_delCell(self):
+        map = self._getSecurityMap()
+        map._byrow[0] = map._empty_mapping()
+        map._bycol[1] = map._empty_mapping()
+        map._byrow[0][1] = 'aa'
+        map._bycol[1][0] = 'aa'
+        map.delCell(0, 1)
+        self.assertEqual(len(map._byrow.get(0)), 0) 
+        self.assertEqual(len(map._bycol.get(1)), 0) 
+
+    def test_queryCell(self):
+        map = self._getSecurityMap()
+        map._byrow[0] = map._empty_mapping()
+        map._bycol[1] = map._empty_mapping()
+        map._byrow[0][1] = 'aa'
+        map._bycol[1][0] = 'aa'
+
+        marker = object()
+        self.assertEqual(map.queryCell(0, 1), 'aa')
+        self.assertEqual(map.queryCell(1, 0), None)
+        self.assertEqual(map.queryCell(1, 0, marker), marker)
+
+    def test_getCell(self):
+        map = self._getSecurityMap()
+        map._byrow[0] = map._empty_mapping()
+        map._bycol[1] = map._empty_mapping()
+        map._byrow[0][1] = 'aa'
+        map._bycol[1][0] = 'aa'
+
+        self.assertEqual(map.getCell(0, 1), 'aa')
+        self.assertRaises(KeyError, map.getCell, 1, 0)
+
+    def test_getRow(self):
+        map = self._getSecurityMap()
+        map._byrow[0] = map._empty_mapping()
+        map._byrow[0][1] = 'ab'
+        map._byrow[0][2] = 'ac'
+        map._byrow[1] = map._empty_mapping()
+        map._byrow[1][1] = 'bb'
+        map._bycol[1] = map._empty_mapping()
+        map._bycol[1][0] = 'ab'
+        map._bycol[1][1] = 'bb'
+        map._bycol[2] = map._empty_mapping()
+        map._bycol[2][0] = 'ac'
+
+        self.assertEqual(map.getRow(0), [(1, 'ab'), (2, 'ac')])
+        self.assertEqual(map.getRow(1), [(1, 'bb')])
+        self.assertEqual(map.getRow(2), [])
+
+    def test_getCol(self):
+        map = self._getSecurityMap()
+        map._byrow[0] = map._empty_mapping()
+        map._byrow[0][1] = 'ab'
+        map._byrow[0][2] = 'ac'
+        map._byrow[1] = map._empty_mapping()
+        map._byrow[1][1] = 'bb'
+        map._bycol[1] = map._empty_mapping()
+        map._bycol[1][0] = 'ab'
+        map._bycol[1][1] = 'bb'
+        map._bycol[2] = map._empty_mapping()
+        map._bycol[2][0] = 'ac'
+
+        self.assertEqual(map.getCol(1), [(0, 'ab'), (1, 'bb')])
+        self.assertEqual(map.getCol(2), [(0, 'ac')])
+        self.assertEqual(map.getCol(0), [])
+
+    def test_getAllCells(self):
+        map = self._getSecurityMap()
+        map._byrow[0] = map._empty_mapping()
+        map._byrow[0][1] = 'ab'
+        map._byrow[0][2] = 'ac'
+        map._byrow[1] = map._empty_mapping()
+        map._byrow[1][1] = 'bb'
+        map._bycol[1] = map._empty_mapping()
+        map._bycol[1][0] = 'ab'
+        map._bycol[1][1] = 'bb'
+        map._bycol[2] = map._empty_mapping()
+        map._bycol[2][0] = 'ac'
+
+        self.assertEqual(map.getCol(1), [(0, 'ab'), (1, 'bb')])
+        self.assertEqual(map.getCol(2), [(0, 'ac')])
+        self.assertEqual(map.getCol(0), [])
+
+
 class TestPersistentSecurityMap(TestSecurityMap):
 
     def testInterface(self):
         verifyClass(ISecurityMap, PersistentSecurityMap)
 
-    # XXX test persistence...
+    def _getSecurityMap(self):
+        return PersistentSecurityMap()
 
 
 def test_suite():



More information about the Zope3-Checkins mailing list