Skip to content

Commit

Permalink
Add tests for config API
Browse files Browse the repository at this point in the history
  • Loading branch information
elizagamedev committed May 16, 2022
1 parent 24a1135 commit 2e0c9cf
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 6 deletions.
13 changes: 7 additions & 6 deletions tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
extern crate dirs;
extern crate tempfile;
extern crate notmuch;
extern crate gethostname;
extern crate maildir;
extern crate lettre;
extern crate lettre_email;
extern crate maildir;
extern crate notmuch;
extern crate tempfile;

mod fixtures;
#[cfg(feature = "v0_32")]
mod test_config;
mod test_database;
mod test_query;
mod test_thread;
mod test_message;
mod test_query;
mod test_tags;

mod test_thread;
131 changes: 131 additions & 0 deletions tests/test_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
use notmuch::ConfigKey;

use crate::fixtures::{MailBox, NotmuchCommand};

struct ConfigFixture {
// Return a read-write Database.
// The database will have 3 messages, 2 threads.
pub mailbox: MailBox,
pub database: notmuch::Database,
}

impl ConfigFixture {
pub fn new() -> Self {
let mailbox = MailBox::new();

let cmd = NotmuchCommand::new(&mailbox.path());
cmd.run(vec!["new"]).unwrap();

let database = notmuch::Database::open_with_config(
Some(&mailbox.path()),
notmuch::DatabaseMode::ReadWrite,
Some(mailbox.path().join("notmuch-config")),
None,
)
.unwrap();

Self { mailbox, database }
}
}

mod config {
use super::*;

#[test]
fn test_config() {
let db = ConfigFixture::new();

assert_eq!(
db.database.config(ConfigKey::UserName).unwrap(),
"Some Hacker"
);

assert_eq!(
db.database.config(ConfigKey::MailRoot).unwrap(),
db.mailbox.path().to_str().unwrap()
);
}

#[test]
fn test_config_set() {
let db = ConfigFixture::new();

const USER_NAME: &str = "Hideo Kojima";

db.database
.config_set(ConfigKey::UserName, USER_NAME)
.unwrap();

assert_eq!(db.database.config(ConfigKey::UserName).unwrap(), USER_NAME);
}

#[test]
fn test_config_values() {
let db = ConfigFixture::new();

let tags: Vec<_> = db
.database
.config_values(ConfigKey::NewTags)
.unwrap()
.collect();

assert_eq!(tags.len(), 2);
assert!(tags.iter().any(|x| x == "unread"));
assert!(tags.iter().any(|x| x == "inbox"));
}

#[test]
fn test_config_values_string() {
let db = ConfigFixture::new();

let tags: Vec<_> = db
.database
.config_values_string("search.exclude_tags")
.unwrap()
.collect();

assert_eq!(tags.len(), 2);
assert!(tags.iter().any(|x| x == "deleted"));
assert!(tags.iter().any(|x| x == "spam"));
}

#[test]
fn test_config_pairs() {
let db = ConfigFixture::new();

let pairs: Vec<(_, _)> = db.database.config_pairs("user").unwrap().collect();

println!("{pairs:?}");

assert_eq!(pairs.len(), 3);
assert!(pairs
.iter()
.any(|(k, v)| k == "user.name" && v.as_deref() == Some("Some Hacker")));
assert!(pairs
.iter()
.any(|(k, v)| k == "user.primary_email" && v.as_deref() == Some("dst@example.com")));
assert!(pairs
.iter()
.any(|(k, v)| k == "user.other_email" && *v == None));
}

#[test]
fn test_config_bool() {
let db = ConfigFixture::new();

assert_eq!(
db.database.config_bool(ConfigKey::MaildirFlags).unwrap(),
true
);
}

#[test]
fn test_config_path() {
let db = ConfigFixture::new();

assert_eq!(
db.database.config_path().unwrap(),
db.mailbox.path().join("notmuch-config"),
);
}
}

0 comments on commit 2e0c9cf

Please sign in to comment.