def testMarkDeletedBlock(self): indexBlock = NanoBlocks.Index.LeafBlock(5, NanoTypes.Int(4)) indexBlock.keys = [1] indexBlock.addresses = [2] self.IndexIO._writeBlockToFile(indexBlock) self.IndexIO._markBlockDeleted(indexBlock) self.assertEqual(self.IndexIO.delMgr.popRef(), 5) self.assertIsNone(self.IndexIO.delMgr.popRef())
def testLookup(self): # Leaf block block = NanoBlocks.Index.LeafBlock(0, NanoTypes.Int(1)) self.assertRaises(NanoBlocks.Index.KeyNotFound, block.lookup, 1) block.keys = [1] block.addresses = [1] self.assertRaises(NanoBlocks.Index.KeyNotFound, block.lookup, 0) self.assertEqual(block.lookup(1), 1) self.assertRaises(NanoBlocks.Index.KeyNotFound, block.lookup, 2) block.keys = [1, 3] block.addresses = ['a', 'b'] self.assertRaises(NanoBlocks.Index.KeyNotFound, block.lookup, 0) self.assertEqual(block.lookup(1), 'a') self.assertRaises(NanoBlocks.Index.KeyNotFound, block.lookup, 2) self.assertEqual(block.lookup(3), 'b') self.assertRaises(NanoBlocks.Index.KeyNotFound, block.lookup, 4) # Interior block block = NanoBlocks.Index.InteriorBlock(0, NanoTypes.Int(1)) self.assertRaises(NanoBlocks.Index.KeyNotFound, block.lookup, 1) block.keys = [1] block.addresses = [1] self.assertRaises(NanoBlocks.Index.KeyNotFound, block.lookup, 0) self.assertEqual(block.lookup(1), 1) self.assertEqual(block.lookup(2), 1) block.keys = [1, 3] block.addresses = ['a', 'b'] self.assertRaises(NanoBlocks.Index.KeyNotFound, block.lookup, 0) self.assertEqual(block.lookup(1), 'a') self.assertEqual(block.lookup(2), 'a') self.assertEqual(block.lookup(3), 'b') self.assertEqual(block.lookup(4), 'b')
def testGetAndWriteBlockAtIndex(self): # Test getting a block from an index with no keys self._assertIndexBlockEqual( self.IndexIO._getBlockAtAddress(0), NanoBlocks.Index.LeafBlock(0, self.IndexIO.colType)) # Test getting a fresh block indexBlock = NanoBlocks.Index.LeafBlock(0, NanoTypes.Int(4)) self.IndexIO.indexFD.write(indexBlock.toString()) self.assertFalse(0 in self.IndexIO.cacheMgr) self._assertIndexBlockEqual(indexBlock, self.IndexIO._getBlockAtAddress(0)) # Test getting a block that should exist in the Block Cache Manager indexBlock.keys = [1] indexBlock.addresses = [2] self.IndexIO._writeBlockToFile(indexBlock) self.assertTrue(0 in self.IndexIO.cacheMgr) self._assertIndexBlockEqual(indexBlock, self.IndexIO._getBlockAtAddress(0))