Test the history

This commit is contained in:
MasseR committed 2026-10-06 13:46:50 +03:00
1 parent 4d8b1b3cfc
commit bdaa4b04c0
1 file changed
+60
+60
View File
@@ -81,6 +81,33 @@ impl ProgressSync {
}).await?; }).await?;
Ok(doc) Ok(doc)
} }
pub async fn get_all_progress(&self, username: &str, document_id: &str) -> Result<Vec<Document>, SyncError> {
let username = username.to_owned();
let document_id = document_id.to_owned();
let docs = self.reader.connection.call(move |conn| -> rusqlite::Result<Vec<Document>> {
let mut stmt = conn.prepare("select
document_id, progress, percentage, device, device_id, timestamp
from progress
left join users on progress.user_id = users.id
where users.username = :username and progress.document_id = :document_id
order by progress.rowid desc")?;
let x = stmt.query_map(named_params!
{ ":username": username,
":document_id": document_id,
}, |row| {
let document_id : String = row.get(0)?;
let progress : String = row.get(1)?;
let percentage : f32 = row.get(2)?;
let device : String = row.get(3)?;
let device_id : Option<String> = row.get(4)?;
let timestamp : i64 = row.get(5)?;
Ok(Document{ document_id, progress, percentage, device, device_id, timestamp })
})?.collect::<rusqlite::Result<Vec<_>>>()?;
Ok(x)
}).await?;
Ok(docs)
}
} }
#[cfg(test)] #[cfg(test)]
@@ -198,4 +225,37 @@ use secrecy::SecretString;
assert_eq!(got, Some(doc2)); assert_eq!(got, Some(doc2));
Ok(()) Ok(())
} }
#[tokio::test]
async fn retains_history() -> anyhow::Result<()> {
let (_t, users, sync) = create_data().await?;
let username = "foo@example.com";
let username = users.create_user(username, SecretString::from("password")).await?;
let document_id = "foo".to_string();
let doc1 = Document {
document_id: document_id.clone(),
progress: "prog".to_string(),
percentage: 0.5,
device: "kobo".to_string(),
device_id: None,
timestamp: now()?,
};
let doc2 = Document {
document_id: document_id.clone(),
progress: "prog2".to_string(),
percentage: 0.5,
device: "kobo".to_string(),
device_id: None,
timestamp: now()? + 3,
};
sync.set_progress(&username, &doc1).await?;
sync.set_progress(&username, &doc2).await?;
let got = sync.get_all_progress(&username, &document_id).await?;
assert_eq!(got, vec![doc2, doc1]);
Ok(())
}
} }