Logo Search packages:      
Sourcecode: ubuntuone-client version File versions

test_fileshelf.py

#
# Author: Guillermo Gonzalez <guillermo.gonzalez@canonical.com>
#
# Copyright 2009 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

""" Test disk cache persistent shelf """

import os
import hashlib
import unittest
import shutil

from ubuntuone.syncdaemon.file_shelf import FileShelf

TESTS_DIR = os.path.join(os.getcwd(), "tmp")

00029 class TestFileShelf(unittest.TestCase):
    """ Test the FileShelf """

00032     def setUp(self):
        """ Sets up a test. """
        try:
            os.mkdir(TESTS_DIR)
        except OSError:
            # already there, remove it to clean and create again
            shutil.rmtree(TESTS_DIR)
            os.mkdir(TESTS_DIR)
        self.path = os.path.join(TESTS_DIR, 'shelf')
        self.shelf = FileShelf(self.path)

00043     def tearDown(self):
        '''Clean up the tests.'''
        shutil.rmtree(TESTS_DIR)

00047     def test_bad_depth(self):
        """ test that the shelf reject invalid depth at creation time """
        self.assertRaises(ValueError, FileShelf, TESTS_DIR, depth=-1)

00051     def test_bad_path(self):
        """ test that the shelf removes the previous shelve file and create a
        directory for the new file based shelf at creation time.
        """
        path = os.path.join(TESTS_DIR, 'shelf_file')
        open(path, 'w').close()
        shelf = FileShelf(path)
        self.assertTrue(os.path.isdir(path))

00060     def test_different_depth_sizes(self):
        """ test the basic operations (delitem, getitem, setitem) with
        depths between 0 and len(hashlib.sha1().hexdigest())
        """
        base_path = os.path.join(TESTS_DIR, 'shelf_depth-')
        sha1 = hashlib.sha1()
        for idx in xrange(0, len(sha1.hexdigest())):
            path = base_path + str(idx)
            shelf = FileShelf(path, depth=idx)
            key = sha1.hexdigest()
            # test __setitem__
            shelf[key] = 'foo'
            key_path = os.path.join(path, *[key[i] for i in xrange(0, idx)])
            self.assertTrue(os.path.exists(os.path.join(key_path, key)))
            # test __getitem__
            self.assertEquals('foo', shelf[key])
            # test __delitem__
            del shelf[key]
            self.assertRaises(KeyError, shelf.__getitem__, key)
            self.assertFalse(os.path.exists(os.path.join(key_path, key)))

00081     def test_invalid_keys(self):
        """ test the exception raised when invalid keys are eused ('', None)"""
        self.assertRaises(ValueError, self.shelf.__setitem__, None, 'foo')
        self.assertRaises(ValueError, self.shelf.__setitem__, '', 'foo')

00086     def test_contains(self):
        """ test that it behaves with the 'in' """
        path = os.path.join(TESTS_DIR, 'shelf_depth')
        shelf = FileShelf(path)
        shelf["foo"] = "bar"
        self.assertTrue("foo" in shelf)
        self.assertFalse("baz" in shelf)
        self.assertEquals('bar', shelf.get('foo'))
        self.assertEquals(None, shelf.get('baz', None))

00096     def test_pop(self):
        """ test that it behaves with the .pop() """
        path = os.path.join(TESTS_DIR, 'shelf_depth')
        shelf = FileShelf(path)
        shelf["foo"] = "bar"
        self.assertEqual("bar", shelf.pop("foo"))
        self.assertFalse("foo" in shelf)

        # bad key
        self.assertRaises(KeyError, shelf.pop, "no-key")

00107     def test_get(self):
        """ test that it behaves with the .get(key, default) """
        path = os.path.join(TESTS_DIR, 'shelf_get')
        shelf = FileShelf(path)
        shelf["foo"] = "bar"
        self.assertEquals('bar', shelf.get('foo'))
        self.assertEquals('bar', shelf.get('foo', None))
        self.assertEquals(None, shelf.get('baz'))
        self.assertFalse(shelf.get('baz', False))

00117     def test_items(self):
        """ test that it behaves with the .items() """
        path = os.path.join(TESTS_DIR, 'shelf_get')
        shelf = FileShelf(path)
        shelf["foo"] = "bar"
        # k, v are temp variables, pylint: disable-msg=W0631
        self.assertEquals([('foo', 'bar')],
                          [(k, v) for k, v in shelf.items()])
        shelf["foo1"] = "bar1"
        self.assertTrue(('foo', 'bar') and ('foo1', 'bar1') in \
                        [(k, v) for k, v in shelf.items()])


def test_suite():
    # pylint: disable-msg=C0111
    return unittest.TestLoader().loadTestsFromName(__name__)

if __name__ == "__main__":
    unittest.main()


Generated by  Doxygen 1.6.0   Back to index