Back to index

plone3  3.1.7
test_UserPropertySheet.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights
00004 # Reserved.
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE.
00013 #
00014 ##############################################################################
00015 import unittest
00016 import os.path
00017 from DateTime.DateTime import DateTime
00018 
00019 from OFS.Image import Image
00020 
00021 from conformance import IPropertySheet_conformance
00022 
00023 path = os.path.dirname(__file__)
00024 path = os.path.join(path, 'image.gif')
00025 img_file = open(path, 'r')
00026 
00027 class UserPropertySheetTests( unittest.TestCase
00028                             , IPropertySheet_conformance
00029                             ):
00030 
00031     _SCHEMA = ( ( 's', 'string'  )
00032               , ( 'i', 'int'     )
00033               , ( 'f', 'float'   )
00034               , ( 'n', 'long'    )
00035               , ( 'd', 'date'    )
00036               , ( 'l', 'lines'   )
00037               , ( 't', 'lines'   )
00038               , ( 'b', 'boolean' )
00039               , ( 'img', 'image' )
00040               )
00041 
00042     _STRING_VALUE = 'string'
00043     _INT_VALUE = 42
00044     _FLOAT_VALUE = 9.8
00045     _LONG_VALUE = 1000000000000
00046     _DATE_VALUE = DateTime()
00047     _LIST_VALUE = [ 'a', 'b', 'c' ]
00048     _TUPLE_VALUE = ( 'd', 'e', 'f' )
00049     _BOOL_VALUE = True
00050     _IMG_VALUE = Image('image', 'Test Image', img_file)
00051 
00052     def _getTargetClass( self ):
00053 
00054         from Products.PluggableAuthService.UserPropertySheet \
00055             import UserPropertySheet
00056 
00057         return UserPropertySheet
00058 
00059     def _makeOne( self, *args, **kw ):
00060 
00061         return self._getTargetClass()( *args, **kw )
00062 
00063     def test_ctor_id_noschema_novalues( self ):
00064 
00065         ups = self._makeOne( 'empty' )
00066 
00067         self.assertEqual( ups.getId(), 'empty' )
00068 
00069         self.failIf( ups.hasProperty( 'empty' ) )
00070         self.failIf( ups.hasProperty( 'foo' ) )
00071         self.failIf( ups.hasProperty( 'bar' ) )
00072 
00073         self.assertEqual( ups.getProperty( 'foo' ), None )
00074         self.assertEqual( ups.getPropertyType( 'foo' ), None )
00075 
00076         self.assertEqual( len( ups.propertyMap() ), 0 )
00077         self.assertEqual( len( ups.propertyIds() ), 0 )
00078         self.assertEqual( len( ups.propertyValues() ), 0 )
00079         self.assertEqual( len( ups.propertyItems() ), 0 )
00080         self.assertEqual( len( ups.propertyIds() ), 0 )
00081 
00082     def _checkStockSchema( self, ups, values_are_none=False ):
00083 
00084         self.failIf( ups.hasProperty( 'x' ) )
00085         self.failUnless( ups.hasProperty( 's' ) )
00086         self.failUnless( ups.hasProperty( 'i' ) )
00087         self.failUnless( ups.hasProperty( 'f' ) )
00088         self.failUnless( ups.hasProperty( 'n' ) )
00089         self.failUnless( ups.hasProperty( 'd' ) )
00090         self.failUnless( ups.hasProperty( 'l' ) )
00091         self.failUnless( ups.hasProperty( 't' ) )
00092         self.failUnless( ups.hasProperty( 'b' ) )
00093         self.failUnless( ups.hasProperty( 'img' ) )
00094 
00095         self.assertEqual( ups.getPropertyType( 's' ), 'string' )
00096         self.assertEqual( ups.propertyInfo( 's' )[ 'type' ], 'string' )
00097         if values_are_none:
00098             self.assertEqual( ups.getProperty( 's' ), None )
00099         else:
00100             self.assertEqual( ups.getProperty( 's' ), self._STRING_VALUE )
00101 
00102         self.assertEqual( ups.getPropertyType( 'i' ), 'int' )
00103         self.assertEqual( ups.propertyInfo( 'i' )[ 'type' ], 'int' )
00104         if values_are_none:
00105             self.assertEqual( ups.getProperty( 'i' ), None )
00106         else:
00107             self.assertEqual( ups.getProperty( 'i' ), self._INT_VALUE )
00108 
00109         self.assertEqual( ups.getPropertyType( 'f' ), 'float' )
00110         self.assertEqual( ups.propertyInfo( 'f' )[ 'type' ], 'float' )
00111         if values_are_none:
00112             self.assertEqual( ups.getProperty( 'f' ), None )
00113         else:
00114             self.assertEqual( ups.getProperty( 'f' ), self._FLOAT_VALUE )
00115 
00116         self.assertEqual( ups.getPropertyType( 'n' ), 'long' )
00117         self.assertEqual( ups.propertyInfo( 'n' )[ 'type' ], 'long' )
00118         if values_are_none:
00119             self.assertEqual( ups.getProperty( 'n' ), None )
00120         else:
00121             self.assertEqual( ups.getProperty( 'n' ), self._LONG_VALUE )
00122 
00123         self.assertEqual( ups.getPropertyType( 'd' ), 'date' )
00124         self.assertEqual( ups.propertyInfo( 'd' )[ 'type' ], 'date' )
00125         if values_are_none:
00126             self.assertEqual( ups.getProperty( 'd' ), None )
00127         else:
00128             self.assertEqual( ups.getProperty( 'd' ), self._DATE_VALUE )
00129 
00130         self.assertEqual( ups.getPropertyType( 'b' ), 'boolean' )
00131         self.assertEqual( ups.propertyInfo( 'b' )[ 'type' ], 'boolean' )
00132         if values_are_none:
00133             self.assertEqual( ups.getProperty( 'b' ), None )
00134         else:
00135             self.assertEqual( ups.getProperty( 'b' ), self._BOOL_VALUE )
00136 
00137         self.assertEqual( ups.getPropertyType( 'l' ), 'lines' )
00138         self.assertEqual( ups.propertyInfo( 'l' )[ 'type' ], 'lines' )
00139 
00140         if values_are_none:
00141             self.assertEqual( ups.getProperty( 'l' ), None )
00142         else:
00143             got = ups.getProperty( 'l' )
00144             self.assertEqual( type( got ), type( () ) )
00145             self.assertEqual( len( got ), len( self._LIST_VALUE ) )
00146 
00147             for i in range( len( self._LIST_VALUE ) ):
00148                 self.assertEqual( got[i], self._LIST_VALUE[i] )
00149 
00150         self.assertEqual( ups.getPropertyType( 't' ), 'lines' )
00151         self.assertEqual( ups.propertyInfo( 't' )[ 'type' ], 'lines' )
00152 
00153         if values_are_none:
00154             self.assertEqual( ups.getProperty( 't' ), None )
00155         else:
00156             got = ups.getProperty( 't' )
00157             self.assertEqual( type( got ), type( () ) )
00158             self.assertEqual( len( got ), len( self._TUPLE_VALUE ) )
00159 
00160             for i in range( len( self._TUPLE_VALUE ) ):
00161                 self.assertEqual( got[i], self._TUPLE_VALUE[i] )
00162 
00163         self.assertEqual( ups.getPropertyType( 'img' ), 'image' )
00164         self.assertEqual( ups.propertyInfo( 'img' )[ 'type' ], 'image' )
00165 
00166         if values_are_none:
00167             self.assertEqual( ups.getProperty( 'img' ), None )
00168         else:
00169             got = ups.getProperty( 'img' )
00170             self.assertEqual( type( got ), Image )
00171             self.assertEqual( got.size, self._IMG_VALUE.size )
00172             self.assertEqual( got, self._IMG_VALUE )
00173 
00174 
00175         pmap = ups.propertyMap()
00176         self.assertEqual( len( pmap ), len( self._SCHEMA ) )
00177 
00178         for i in range( len( pmap ) ):
00179             info = pmap[i]
00180             spec = [ x for x in self._SCHEMA if x[0] == info['id' ] ][0]
00181             self.assertEqual( info[ 'id' ], spec[0] )
00182             self.assertEqual( info[ 'type' ], spec[1] )
00183             self.assertEqual( info[ 'mode' ], '' )  # readonly, no delete
00184 
00185 
00186     def test_ctor__guessSchema( self ):
00187 
00188         ups = self._makeOne( 'guessed'
00189                            , s=self._STRING_VALUE
00190                            , i=self._INT_VALUE
00191                            , f=self._FLOAT_VALUE
00192                            , n=self._LONG_VALUE
00193                            , d=self._DATE_VALUE
00194                            , l=self._LIST_VALUE
00195                            , t=self._TUPLE_VALUE
00196                            , b=self._BOOL_VALUE
00197                            , img=self._IMG_VALUE
00198                            )
00199 
00200         self._checkStockSchema( ups )
00201 
00202 
00203     def test_ctor_w_schema(self):
00204 
00205         ups = self._makeOne( 'w_schema'
00206                            , self._SCHEMA
00207                            , s=self._STRING_VALUE
00208                            , i=self._INT_VALUE
00209                            , f=self._FLOAT_VALUE
00210                            , n=self._LONG_VALUE
00211                            , d=self._DATE_VALUE
00212                            , l=self._LIST_VALUE
00213                            , t=self._TUPLE_VALUE
00214                            , b=self._BOOL_VALUE
00215                            , img=self._IMG_VALUE
00216                            )
00217 
00218         self._checkStockSchema( ups )
00219 
00220 
00221     def test_ctor_w_schema_no_values(self):
00222 
00223         ups = self._makeOne( 'w_schema'
00224                            , self._SCHEMA
00225                            )
00226 
00227         self._checkStockSchema( ups, values_are_none=True )
00228 
00229 if __name__ == "__main__":
00230     unittest.main()
00231 
00232 def test_suite():
00233     return unittest.TestSuite((
00234         unittest.makeSuite( UserPropertySheetTests ),
00235         ))