summaryrefslogtreecommitdiff
path: root/obnamlib/fmt_ga/leaf_list_tests.py
blob: ad9541e946f0037fd10a431c9101452b35751a22 (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
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
# Copyright 2017  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/>.
#
# =*= License: GPL-3+ =*=


import unittest


import obnamlib


class LeafListTests(unittest.TestCase):

    def test_is_empty_initially(self):
        ll = obnamlib.LeafList()
        self.assertEqual(len(ll), 0)

    def test_adds_leaf(self):
        first_key = 'aaaa'
        last_key = 'bbbb'
        leaf_id = 'leaf-1'

        ll = obnamlib.LeafList()
        ll.add(leaf_id, first_key, last_key)
        self.assertEqual(len(ll), 1)
        self.assertEqual(ll.leaves(), [leaf_id])

    def test_adding_overlapping_leaf_raises_exception(self):
        first_key = 'a'
        last_key = 'b'
        leaf_id_1 = 'leaf-1'
        leaf_id_2 = 'leaf-2'

        ll = obnamlib.LeafList()
        ll.add(leaf_id_1, first_key, last_key)

        with self.assertRaises(Exception):
            ll.add(leaf_id_2, first_key, last_key)

    def test_adds_second_leaf(self):
        leaf1 = ('leaf-1', 'a', 'b')
        leaf2 = ('leaf-2', 'c', 'd')

        ll = obnamlib.LeafList()
        # Note that we insert in reverse order to make sure .add puts things
        # into the right order.
        ll.add(*leaf2)
        ll.add(*leaf1)

        self.assertEqual(len(ll), 2)
        self.assertEqual(ll.leaves(), [leaf1[0], leaf2[0]])

    def test_adds_third_leaf_in_the_middle(self):
        leaf1 = ('leaf-1', 'a', 'b')
        leaf2 = ('leaf-2', 'e', 'f')
        leaf3 = ('leaf-2', 'c', 'd')

        ll = obnamlib.LeafList()
        ll.add(*leaf1)
        ll.add(*leaf3)
        ll.add(*leaf2)

        self.assertEqual(len(ll), 3)
        self.assertEqual(ll.leaves(), [leaf1[0], leaf2[0], leaf3[0]])

    def test_finds_correct_leaf(self):
        leaf1 = ('leaf-1', 'a', 'b')
        leaf2 = ('leaf-2', 'c', 'd')
        leaf3 = ('leaf-3', 'e', 'f')

        ll = obnamlib.LeafList()
        ll.add(*leaf1)
        ll.add(*leaf3)
        ll.add(*leaf2)

        found = ll.find_leaf('aaa')
        self.assertNotEqual(found, None)
        self.assertEqual(found, leaf1[0])

    def test_finds_none(self):
        leaf1 = ('leaf-1', 'a', 'b')
        leaf2 = ('leaf-2', 'c', 'd')
        leaf3 = ('leaf-3', 'e', 'f')

        ll = obnamlib.LeafList()
        ll.add(*leaf1)
        ll.add(*leaf3)
        ll.add(*leaf2)

        found = ll.find_leaf('z')
        self.assertEqual(found, None)

    def test_serialisation_roundtrip(self):
        leaf1 = ('leaf-1', 'a', 'b')
        leaf2 = ('leaf-2', 'c', 'd')
        leaf3 = ('leaf-3', 'e', 'f')

        orig = obnamlib.LeafList()
        orig.add(*leaf1)
        orig.add(*leaf3)
        orig.add(*leaf2)

        serialised = orig.serialise()
        new = obnamlib.LeafList.unserialise(serialised)

        self.assertEqual(orig.leaves(), new.leaves())