updating the core for actual builds and working tests

This commit is contained in:
Chris Cochrun 2024-11-12 13:10:12 -06:00
parent e82a9c161b
commit a94ad65914
10 changed files with 219 additions and 86 deletions

View file

@ -1,12 +1,14 @@
use super::model::Model;
use cosmic::{executor, iced::Executor};
use miette::{Result, miette, IntoDiagnostic};
use miette::{miette, IntoDiagnostic, Result};
use serde::{Deserialize, Serialize};
use sqlx::{query_as, SqliteConnection};
use std::path::PathBuf;
use tracing::error;
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[derive(
Clone, Debug, Default, PartialEq, Serialize, Deserialize,
)]
pub struct Video {
pub id: i32,
pub title: String,
@ -25,12 +27,17 @@ impl Model<Video> {
let _ = self.add_item(video);
}
}
Err(e) => error!("There was an error in converting videos: {e}"),
Err(e) => {
error!("There was an error in converting videos: {e}")
}
};
}
}
pub async fn get_video_from_db(database_id: i32, db: &mut SqliteConnection) -> Result<Video> {
pub async fn get_video_from_db(
database_id: i32,
db: &mut SqliteConnection,
) -> Result<Video> {
Ok(query_as!(Video, r#"SELECT title as "title!", file_path as "path!", start_time as "start_time!: f32", end_time as "end_time!: f32", loop as "looping!", id as "id: i32" from videos where id = ?"#, database_id).fetch_one(db).await.into_diagnostic()?)
}
@ -51,7 +58,7 @@ mod test {
async fn test_db_and_model() {
let mut video_model: Model<Video> = Model {
items: vec![],
db: crate::core::model::get_db().await
db: crate::core::model::get_db().await,
};
video_model.load_from_db().await;
if let Some(video) = video_model.find(|v| v.id == 73) {
@ -70,16 +77,26 @@ mod test {
let video = test_video("A new video".into());
let mut video_model: Model<Video> = Model {
items: vec![],
db: crate::core::model::get_db().await
db: crate::core::model::get_db().await,
};
let result = video_model.add_item(video.clone());
let new_video = test_video("A newer video".into());
match result {
Ok(_) => {
assert_eq!(&video, video_model.find(|v| v.id == 0).unwrap());
assert_ne!(&new_video, video_model.find(|v| v.id == 0).unwrap());
assert_eq!(
&video,
video_model.find(|v| v.id == 0).unwrap()
);
assert_ne!(
&new_video,
video_model.find(|v| v.id == 0).unwrap()
);
}
Err(e) => assert!(false, "There was an error adding the video: {:?}", e),
Err(e) => assert!(
false,
"There was an error adding the video: {:?}",
e
),
}
}
}