Skip to content

Commit

Permalink
Fix martin integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Oct 19, 2023
1 parent aadd20c commit 806c6dc
Show file tree
Hide file tree
Showing 15 changed files with 549 additions and 231 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ env_logger = "0.10"
flate2 = "1"
futures = "0.3"
indoc = "2"
insta = { version = "1", features = ["toml"] }
insta = "1"
itertools = "0.11"
json-patch = "1.2"
log = "0.4"
Expand Down
9 changes: 7 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

set shell := ["bash", "-c"]

#export DATABASE_URL="postgres://postgres:postgres@localhost:5411/db"
export PGPORT := "5411"
export DATABASE_URL := "postgres://postgres:postgres@localhost:" + PGPORT + "/db"
export CARGO_TERM_COLOR := "always"
Expand Down Expand Up @@ -138,18 +139,22 @@ test-int: clean-test install-sqlx
fi
# Run integration tests and save its output as the new expected output
bless: restart clean-test bless-insta
bless: restart clean-test bless-insta-martin bless-insta-mbtiles
rm -rf tests/temp
cargo test -p martin --features bless-tests
tests/test.sh
rm -rf tests/expected
mv tests/output tests/expected

# Run integration tests and save its output as the new expected output
bless-insta *ARGS: (cargo-install "insta" "cargo-insta")
bless-insta-mbtiles *ARGS: (cargo-install "insta" "cargo-insta")
#rm -rf martin-mbtiles/tests/snapshots
cargo insta test --accept --unreferenced=auto -p martin-mbtiles {{ ARGS }}

# Run integration tests and save its output as the new expected output
bless-insta-martin *ARGS: (cargo-install "insta" "cargo-insta")
cargo insta test --accept --unreferenced=auto -p martin {{ ARGS }}

# Build and open mdbook documentation
book: (cargo-install "mdbook")
mdbook serve docs --open --port 8321
Expand Down
2 changes: 1 addition & 1 deletion martin-mbtiles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tokio = { workspace = true, features = ["rt-multi-thread"], optional = true }
actix-rt.workspace = true
ctor.workspace = true
env_logger.workspace = true
insta.workspace = true
insta = { workspace = true, features = ["toml"] }
pretty_assertions.workspace = true
rstest.workspace = true

Expand Down
1 change: 1 addition & 0 deletions martin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ cargo-husky.workspace = true
criterion.workspace = true
ctor.workspace = true
indoc.workspace = true
insta = { workspace = true, features = ["json"] }
#test-log = "0.2"

[[bench]]
Expand Down
2 changes: 1 addition & 1 deletion martin/src/srv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ mod config;
mod server;

pub use config::{SrvConfig, KEEP_ALIVE_DEFAULT, LISTEN_ADDRESSES_DEFAULT};
pub use server::{new_server, router, RESERVED_KEYWORDS};
pub use server::{new_server, router, Catalog, RESERVED_KEYWORDS};

pub use crate::source::CatalogSourceEntry;
23 changes: 14 additions & 9 deletions martin/src/srv/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::sprites::{SpriteCatalog, SpriteError, SpriteSources};
use crate::srv::config::{SrvConfig, KEEP_ALIVE_DEFAULT, LISTEN_ADDRESSES_DEFAULT};
use crate::utils::{decode_brotli, decode_gzip, encode_brotli, encode_gzip};
use crate::Error::BindingError;
use crate::Xyz;
use crate::{Error, Xyz};

/// List of keywords that cannot be used as source IDs. Some of these are reserved for future use.
/// Reserved keywords must never end in a "dot number" (e.g. ".1").
Expand All @@ -51,6 +51,15 @@ pub struct Catalog {
pub sprites: SpriteCatalog,
}

impl Catalog {
pub fn new(state: &ServerState) -> Result<Self, Error> {
Ok(Self {
tiles: state.tiles.get_catalog(),
sprites: state.sprites.get_catalog()?,
})
}
}

#[derive(Deserialize)]
struct TileJsonRequest {
source_ids: String,
Expand Down Expand Up @@ -421,26 +430,22 @@ pub fn router(cfg: &mut web::ServiceConfig) {
}

/// Create a new initialized Actix `App` instance together with the listening address.
pub fn new_server(config: SrvConfig, all_sources: ServerState) -> crate::Result<(Server, String)> {
pub fn new_server(config: SrvConfig, state: ServerState) -> crate::Result<(Server, String)> {
let catalog = Catalog::new(&state)?;
let keep_alive = Duration::from_secs(config.keep_alive.unwrap_or(KEEP_ALIVE_DEFAULT));
let worker_processes = config.worker_processes.unwrap_or_else(num_cpus::get);
let listen_addresses = config
.listen_addresses
.unwrap_or_else(|| LISTEN_ADDRESSES_DEFAULT.to_owned());

let catalog = Catalog {
tiles: all_sources.tiles.get_catalog(),
sprites: all_sources.sprites.get_catalog()?,
};

let server = HttpServer::new(move || {
let cors_middleware = Cors::default()
.allow_any_origin()
.allowed_methods(vec!["GET"]);

App::new()
.app_data(Data::new(all_sources.tiles.clone()))
.app_data(Data::new(all_sources.sprites.clone()))
.app_data(Data::new(state.tiles.clone()))
.app_data(Data::new(state.sprites.clone()))
.app_data(Data::new(catalog.clone()))
.wrap(cors_middleware)
.wrap(middleware::NormalizePath::new(TrailingSlash::MergeOnly))
Expand Down
81 changes: 64 additions & 17 deletions tests/mb_server_test.rs → martin/tests/mb_server_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use actix_web::http::header::{ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_TYPE};
use actix_web::test::{call_service, read_body, read_body_json, TestRequest};
use ctor::ctor;
use indoc::indoc;
use insta::assert_json_snapshot;
use martin::decode_gzip;
use martin::srv::IndexEntry;
use tilejson::TileJSON;

pub mod utils;
Expand All @@ -16,11 +16,13 @@ fn init() {

macro_rules! create_app {
($sources:expr) => {{
let sources = mock_sources(mock_cfg($sources)).await.0;
let state = crate::utils::mock_app_data(sources).await;
let state = mock_sources(mock_cfg($sources)).await.0;
::actix_web::test::init_service(
::actix_web::App::new()
.app_data(state)
.app_data(actix_web::web::Data::new(
::martin::srv::Catalog::new(&state).unwrap(),
))
.app_data(actix_web::web::Data::new(state.tiles))
.configure(::martin::srv::router),
)
.await
Expand All @@ -34,10 +36,10 @@ fn test_get(path: &str) -> TestRequest {
const CONFIG: &str = indoc! {"
mbtiles:
sources:
m_json: tests/fixtures/mbtiles/json.mbtiles
m_mvt: tests/fixtures/mbtiles/world_cities.mbtiles
m_raw_mvt: tests/fixtures/mbtiles/uncompressed_mvt.mbtiles
m_webp: tests/fixtures/mbtiles/webp.mbtiles
m_json: ../tests/fixtures/mbtiles/json.mbtiles
m_mvt: ../tests/fixtures/mbtiles/world_cities.mbtiles
m_raw_mvt: ../tests/fixtures/mbtiles/uncompressed_mvt.mbtiles
m_webp: ../tests/fixtures/mbtiles/webp.mbtiles
"};

#[actix_rt::test]
Expand All @@ -47,11 +49,33 @@ async fn mbt_get_catalog() {
let req = test_get("/catalog").to_request();
let response = call_service(&app, req).await;
assert!(response.status().is_success());
let body = read_body(response).await;
let sources: Vec<IndexEntry> = serde_json::from_slice(&body).unwrap();
assert_eq!(sources.iter().filter(|v| v.id == "m_mvt").count(), 1);
assert_eq!(sources.iter().filter(|v| v.id == "m_webp").count(), 1);
assert_eq!(sources.iter().filter(|v| v.id == "m_raw_mvt").count(), 1);
let body: serde_json::Value = read_body_json(response).await;
assert_json_snapshot!(body, @r###"
{
"tiles": {
"m_json": {
"content_type": "application/json",
"name": "Dummy json data"
},
"m_mvt": {
"content_type": "application/x-protobuf",
"content_encoding": "gzip",
"name": "Major cities from Natural Earth data",
"description": "Major cities from Natural Earth data"
},
"m_raw_mvt": {
"content_type": "application/x-protobuf",
"name": "Major cities from Natural Earth data",
"description": "Major cities from Natural Earth data"
},
"m_webp": {
"content_type": "image/webp",
"name": "ne2sr"
}
},
"sprites": {}
}
"###);
}

#[actix_rt::test]
Expand All @@ -62,10 +86,33 @@ async fn mbt_get_catalog_gzip() {
let response = call_service(&app, req).await;
assert!(response.status().is_success());
let body = decode_gzip(&read_body(response).await).unwrap();
let sources: Vec<IndexEntry> = serde_json::from_slice(&body).unwrap();
assert_eq!(sources.iter().filter(|v| v.id == "m_mvt").count(), 1);
assert_eq!(sources.iter().filter(|v| v.id == "m_webp").count(), 1);
assert_eq!(sources.iter().filter(|v| v.id == "m_raw_mvt").count(), 1);
let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_json_snapshot!(body, @r###"
{
"tiles": {
"m_json": {
"content_type": "application/json",
"name": "Dummy json data"
},
"m_mvt": {
"content_type": "application/x-protobuf",
"content_encoding": "gzip",
"name": "Major cities from Natural Earth data",
"description": "Major cities from Natural Earth data"
},
"m_raw_mvt": {
"content_type": "application/x-protobuf",
"name": "Major cities from Natural Earth data",
"description": "Major cities from Natural Earth data"
},
"m_webp": {
"content_type": "image/webp",
"name": "ne2sr"
}
},
"sprites": {}
}
"###);
}

#[actix_rt::test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ctor::ctor;
use indoc::indoc;
use itertools::Itertools;
use insta::assert_json_snapshot;
use martin::Xyz;

pub mod utils;
Expand All @@ -14,18 +14,17 @@ fn init() {
#[actix_rt::test]
async fn function_source_tilejson() {
let mock = mock_sources(mock_pgcfg("connection_string: $DATABASE_URL")).await;
assert_eq!(
source(&mock, "function_zxy_query").get_tilejson(),
serde_json::from_str(indoc! {r#"
{
"name": "function_zxy_query",
"description": "public.function_zxy_query",
"tilejson": "3.0.0",
"tiles": []
}
"#})
.unwrap()
);
let tj = source(&mock, "function_zxy_query").get_tilejson();
assert_json_snapshot!(tj, @r###"
{
"tilejson": "3.0.0",
"tiles": [],
"name": "function_zxy_query",
"foo": {
"bar": "foo"
}
}
"###);
}

#[actix_rt::test]
Expand Down Expand Up @@ -55,9 +54,13 @@ async fn function_source_schemas() {
functions:
from_schemas: MixedCase
"});
let sources = mock_sources(cfg).await.0;
assert_eq!(
sources.keys().sorted().collect::<Vec<_>>(),
vec!["function_Mixed_Name"],
);
let sources = mock_sources(cfg).await.0.tiles;
assert_json_snapshot!(sources.get_catalog(), @r###"
{
"function_Mixed_Name": {
"content_type": "application/x-protobuf",
"description": "a function source with MixedCase name"
}
}
"###);
}
Loading

0 comments on commit 806c6dc

Please sign in to comment.