use std::sync::Arc;
use mln_style::tileset::Scheme;
use super::{answer, fixture};
use crate::storage::constants::{FILE_PROTOCOL, PMTILES_PROTOCOL};
use crate::storage::file_source::FileSource;
use crate::storage::loader::Loader;
use crate::storage::pmtiles_file_source::PMTilesFileSource;
use crate::storage::resource::{Kind, LoadingMethod, Resource, TileFormat};
use crate::storage::resource_options::ResourceOptions;
use crate::storage::response::Reason;
fn fixture_url(file_name: &str) -> String {
format!("{PMTILES_PROTOCOL}{FILE_PROTOCOL}{}", fixture("storage/pmtiles").join(file_name).display())
}
fn pmtiles_file_source() -> PMTilesFileSource {
PMTilesFileSource::new(Arc::new(Loader::new(&ResourceOptions::default(), &super::pool())), super::pool())
}
fn tile(file_name: &str, z: i8) -> Resource {
Resource::tile(&fixture_url(file_name), 1.0, 0, 0, z, Scheme::Xyz, TileFormat::Image, LoadingMethod::ALL)
}
#[test]
fn it_loads_pmtiles_urls() {
let fs = pmtiles_file_source();
assert!(fs.can_request(&Resource::style("pmtiles:///test")));
for url in ["pmtile://test", "pmtiles:", ""] {
assert!(!fs.can_request(&Resource::style(url)), "{url}");
}
}
#[test]
fn an_archive_answers_with_its_tilejson() {
let res = answer(&pmtiles_file_source(), &Resource::new(Kind::Unknown, fixture_url("geography-class-png.pmtiles")));
assert_eq!(None, res.error);
let tilejson: serde_json::Value = serde_json::from_slice(&res.data.expect("data")).expect("JSON");
assert!(tilejson["tiles"][0].as_str().expect("tile URL").ends_with("geography-class-png.pmtiles"));
assert_eq!("xyz", tilejson["scheme"]);
assert_eq!((0, 1), (tilejson["minzoom"].as_i64().unwrap(), tilejson["maxzoom"].as_i64().unwrap()));
assert_eq!(serde_json::json!([-180.0, -85.0511, 180.0, 85.0511]), tilejson["bounds"]);
assert_eq!("TileMill", tilejson["attribution"]);
}
#[test]
fn a_tile_answers_with_its_data_or_no_content() {
let fs = pmtiles_file_source();
for (file_name, z, has_data) in [
("geography-class-png.pmtiles", 0, true),
("geography-class-png.pmtiles", 4, false),
("uncompressed-tiles.pmtiles", 0, true),
] {
let res = answer(&fs, &tile(file_name, z));
assert_eq!(None, res.error, "{file_name} {z}");
assert_eq!((has_data, !has_data), (res.data.is_some(), res.no_content), "{file_name} {z}");
}
}
#[test]
fn an_archive_it_cannot_read_is_an_error() {
let cases = [
(Resource::new(Kind::Unknown, fixture_url("does_not_exist")), Reason::NotFound, "Cannot find file"),
(tile("corrupt-gzip-tile.pmtiles", 0), Reason::Other, "Error decompressing PMTiles tile:"),
(
Resource::new(Kind::Unknown, fixture_url("corrupt-gzip-metadata.pmtiles")),
Reason::Other,
"Error decompressing PMTiles metadata:",
),
];
let fs = pmtiles_file_source();
for (resource, reason, message) in cases {
let res = answer(&fs, &resource);
let error = res.error.as_ref().unwrap_or_else(|| panic!("{}: {res:?}", resource.url));
assert_eq!(reason, error.reason, "{}", resource.url);
assert!(error.message.contains(message), "{}", error.message);
}
}