Skip to content

Commit

Permalink
move db test in own file
Browse files Browse the repository at this point in the history
  • Loading branch information
wiiznokes committed Oct 2, 2024
1 parent 502b6cd commit 0163349
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 295 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl cosmic::Application for AppState {
}
ConfigMsg::UniqueSession(unique_session) => {
config_set!(unique_session, unique_session);
},
}
},
AppMsg::AddFavorite(entry) => {
block_on(async {
Expand Down
File renamed without changes.
297 changes: 3 additions & 294 deletions src/db.rs → src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ use crate::{
utils::{self, now_millis},
};

#[cfg(test)]
pub mod test;

type TimeId = i64;

const DB_VERSION: &str = "4";
Expand Down Expand Up @@ -777,297 +780,3 @@ pub enum DbMessage {
pub fn sub() -> Subscription<DbMessage> {
cosmic::iced::time::every(Duration::from_millis(1000)).map(|_| DbMessage::CheckUpdate)
}

#[cfg(test)]
mod test {
use std::{
fs::{self, File},
io::{Read, Write},
path::PathBuf,
thread::sleep,
time::Duration,
};

use serial_test::serial;

use anyhow::Result;
use cosmic::{iced_sctk::util, widget::canvas::Path};
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

use crate::{
config::Config,
utils::{self, remove_dir_contents},
};

use super::{Db, Entry};

fn prepare_db_dir() -> PathBuf {
let fmt_layer = fmt::layer().with_target(false);
let filter_layer = EnvFilter::try_from_default_env().unwrap_or(EnvFilter::new(format!(
"warn,{}=info",
env!("CARGO_CRATE_NAME")
)));
let _ = tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.try_init();

let db_dir = PathBuf::from("tests");
let _ = std::fs::create_dir_all(&db_dir);
remove_dir_contents(&db_dir);
db_dir
}

#[tokio::test]
#[serial]
async fn test() -> Result<()> {
let db_dir = prepare_db_dir();

let mut db = Db::inner_new(&Config::default(), &db_dir).await?;

test_db(&mut db).await.unwrap();

db.clear().await?;

test_db(&mut db).await.unwrap();

Ok(())
}

async fn test_db(db: &mut Db) -> Result<()> {
assert!(db.len() == 0);

let data = Entry::new_now(
"text/plain".into(),
"content".as_bytes().into(),
None,
false,
);

db.insert(data).await.unwrap();

assert!(db.len() == 1);

sleep(Duration::from_millis(1000));

let data = Entry::new_now(
"text/plain".into(),
"content".as_bytes().into(),
None,
false,
);

db.insert(data).await.unwrap();

assert!(db.len() == 1);

sleep(Duration::from_millis(1000));

let data = Entry::new_now(
"text/plain".into(),
"content2".as_bytes().into(),
None,
false,
);

db.insert(data.clone()).await.unwrap();

assert!(db.len() == 2);

let next = db.iter().next().unwrap();

assert!(next.creation == data.creation);
assert!(next.content == data.content);

Ok(())
}

#[tokio::test]
#[serial]
async fn test_delete_old_one() {
let db_path = prepare_db_dir();

let mut db = Db::inner_new(&Config::default(), &db_path).await.unwrap();

let data = Entry::new_now(
"text/plain".into(),
"content".as_bytes().into(),
None,
false,
);
db.insert(data).await.unwrap();

sleep(Duration::from_millis(100));

let data = Entry::new_now(
"text/plain".into(),
"content2".as_bytes().into(),
None,
false,
);
db.insert(data).await.unwrap();

assert!(db.len() == 2);

let db = Db::inner_new(&Config::default(), &db_path).await.unwrap();

assert!(db.len() == 2);

let config = Config {
maximum_entries_lifetime: Some(Duration::ZERO),
..Default::default()
};
let db = Db::inner_new(&config, &db_path).await.unwrap();

assert!(db.len() == 0);
}

#[tokio::test]
#[serial]
async fn same() {
let db_path = prepare_db_dir();

let mut db = Db::inner_new(&Config::default(), &db_path).await.unwrap();

let now = utils::now_millis();

let data = Entry::new(
now,
"text/plain".into(),
"content".as_bytes().into(),
None,
false,
);

db.insert(data).await.unwrap();

let data = Entry::new(
now,
"text/plain".into(),
"content".as_bytes().into(),
None,
false,
);

db.insert(data).await.unwrap();
assert!(db.len() == 1);
}

#[tokio::test]
#[serial]
async fn different_content_same_time() {
let db_path = prepare_db_dir();

let mut db = Db::inner_new(&Config::default(), &db_path).await.unwrap();

let now = utils::now_millis();

let data = Entry::new(
now,
"text/plain".into(),
"content".as_bytes().into(),
None,
false,
);

db.insert(data).await.unwrap();

let data = Entry::new(
now,
"text/plain".into(),
"content2".as_bytes().into(),
None,
false,
);

db.insert(data).await.unwrap();
assert!(db.len() == 2);
}

#[tokio::test]
#[serial]
async fn favorites() {
let db_path = prepare_db_dir();

let mut db = Db::inner_new(&Config::default(), &db_path).await.unwrap();

let now1 = 1000;

let data1 = Entry::new(
now1,
"text/plain".into(),
"content1".as_bytes().into(),
None,
false,
);

db.insert(data1).await.unwrap();

let now2 = 2000;

let data2 = Entry::new(
now2,
"text/plain".into(),
"content2".as_bytes().into(),
None,
false,
);

db.insert(data2).await.unwrap();

let now3 = 3000;

let data3 = Entry::new(
now3,
"text/plain".into(),
"content3".as_bytes().into(),
None,
false,
);

db.insert(data3.clone()).await.unwrap();

db.add_favorite(&db.state.get(&now3).unwrap().clone(), None)
.await
.unwrap();

db.delete(&db.state.get(&now3).unwrap().clone())
.await
.unwrap();

assert_eq!(db.favorite_len(), 0);

db.insert(data3).await.unwrap();

db.add_favorite(&db.state.get(&now1).unwrap().clone(), None)
.await
.unwrap();

db.add_favorite(&db.state.get(&now3).unwrap().clone(), None)
.await
.unwrap();

db.add_favorite(&db.state.get(&now2).unwrap().clone(), Some(1))
.await
.unwrap();

assert_eq!(db.favorite_len(), 3);

assert_eq!(db.favorites.fav(), &vec![now1, now2, now3]);

let db = Db::inner_new(
&Config {
maximum_entries_lifetime: None,
..Default::default()
},
&db_path,
)
.await
.unwrap();

assert_eq!(db.len(), 3);

assert_eq!(db.favorite_len(), 3);
assert_eq!(db.favorites.fav(), &vec![now1, now2, now3]);
}
}
Loading

0 comments on commit 0163349

Please sign in to comment.