Back to index

obnam  1.1
checksumtree_tests.py
Go to the documentation of this file.
00001 # Copyright 2010  Lars Wirzenius
00002 # 
00003 # This program is free software: you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation, either version 3 of the License, or
00006 # (at your option) any later version.
00007 # 
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 # 
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 
00017 import hashlib
00018 import shutil
00019 import tempfile
00020 import unittest
00021 
00022 import obnamlib
00023 
00024 
00025 class ChecksumTreeTests(unittest.TestCase):
00026 
00027     def setUp(self):
00028         self.tempdir = tempfile.mkdtemp()
00029         fs = obnamlib.LocalFS(self.tempdir)
00030         self.hooks = obnamlib.HookManager()
00031         self.hooks.new('repository-toplevel-init')
00032         self.checksum = hashlib.md5('foo').digest()
00033         self.tree = obnamlib.ChecksumTree(fs, 'x', len(self.checksum), 
00034                                           obnamlib.DEFAULT_NODE_SIZE,
00035                                           obnamlib.DEFAULT_UPLOAD_QUEUE_SIZE,
00036                                           obnamlib.DEFAULT_LRU_SIZE, self)
00037 
00038     def tearDown(self):
00039         self.tree.commit()
00040         shutil.rmtree(self.tempdir)
00041 
00042     def test_is_empty_initially(self):
00043         self.assertEqual(self.tree.find(self.checksum), [])
00044 
00045     def test_finds_checksums(self):
00046         self.tree.add(self.checksum, 1, 3)
00047         self.tree.add(self.checksum, 2, 4)
00048         self.assertEqual(sorted(self.tree.find(self.checksum)), [1, 2])
00049 
00050     def test_finds_only_the_right_checksums(self):
00051         self.tree.add(self.checksum, 1, 2)
00052         self.tree.add(self.checksum, 3, 4)
00053         self.tree.add(hashlib.md5('bar').digest(), 5, 6)
00054         self.assertEqual(sorted(self.tree.find(self.checksum)), [1, 3])
00055 
00056     def test_removes_checksum(self):
00057         self.tree.add(self.checksum, 1, 3)
00058         self.tree.add(self.checksum, 2, 4)
00059         self.tree.remove(self.checksum, 2, 4)
00060         self.assertEqual(self.tree.find(self.checksum), [1])
00061 
00062     def test_adds_same_id_only_once(self):
00063         self.tree.add(self.checksum, 1, 2)
00064         self.tree.add(self.checksum, 1, 2)
00065         self.assertEqual(self.tree.find(self.checksum), [1])
00066 
00067     def test_unknown_chunk_is_not_used(self):
00068         self.assertFalse(self.tree.chunk_is_used(self.checksum, 0))
00069 
00070     def test_known_chunk_is_used(self):
00071         self.tree.add(self.checksum, 0, 1)
00072         self.assertTrue(self.tree.chunk_is_used(self.checksum, 0))
00073