core/db/queue.rs: add delete_from_queue() test
parent
154ea631c6
commit
0bb08a1b08
|
@ -239,7 +239,7 @@ mod subscription_policy {
|
|||
Ok(ret)
|
||||
}
|
||||
|
||||
/// Remove an existing list policy.
|
||||
/// Remove an existing subscription policy.
|
||||
///
|
||||
/// ```
|
||||
/// # use mailpot::{models::*, Configuration, Connection, SendMail};
|
||||
|
@ -256,7 +256,7 @@ mod subscription_policy {
|
|||
///
|
||||
/// # fn do_test(config: Configuration) {
|
||||
/// let db = Connection::open_or_create_db(config).unwrap().trusted();
|
||||
/// let list_pk = db
|
||||
/// let list = db
|
||||
/// .create_list(MailingList {
|
||||
/// pk: 0,
|
||||
/// name: "foobar chat".into(),
|
||||
|
@ -265,19 +265,23 @@ mod subscription_policy {
|
|||
/// description: None,
|
||||
/// archive_url: None,
|
||||
/// })
|
||||
/// .unwrap()
|
||||
/// .pk;
|
||||
/// db.set_list_post_policy(PostPolicy {
|
||||
/// pk: 0,
|
||||
/// list: list_pk,
|
||||
/// announce_only: false,
|
||||
/// subscription_only: true,
|
||||
/// approval_needed: false,
|
||||
/// open: false,
|
||||
/// custom: false,
|
||||
/// })
|
||||
/// .unwrap();
|
||||
/// db.remove_list_post_policy(1, 1).unwrap();
|
||||
/// .unwrap();
|
||||
/// # assert!(db.list_subscription_policy(list.pk()).unwrap().is_none());
|
||||
/// let pol = db
|
||||
/// .set_list_subscription_policy(SubscriptionPolicy {
|
||||
/// pk: -1,
|
||||
/// list: list.pk(),
|
||||
/// send_confirmation: false,
|
||||
/// open: true,
|
||||
/// manual: false,
|
||||
/// request: false,
|
||||
/// custom: false,
|
||||
/// })
|
||||
/// .unwrap();
|
||||
/// # assert_eq!(db.list_subscription_policy(list.pk()).unwrap().as_ref(), Some(&pol));
|
||||
/// db.remove_list_subscription_policy(list.pk(), pol.pk())
|
||||
/// .unwrap();
|
||||
/// # assert!(db.list_subscription_policy(list.pk()).unwrap().is_none());
|
||||
/// # }
|
||||
/// # do_test(config);
|
||||
/// ```
|
||||
|
|
|
@ -258,3 +258,73 @@ impl Connection {
|
|||
Ok(ret)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_queue_delete_array() {
|
||||
use tempfile::TempDir;
|
||||
|
||||
let tmp_dir = TempDir::new().unwrap();
|
||||
let db_path = tmp_dir.path().join("mpot.db");
|
||||
let config = Configuration {
|
||||
send_mail: SendMail::ShellCommand("/usr/bin/false".to_string()),
|
||||
db_path,
|
||||
data_path: tmp_dir.path().to_path_buf(),
|
||||
administrators: vec![],
|
||||
};
|
||||
|
||||
let mut db = Connection::open_or_create_db(config).unwrap().trusted();
|
||||
for i in 0..5 {
|
||||
db.insert_to_queue(
|
||||
QueueEntry::new(
|
||||
Queue::Hold,
|
||||
None,
|
||||
None,
|
||||
format!("Subject: testing\r\nMessage-Id: {i}@localhost\r\n\r\nHello\r\n")
|
||||
.as_bytes(),
|
||||
None,
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
let entries = db.queue(Queue::Hold).unwrap();
|
||||
assert_eq!(entries.len(), 5);
|
||||
let out_entries = db.delete_from_queue(Queue::Out, vec![]).unwrap();
|
||||
assert_eq!(db.queue(Queue::Hold).unwrap().len(), 5);
|
||||
assert!(out_entries.is_empty());
|
||||
let deleted_entries = db.delete_from_queue(Queue::Hold, vec![]).unwrap();
|
||||
assert_eq!(deleted_entries.len(), 5);
|
||||
assert_eq!(
|
||||
&entries
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(DbVal::into_inner)
|
||||
.map(|mut e| {
|
||||
e.pk = -1;
|
||||
e
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
&deleted_entries
|
||||
);
|
||||
|
||||
for e in deleted_entries {
|
||||
db.insert_to_queue(e).unwrap();
|
||||
}
|
||||
|
||||
let index = db
|
||||
.queue(Queue::Hold)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.skip(2)
|
||||
.map(|e| e.pk())
|
||||
.take(2)
|
||||
.collect::<Vec<i64>>();
|
||||
let deleted_entries = db.delete_from_queue(Queue::Hold, index).unwrap();
|
||||
assert_eq!(deleted_entries.len(), 2);
|
||||
assert_eq!(db.queue(Queue::Hold).unwrap().len(), 3);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue