def setup(self): TileCacheTestBase.setup(self) self.gpkg_file = os.path.join(self.cache_dir, 'tmp.gpkg') self.table_name = 'test_tiles' self.cache = GeopackageCache( self.gpkg_file, tile_grid=tile_grid(3857, name='global-webmarcator'), table_name=self.table_name, )
class TestGeopackageCache(object): def setup(self): tg = tile_grid(num_levels=8) self.cache = GeopackageCache("visibleEarth3395.gpkg", tg) def teardown(self): if hasattr(self, 'cache_dir') and os.path.exists(self.cache_dir): shutil.rmtree(self.cache_dir) def test_is_cached_miss(self): assert not self.cache.is_cached(Tile((-1, 0, 4))) def test_is_cached_hit(self): assert self.cache.is_cached(Tile((0, 0, 4))) def test_is_cached_none(self): assert self.cache.is_cached(Tile(None)) def test_load_tile_none(self): assert self.cache.load_tile(Tile(None)) def test_load_tile_not_cached(self): tile = Tile((-1, 0, 4)) assert not self.cache.load_tile(tile) assert tile.source is None assert tile.is_missing() def test_load_tile_cached(self): tile = Tile((0, 0, 4)) assert self.cache.load_tile(tile) == True assert not tile.is_missing() def test_load_tiles_cached(self): tiles = [Tile((0, 0, 1)), Tile((0, 1, 1))] assert self.cache.load_tiles(tiles) assert not tiles[0].is_missing() assert not tiles[1].is_missing() def test_load_tiles_mixed(self): tiles = [Tile(None), Tile((0, 0, 1)), Tile((-1, 0, 1))] assert self.cache.load_tiles(tiles) == False assert not tiles[0].is_missing() assert not tiles[1].is_missing() assert tiles[2].is_missing() def test_load_empty_tileset(self): assert self.cache.load_tiles([Tile(None)]) == True assert self.cache.load_tiles([Tile(None), Tile(None), Tile(None)]) == True def test_load_334_tiles(self): assert_raises(CacheBackendError, self.cache.load_tiles, [Tile((0, 0, 1))] * 334)
def test_bad_config_geopackage_no_gpkg_contents(self, app, base_dir): gpkg_file = base_dir.join("cache.gpkg").strpath table_name = "no_gpkg_contents" with sqlite3.connect(gpkg_file) as db: cur = db.execute( """SELECT name FROM sqlite_master WHERE type='table' AND name=?""", (table_name, ), ) content = cur.fetchone() assert content[0] == table_name with sqlite3.connect(gpkg_file) as db: cur = db.execute( """SELECT table_name FROM gpkg_contents WHERE table_name=?""", (table_name, ), ) content = cur.fetchone() assert not content GeopackageCache(gpkg_file, TileGrid(srs=4326), table_name=table_name) with sqlite3.connect(gpkg_file) as db: cur = db.execute( """SELECT table_name FROM gpkg_contents WHERE table_name=?""", (table_name, ), ) content = cur.fetchone() assert content[0] == table_name
def test_bad_config_geopackage_srs(self): error_msg = None gpkg_file = os.path.join( os.path.join(os.path.dirname(__file__), 'fixture'), 'cache.gpkg') table_name = 'cache' try: GeopackageCache(gpkg_file, TileGrid(srs=4326), table_name) except ValueError as ve: error_msg = ve assert "srs is improperly configured." in str(error_msg)
def test_bad_config_geopackage_no_spatial_ref_sys(self): gpkg_file = os.path.join(test_config['base_dir'], 'cache.gpkg') organization_coordsys_id = 3785 table_name='no_gpkg_spatial_ref_sys' with sqlite3.connect(gpkg_file) as db: cur = db.execute('''SELECT organization_coordsys_id FROM gpkg_spatial_ref_sys WHERE organization_coordsys_id=?''', (organization_coordsys_id,)) content = cur.fetchone() assert not content GeopackageCache(gpkg_file, TileGrid(srs=3785), table_name=table_name) with sqlite3.connect(gpkg_file) as db: cur = db.execute( '''SELECT organization_coordsys_id FROM gpkg_spatial_ref_sys WHERE organization_coordsys_id=?''', (organization_coordsys_id,)) content = cur.fetchone() assert content[0] == organization_coordsys_id
def test_bad_config_geopackage_no_spatial_ref_sys(self, base_dir): gpkg_file = base_dir.join("cache.gpkg").strpath organization_coordsys_id = 3785 table_name = "no_gpkg_spatial_ref_sys" with sqlite3.connect(gpkg_file) as db: cur = db.execute( """SELECT organization_coordsys_id FROM gpkg_spatial_ref_sys WHERE organization_coordsys_id=?""", (organization_coordsys_id, ), ) content = cur.fetchone() assert not content GeopackageCache(gpkg_file, TileGrid(srs=3785), table_name=table_name) with sqlite3.connect(gpkg_file) as db: cur = db.execute( """SELECT organization_coordsys_id FROM gpkg_spatial_ref_sys WHERE organization_coordsys_id=?""", (organization_coordsys_id, ), ) content = cur.fetchone() assert content[0] == organization_coordsys_id
def test_bad_config_geopackage_no_gpkg_contents(self): gpkg_file = os.path.join(test_config['base_dir'], 'cache.gpkg') table_name = 'no_gpkg_contents' with sqlite3.connect(gpkg_file) as db: cur = db.execute('''SELECT name FROM sqlite_master WHERE type='table' AND name=?''', (table_name,)) content = cur.fetchone() assert content[0] == table_name with sqlite3.connect(gpkg_file) as db: cur = db.execute('''SELECT table_name FROM gpkg_contents WHERE table_name=?''', (table_name,)) content = cur.fetchone() assert not content GeopackageCache(gpkg_file, TileGrid(srs=4326), table_name=table_name) with sqlite3.connect(gpkg_file) as db: cur = db.execute('''SELECT table_name FROM gpkg_contents WHERE table_name=?''', (table_name,)) content = cur.fetchone() assert content[0] == table_name
class TestGeopackageCache(TileCacheTestBase): always_loads_metadata = True def setup(self): TileCacheTestBase.setup(self) self.gpkg_file = os.path.join(self.cache_dir, 'tmp.gpkg') self.table_name = 'test_tiles' self.cache = GeopackageCache( self.gpkg_file, tile_grid=tile_grid(3857, name='global-webmarcator'), table_name=self.table_name, ) def teardown(self): if self.cache: self.cache.cleanup() TileCacheTestBase.teardown(self) def test_new_geopackage(self): assert os.path.exists(self.gpkg_file) with sqlite3.connect(self.gpkg_file) as db: cur = db.execute('''SELECT name FROM sqlite_master WHERE type='table' AND name=?''', (self.table_name,)) content = cur.fetchone() assert content[0] == self.table_name with sqlite3.connect(self.gpkg_file) as db: cur = db.execute('''SELECT table_name, data_type FROM gpkg_contents WHERE table_name = ?''', (self.table_name,)) content = cur.fetchone() assert content[0] == self.table_name assert content[1] == 'tiles' with sqlite3.connect(self.gpkg_file) as db: cur = db.execute('''SELECT table_name FROM gpkg_tile_matrix WHERE table_name = ?''', (self.table_name,)) content = cur.fetchall() assert len(content) == 20 with sqlite3.connect(self.gpkg_file) as db: cur = db.execute('''SELECT table_name FROM gpkg_tile_matrix_set WHERE table_name = ?''', (self.table_name,)) content = cur.fetchone() assert content[0] == self.table_name def test_load_empty_tileset(self): assert self.cache.load_tiles([Tile(None)]) == True assert self.cache.load_tiles([Tile(None), Tile(None), Tile(None)]) == True def test_load_more_than_2000_tiles(self): # prepare data for i in range(0, 2010): assert self.cache.store_tile(Tile((i, 0, 10), ImageSource(BytesIO(b'foo')))) tiles = [Tile((i, 0, 10)) for i in range(0, 2010)] assert self.cache.load_tiles(tiles) def test_timeouts(self): self.cache._db_conn_cache.db = sqlite3.connect(self.cache.geopackage_file, timeout=0.05) def block(): # block database by delaying the commit db = sqlite3.connect(self.cache.geopackage_file) cur = db.cursor() stmt = "INSERT OR REPLACE INTO {0} (zoom_level, tile_column, tile_row, tile_data) " \ "VALUES (?,?,?,?)".format(self.table_name) cur.execute(stmt, (3, 1, 1, '1234')) time.sleep(0.2) db.commit() try: assert self.cache.store_tile(self.create_tile((0, 0, 1))) == True t = threading.Thread(target=block) t.start() time.sleep(0.05) assert self.cache.store_tile(self.create_tile((0, 0, 1))) == False finally: t.join() assert self.cache.store_tile(self.create_tile((0, 0, 1))) == True
def setup(self): tg = tile_grid(num_levels=8) self.cache = GeopackageCache("visibleEarth3395.gpkg", tg)