~bzr-pqm/bzr/bzr.dev

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Copyright (C) 2005-2009, 2016 Canonical Ltd
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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

"""Tests for library API infrastructure

This is specifically for things controlling the interface, such as versioning.
Tests for particular parts of the library interface should be in specific
relevant test modules.
"""

import bzrlib
import bzrlib.api
from bzrlib.errors import IncompatibleAPI
from bzrlib.tests import TestCase

class APITests(TestCase):

    def test_library_version(self):
        """Library API version is exposed"""
        self.assertTrue(isinstance(bzrlib.__version__, str))
        self.assertTrue(isinstance(bzrlib.version_string, str))
        self.assertTrue(isinstance(bzrlib.version_info, tuple))
        self.assertEqual(len(bzrlib.version_info), 5)


class TrivialObject(object):
    """This class allows assignment to any attribute."""


class TestAPIVersioning(TestCase):

    def test_get_minimum_api_version_reads_api_minimum_version(self):
        an_object = TrivialObject()
        an_object.api_minimum_version = (0, 1, 2)
        self.assertEqual((0, 1, 2),
            bzrlib.api.get_minimum_api_version(an_object))

    def test_get_minimum_api_version_fallsback_to_bzr_minimum_version(self):
        an_object = TrivialObject()
        self.assertEqual(bzrlib.api_minimum_version,
            bzrlib.api.get_minimum_api_version(an_object))

    def test_get_current_api_version_reads_api_current_version(self):
        an_object = TrivialObject()
        an_object.api_current_version = (3, 2, 1)
        an_object.version_info = (1, 2, 3, "final", 0)
        self.assertEqual((3, 2, 1),
            bzrlib.api.get_current_api_version(an_object))

    def test_get_current_api_version_fallsback_to_version_info(self):
        an_object = TrivialObject()
        an_object.version_info = (1, 2, 3, "final", 0)
        self.assertEqual((1, 2, 3),
            bzrlib.api.get_current_api_version(an_object))

    def test_get_current_api_version_fallsback_to_bzrlib_version_info(self):
        an_object = TrivialObject()
        self.assertEqual(bzrlib.version_info[0:3],
            bzrlib.api.get_current_api_version(an_object))

    def test_require_any_api_wanted_one(self):
        an_object = TrivialObject()
        an_object.api_minimum_version = (1, 2, 3)
        an_object.api_current_version = (4, 5, 6)
        bzrlib.api.require_any_api(an_object, [(1, 2, 3)])

    def test_require_any_api_wanted_first_compatible(self):
        an_object = TrivialObject()
        an_object.api_minimum_version = (1, 2, 3)
        an_object.api_current_version = (4, 5, 6)
        bzrlib.api.require_any_api(an_object, [(1, 2, 3), (5, 6, 7)])

    def test_require_any_api_wanted_second_compatible(self):
        an_object = TrivialObject()
        an_object.api_minimum_version = (1, 2, 3)
        an_object.api_current_version = (4, 5, 6)
        bzrlib.api.require_any_api(an_object, [(5, 6, 7), (1, 2, 3)])

    def test_require_any_api_wanted_none_compatible(self):
        an_object = TrivialObject()
        an_object.api_minimum_version = (1, 2, 3)
        an_object.api_current_version = (4, 5, 6)
        self.assertRaises(IncompatibleAPI, bzrlib.api.require_any_api,
            an_object, [(1, 2, 2), (5, 6, 7)])

    def test_require_api_wanted_is_minimum_is_ok(self):
        an_object = TrivialObject()
        an_object.api_minimum_version = (1, 2, 3)
        an_object.api_current_version = (4, 5, 6)
        bzrlib.api.require_api(an_object, (1, 2, 3))

    def test_require_api_wanted_is_current_is_ok(self):
        an_object = TrivialObject()
        an_object.api_minimum_version = (1, 2, 3)
        an_object.api_current_version = (4, 5, 6)
        bzrlib.api.require_api(an_object, (4, 5, 6))

    def test_require_api_wanted_is_above_minimum_is_ok(self):
        an_object = TrivialObject()
        an_object.api_minimum_version = (1, 2, 3)
        an_object.api_current_version = (4, 5, 6)
        bzrlib.api.require_api(an_object, (1, 2, 4))

    def test_require_api_wanted_is_below_current_is_ok(self):
        an_object = TrivialObject()
        an_object.api_minimum_version = (1, 2, 3)
        an_object.api_current_version = (4, 5, 6)
        bzrlib.api.require_api(an_object, (4, 5, 5))

    def test_require_api_wanted_is_below_minimum_raises(self):
        an_object = TrivialObject()
        an_object.api_minimum_version = (1, 2, 3)
        an_object.api_current_version = (4, 5, 6)
        err = self.assertRaises(IncompatibleAPI,
            bzrlib.api.require_api, an_object, (1, 2, 2))
        self.assertEqual(err.api, an_object)
        self.assertEqual(err.wanted, (1, 2, 2))
        self.assertEqual(err.minimum, (1, 2, 3))
        self.assertEqual(err.current, (4, 5, 6))

    def test_require_api_wanted_is_above_current_raises(self):
        an_object = TrivialObject()
        an_object.api_minimum_version = (1, 2, 3)
        an_object.api_current_version = (4, 5, 6)
        err = self.assertRaises(IncompatibleAPI,
            bzrlib.api.require_api, an_object, (4, 5, 7))
        self.assertEqual(err.api, an_object)
        self.assertEqual(err.wanted, (4, 5, 7))
        self.assertEqual(err.minimum, (1, 2, 3))
        self.assertEqual(err.current, (4, 5, 6))