Beispiel #1
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")
Beispiel #2
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")
Beispiel #3
0
 def test_read_subdir_versions(self) -> None:
     fs = MemoryFS()
     d = read_subdir_versions_from_directory(fs, "/")
     self.assertEqual({}, d)
     fs.writetext(SUBDIR_VERSIONS_FILENAME, self.txt)
     d = read_subdir_versions_from_directory(fs, "/")
     self.assertEqual(self.d, d)
     fs.close()
Beispiel #4
0
    def test_get_set_items(self) -> None:
        no_lidvids: Set[LIDVID] = set()

        # Set empty contents
        mv = Multiversioned(MemoryFS())
        empty_lidvid = LIDVID("urn:nasa:pds:empty-bundle::3.14")
        empty_contents = VersionContents.create_from_lidvids(
            no_lidvids, MemoryFS(), set())
        mv[empty_lidvid] = empty_contents
        self.assertTrue(empty_lidvid in mv)
        self.assertEqual(1, len(mv))
        self.assertEqual(empty_contents, mv[empty_lidvid])

        # TODO Is IndexError the right exception to raise?
        with self.assertRaises(IndexError):
            mv[empty_lidvid] = empty_contents

        # Set contents with a single file down a long path
        single_file_lidvid = LIDVID("urn:nasa:pds:single-file::3.14")
        single_file_fs = MemoryFS()
        single_file_path = "/down/a/lot/of/dirs/text.txt"
        single_file_fs.makedirs(fs.path.dirname(single_file_path), None, True)
        single_file_fs.writetext(single_file_path, "Hello, there!")
        single_file_contents = VersionContents.create_from_lidvids(
            no_lidvids, single_file_fs, set([single_file_path]))
        mv[single_file_lidvid] = single_file_contents
        self.assertTrue(empty_lidvid in mv)
        self.assertTrue(single_file_lidvid in mv)
        print("****", set(mv.lidvids()))
        self.assertEqual(2, len(mv))
        self.assertEqual(single_file_contents, mv[single_file_lidvid])

        # Test that LIDVIDs get put correctly into the
        # subdir$version.txts.
        hierarchic = Multiversioned(MemoryFS())
        b_lidvid = LIDVID("urn:nasa:pds:b::1.5")
        c_lidvid = LIDVID("urn:nasa:pds:b:c::2.5")
        p_lidvid = LIDVID("urn:nasa:pds:b:c:p::333.123")

        p_contents = VersionContents.create_from_lidvids(
            no_lidvids, MemoryFS(), set([]))
        hierarchic[p_lidvid] = p_contents
        self.assertEqual(p_contents, hierarchic[p_lidvid])

        c_contents = VersionContents.create_from_lidvids(
            set([p_lidvid]), MemoryFS(), set([]))
        hierarchic[c_lidvid] = c_contents
        self.assertEqual(c_contents, hierarchic[c_lidvid])

        b_contents = VersionContents.create_from_lidvids(
            set([c_lidvid]), MemoryFS(), set([]))
        hierarchic[b_lidvid] = b_contents
        self.assertEqual(b_contents, hierarchic[b_lidvid])

        self.assertEqual(3, len(hierarchic))
        self.assertEqual({p_lidvid, c_lidvid, b_lidvid}, hierarchic.lidvids())
Beispiel #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")
Beispiel #6
0
class StandardLayoutMixin(object):
    locales = ["en"]

    def setUp(self):
        super(StandardLayoutMixin, self).setUp()
        self.runner = CliRunner()
        self.locales_fs = MemoryFS()
        self.output_fs = MemoryFS()

        def get_locales_fs(path):
            return self.locales_fs.opendir(path)

        def get_output_fs(path):
            return self.output_fs.opendir(path)

        self.locales_fs_patcher = mock.patch('elm_fluent.cli.get_locales_fs',
                                             new=get_locales_fs)
        self.locales_fs_patcher.start()
        self.output_fs_patcher = mock.patch('elm_fluent.cli.get_output_fs',
                                            new=get_output_fs)
        self.output_fs_patcher.start()
        self.setup_fs()

    def tearDown(self):
        self.locales_fs_patcher.stop()
        super(StandardLayoutMixin, self).tearDown()

    def setup_fs(self):
        sub = self.locales_fs.makedir("locales")
        for l in self.locales:
            sub.makedir(l)

    def write_ftl_file(self, path, contents):
        self.locales_fs.writetext(path, dedent_ftl(contents))

    def get_all_files(self, fs):
        return {p: fs.readtext(p) for p in fs.walk.files()}

    def assertFileSystemEquals(self, fs, files):
        all_files = self.get_all_files(fs)
        self.assertEqual({p: c.rstrip()
                          for p, c in all_files.items()},
                         {p: c.rstrip()
                          for p, c in files.items()})

    def run_main(self, args=None):
        result = self.runner.invoke(cli.main,
                                    args=[] if args is None else args)
        if result.exception is not None and not isinstance(
                result.exception, SystemExit):
            exc_info = result.exc_info
            raise exc_info[0].with_traceback(exc_info[1], exc_info[2])
        return result
Beispiel #7
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))
Beispiel #8
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"]))
Beispiel #9
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)
Beispiel #10
0
    def test_listdir(self) -> None:
        fs = MemoryFS()
        fs.makedirs("/b$")
        fs.makedirs("/b$/dir1")
        fs.makedirs("/b$/dir2")
        fs.writetext("/b$/file1.txt", "file1")
        fs.writetext("/b$/file2.txt", "file2")
        fs.writetext("/b$/dir1/file1.txt", "file1")
        fs.writetext("/b$/dir1/file2.txt", "file2")
        fs.writetext("/b$/dir2/file1.txt", "file1")
        fs.writetext("/b$/dir2/file2.txt", "file2")

        c = COWFS(fs)
        path = "/b$/dir1/file2.txt"
        c.writetext(path, "xxxx")

        # Now the COW version is different.  But it should still have
        # the old unchanged files.

        self.assertTrue(c.exists("/b$/dir1/file1.txt"))  # Yes, but...
        self.assertEqual({"dir1", "dir2", "file1.txt", "file2.txt"},
                         set(c.listdir("/b$")))
Beispiel #11
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))