Ignore:
Timestamp:
2 Jul 2016, 17:10:46 (8 years ago)
Author:
uli
Message:

Example for usage of defaultFactory in ifaces.

This might help with empty lists as default values in interfaces.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_converters.py

    r14005 r14006  
    9797        value_type=ResultEntryField(),
    9898        required=True,
    99         #defaultFactory=list,
    100         default=[],
     99        defaultFactory=list
    101100        )
    102101    fav_colors = schema.List(
     
    106105            ),
    107106        required=True,
    108         #defaultFactory=list
    109         default=[],
     107        defaultFactory=list
    110108        )
    111109    friends = schema.List(
     
    142140    def getInterfaces(self):
    143141        return implementedBy(Faculty)
     142
     143
     144class TestContacts(unittest.TestCase):
     145    # make sure the Contact class inhere works as expected
     146
     147    def test_default_lists_not_shared(self):
     148        # 'grades' is a Contact property with empty list as default.
     149        # Ensure lists are unique if set as default this test fails if
     150        # `default` is set to empty list in interface.
     151        c1 = Contact()
     152        c2 = Contact()
     153        c1.grades.append('foo')
     154        self.assertTrue(hasattr(c2, 'grades'))
     155        self.assertTrue(isinstance(c2.grades, list))
     156        self.assertEqual(c2.grades, [])
    144157
    145158
Note: See TracChangeset for help on using the changeset viewer.