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,11 +1,13 @@
use super::model::Model;
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 Image {
pub id: i32,
pub title: String,
@ -26,12 +28,17 @@ impl Model<Image> {
let _ = self.add_item(image);
}
}
Err(e) => error!("There was an error in converting images: {e}"),
Err(e) => {
error!("There was an error in converting images: {e}")
}
};
}
}
pub async fn get_image_from_db(database_id: i32, db: &mut SqliteConnection) -> Result<Image> {
pub async fn get_image_from_db(
database_id: i32,
db: &mut SqliteConnection,
) -> Result<Image> {
Ok(query_as!(Image, r#"SELECT title as "title!", file_path as "path!", id as "id: i32" from images where id = ?"#, database_id).fetch_one(db).await.into_diagnostic()?)
}
@ -52,7 +59,7 @@ mod test {
pub async fn test_db_and_model() {
let mut image_model: Model<Image> = Model {
items: vec![],
db: crate::core::model::get_db().await
db: crate::core::model::get_db().await,
};
image_model.load_from_db().await;
dbg!(&image_model.items);
@ -70,16 +77,26 @@ mod test {
let image = test_image("A new image".into());
let mut image_model: Model<Image> = Model {
items: vec![],
db: crate::core::model::get_db().await
db: crate::core::model::get_db().await,
};
let result = image_model.add_item(image.clone());
let new_image = test_image("A newer image".into());
match result {
Ok(_) => {
assert_eq!(&image, image_model.find(|i| i.id == 0).unwrap());
assert_ne!(&new_image, image_model.find(|i| i.id == 0).unwrap());
assert_eq!(
&image,
image_model.find(|i| i.id == 0).unwrap()
);
assert_ne!(
&new_image,
image_model.find(|i| i.id == 0).unwrap()
);
}
Err(e) => assert!(false, "There was an error adding the image: {:?}", e),
Err(e) => assert!(
false,
"There was an error adding the image: {:?}",
e
),
}
}
}