summaryrefslogtreecommitdiff
path: root/morphlib/cachedir_tests.py
blob: 172b7d86c74b9bae45d4aa34e1834da87108cf93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Copyright (C) 2011-2012  Codethink Limited
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import unittest
import os

import morphlib


class CacheDirTests(unittest.TestCase):

    def setUp(self):
        self.dirname = 'cache/dir'
        self.cachedir = morphlib.cachedir.CacheDir(self.dirname)

    def test_sets_dirname_attribute(self):
        self.assertEqual(self.cachedir.dirname, os.path.abspath(self.dirname))

    def test_generates_string_key_for_arbitrary_dict_key(self):
        key = self.cachedir.key({
            'foo': 'bar',
            'xyzzy': 'plugh',
        })
        self.assertEqual(type(key), str)
        self.assertNotEqual(key, '')

    def test_generates_same_string_key_twice(self):
        dict_key = {
            'foo': 'bar',
            'xyzzy': 'plugh',
        }
        self.assertEqual(self.cachedir.key(dict_key), 
                         self.cachedir.key(dict_key))

    def test_generates_different_string_keys(self):
        dict_key_1 = {
            'foo': 'bar',
            'xyzzy': 'plugh',
        }
        dict_key_2 = {
            'foo': 'foobar',
            'xyzzy': 'stevenage',
        }
        self.assertNotEqual(self.cachedir.key(dict_key_1), 
                            self.cachedir.key(dict_key_2))

    def test_returns_a_chunk_pathname_in_cache_directory(self):
        dict_key = {
            'kind': 'chunk',
            'ref': 'DEADBEEF',
            'repo': 'git://git.baserock.org/hello/',
            'arch': 'armel',
        }
        pathname = self.cachedir.name(dict_key)
        self.assert_(pathname.startswith(self.cachedir.dirname + '/'))
        self.assert_(pathname.endswith('.chunk'))

    def test_returns_a_stratum_pathname_in_cache_directory(self):
        dict_key = {
            'kind': 'stratum',
            'ref': 'DEADBEEF',
            'repo': 'git://git.baserock.org/hello/',
            'arch': 'armel',
        }
        pathname = self.cachedir.name(dict_key)
        self.assert_(pathname.startswith(self.cachedir.dirname + '/'))
        self.assert_(pathname.endswith('.stratum'))

    def test_returns_a_valid_pathname_in_cache_directory(self):
        dict_key = {
            'ref': 'DEADBEEF',
            'repo': 'git://git.baserock.org/hello/',
            'arch': 'armel',
        }
        pathname = self.cachedir.name(dict_key)
        self.assert_(pathname.startswith(self.cachedir.dirname + '/'))