summaryrefslogtreecommitdiff
path: root/simplejenkinsapi/order_tests.py
blob: 1eed6da98c804fedcb56951acfd11b26b32562fc (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
# simplejenkinsapi/order_tests.py -- unit tests for build ordering
#
# Copyright 2012  Lars Wirzenius
#
# 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 3 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, see <http://www.gnu.org/licenses/>.


import unittest

from simplejenkinsapi import order


def _names(projects):
    return [p['name'] for p in projects]


class OrderTests(unittest.TestCase):

    def setUp(self):
        self.A = { 'name': 'A', 'build-depends': [] }
        self.B = { 'name': 'B', 'build-depends': [] }
        self.C = { 'name': 'C', 'build-depends': [] }
        self.D = { 'name': 'D', 'build-depends': [] }

    def test_returns_empty_list_for_empty_list(self):
        self.assertEqual(order([]), [])

    def test_returns_identical_list_for_one_project(self):
        ps = [self.A]
        self.assertEqual(order(ps), ps)

    def test_returns_correct_list_for_two_independent_projects(self):
        ps = [self.A, self.B]
        self.assertEqual(order(ps), ps)

    def test_returns_correct_list_for_two_dependent_projects(self):
        self.A['build-depends'] = ['B']
        ps = [self.A, self.B]
        new = order(ps)
        self.assertEqual(_names(new), _names([self.B, self.A]))

    def test_returns_correct_list_for_three_with_linear_deps(self):
        self.B['build-depends'] = ['A']
        self.C['build-depends'] = ['A']
        ps = [self.A, self.B, self.C]
        self.assertEqual(_names(order(ps)), _names(ps))

    def test_returns_correct_list_for_three_with_nonlinear_deps(self):
        self.A['build-depends'] = ['B', 'C']
        ps = [self.A, self.B, self.C]
        self.assertEqual(_names(order(ps)), _names([self.B, self.C, self.A]))

    def test_returns_correct_list_for_diamond(self):
        self.A['build-depends'] = ['B', 'C']
        self.B['build-depends'] = ['D']
        self.C['build-depends'] = ['D']
        ps = [self.A, self.B, self.C, self.D]
        self.assertEqual(_names(order(ps)),
                         _names([self.D, self.B, self.C, self.A]))