orderin entries by start time is better than id

This commit is contained in:
Abraham Toriz 2022-07-30 22:46:46 +08:00
parent 0392e91985
commit 411c0891a3
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
1 changed files with 30 additions and 1 deletions

View File

@ -176,7 +176,7 @@ pub trait Database {
}
fn last_entry_of_sheet(&self, sheet: &str) -> Result<Option<Entry>> {
Ok(self.entry_query("select * from entries where sheet=?1 order by id desc limit 1", &[&sheet])?.into_iter().next())
Ok(self.entry_query("select * from entries where sheet=?1 order by start desc limit 1", &[&sheet])?.into_iter().next())
}
fn delete_entry_by_id(&mut self, id: u64) -> Result<()> {
@ -324,6 +324,7 @@ impl Database for SqliteDatabase {
#[cfg(test)]
mod tests {
use chrono::TimeZone;
use pretty_assertions::assert_eq;
use super::*;
@ -490,4 +491,32 @@ mod tests {
]
);
}
/// Due to the "archive by time" feature it can happen that an entry is
/// split in two: the first (old) entry is archived with an updated end time
/// and the second (new) entry is created with the remaining time of the
/// original entry. In this case the last entry of the sheet is not the one
/// with the largest id, but the one with the latest start time.
#[test]
fn last_entry_of_sheet_considers_split_entries() {
let mut db = SqliteDatabase::from_memory().unwrap();
db.init().unwrap();
let sometime = Utc.ymd(2022, 7, 27);
db.entry_insert(sometime.and_hms(11, 0, 0), Some(sometime.and_hms(12, 0, 0)), Some("latest".into()), "foo").unwrap();
db.entry_insert(sometime.and_hms(10, 0, 0), Some(sometime.and_hms(11, 0, 0)), Some("oldest".into()), "foo").unwrap();
// filter by start and end
assert_eq!(
db.last_entry_of_sheet("foo").unwrap().unwrap(),
Entry {
id: 1,
note: Some("latest".into()),
start: sometime.and_hms(11, 0, 0),
end: Some(sometime.and_hms(12, 0, 0)),
sheet: "foo".into(),
}
);
}
}