diff options
| author | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2019-11-15 14:05:29 +0100 |
|---|---|---|
| committer | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2019-11-15 14:05:29 +0100 |
| commit | 5d1bb582ae26ba83629bfd6fe0a8d64f816acfee (patch) | |
| tree | 90e4ac7c59f27fc9c6e3aa65c723141f1b2298ac | |
| parent | cc6896cca0839f5d97c5daee8ffba824c3c0d229 (diff) | |
| download | mail-5d1bb582ae26ba83629bfd6fe0a8d64f816acfee.tar.gz | |
more tests for message
| -rw-r--r-- | src/ffi.rs | 17 | ||||
| -rw-r--r-- | src/message.rs | 55 | ||||
| -rw-r--r-- | tests/test_message.rs | 147 |
3 files changed, 121 insertions, 98 deletions
@@ -1445,6 +1445,23 @@ extern "C" { key: *const c_char, ) -> notmuch_status_t; + /// Remove all (prefix*,value) pairs from the given message + /// + /// @param[in,out] message message to operate on. + /// @param[in] prefix delete properties with keys that start with prefix. + /// If NULL, delete all properties + /// @returns + /// - NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in + /// read-only mode so message cannot be modified. + /// - NOTMUCH_STATUS_SUCCESS: No error occurred. + /// + /// @since libnotmuch 5.1 (notmuch 0.26) + /// + pub fn notmuch_message_remove_all_properties_with_prefix( + message: *mut notmuch_message_t, + prefix: *const c_char, + ) -> notmuch_status_t; + /// Get the properties for *message*, returning a /// `notmuch_message_properties_t` object which can be used to iterate over /// all properties. diff --git a/src/message.rs b/src/message.rs index 03623a1..8263908 100644 --- a/src/message.rs +++ b/src/message.rs @@ -144,19 +144,47 @@ where unsafe { ffi::notmuch_message_thaw(self.ptr) }.as_result() } - pub fn properties<'m>(&'m self, key: &str, exact: bool) -> MessageProperties<'m, 'o, O> - { + pub fn properties<'m>(&'m self, key: &str, exact: bool) -> MessageProperties<'m, 'o, O> { <Self as MessageExt<'o, O>>::properties(self, key, exact) } - pub fn remove_all_properties(&self, key: &str) -> Result<()> + pub fn remove_all_properties(&self, key: Option<&str>) -> Result<()> { - let key_str = CString::new(key).unwrap(); - unsafe { - ffi::notmuch_message_remove_all_properties(self.ptr, key_str.as_ptr()) - }.as_result() + match key { + Some(k) => { + let key_str = CString::new(k).unwrap(); + unsafe { + ffi::notmuch_message_remove_all_properties(self.ptr, key_str.as_ptr()) + }.as_result() + }, + None => { + let p = ptr::null(); + unsafe { + ffi::notmuch_message_remove_all_properties(self.ptr, p) + }.as_result() + } + } + } + + pub fn remove_all_properties_with_prefix(&self, prefix: Option<&str>) -> Result<()> + { + match prefix { + Some(k) => { + let key_str = CString::new(k).unwrap(); + unsafe { + ffi::notmuch_message_remove_all_properties_with_prefix(self.ptr, key_str.as_ptr()) + }.as_result() + }, + None => { + let p = ptr::null(); + unsafe { + ffi::notmuch_message_remove_all_properties_with_prefix(self.ptr, p) + }.as_result() + } + } } + pub fn count_properties(&self, key: &str) -> Result<u32> { let key_str = CString::new(key).unwrap(); @@ -176,10 +204,14 @@ where ffi::notmuch_message_get_property(self.ptr, key_str.as_ptr(), &mut prop) }.as_result()?; - // TODO: the unwrap here is not good - Ok(unsafe{ - CStr::from_ptr(prop) - }.to_str().unwrap().to_string()) + if prop.is_null() { + Err(Error::UnspecifiedError) + } else { + // TODO: the unwrap here is not good + Ok(unsafe{ + CStr::from_ptr(prop) + }.to_str().unwrap().to_string()) + } } pub fn add_property(&self, key: &str, value: &str) -> Result<()> @@ -199,7 +231,6 @@ where ffi::notmuch_message_remove_property(self.ptr, key_str.as_ptr(), value_str.as_ptr()) }.as_result() } - } pub trait MessageExt<'o, O> diff --git a/tests/test_message.rs b/tests/test_message.rs index ce86df0..152f565 100644 --- a/tests/test_message.rs +++ b/tests/test_message.rs @@ -165,35 +165,6 @@ mod message { // now = int(time.time()) // assert abs(now - msg.date) < 3600*24 - -// struct MessagePropertiesFixture { -// // Return a single thread with 2 messages -// pub mailbox: MailBox, -// pub database: Arc<notmuch::Database>, -// pub maildir_msg: (String, PathBuf), -// pub message: notmuch::Message<'static, notmuch::Database>, -// } - -// impl MessagePropertiesFixture { -// pub fn new() -> Self{ -// let mailbox = MailBox::new(); - -// let (msgid, filename) = mailbox.deliver(None, None, None, None, vec![], true, None, false, false, false).unwrap(); - -// let database = Arc::new(notmuch::Database::create(&mailbox.path()).unwrap()); -// let message = <notmuch::Database as notmuch::DatabaseExt>::index_file(database.clone(), &filename, None).unwrap(); -// let properties = <notmuch::Message as notmuch::MessageExt>::properties(&message, false); - -// Self { -// mailbox, -// database, -// maildir_msg: (msgid, filename), -// message -// } -// } -// } - - mod properties { use super::*; @@ -245,72 +216,76 @@ mod properties { } #[test] + fn test_del() { + let msg = MessageFixture::new(); + msg.message.add_property(&"foo", &"a").unwrap(); + msg.message.add_property(&"foo", &"b").unwrap(); + + msg.message.remove_all_properties(Some(&"foo")).unwrap(); + assert!(msg.message.property(&"foo", true).is_err()); + } + + #[test] fn test_remove() { let msg = MessageFixture::new(); msg.message.add_property(&"foo", &"a").unwrap(); msg.message.add_property(&"foo", &"b").unwrap(); msg.message.remove_property(&"foo", &"a").unwrap(); - assert_eq!(msg.message.property(&"foo", true).unwrap(), "b"); + } + + #[test] + fn test_clear() { + let msg = MessageFixture::new(); + msg.message.add_property(&"foo", &"a").unwrap(); + + msg.message.remove_all_properties(None).unwrap(); + assert!(msg.message.property(&"foo", true).is_err()); + } + + #[test] + fn test_getall() { + let msg = MessageFixture::new(); + msg.message.add_property(&"foo", &"a").unwrap(); + + let mut prop_keys: Vec<String> = msg.message.properties(&"foo", false).map(|x| x.0).collect(); + assert_eq!(prop_keys.len(), 1); + assert_eq!(prop_keys, vec!["foo"]); + let mut prop_vals: Vec<String> = msg.message.properties(&"foo", false).map(|x| x.1).collect(); + assert_eq!(prop_vals.len(), 1); + assert_eq!(prop_vals, vec!["a"]); } + #[test] + fn test_getall_prefix() { + let msg = MessageFixture::new(); + msg.message.add_property(&"foo", &"a").unwrap(); + msg.message.add_property(&"foobar", &"b").unwrap(); + + let mut prop_keys: Vec<String> = msg.message.properties(&"foo", false).map(|x| x.0).collect(); + assert_eq!(prop_keys.len(), 2); + assert_eq!(prop_keys, vec!["foo", "foobar"]); + + let mut prop_vals: Vec<String> = msg.message.properties(&"foo", false).map(|x| x.1).collect(); + assert_eq!(prop_vals.len(), 2); + assert_eq!(prop_vals, vec!["a", "b"]); + } + + #[test] + fn test_getall_exact() { + let msg = MessageFixture::new(); + msg.message.add_property(&"foo", &"a").unwrap(); + msg.message.add_property(&"foobar", &"b").unwrap(); + + let mut prop_keys: Vec<String> = msg.message.properties(&"foo", true).map(|x| x.0).collect(); + assert_eq!(prop_keys.len(), 1); + assert_eq!(prop_keys, vec!["foo"]); + + let mut prop_vals: Vec<String> = msg.message.properties(&"foo", true).map(|x| x.1).collect(); + assert_eq!(prop_vals.len(), 1); + assert_eq!(prop_vals, vec!["a"]); + } } - - - -// def test_del(self, props): -// props.add('foo', 'a') -// props.add('foo', 'b') -// del props['foo'] -// with pytest.raises(KeyError): -// props['foo'] - -// def test_remove(self, props): -// props.add('foo', 'a') -// props.add('foo', 'b') -// props.remove('foo', 'a') -// assert props['foo'] == 'b' - -// def test_view_abcs(self, props): -// assert isinstance(props.keys(), collections.abc.KeysView) -// assert isinstance(props.values(), collections.abc.ValuesView) -// assert isinstance(props.items(), collections.abc.ItemsView) - -// def test_pop(self, props): -// props.add('foo', 'a') -// props.add('foo', 'b') -// val = props.pop('foo') -// assert val == 'a' - -// def test_pop_default(self, props): -// with pytest.raises(KeyError): -// props.pop('foo') -// assert props.pop('foo', 'default') == 'default' - -// def test_popitem(self, props): -// props.add('foo', 'a') -// assert props.popitem() == ('foo', 'a') -// with pytest.raises(KeyError): -// props.popitem() - -// def test_clear(self, props): -// props.add('foo', 'a') -// props.clear() -// assert len(props) == 0 - -// def test_getall(self, props): -// props.add('foo', 'a') -// assert set(props.getall('foo')) == {('foo', 'a')} - -// def test_getall_prefix(self, props): -// props.add('foo', 'a') -// props.add('foobar', 'b') -// assert set(props.getall('foo')) == {('foo', 'a'), ('foobar', 'b')} - -// def test_getall_exact(self, props): -// props.add('foo', 'a') -// props.add('foobar', 'b') -// assert set(props.getall('foo', exact=True)) == {('foo', 'a')} |
