aboutsummaryrefslogtreecommitdiffstats
path: root/tests/integration.rs
blob: e50021958a248ace9e27362dfcd2930ffb7faf9f (plain)
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
use log_db::log_db;
use serial_test::serial;

const TEST_DATA_DIR: &str = "test_db_data";
const TEST_SEGMENT_SIZE: u64 = 1024 * 1024; // 1 MB
const TEST_MEMTABLE_SIZE: u64 = 1024 * 1024; // 1 MB

struct TestRecord {
    id: u64,
    data: String,
}

impl log_db::Record for TestRecord {
    fn serialize(&self) -> Vec<u8> {
        format!("{}:{}", self.id, self.data).into_bytes()
    }

    fn deserialize(data: Vec<u8>) -> Self {
        let data = String::from_utf8(data).unwrap();
        let parts: Vec<&str> = data.split(':').collect();
        TestRecord {
            id: parts[0].parse().unwrap(),
            data: parts[1].to_string(),
        }
    }
}

#[test]
#[serial]
fn test_initialize() {
    let _db: log_db::DB<TestRecord> = log_db::DB::initialize(&log_db::Config {
        data_dir: TEST_DATA_DIR.to_string(),
        segment_size: TEST_SEGMENT_SIZE,
        memtable_size: TEST_MEMTABLE_SIZE,
    })
    .unwrap();

    // Clean up
    std::fs::remove_dir_all(TEST_DATA_DIR.to_string()).unwrap();
}

#[test]
#[serial]
fn test_upsert_to_empty_db() {
    let db: log_db::DB<TestRecord> = log_db::DB::initialize(&log_db::Config {
        data_dir: TEST_DATA_DIR.to_string(),
        segment_size: TEST_SEGMENT_SIZE,
        memtable_size: TEST_MEMTABLE_SIZE,
    })
    .unwrap();

    let record = TestRecord {
        id: 1,
        data: "hello".to_string(),
    };
    db.upsert(&record).unwrap();

    // Clean up
    std::fs::remove_dir_all(TEST_DATA_DIR.to_string()).unwrap();
}