diff options
| author | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2019-11-15 20:12:59 +0100 |
|---|---|---|
| committer | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2019-11-15 20:12:59 +0100 |
| commit | 084675fdfe1329cbd58a48f2306b7c61dec08834 (patch) | |
| tree | 258b77858a35e33dee77684817839858a06d26b4 | |
| parent | 5d1bb582ae26ba83629bfd6fe0a8d64f816acfee (diff) | |
| download | mail-084675fdfe1329cbd58a48f2306b7c61dec08834.tar.gz | |
more tests for tags
| -rw-r--r-- | tests/fixtures.rs | 31 | ||||
| -rw-r--r-- | tests/lib.rs | 1 | ||||
| -rw-r--r-- | tests/test_tags.rs | 144 |
3 files changed, 173 insertions, 3 deletions
diff --git a/tests/fixtures.rs b/tests/fixtures.rs index c9e04c8..a8166be 100644 --- a/tests/fixtures.rs +++ b/tests/fixtures.rs @@ -147,16 +147,41 @@ impl MailBox { if seen { flags += "S"; } - self.maildir.store_cur_with_flags(&msg.message_to_string().unwrap().as_bytes(), flags.as_str()).unwrap() - }; + println!("flags: {:?}", flags); + let mid = self.maildir.store_cur_with_flags(&msg.message_to_string().unwrap().as_bytes(), flags.as_str()).unwrap(); + // I have no idea what the reasoning for the :2 here is, but ok. + format!("{}:2,{}", mid, flags) + }; + + // let mut flags = String::from(""); + // if flagged { + // flags += "F"; + // } + // if replied { + // flags += "R"; + // } + // if seen { + // flags += "S"; + // } + // println!("flags: {:?}", flags); + // let id = self.maildir.store_cur_with_flags(&msg.message_to_string().unwrap().as_bytes(), flags.as_str()).unwrap(); + + // if is_new { + // let msgpath = format!("{}{}", id, flags); + // std::fs::rename(msgpath, newpath)?; + + // self.maildir.path() + // } + + let mut msgpath = self.path(); msgpath = if is_new { msgpath.join("new") } else { msgpath.join("cur") }; - + msgpath = msgpath.join(&id); Ok((msg_id, msgpath)) diff --git a/tests/lib.rs b/tests/lib.rs index bd3138d..e7ee0bc 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -10,4 +10,5 @@ mod fixtures; mod test_database; mod test_thread; mod test_message; +mod test_tags; diff --git a/tests/test_tags.rs b/tests/test_tags.rs new file mode 100644 index 0000000..5ddabeb --- /dev/null +++ b/tests/test_tags.rs @@ -0,0 +1,144 @@ +use std::sync::Arc; +use std::path::PathBuf; +use fixtures::{MailBox, NotmuchCommand}; + +struct TagSetFixture { + // An non-empty immutable tagset. + // This will have the default new mail tags: inbox, unread. + pub mailbox: MailBox, + pub cmd: NotmuchCommand, + pub database: Arc<notmuch::Database>, + pub message: notmuch::Message<'static, notmuch::Database> +} + +impl TagSetFixture { + pub fn new(mutable: bool, flagged: bool) -> Self{ + let mailbox = MailBox::new(); + let (_msg, filename) = mailbox.deliver(None, None, None, None, vec![], !flagged, None, false, false, flagged).unwrap(); + + let cmd = NotmuchCommand::new(&mailbox.path()); + cmd.run(vec!["new"]).unwrap(); + + let database = Arc::new(notmuch::Database::open(&mailbox.path(), if !mutable {notmuch::DatabaseMode::ReadOnly} else { notmuch::DatabaseMode::ReadWrite }).unwrap()); + let message = <notmuch::Database as notmuch::DatabaseExt>::find_message_by_filename(database.clone(), &filename).unwrap().unwrap(); + + Self { + mailbox, + database, + cmd, + message + } + } +} + +mod immutable { + + use super::*; + + #[test] + fn test_neg(){ + let tagset = TagSetFixture::new(false, false); + + let tags: Vec<String> = tagset.database.all_tags().unwrap().collect(); + tagset.cmd.run(vec!["tag", "+foo", "*"]).unwrap(); + + let database = notmuch::Database::open(&tagset.mailbox.path(), notmuch::DatabaseMode::ReadOnly).unwrap(); + let ntags: Vec<String> = database.all_tags().unwrap().collect(); + + assert_ne!(tags, ntags); + } + + #[test] + fn test_contains(){ + let tagset = TagSetFixture::new(false, false); + let tags: Vec<String> = tagset.database.all_tags().unwrap().collect(); + + assert!(tags.iter().any(|x| x == "unread")); + assert!(!tags.iter().any(|x| x == "foo")); + } + + + #[test] + fn test_len(){ + let tagset = TagSetFixture::new(false, false); + assert_eq!(tagset.database.all_tags().unwrap().count(), 2); + } + +} + +mod mutable { + + use super::*; + + #[test] + fn test_add(){ + let tagset = TagSetFixture::new(true, false); + assert!(!tagset.message.tags().any(|x| x == "foo")); + + tagset.message.add_tag("foo").unwrap(); + assert!(tagset.message.tags().any(|x| x == "foo")); + } + + #[test] + fn test_discard(){ + let tagset = TagSetFixture::new(true, false); + assert!(tagset.message.tags().any(|x| x == "inbox")); + + tagset.message.remove_tag("inbox").unwrap(); + assert!(!tagset.message.tags().any(|x| x == "unbox")); + } + + #[test] + fn test_discard_not_present(){ + let tagset = TagSetFixture::new(true, false); + assert!(!tagset.message.tags().any(|x| x == "foo")); + + tagset.message.remove_tag("foo").unwrap(); + } + + #[test] + fn test_clear(){ + let tagset = TagSetFixture::new(true, false); + assert!(tagset.message.tags().count() > 0); + tagset.message.remove_all_tags().unwrap(); + + assert!(tagset.message.tags().count() == 0); + } + + #[test] + fn test_from_maildir_flags(){ + let tagset = TagSetFixture::new(true, true); + + let msgid = tagset.message.id(); + tagset.message.remove_tag(&"flagged").unwrap(); + tagset.message.maildir_flags_to_tags().unwrap(); + + assert!(tagset.message.tags().any(|x| x == "flagged")); + } + + + #[test] + fn test_to_maildir_flags(){ + + let tagset = TagSetFixture::new(true, true); + + let filename = tagset.message.filename(); + let filestr = filename.to_string_lossy(); + + let file_parts: Vec<&str> = filestr.split(",").collect(); + let flags = file_parts.last().unwrap(); + println!("Flags {:?}", flags); + + assert!(flags.contains("F")); + tagset.message.remove_tag(&"flagged").unwrap(); + tagset.message.tags_to_maildir_flags().unwrap(); + + let filename = tagset.message.filename(); + let filestr = filename.to_string_lossy(); + + let file_parts: Vec<&str> = filestr.split(",").collect(); + let flags = file_parts.last().unwrap(); + assert!(!flags.contains("F")); + } + +}
\ No newline at end of file |
