summaryrefslogtreecommitdiff
path: root/tests/db_integration.rs
blob: 8c3d19475e5a7ca16d7daa8f25e0a6785d4cfe88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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_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();
}