test_tileset.py 5.91 KB
Newer Older
1
from io import StringIO
2
3
from unittest import TestCase, mock

4
from wmfmaps.tileset import Tile, TileSet
5
6
7
8
9
10
11
12
13
14


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
15
16
        t1 = Tile(1, 2, 3)
        t2 = Tile(1, 2, 3)
17
18
        self.assertEqual(hash(t1), hash(t2))

Jgiannelos's avatar
Jgiannelos committed
19

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

30
31
        tileset = TileSet(test_input_file, 0, 15)
        tileset.read()
32

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        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)
54
55
56
57
58
59
60
61
62
63
64

    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))

65
    def test_tileset_add_single_minzoom(self):
66
        test_input = ["0/0/0"]
67
68
69
70
        test_input = "\n".join(test_input)
        test_input_file = StringIO()
        test_input_file.write(test_input)
        test_input_file.seek(0)
71

72
73
        tileset = TileSet(test_input_file, 0, 1)
        tileset.read()
74
75
        self.assertEqual(len(tileset.tileset), 5)

76
    def test_tileset_add_multiple_minzoom(self):
77
        test_input = ["0/0/0", "0/1/1"]
78
79
80
81
        test_input = "\n".join(test_input)
        test_input_file = StringIO()
        test_input_file.write(test_input)
        test_input_file.seek(0)
82

83
84
        tileset = TileSet(test_input_file, 0, 1)
        tileset.read()
85
86
        self.assertEqual(len(tileset.tileset), 10)

87
    def test_tileset_add_multiple_overlapping_minzoom(self):
88
        test_input = ["0/0/0", "1/0/0"]
89
90
91
92
        test_input = "\n".join(test_input)
        test_input_file = StringIO()
        test_input_file.write(test_input)
        test_input_file.seek(0)
93

94
95
        tileset = TileSet(test_input_file, 0, 1)
        tileset.read()
96
        self.assertEqual(len(tileset.tileset), 5)
97
98
99

    def test_tileset_add_single_z_between_maxzoom(self):
        test_input = ["2/0/0"]
100
101
102
103
        test_input = "\n".join(test_input)
        test_input_file = StringIO()
        test_input_file.write(test_input)
        test_input_file.seek(0)
104

105
106
        tileset = TileSet(test_input_file, 0, 3)
        tileset.read()
107
108
109
110
        self.assertEqual(len(tileset.tileset), 7)

    def test_tileset_add_multiple_z_between_maxzoom(self):
        test_input = ["2/0/0", "2/10/10"]
111
112
113
114
        test_input = "\n".join(test_input)
        test_input_file = StringIO()
        test_input_file.write(test_input)
        test_input_file.seek(0)
115

116
117
        tileset = TileSet(test_input_file, 0, 3)
        tileset.read()
118
119
120
121
        self.assertEqual(len(tileset.tileset), 14)

    def test_tileset_add_multiple_overlapping_z_between_maxzoom(self):
        test_input = ["2/0/0", "3/0/0"]
122
123
124
125
        test_input = "\n".join(test_input)
        test_input_file = StringIO()
        test_input_file.write(test_input)
        test_input_file.seek(0)
126

127
128
        tileset = TileSet(test_input_file, 0, 3)
        tileset.read()
129
130
131
132
        self.assertEqual(len(tileset.tileset), 7)

    def test_tileset_add_single_maxzoom(self):
        test_input = ["3/0/0"]
133
134
135
136
        test_input = "\n".join(test_input)
        test_input_file = StringIO()
        test_input_file.write(test_input)
        test_input_file.seek(0)
137

138
139
        tileset = TileSet(test_input_file, 0, 3)
        tileset.read()
140
141
142
143
        self.assertEqual(len(tileset.tileset), 4)

    def test_tileset_add_multiple_maxzoom(self):
        test_input = ["3/0/0", "3/10/10"]
144
145
146
147
        test_input = "\n".join(test_input)
        test_input_file = StringIO()
        test_input_file.write(test_input)
        test_input_file.seek(0)
148

149
150
        tileset = TileSet(test_input_file, 0, 3)
        tileset.read()
151
152
153
154
        self.assertEqual(len(tileset.tileset), 8)

    def test_tileset_add_multiple_overlapping_maxzoom(self):
        test_input = ["3/0/0", "3/1/1"]
155
156
157
158
        test_input = "\n".join(test_input)
        test_input_file = StringIO()
        test_input_file.write(test_input)
        test_input_file.seek(0)
159

160
161
        tileset = TileSet(test_input_file, 0, 3)
        tileset.read()
162
        self.assertEqual(len(tileset.tileset), 5)
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

    def test_tileset_filter_zoom_in_exact_range_z1_to_z3(self):
        test_input = ["0/0/0"]
        test_input = "\n".join(test_input)
        test_input_file = StringIO()
        test_input_file.write(test_input)
        test_input_file.seek(0)

        tileset = TileSet(test_input_file, 1, 3)
        tileset.read()
        self.assertEqual(len(tileset.tileset), 84)

    def test_tileset_filter_zoom_in_exact_range_z5(self):
        test_input = ["0/0/0"]
        test_input = "\n".join(test_input)
        test_input_file = StringIO()
        test_input_file.write(test_input)
        test_input_file.seek(0)

        tileset = TileSet(test_input_file, 5, 5)
        tileset.read()
        self.assertEqual(len(tileset.tileset), 1024)