Пример #1
0
    def test_constructor(self):
        # TODO: Construct a layers object

        fs = MemoryFS()
        fs.writetext('1.sql', 'select 1')
        fs.writetext('2.sql', 'select 2')
        layers = [
            Layer(
                "water", {
                    "fields": {},
                    "description":
                    "Waterbody and ocean areas",
                    "sql": [{
                        "minzoom": 0,
                        "maxzoom": 4,
                        "file": "1.sql"
                    }, {
                        "minzoom": 5,
                        "maxzoom": 8,
                        "file": "2.sql"
                    }]
                }, fs)
        ]
        gen = Generator("v1", layers, {"port": 1234}, "dir")

        self.assertEqual(gen.id, "v1")
        self.assertEqual(gen.layers, layers)
        self.assertEqual(gen.dbconn, {"port": 1234})
        self.assertEqual(gen.storage_location, "dir")
Пример #2
0
    def test_write_tiles(self, mock_open, mock_connect, mock_db):
        fs = MemoryFS()
        fs.writetext('1.sql', 'select 1')
        layers = [
            Layer(
                "water", {
                    "fields": {},
                    "description": "Waterbody and ocean areas",
                    "sql": [{
                        "minzoom": 0,
                        "maxzoom": 4,
                        "file": "1.sql"
                    }]
                }, fs)
        ]

        out_fs = MemoryFS()

        mock_open.return_value = out_fs
        mock_connect.return_value = None
        mock_db.return_value = MockDB()

        gen = Generator("v1", layers, {"port": 1234}, "storage")

        gen.write_tiles([(0, 0, 0)])

        self.assertEqual(out_fs.openbin("v1/0/0/0.mvt").read(), b"result")
Пример #3
0
    def test_render_tile(self):
        fs = MemoryFS()
        fs.writetext('1.sql', 'select 1')
        fs.writetext('2.sql', 'select 2')
        layer = Layer("water",
                      {"fields": {},
                       "description": "Waterbody and ocean areas",
                       "sql": [{"minzoom": 0, "maxzoom": 4, "file": "1.sql"},
                               {"minzoom": 5, "maxzoom": 8, "file": "2.sql"}]
                       }, fs)

        db = Database(Mock())
        db.generate_tilelayer = Mock(return_value=b'foo')

        layer.render_tile((0, 0, 0), db)
        db.generate_tilelayer.assert_called_with(layer.definition_for_zoom(0),
                                                 (0, 0, 0))
Пример #4
0
    def test_geometry_type(self):
        fs = MemoryFS()
        fs.writetext('1.sql', 'select 1')
        layer = Layer("water",
                      {"geometry_type": ["polygon"],
                       "description": "Waterbody and ocean areas",
                       "sql": [{"minzoom": 0, "maxzoom": 4, "file": "1.sql"}]
                       }, fs)

        self.assertEqual(layer.geometry_type, set(["polygon"]))

        layer = Layer("water",
                      {"geometry_type": ["polygon", "point"],
                       "description": "Waterbody and ocean areas",
                       "sql": [{"minzoom": 0, "maxzoom": 4, "file": "1.sql"}]
                       }, fs)

        self.assertEqual(layer.geometry_type, set(["polygon", "point"]))
Пример #5
0
    def test_fields(self):
        fs = MemoryFS()
        fs.writetext('1.sql', 'select 1')
        layer = Layer("water",
                      {"fields": {"water": "type of water"},
                       "description": "Waterbody and ocean areas",
                       "sql": [{"minzoom": 0, "maxzoom": 4, "file": "1.sql"}]
                       }, fs)

        self.assertEqual(layer.fields["water"], "type of water")
Пример #6
0
    def test_definition_for_zoom(self):
        fs = MemoryFS()
        fs.writetext('1.sql', 'select 1')
        fs.writetext('2.sql', 'select 2')
        layer = Layer("water",
                      {"fields": {},
                       "description": "Waterbody and ocean areas",
                       "sql": [{"minzoom": 0, "maxzoom": 4, "file": "1.sql"},
                               {"minzoom": 5, "maxzoom": 8, "file": "2.sql"}]
                       }, fs)

        for i in range(0, 5):
            self.assertEqual(layer.definition_for_zoom(i),
                             Definition("water", "select 1", 0, 4, None))

        for i in range(5, 9):
            self.assertEqual(layer.definition_for_zoom(i),
                             Definition("water", "select 2", 5, 8, None))

        self.assertIsNone(layer.definition_for_zoom(9), None)
Пример #7
0
    def test_properties(self):
        with MemoryFS() as fs:
            c = Config('''{"metadata": {"id":"v1"}}''', fs)
            self.assertEqual(c.id, "v1")
            self.assertEqual(c.name, None)
            self.assertEqual(c.description, None)
            self.assertEqual(c.attribution, None)
            self.assertEqual(c.version, None)
            self.assertEqual(c.bounds, None)
            self.assertEqual(c.center, None)

        with MemoryFS() as fs:
            c_str = ('''{"metadata": {"id":"v1", "name": "name", '''
                     '''"description":"description", '''
                     '''"attribution":"attribution", "version": "1.0.0",'''
                     '''"bounds": [-180, -85, 180, 85], "center": [0, 0]},'''
                     '''"vector_layers": {"building":{'''
                     '''"description": "buildings",'''
                     '''"fields":{}}}}''')

            # Check the test is valid yaml to save debugging
            yaml.safe_load(c_str)
            c = Config(c_str, fs)
            self.assertEqual(c.id, "v1")
            self.assertEqual(c.name, "name")
            self.assertEqual(c.description, "description")
            self.assertEqual(c.attribution, "attribution")
            self.assertEqual(c.version, "1.0.0")
            self.assertEqual(c.bounds, [-180, -85, 180, 85])
            self.assertEqual(c.center, [0, 0])
            self.assertEqual(len(c.layers), 1)
            self.assertEqual(
                c.layers[0],
                Layer("building", {
                    "description": "buildings",
                    "fields": {}
                }, fs))
Пример #8
0
    def test_equals(self):
        fs1 = MemoryFS()
        fs1.writetext('foo.sql', 'select 1')
        fs2 = MemoryFS()
        fs2.writetext('foo.sql', 'select 2')
        fs2.writetext('bar.sql', 'select 1')

        self.assertEqual(Layer("water",
                               {"fields": {"water": "type of water"},
                                "geometry_type": ["polygon"],
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1),
                         Layer("water",
                               {"fields": {"water": "type of water"},
                                "geometry_type": ["polygon"],
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1))

        # Only file contents should matter
        self.assertEqual(Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1),
                         Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "bar.sql"}]
                                }, fs2))

        # Order of geometry_type doesn't matter
        self.assertEqual(Layer("water",
                               {"fields": {"water": "type of water"},
                                "geometry_type": ["point", "polygon"],
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1),
                         Layer("water",
                               {"fields": {"water": "type of water"},
                                "geometry_type": ["polygon", "point"],
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1))

        # id
        self.assertFalse(Layer("land",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1) ==
                         Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1))

        # fields
        self.assertFalse(Layer("water",
                               {"fields": {"water": "type of land"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1) ==
                         Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1))
        self.assertFalse(Layer("water",
                               {"fields": {"land": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1) ==
                         Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1))

        # geometry_type
        self.assertFalse(Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "geometry_type": ["polygon"],
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1) ==
                         Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "geometry_type": ["point"],
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1))

        self.assertFalse(Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "geometry_type": ["polygon", "point"],
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1) ==
                         Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "geometry_type": ["point"],
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1))

        # zooms
        self.assertFalse(Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 2,
                                         "file": "foo.sql"}]
                                }, fs1) ==
                         Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1))
        self.assertFalse(Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 2, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1) ==
                         Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1))

        # sql
        self.assertFalse(Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs1) ==
                         Layer("water",
                               {"fields": {"water": "type of water"},
                                "description": "Waterbody and ocean areas",
                                "sql": [{"minzoom": 0, "maxzoom": 4,
                                         "file": "foo.sql"}]
                                }, fs2))