~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to fai/arch/tests/test_namespace.py

  • Committer: Aaron Bentley
  • Date: 2011-09-25 01:17:15 UTC
  • mfrom: (776.1.2 fix-bzr.dev-compat)
  • Revision ID: aaron@aaronbentley.com-20110925011715-o1akrv7xjl3e4iys
Fixed compatibility with bzr.dev (jelmer)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# arch-tag: fb639c6b-566d-4b7c-be27-cfc406fc2fb8
3
 
# Copyright (C) 2004 David Allouche <david@allouche.net>
4
 
#                    Canonical Ltd.
5
 
#
6
 
#    This program is free software; you can redistribute it and/or modify
7
 
#    it under the terms of the GNU General Public License as published by
8
 
#    the Free Software Foundation; either version 2 of the License, or
9
 
#    (at your option) any later version.
10
 
#
11
 
#    This program is distributed in the hope that it will be useful,
12
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
#    GNU General Public License for more details.
15
 
#
16
 
#    You should have received a copy of the GNU General Public License
17
 
#    along with this program; if not, write to the Free Software
18
 
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
 
20
 
"""Test access to containers and name part properties of namespace classes.
21
 
"""
22
 
 
23
 
import sys
24
 
import arch
25
 
from arch.tests import framework
26
 
from arch.tests.framework import TestCase, TestParams
27
 
 
28
 
 
29
 
class ArchNameProperties(TestCase):
30
 
 
31
 
    """Test cases access to container and name part properties
32
 
 
33
 
    Check the availability and correctness of the "archive",
34
 
    "category", "branch", "version", "nonarch", and "patchlevel" of
35
 
    archive objects.
36
 
    """
37
 
 
38
 
    def setUp(self):
39
 
        self.params = TestParams()
40
 
        def set(**kw):
41
 
            for k, v in kw.items():
42
 
                v = v % self.__dict__
43
 
                setattr(self, k, v)
44
 
                setattr(self, k+'_full', '%s/%s' % (self.arch_name, v))
45
 
        set(category='frob')
46
 
        set(branch='%(category)s--devo')
47
 
        set(version='%(branch)s--1.0')
48
 
        self.patchlevel = 'patch-1'
49
 
        set(revision='%(version)s--%(patchlevel)s')
50
 
        set(nameless_branch='%(category)s')
51
 
        set(nameless_version='%(category)s--1.0')
52
 
        set(nameless_revision='%(nameless_version)s--%(patchlevel)s')
53
 
 
54
 
    tests = []
55
 
 
56
 
    def readonly(self, obj, *attrs):
57
 
        for attr in attrs:
58
 
            def try_set(): setattr(obj, attr, 'error')
59
 
            self.assertRaises(AttributeError, try_set)
60
 
 
61
 
    def check_hasnot(self, obj, *attrs):
62
 
        for attr in attrs:
63
 
            self.assert_(not hasattr(obj, attr))
64
 
 
65
 
    def assertInstance(self, obj, cls):
66
 
        self.assert_(isinstance(obj, cls))
67
 
 
68
 
    def check_names(self, obj, namekind):
69
 
        self.readonly(obj, 'nonarch', 'fullname')
70
 
        self.assertEqual(obj.nonarch, getattr(self, namekind))
71
 
        self.assertEqual(str(obj), obj.fullname)
72
 
        self.assertEqual(obj.fullname, getattr(self, namekind+'_full'))
73
 
 
74
 
    enclosing = {'category': 'archive', 'branch': 'category',
75
 
                 'version': 'branch', 'revision': 'version'}
76
 
 
77
 
    def check_init(self, cls, kind, nameless=''):
78
 
        def check_names(obj): self.check_names(obj, nameless+kind)
79
 
        obj = cls(getattr(self, nameless+kind+'_full'))
80
 
        check_names(obj)
81
 
        def unsafe_ctor(*args): return cls(arch._builtin._unsafe(args))
82
 
        if kind != 'revision':
83
 
            obj = unsafe_ctor(self.arch_name, getattr(self, nameless+kind))
84
 
        else:
85
 
            obj = unsafe_ctor(self.arch_name,
86
 
                              getattr(self, nameless+self.enclosing[kind]),
87
 
                              self.patchlevel)
88
 
        check_names(obj)
89
 
        obj = cls(obj)
90
 
        check_names(obj)
91
 
        return obj
92
 
 
93
 
    def check_badinit(self, cls, *names):
94
 
        for name in names:
95
 
            func = lambda: cls(getattr(self, name))
96
 
            self.assertRaises(arch.errors.NamespaceError, func)
97
 
 
98
 
    def check_container_helper(self, obj, prop, strval):
99
 
        self.readonly(obj, prop)
100
 
        val = getattr(obj, prop)
101
 
        typ = getattr(arch, prop.capitalize())
102
 
        self.assertInstance(val, typ)
103
 
        self.assertEqual(str(val), strval)
104
 
 
105
 
    def check_containers(self, obj, kind, nameless=''):
106
 
        if kind == 'archive':
107
 
            self.check_container_helper(obj, 'archive', self.arch_name)
108
 
            return
109
 
        self.check_containers(obj, self.enclosing[kind], nameless)
110
 
        if kind == 'category': namekind=kind
111
 
        else: namekind = nameless+kind
112
 
        self.check_container_helper(obj, kind, getattr(self, namekind+'_full'))
113
 
 
114
 
    def test_category(self):
115
 
        """Category has correct constructor and properties."""
116
 
        cat = self.check_init(arch.Category, 'category')
117
 
        self.check_containers(cat, 'archive')
118
 
        self.check_badinit(arch.Category, 'arch_name', 'branch_full')
119
 
        self.check_hasnot(cat, 'category', 'branch', 'version', 'patchlevel')
120
 
 
121
 
    tests.append('test_category')
122
 
 
123
 
    def test_branch(self, nameless=False):
124
 
        """Branch has correct constructor and properties."""
125
 
        nameless = nameless and 'nameless_' or ''
126
 
        brn = self.check_init(arch.Branch, 'branch', nameless)
127
 
        self.check_containers(brn, 'category')
128
 
        self.check_badinit(arch.Branch, 'arch_name',
129
 
                           nameless+'version_full')
130
 
        self.check_hasnot(brn, 'branch', 'version', 'patchlevel')
131
 
 
132
 
    tests.append('test_branch')
133
 
 
134
 
    def test_version(self, nameless=False):
135
 
        """Version has correct constructor and properties."""
136
 
        nameless = nameless and 'nameless_' or ''
137
 
        vsn = self.check_init(arch.Version, 'version', nameless)
138
 
        self.check_containers(vsn, 'branch', nameless)
139
 
        self.check_badinit(arch.Version, nameless+'branch_full',
140
 
                           nameless+'revision_full')
141
 
        self.check_hasnot(vsn, 'version', 'patchlevel')
142
 
 
143
 
    tests.append('test_version')
144
 
 
145
 
    def test_revision(self, nameless=False):
146
 
        """Revision has correct constructor and properties."""
147
 
        nameless = nameless and 'nameless_' or ''
148
 
        rvsn = self.check_init(arch.Revision, 'revision', nameless)
149
 
        self.check_containers(rvsn, 'version', nameless)
150
 
        self.check_badinit(arch.Revision, nameless+'version_full')
151
 
        self.readonly(rvsn, 'patchlevel')
152
 
        self.assertEqual(rvsn.patchlevel, self.patchlevel)
153
 
 
154
 
    tests.append('test_revision')
155
 
 
156
 
    def test_nameless_branch(self):
157
 
        """Branch (nameless branch) has correct ctor and properties."""
158
 
        self.test_branch(nameless=True)
159
 
    tests.append('test_nameless_branch')
160
 
 
161
 
    def test_nameless_version(self):
162
 
        """Version (nameless branch) has correct ctor and properties."""
163
 
        self.test_version(nameless=True)
164
 
    tests.append('test_nameless_version')
165
 
 
166
 
    def test_nameless_revision(self):
167
 
        """Revision (nameless branch) has correct ctor and properties."""
168
 
        self.test_revision(nameless=True)
169
 
    tests.append('test_nameless_revision')
170
 
 
171
 
 
172
 
def test_suite(limit=()):
173
 
    classes = (
174
 
        'ArchNameProperties',
175
 
        )
176
 
    return framework.make_test_suite(globals(), classes, limit)
177
 
 
178
 
def main(argv):
179
 
    return framework.run_test_suite(argv, test_suite)
180
 
 
181
 
if __name__ == "__main__":
182
 
    sys.exit(main(sys.argv))
183