test_tileset.py 5.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
from unittest import TestCase, mock

from tileset import Tile, TileSet


class TestTile(TestCase):
    def test_tile_equal(self):
        self.assertEqual(Tile(1, 2, 3), Tile(1, 2, 3))

    def test_tile_not_equal(self):
        self.assertNotEqual(Tile(1, 2, 3), Tile(4, 5, 6))

    def test_tile_hashable(self):
Jgiannelos's avatar
Jgiannelos committed
14
15
        t1 = Tile(1, 2, 3)
        t2 = Tile(1, 2, 3)
16
17
        self.assertEqual(hash(t1), hash(t2))

Jgiannelos's avatar
Jgiannelos committed
18

19
class TestTileSet(TestCase):
20
21
22
    @mock.patch("tileset.TileSet.addChildren")
    @mock.patch("tileset.TileSet.addParent")
    def test_tileset_read(self, mock_addParent, mock_addChildren):
23
24
25
26
27
        test_input = ["15/5/5", "15/12/12"]
        test_input_file = "\n".join(test_input)

        mock_open = mock.mock_open(read_data=test_input_file)
        with mock.patch("builtins.open", mock_open):
28
            tileset = TileSet("/path/to/tilelist", 0, 15)
29
30
            tileset.read()

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
        parent_list = [mock.call(Tile(14, 2, 2)), mock.call(Tile(14, 6, 6))]
        mock_addParent.assert_has_calls(parent_list)
        children_list = [
            mock.call(
                [
                    Tile(z=16, x=10, y=10),
                    Tile(z=16, x=11, y=10),
                    Tile(z=16, x=10, y=11),
                    Tile(z=16, x=11, y=11),
                ]
            ),
            mock.call(
                [
                    Tile(z=16, x=24, y=24),
                    Tile(z=16, x=25, y=24),
                    Tile(z=16, x=24, y=25),
                    Tile(z=16, x=25, y=25),
                ]
            ),
        ]
        mock_addChildren.assert_has_calls(children_list)
52
53
54
55
56
57
58
59
60
61
62

    def test_parse_entry(self):
        entry = "4/15/100"
        parsed = TileSet.parse(entry)
        self.assertEqual(parsed, Tile(4, 15, 100))

    def test_parse_entry_trailing_newline(self):
        entry = "4/15/100\n"
        parsed = TileSet.parse(entry)
        self.assertEqual(parsed, Tile(4, 15, 100))

63
    def test_tileset_add_single_minzoom(self):
64
65
66
67
68
        test_input = ["0/0/0"]
        test_input_file = "\n".join(test_input)

        mock_open = mock.mock_open(read_data=test_input_file)
        with mock.patch("builtins.open", mock_open):
69
            tileset = TileSet("/path/to/tilelist", 0, 1)
70
71
72
            tileset.read()
        self.assertEqual(len(tileset.tileset), 5)

73
    def test_tileset_add_multiple_minzoom(self):
74
75
76
77
78
        test_input = ["0/0/0", "0/1/1"]
        test_input_file = "\n".join(test_input)

        mock_open = mock.mock_open(read_data=test_input_file)
        with mock.patch("builtins.open", mock_open):
79
            tileset = TileSet("/path/to/tilelist", 0, 1)
80
81
82
            tileset.read()
        self.assertEqual(len(tileset.tileset), 10)

83
    def test_tileset_add_multiple_overlapping_minzoom(self):
84
85
86
87
88
        test_input = ["0/0/0", "1/0/0"]
        test_input_file = "\n".join(test_input)

        mock_open = mock.mock_open(read_data=test_input_file)
        with mock.patch("builtins.open", mock_open):
89
            tileset = TileSet("/path/to/tilelist", 0, 1)
90
91
            tileset.read()
        self.assertEqual(len(tileset.tileset), 5)
92
93
94
95
96
97
98

    def test_tileset_add_single_z_between_maxzoom(self):
        test_input = ["2/0/0"]
        test_input_file = "\n".join(test_input)

        mock_open = mock.mock_open(read_data=test_input_file)
        with mock.patch("builtins.open", mock_open):
99
            tileset = TileSet("/path/to/tilelist", 0, 3)
100
101
102
103
104
105
106
107
108
            tileset.read()
        self.assertEqual(len(tileset.tileset), 7)

    def test_tileset_add_multiple_z_between_maxzoom(self):
        test_input = ["2/0/0", "2/10/10"]
        test_input_file = "\n".join(test_input)

        mock_open = mock.mock_open(read_data=test_input_file)
        with mock.patch("builtins.open", mock_open):
109
            tileset = TileSet("/path/to/tilelist", 0, 3)
110
111
112
113
114
115
116
117
118
            tileset.read()
        self.assertEqual(len(tileset.tileset), 14)

    def test_tileset_add_multiple_overlapping_z_between_maxzoom(self):
        test_input = ["2/0/0", "3/0/0"]
        test_input_file = "\n".join(test_input)

        mock_open = mock.mock_open(read_data=test_input_file)
        with mock.patch("builtins.open", mock_open):
119
            tileset = TileSet("/path/to/tilelist", 0, 3)
120
121
122
123
124
125
126
127
128
            tileset.read()
        self.assertEqual(len(tileset.tileset), 7)

    def test_tileset_add_single_maxzoom(self):
        test_input = ["3/0/0"]
        test_input_file = "\n".join(test_input)

        mock_open = mock.mock_open(read_data=test_input_file)
        with mock.patch("builtins.open", mock_open):
129
            tileset = TileSet("/path/to/tilelist", 0, 3)
130
131
132
133
134
135
136
137
138
            tileset.read()
        self.assertEqual(len(tileset.tileset), 4)

    def test_tileset_add_multiple_maxzoom(self):
        test_input = ["3/0/0", "3/10/10"]
        test_input_file = "\n".join(test_input)

        mock_open = mock.mock_open(read_data=test_input_file)
        with mock.patch("builtins.open", mock_open):
139
            tileset = TileSet("/path/to/tilelist", 0, 3)
140
141
142
143
144
145
146
147
148
            tileset.read()
        self.assertEqual(len(tileset.tileset), 8)

    def test_tileset_add_multiple_overlapping_maxzoom(self):
        test_input = ["3/0/0", "3/1/1"]
        test_input_file = "\n".join(test_input)

        mock_open = mock.mock_open(read_data=test_input_file)
        with mock.patch("builtins.open", mock_open):
149
            tileset = TileSet("/path/to/tilelist", 0, 3)
150
151
            tileset.read()
        self.assertEqual(len(tileset.tileset), 5)