Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Oct 5, 2023
1 parent 3f058f3 commit 9d50970
Show file tree
Hide file tree
Showing 19 changed files with 204 additions and 32 deletions.
48 changes: 16 additions & 32 deletions martin-mbtiles/tests/mbtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use std::str::from_utf8;
use ctor::ctor;
use martin_mbtiles::MbtType::{Flat, FlatWithHash, Normalized};
use martin_mbtiles::{apply_diff, create_flat_tables, MbtResult, MbtType, Mbtiles, MbtilesCopier};
use pretty_assertions::assert_eq as pretty_assert_eq;
use rstest::{fixture, rstest};
use serde::Serialize;
use sqlx::{query, query_as, Executor as _, Row, SqliteConnection};

use pretty_assertions::assert_eq as pretty_assert_eq;
const TILES_V1: &str = "
INSERT INTO tiles (zoom_level, tile_column, tile_row, tile_data) VALUES
(5, 0, 0, cast('same' as blob))
Expand Down Expand Up @@ -139,42 +138,26 @@ type Databases = HashMap<(&'static str, MbtType), Vec<SqliteEntry>>;
#[once]
fn databases() -> Databases {
futures::executor::block_on(async {
let mem = Mbtiles::new(":memory:").unwrap();
let mut result = HashMap::new();
for &typ in &[Flat, FlatWithHash, Normalized] {
let (_, mut cn) =
let (mbt, mut cn) =
new_file_no_hash!(databases, typ, METADATA_V1, TILES_V1, "v1-no-hash-{typ}");
result.insert(("v1_no_hash", typ), dump(&mut cn).await.unwrap());
let dmp = assert_dump!(&mut cn, "v1-no-hash__{typ}");
result.insert(("v1_no_hash", typ), dmp);

let (_, mut cn) = new_file!(databases, typ, METADATA_V1, TILES_V1, "v1-{typ}");
result.insert(("v1", typ), dump(&mut cn).await.unwrap());
let mut cn = copier(&mbt, &mem).run().await.unwrap();
let dmp = assert_dump!(&mut cn, "v1__{typ}");
result.insert(("v1", typ), dmp);

let (_, mut cn) = new_file!(databases, typ, METADATA_V2, TILES_V2, "v2-{typ}");
result.insert(("v2", typ), dump(&mut cn).await.unwrap());
let dmp = assert_dump!(&mut cn, "v2__{typ}");
result.insert(("v2", typ), dmp);
}
result
})
}

#[rstest]
#[actix_rt::test]
async fn copy(
#[values(Flat, FlatWithHash, Normalized)] frm_type: MbtType,
databases: &Databases,
) -> MbtResult<()> {
let frm = shorten(frm_type);
let mem = Mbtiles::new(":memory:")?;
let (frm_mbt, mut frm_cn) = new_file_no_hash!(copy, frm_type, METADATA_V1, TILES_V1, "{frm}");

let dmp = assert_dump!(&mut frm_cn, "v1_orig__{frm}");
pretty_assert_eq!(databases.get(&("v1_no_hash", frm_type)).unwrap(), &dmp);

let opt = copier(&frm_mbt, &mem);
let dmp = assert_dump!(&mut opt.run().await?, "v1_cp__{frm}");
pretty_assert_eq!(databases.get(&("v1", frm_type)).unwrap(), &dmp);

Ok(())
}

#[rstest]
#[trace]
#[actix_rt::test]
Expand All @@ -196,7 +179,7 @@ async fn convert(
opt.dst_type = Some(dst_type);
opt.zoom_levels.insert(6);
let z6only = dump(&mut opt.run().await?).await?;
assert_snapshot!(z6only, "v1_z2__{frm}-{to}");
assert_snapshot!(z6only, "v1__z6__{frm}-{to}");

let mut opt = copier(&frm_mbt, &mem);
opt.dst_type = Some(dst_type);
Expand Down Expand Up @@ -245,17 +228,18 @@ async fn diff_apply(
let (tar1_mbt, mut tar1_cn) = new_file! {diff_apply, *target_type, METADATA_V1, TILES_V1, "apply__{src}@{dif}={dst}__to__{tar}-v1"};
apply_diff(path(&tar1_mbt), path(&dst_mbt)).await?;
let dmp = dump(&mut tar1_cn).await?;
// pretty_assert_eq!(databases.get(&("v2", *target_type)).unwrap(), &dmp);
// pretty_assert_eq!(&dmp, expected_v2);
if &dmp != expected_v2 {
assert_snapshot!(dmp, "v2_applied__{src}@{dif}={dst}__to__{tar}__bad_from_v1");
}

let (tar2_mbt, mut tar2_cn) = new_file! {diff_apply, *target_type, METADATA_V2, TILES_V2, "apply__{src}@{dif}={dst}__to__{tar}-v2"};
apply_diff(path(&tar2_mbt), path(&dst_mbt)).await?;
let dmp = dump(&mut tar2_cn).await?;
if &dmp != expected_v2 {
assert_snapshot!(dmp, "v2_applied__{src}@{dif}={dst}__to__{tar}__bad_from_v2");
}
pretty_assert_eq!(&dmp, expected_v2);
// if &dmp != expected_v2 {
// assert_snapshot!(dmp, "v2_applied__{src}@{dif}={dst}__to__{tar}__bad_from_v2");
// }
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
source: martin-mbtiles/tests/mbtiles.rs
expression: actual_value
---
[[]]
type = 'table'
tbl_name = 'metadata'
sql = '''
CREATE TABLE metadata (
name text NOT NULL PRIMARY KEY,
value text)'''
values = [
'( "agg_tiles_hash", "5364C57282B73C341D5D801B8F7675C4" )',
'( "md-edit", "value - v2" )',
'( "md-new", "value - new" )',
'( "md-same", "value - same" )',
]

[[]]
type = 'table'
tbl_name = 'tiles_with_hash'
sql = '''
CREATE TABLE tiles_with_hash (
zoom_level integer NOT NULL,
tile_column integer NOT NULL,
tile_row integer NOT NULL,
tile_data blob,
tile_hash text,
PRIMARY KEY(zoom_level, tile_column, tile_row))'''
values = [
'( 5, 0, 0, blob(same), "51037A4A37730F52C8732586D3AAA316" )',
'( 5, 0, 1, blob(edit-v2), "FF76830FF90D79BB335884F256031731" )',
'( 5, 1, 0, blob(new), "22AF645D1859CB5CA6DA0C484F1F37EA" )',
'( 6, 0, 0, blob(same), "51037A4A37730F52C8732586D3AAA316" )',
'( 6, 0, 1, blob(edit-v2a), "03132BFACDB00CC63D6B7DD98D974DD5" )',
'( 6, 1, 4, blob(one-keep-one-remove), "EFC416C7C53A81E7363BE6C1A389131A" )',
'( 6, 1, 6, blob(new), "22AF645D1859CB5CA6DA0C484F1F37EA" )',
]

[[]]
type = 'index'
tbl_name = 'metadata'

[[]]
type = 'index'
tbl_name = 'tiles_with_hash'

[[]]
type = 'view'
tbl_name = 'tiles'
sql = '''
CREATE VIEW tiles AS
SELECT zoom_level, tile_column, tile_row, tile_data FROM tiles_with_hash'''
45 changes: 45 additions & 0 deletions martin-mbtiles/tests/snapshots/mbtiles__databases@v2__flat.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
source: martin-mbtiles/tests/mbtiles.rs
expression: actual_value
---
[[]]
type = 'table'
tbl_name = 'metadata'
sql = '''
CREATE TABLE metadata (
name text NOT NULL PRIMARY KEY,
value text)'''
values = [
'( "agg_tiles_hash", "5364C57282B73C341D5D801B8F7675C4" )',
'( "md-edit", "value - v2" )',
'( "md-new", "value - new" )',
'( "md-same", "value - same" )',
]

[[]]
type = 'table'
tbl_name = 'tiles'
sql = '''
CREATE TABLE tiles (
zoom_level integer NOT NULL,
tile_column integer NOT NULL,
tile_row integer NOT NULL,
tile_data blob,
PRIMARY KEY(zoom_level, tile_column, tile_row))'''
values = [
'( 5, 0, 0, blob(same) )',
'( 5, 0, 1, blob(edit-v2) )',
'( 5, 1, 0, blob(new) )',
'( 6, 0, 0, blob(same) )',
'( 6, 0, 1, blob(edit-v2a) )',
'( 6, 1, 4, blob(one-keep-one-remove) )',
'( 6, 1, 6, blob(new) )',
]

[[]]
type = 'index'
tbl_name = 'metadata'

[[]]
type = 'index'
tbl_name = 'tiles'
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
source: martin-mbtiles/tests/mbtiles.rs
expression: actual_value
---
[[]]
type = 'table'
tbl_name = 'images'
sql = '''
CREATE TABLE images (
tile_id text NOT NULL PRIMARY KEY,
tile_data blob)'''
values = [
'( "03132BFACDB00CC63D6B7DD98D974DD5", blob(edit-v2a) )',
'( "22AF645D1859CB5CA6DA0C484F1F37EA", blob(new) )',
'( "51037A4A37730F52C8732586D3AAA316", blob(same) )',
'( "EFC416C7C53A81E7363BE6C1A389131A", blob(one-keep-one-remove) )',
'( "FF76830FF90D79BB335884F256031731", blob(edit-v2) )',
]

[[]]
type = 'table'
tbl_name = 'map'
sql = '''
CREATE TABLE map (
zoom_level integer NOT NULL,
tile_column integer NOT NULL,
tile_row integer NOT NULL,
tile_id text,
PRIMARY KEY(zoom_level, tile_column, tile_row))'''
values = [
'( 5, 0, 0, "51037A4A37730F52C8732586D3AAA316" )',
'( 5, 0, 1, "FF76830FF90D79BB335884F256031731" )',
'( 5, 1, 0, "22AF645D1859CB5CA6DA0C484F1F37EA" )',
'( 6, 0, 0, "51037A4A37730F52C8732586D3AAA316" )',
'( 6, 0, 1, "03132BFACDB00CC63D6B7DD98D974DD5" )',
'( 6, 1, 4, "EFC416C7C53A81E7363BE6C1A389131A" )',
'( 6, 1, 6, "22AF645D1859CB5CA6DA0C484F1F37EA" )',
]

[[]]
type = 'table'
tbl_name = 'metadata'
sql = '''
CREATE TABLE metadata (
name text NOT NULL PRIMARY KEY,
value text)'''
values = [
'( "agg_tiles_hash", "5364C57282B73C341D5D801B8F7675C4" )',
'( "md-edit", "value - v2" )',
'( "md-new", "value - new" )',
'( "md-same", "value - same" )',
]

[[]]
type = 'index'
tbl_name = 'images'

[[]]
type = 'index'
tbl_name = 'map'

[[]]
type = 'index'
tbl_name = 'metadata'

[[]]
type = 'view'
tbl_name = 'tiles'
sql = '''
CREATE VIEW tiles AS
SELECT map.zoom_level AS zoom_level,
map.tile_column AS tile_column,
map.tile_row AS tile_row,
images.tile_data AS tile_data
FROM map
JOIN images ON images.tile_id = map.tile_id'''
[[]]
type = 'view'
tbl_name = 'tiles_with_hash'
sql = '''
CREATE VIEW tiles_with_hash AS
SELECT
map.zoom_level AS zoom_level,
map.tile_column AS tile_column,
map.tile_row AS tile_row,
images.tile_data AS tile_data,
images.tile_id AS tile_hash
FROM map
JOIN images ON images.tile_id = map.tile_id'''

0 comments on commit 9d50970

Please sign in to comment.