mod common; mod worlds; use df_st_core::fillable::{Fillable, Filler}; use df_st_db::{id_filter, string_filter, DBObject}; #[allow(unused_imports)] use log::{debug, error, info, trace, warn}; use pretty_assertions::assert_eq; use std::fmt::Debug; use std::path::PathBuf; use worlds::v47_04::region4 as control_data; fn setup() { common::setup(); } // ----------------------------- // --------- Unit Tests -------- // ----------------------------- // ----------------------------- // ----- Integration Tests ----- // ----------------------------- #[test] #[rustfmt::skip] fn full_import_v47_04_region_4() { setup(); let (config, _temp_dir) = common::setup_default_db(); let file = PathBuf::from("./tests/worlds/v47_04/region4/"); let world = 4; // Same as `import` subcommand df_st_parser::parse_and_store_xml(&file, &config, world); df_st_parser::parse_world_map_images(&file, &config, world); df_st_parser::parse_site_map_images(&file, &config, world); // Open DB connection let pool = df_st_db::establish_connection(&config).expect("Failed to connect to database."); let conn = pool.get().expect("Couldn't get db connection from pool."); check_objects::(&conn, world, control_data::get_artifacts(), 11); check_objects::(&conn, world, control_data::get_creatures(), 7); check_objects::(&conn, world, control_data::get_dance_forms(), 11); check_objects::(&conn, world, control_data::get_entities(), 11); check_objects::(&conn, world, control_data::get_entity_populations(), 11); check_objects::(&conn, world, control_data::get_historical_eras(), 11); // check_objects::(&conn, world, control_data::get_historical_eras(), 11); // check_objects::(&conn, world, control_data::get_historical_eras(), 11); // check_objects::(&conn, world, control_data::get_historical_eras(), 11); check_objects::(&conn, world, control_data::get_identities(), 11); check_objects::(&conn, world, control_data::get_landmasses(), 11); check_objects::(&conn, world, control_data::get_mountain_peaks(), 11); check_objects::(&conn, world, control_data::get_musical_forms(), 11); check_objects::(&conn, world, control_data::get_poetic_forms(), 11); // check_objects::(&conn, world, control_data::get_poetic_forms(), 11); // check_objects::(&conn, world, control_data::get_poetic_forms(), 11); // check_objects::(&conn, world, control_data::get_poetic_forms(), 11); check_objects::(&conn, world, control_data::get_regions(), 11); check_objects::(&conn, world, control_data::get_rivers(), 11); check_objects::(&conn, world, control_data::get_sites(), 11); // TODO structures and site_properties // TODO underground_region, world_construction, world_info, written_content // Images check_objects_std::(&conn, world, control_data::get_world_maps(), 18); check_objects_std_opt::(&conn, world, control_data::get_site_maps(), 6); } fn check_objects_std_opt( conn: &df_st_db::DbConnection, world_id: i32, list: Vec>, count: u32, ) where DB: DBObject, C: Fillable + Filler + Default + Debug + Clone, D: PartialEq + Debug + Clone, { // Get items // Compare 1 extra make sure they are both `None` for i in 0..=list.len() { if let Some(control_item) = list.get(i) { let item_search = DB::get_from_db( conn, id_filter!["id" => i as i32, "world_id" => world_id], true, ) .unwrap(); std::assert_eq!(format!("{:?}", item_search), format!("{:?}", control_item)); } } // List items // Remove Option wrapper let list_no_opt: Vec = list .iter() .filter(|item| item.is_some()) .map(|item| item.clone().unwrap()) .collect(); let result_list = DB::get_list_from_db( &*conn, id_filter!["world_id" => world_id], string_filter![], 0, 20, None, None, None, true, ) .unwrap(); std::assert_eq!(format!("{:?}", result_list), format!("{:?}", list_no_opt)); // Count items let total_count = control_data::get_count(count); let count_list = DB::get_count_from_db( &*conn, id_filter!["world_id" => world_id], string_filter![], 0, 20, None, None, ) .unwrap(); std::assert_eq!(format!("{:?}", count_list), format!("{:?}", total_count)); } fn check_objects_std( conn: &df_st_db::DbConnection, world_id: i32, list: Vec, count: u32, ) where DB: DBObject, C: Fillable + Filler + Default + Debug + Clone, D: PartialEq + Debug + Clone, { // Get items // Compare 1 extra make sure they are both `None` for i in 0..=list.len() { let control_item = list.get(i); let item_search = DB::get_from_db( conn, id_filter!["id" => i as i32, "world_id" => world_id], true, ) .unwrap(); std::assert_eq!(format!("{:?}", item_search), format!("{:?}", control_item)); } // List items let result_list = DB::get_list_from_db( &*conn, id_filter!["world_id" => world_id], string_filter![], 0, 20, None, None, None, true, ) .unwrap(); std::assert_eq!(format!("{:?}", result_list), format!("{:?}", list)); // Count items let total_count = control_data::get_count(count); let count_list = DB::get_count_from_db( &*conn, id_filter!["world_id" => world_id], string_filter![], 0, 20, None, None, ) .unwrap(); std::assert_eq!(format!("{:?}", count_list), format!("{:?}", total_count)); } fn check_objects(conn: &df_st_db::DbConnection, world_id: i32, list: Vec, count: u32) where DB: DBObject, C: Fillable + Filler + Default + Debug + Clone, D: PartialEq + Debug + Clone, { // Get items // Compare 1 extra make sure they are both `None` for i in 0..=list.len() { let control_item = list.get(i); let item_search = DB::get_from_db( conn, id_filter!["id" => i as i32, "world_id" => world_id], true, ) .unwrap(); assert_eq!(format!("{:?}", item_search), format!("{:?}", control_item)); } // List items let result_list = DB::get_list_from_db( &*conn, id_filter!["world_id" => world_id], string_filter![], 0, 20, None, None, None, true, ) .unwrap(); assert_eq!(format!("{:?}", result_list), format!("{:?}", list)); // Count items let total_count = control_data::get_count(count); let count_list = DB::get_count_from_db( &*conn, id_filter!["world_id" => world_id], string_filter![], 0, 20, None, None, ) .unwrap(); assert_eq!(format!("{:?}", count_list), format!("{:?}", total_count)); }