def test_order_events(self): # Prepare the setup:: from zope.container.contained import ContainerModifiedEvent # Prepare some objects:: from zope.container.ordered import OrderedContainer oc = OrderedContainer() oc['foo'] = 'bar' oc['baz'] = 'quux' oc['zork'] = 'grue' self.assertEqual(oc.keys(), ['foo', 'baz', 'zork']) # Now change the order:: clearEvents() oc.updateOrder(['baz', 'foo', 'zork']) self.assertEqual(oc.keys(), ['baz', 'foo', 'zork']) # Check what events have been sent:: events = getEvents() self.assertEqual(1, len(events)) self.assertIsInstance(events[0], ContainerModifiedEvent) # This is in fact a specialized modification event:: from zope.lifecycleevent.interfaces import IObjectModifiedEvent self.assertTrue(IObjectModifiedEvent.providedBy(events[0]))
def test_order_updateOrder_bytes(self): # https://github.com/zopefoundation/zope.container/issues/21 from zope.container.ordered import OrderedContainer keys = [u'a', u'b'] oc = OrderedContainer() oc[keys[0]] = 0 oc[keys[1]] = 1 self.assertEqual(keys, oc.keys()) # Updating with bytes keys... oc.updateOrder((b'b', b'a')) # still produces text keys text_type = str if str is not bytes else unicode self.assertEqual(list(reversed(keys)), oc.keys()) self.assertIsInstance(oc.keys()[0], text_type)