From ea50af892c4268ae06f6df40ee435eadd076228d Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 6 Nov 2022 23:06:54 +0100 Subject: halfswitch to rusqlite; db tests --- tests/db_integration.rs | 115 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 3 deletions(-) (limited to 'tests/db_integration.rs') diff --git a/tests/db_integration.rs b/tests/db_integration.rs index f580595..8c3d194 100644 --- a/tests/db_integration.rs +++ b/tests/db_integration.rs @@ -1,6 +1,115 @@ -mod db; +use std::{fs::{File, create_dir_all}, path::Path, sync::Once}; + +use modlist::{config::{Cfg, Apis}, db::{mods_insert, db_setup, user_dbversion, mods_get_all_ids, mods_get_id, mods_remove, userlist_insert, lists_insert, userlist_get_all_ids, userlist_remove}, Modloader}; + +static INIT: Once = Once::new(); + +fn setup() -> Cfg { + let db_pathstr = "./test_tmp/db"; + + let config = Cfg { data: String::from(db_pathstr), downloads: String::from("-"), clean_remove: false, apis: Apis { modrinth: String::from("-") } }; + + INIT.call_once(|| { + let db_path = Path::new(db_pathstr); + create_dir_all(db_path).unwrap(); + let db_filestr = format!("{}/data.db", db_pathstr); + File::create(db_filestr).unwrap(); + println!("INIT TEST DB"); + db_setup(config.clone()).unwrap(); + }); + config +} + +#[test] +fn test_user_dbversion() { + let config = setup(); + + assert_eq!(user_dbversion(config).unwrap(), "0.2"); +} + +#[test] +fn test_mods_insert() { + let config = setup(); + + mods_insert(config.clone(), String::from("I"), String::from("INSERT_TEST"), vec![String::from("INSERT_VER1"), String::from("INSERT_VER2")]).unwrap(); + let ids = mods_get_all_ids(config).unwrap(); + + assert!(ids.contains(&String::from("I"))); +} + +#[test] +fn test_mods_get_all_ids() { + let config = setup(); + + mods_insert(config.clone(), String::from("GAI1"), String::from("GETALLIDS_TEST1"), vec![String::from("GAI1_VER1"), String::from("GAI1_VER2")]).unwrap(); + mods_insert(config.clone(), String::from("GAI2"), String::from("GETALLIDS_TEST2"), vec![String::from("GAI2_VER1"), String::from("GAI2_VER2")]).unwrap(); + let ids = mods_get_all_ids(config).unwrap(); + + assert!(ids.contains(&String::from("GAI1"))); + assert!(ids.contains(&String::from("GAI2"))); +} + +#[test] +fn test_mods_get_id() { + let config = setup(); + + mods_insert(config.clone(), String::from("GI"), String::from("GETID_TEST"), vec![String::from("GI_VER1"), String::from("GI_VER2")]).unwrap(); + let id = mods_get_id(config, String::from("GETID_TEST")).unwrap(); + + assert_eq!(id, String::from("GI")); +} + +#[test] +fn test_mods_remove() { + let config = setup(); + + mods_insert(config.clone(), String::from("R"), String::from("REMOVE_TEST"), vec![String::from("R_VER1"), String::from("R_VER2")]).unwrap(); + let ids = mods_get_all_ids(config.clone()).unwrap(); + assert!(ids.contains(&String::from("R"))); + mods_remove(config.clone(), String::from("R")).unwrap(); + assert!(mods_get_id(config, String::from("REMOVE_TEST")).is_err()); +} + +//user_list +#[test] +fn test_userlist_insert() { + let config = setup(); + + lists_insert(config.clone(), String::from("UL_I_LIST"), String::from("UL_I_MC"), Modloader::Fabric).unwrap(); + userlist_insert(config, String::from("UL_I_LIST"), String::from("UL_I"), String::from("UL_I_VER1"), vec![String::from("UL_I_VER1"), String::from("UL_I_VER2")], String::from("localhost:8080/dl/UL_I_VER1.test")).unwrap(); + //let list = mods_get_all_ids(config).unwrap(); + + //assert!(ids.contains(&String::from("I"))); +} + +#[test] +fn test_userlist_get_all_ids() { + let config = setup(); + + lists_insert(config.clone(), String::from("UL_GAI_LIST"), String::from("UL_GAI_MC"), Modloader::Fabric).unwrap(); + userlist_insert(config.clone(), String::from("UL_GAI_LIST"), String::from("UL_GAI1"), String::from("UL_GAI1_VER1"), vec![String::from("UL_GAI2_VER1"), String::from("UL_GAI1_VER2")], String::from("localhost:8080/dl/UL_GAI1_VER1.test")).unwrap(); + userlist_insert(config.clone(), String::from("UL_GAI_LIST"), String::from("UL_GAI2"), String::from("UL_GAI2_VER1"), vec![String::from("UL_GAI2_VER1"), String::from("UL_GAI2_VER2")], String::from("localhost:8080/dl/UL_GAI2_VER1.test")).unwrap(); + let ids = userlist_get_all_ids(config, String::from("UL_GAI_LIST")).unwrap(); + + assert!(ids.contains(&String::from("UL_GAI1"))); + assert!(ids.contains(&String::from("UL_GAI2"))); +} #[test] -fn test_add() { - db::setup(); +fn test_userlist_remove() { + let config = setup(); + + lists_insert(config.clone(), String::from("UL_R_LIST"), String::from("UL_R_MC"), Modloader::Fabric).unwrap(); + userlist_insert(config.clone(), String::from("UL_R_LIST"), String::from("UL_R"), String::from("UL_R_VER1"), vec![String::from("UL_R_VER1"), String::from("UL_R_VER2")], String::from("localhost:8080/dl/UL_R_VER1.test")).unwrap(); + let ids = userlist_get_all_ids(config.clone(), String::from("UL_R_LIST")).unwrap(); + assert!(ids.contains(&String::from("UL_R"))); + userlist_remove(config.clone(), String::from("UL_R_LIST"), String::from("UL_R")).unwrap(); + assert!(userlist_get_all_ids(config, String::from("UL_R_LIST")).is_err()) +} +//lists +#[test] +fn test_lists_insert() { + let config = setup(); + + lists_insert(config, String::from("TESTLIST"), String::from("L_I_LIST"), Modloader::Fabric).unwrap(); } -- cgit v1.2.3