def testGetNonExisting(self):
     portal = self.layer['portal']
     setRoles(portal, TEST_USER_ID, ('Manager',))
     doc = self._makeOne()
     from collective.btrees.interfaces import IOOBTreeContainer
     container = IOOBTreeContainer(doc)
     # Getting a non-existing item.
     self.assertEqual(container.get('foobar'), None)
     self.assertEqual(container.get('foobar', 'ni'), 'ni')
     self.assertEqual(container.pop('none'), None)
     self.assertEqual(container.pop('none', 'hi'), 'hi')
 def testOOBTree(self):
     portal = self.layer['portal']
     setRoles(portal, TEST_USER_ID, ('Manager',))
     doc = self._makeOne()
     from collective.btrees.interfaces import IOOBTreeContainer
     container = IOOBTreeContainer(doc)
     self.assertEqual(len(container.keys()), 0)
     key = object()
     value = object()
     self.assertEqual(container.get(key), None)
     container.insert(key, value)
     self.assertEqual(container.get(key), value)
     self.assertEqual(list(container.items()), [(key, value)])
     container.clear()
     self.assertEqual(list(container.keys()), [])
 def testPopItem(self):
     portal = self.layer['portal']
     setRoles(portal, TEST_USER_ID, ('Manager',))
     doc = self._makeOne()
     from collective.btrees.interfaces import IOOBTreeContainer
     container = IOOBTreeContainer(doc)
     # Insert an item and pop it.
     key = object()
     value = object()
     container.insert(key, value)
     self.assertEqual(len(container.items()), 1)
     self.assertEqual(container.pop(key), value)
     self.assertEqual(len(container.items()), 0)
     self.assertEqual(container.pop(key), None)
 def testUpdate(self):
     portal = self.layer['portal']
     setRoles(portal, TEST_USER_ID, ('Manager',))
     doc = self._makeOne()
     from collective.btrees.interfaces import IOOBTreeContainer
     container = IOOBTreeContainer(doc)
     key1 = object()
     key2 = object()
     value1 = object()
     value2 = object()
     container.update({key1: value1, key2: value2})
     self.assertEqual(container.get(key1), value1)
     self.assertEqual(container.get(key2), value2)
     # Turn it around.
     container.update({key1: value2, key2: value1})
     self.assertEqual(container.get(key1), value2)
     self.assertEqual(container.get(key2), value1)
 def testSetdefault(self):
     portal = self.layer['portal']
     setRoles(portal, TEST_USER_ID, ('Manager',))
     doc = self._makeOne()
     from collective.btrees.interfaces import IOOBTreeContainer
     container = IOOBTreeContainer(doc)
     key = object()
     first = object()
     second = object()
     self.assertEqual(container.get(key), None)
     self.assertEqual(container.setdefault(key, first), first)
     self.assertEqual(container.get(key), first)
     self.assertEqual(container.setdefault(key, second), first)
     self.assertEqual(container.get(key), first)
 def testClear(self):
     portal = self.layer['portal']
     setRoles(portal, TEST_USER_ID, ('Manager',))
     doc = self._makeOne()
     from collective.btrees.interfaces import IOOBTreeContainer
     container = IOOBTreeContainer(doc)
     container.insert(object(), object())
     container.insert(object(), object())
     container.insert(object(), object())
     self.assertEqual(len(container.items()), 3)
     # Remove all.
     container.clear()
     self.assertEqual(len(container.items()), 0)
     self.assertEqual(len(container.keys()), 0)
     self.assertEqual(len(container.values()), 0)
     self.assertRaises(StopIteration, container.iteritems().next)
     self.assertRaises(StopIteration, container.iterkeys().next)
     self.assertRaises(StopIteration, container.itervalues().next)
     # Clearing an empty container should work too of course.
     container.clear()
    def testItems_keys_values_and_iterators(self):
        portal = self.layer['portal']
        setRoles(portal, TEST_USER_ID, ('Manager',))
        doc = self._makeOne()
        from collective.btrees.interfaces import IOOBTreeContainer
        container = IOOBTreeContainer(doc)
        self.assertEqual(len(container.keys()), 0)
        key = object()
        value = object()
        container.insert(key, value)
        self.assertEqual(container.get(key), value)
        self.assertEqual(len(container.items()), 1)
        self.assertEqual(len(container.keys()), 1)
        self.assertEqual(len(container.values()), 1)
        self.assertEqual(list(container.items()), [(key, value)])
        self.assertEqual(list(container.keys()), [key])
        self.assertEqual(list(container.values()), [value])

        # Test the iterators.
        item_iterator = container.iteritems()
        self.assertEqual(item_iterator.next(), (key, value))
        self.assertRaises(StopIteration, item_iterator.next)
        key_iterator = container.iterkeys()
        self.assertEqual(key_iterator.next(), key)
        self.assertRaises(StopIteration, key_iterator.next)
        value_iterator = container.itervalues()
        self.assertEqual(value_iterator.next(), value)
        self.assertRaises(StopIteration, value_iterator.next)