From fadd82ff3b38a80dffeb12bde44306e05517105c Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 6 Mar 2025 04:43:37 +0100 Subject: [PATCH] Updates coverage --- Cargo.toml | 4 +- README.md | 2 +- badges/coverage.svg | 2 +- weaponry/Cargo.toml | 3 ++ weaponry/src/database.rs | 18 +++++--- weaponry/src/error.rs | 3 ++ weaponry/src/interfaces.rs | 45 ++++++++++++++++--- .../equipement_impl_advanced.rs | 1 + .../equipement_impl_database.rs | 34 +++----------- .../weaponry_structs/equipement_impl_math.rs | 20 +++++++++ .../weaponry_structs/equipement_scenario.rs | 25 ++++++----- .../weaponry_structs/equipement_thin_types.rs | 16 +++++++ xtask/Cargo.toml | 7 --- xtask/src/main.rs | 3 -- 14 files changed, 115 insertions(+), 68 deletions(-) delete mode 100644 xtask/Cargo.toml delete mode 100644 xtask/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 0b4e538..3d16c47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ [workspace] members = [ "cli_parser", "config", "graphs", "make", "more_fields", "more_fields_macro", "ravenous-tools", "stats", - "tablegen", "weaponry", "xtask", + "tablegen", "weaponry", ] resolver = "3" @@ -31,5 +31,3 @@ lto = false codegen-units = 256 incremental = true panic = "abort" - - diff --git a/README.md b/README.md index 77ed0ed..4c10907 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![version](badges/version.svg) ![code coverage](badges/plastic.svg) +![version](badges/version.svg) ![code coverage](badges/coverage.svg) # ravenous-tools diff --git a/badges/coverage.svg b/badges/coverage.svg index 950fe17..c78d807 100644 --- a/badges/coverage.svg +++ b/badges/coverage.svg @@ -1 +1 @@ -test-coverage: 17%test-coverage17% +test-coverage: 23%test-coverage23% diff --git a/weaponry/Cargo.toml b/weaponry/Cargo.toml index b1a596a..43e3a6f 100644 --- a/weaponry/Cargo.toml +++ b/weaponry/Cargo.toml @@ -28,3 +28,6 @@ clap = { version = "4.5.30", features = ["derive"] } [[bench]] name = "iter_creation" harness = false + +# [lints.rust] +# unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tarpaulin_include)'] } diff --git a/weaponry/src/database.rs b/weaponry/src/database.rs index 755a68c..5f5c048 100644 --- a/weaponry/src/database.rs +++ b/weaponry/src/database.rs @@ -30,17 +30,21 @@ where /// Generate a SELECT query with fields generated from a struct. Returns a SQL request as a String #[inline] +#[coverage(off)] // INCORRECT: Error with line report fn generate_select_query( table_name: &str, filter: Option<&str>, ) -> String { - let fields = T::attributes_slice() - .iter() - .map(|(k, _v)| k.to_owned()) - .collect::>() - .join(","); - let filter = filter.map_or_else(String::default, |j| " WHERE ".to_string() + j); - format!("SELECT {} FROM {}{};", fields, table_name, filter) + format!( + "SELECT {} FROM {}{};", + T::attributes_slice() + .iter() + .map(|(k, _v)| k.to_owned()) + .collect::>() + .join(","), + table_name, + filter.map_or_else(String::default, |j| " WHERE ".to_string() + j) + ) } /// Generate create table_name with fields as columns diff --git a/weaponry/src/error.rs b/weaponry/src/error.rs index d2396e7..ffc4737 100644 --- a/weaponry/src/error.rs +++ b/weaponry/src/error.rs @@ -42,6 +42,9 @@ pub enum FatalEquipementError { #[error("Couldn't parse the CLI argument given \"{0}\"")] CLIParseError(String), + + #[error("Couldn't serialize the object to make a query: \n{0}")] + SerdeRSQLite(#[from] serde_rusqlite::Error), } // #[derive(Error, Debug)] diff --git a/weaponry/src/interfaces.rs b/weaponry/src/interfaces.rs index c9be6bf..f442d08 100644 --- a/weaponry/src/interfaces.rs +++ b/weaponry/src/interfaces.rs @@ -9,7 +9,7 @@ use std::{path::Path, rc::Rc}; use more_fields::FieldsAsSlice; use rusqlite::Connection; -use serde::de::DeserializeOwned; +use serde::{Serialize, de::DeserializeOwned}; use crate::error::FatalEquipementError; @@ -95,6 +95,7 @@ where fn from_vectored(list: Vec) -> Self; /// Create a collection from an iterator of intermediate values T + #[coverage(off)] // INCORRECT: Error with line report fn from_iter_intermediate( iter_inter: impl Iterator>, ) -> Result @@ -218,18 +219,50 @@ pub trait RecipeDatabase { fn get_connection(&self) -> &Connection; /// Initialize an empty database & table - fn initialize_empty_table( + fn initialize_empty_table( &self, table: &str, - ) -> Result<(), FatalEquipementError>; + ) -> Result<(), crate::error::FatalEquipementError> { + // A vector of + self.get_connection().execute( + crate::database::generate_create_table_query(table, T::attributes_slice()).as_str(), + (), + )?; + Ok(()) + } /// Insert a row inside the database, while mostly unused this function is needed to initialize the empty table with an empty row if needed - fn insert_row(&self, row: T) -> Result<(), FatalEquipementError>; + #[coverage(off)] // INCORRECT: Error with line report + fn insert_row( + &self, + table_name: &str, + row: T, + ) -> Result<(), FatalEquipementError> { + let query = format!( + "INSERT INTO {} ({}) VALUES ({});", + table_name, + T::attributes_slice() + .iter() + .map(|(k, _v)| *k) + .collect::>() + .join(","), + T::attributes_slice() + .iter() + .map(|(k, _v)| ":".to_owned() + *k) + .collect::>() + .join(","), + ); + self.get_connection().execute( + &query, + serde_rusqlite::to_params_named(row)?.to_slice().as_slice(), + )?; + Ok(()) + } /// Serialize a request into an intermediate T representation, /// T having all the implementation specifics of the part in details /// eg. EqShape, EqPart etc.. - fn serialise_into_intermediate( + fn serialize_into_intermediate( &self, table: &str, filter: Option<&str>, @@ -312,7 +345,7 @@ pub trait Scenario where for<'a> &'a <<::InitialEquipementList { RecipeCollection::from_iter_intermediate::( self.get_database() - .serialise_into_intermediate::(self.get_tablename(), filter)?, + .serialize_into_intermediate::(self.get_tablename(), filter)?, ) } } diff --git a/weaponry/src/weaponry_structs/equipement_impl_advanced.rs b/weaponry/src/weaponry_structs/equipement_impl_advanced.rs index cf2e3a5..6fa4647 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_advanced.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_advanced.rs @@ -18,6 +18,7 @@ impl Default for FilterFunction { } } impl PartialEq for FilterFunction { + #[coverage(off)] // INCORRECT: Error with line report fn eq(&self, _other: &Self) -> bool { true } diff --git a/weaponry/src/weaponry_structs/equipement_impl_database.rs b/weaponry/src/weaponry_structs/equipement_impl_database.rs index a1c06b0..790f309 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_database.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_database.rs @@ -2,19 +2,12 @@ // File: weaponry/src/weaponry_structs/equipement_impl_database.rs //! All database specific implementations -// use crate::interfaces::DbSerializable; - -// use super::equipement_structs::{EqFinishing, EqMaterial, EqNature, EqShape}; - -use crate::database::generate_create_table_query; -use crate::error::FatalEquipementError; -use crate::interfaces::{FixedRecipe, RecipeDatabase}; -use rusqlite::Connection; -use serde::de::DeserializeOwned; - use super::EquipementSerializedCategory; use super::equipement_fields::{WeaponBaseShape, WeaponBaseSize}; use super::equipement_structs::{DatabaseEquipementStats, InnerEquipementStats}; +use crate::error::FatalEquipementError; +use crate::interfaces::{FixedRecipe, RecipeDatabase}; +use rusqlite::Connection; /// Struct for database connection. #[derive(Debug)] @@ -27,34 +20,18 @@ impl RecipeDatabase for EquipementDatabase { type DeserialisationType = DatabaseEquipementStats; type InnerType = InnerEquipementStats; - /// Checks if database exists, and returns a rusqlite::Connection. If not, opens database in memory. fn new(filename: &str) -> Result { Ok(EquipementDatabase { conn: Connection::open(filename)?, }) } - fn initialize_empty_table( - &self, - table: &str, - ) -> Result<(), crate::error::FatalEquipementError> { - // A vector of - let attr_map = T::attributes_slice(); - self.conn - .execute(generate_create_table_query(table, attr_map).as_str(), ())?; - Ok(()) - } - - fn insert_row(&self, row: T) -> Result<(), FatalEquipementError> { - let _ = row; - todo!() - } - fn get_connection(&self) -> &Connection { &self.conn } - fn serialise_into_intermediate( + #[coverage(off)] // INCORRECT: Error with line report + fn serialize_into_intermediate( &self, table: &str, filter: Option<&str>, @@ -154,6 +131,7 @@ impl TryFrom for InnerEquipementStats { type SubCategories = (Option, Option); /// Infer all subcategories from a deserialized DatabaseCategory +#[coverage(off)] // A test would literally be a copy paste of this match fn get_sub_categories_from_serde(category: &str) -> Result { Ok(if let Some(category) = from_serde!(category)? { match category { diff --git a/weaponry/src/weaponry_structs/equipement_impl_math.rs b/weaponry/src/weaponry_structs/equipement_impl_math.rs index ae883ac..32ad6f3 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_math.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_math.rs @@ -94,3 +94,23 @@ impl std::ops::Add for SpecialField { c } } + +#[cfg(test)] +mod test { + #[test] + fn test_macros() { + assert_eq!(first_correct_option!(None::, None::), None); + assert_eq!(first_correct_option!(Some(3), None::), Some(3)); + assert_eq!(first_correct_option!(None::, Some(4)), Some(4)); + assert_eq!(first_correct_option!(Some(1), Some(3)), Some(1)); + + assert_eq!(add_two_options!(Some(1), Some(3)), Some(4)); + assert_eq!(add_two_options!(Some(3), None::), Some(3)); + assert_eq!(add_two_options!(None::, Some(4)), Some(4)); + + let a = vec![1, 2]; + let b = vec![3]; + + assert_eq!(add_to_vec!(a, b), vec![1, 2, 3]); + } +} diff --git a/weaponry/src/weaponry_structs/equipement_scenario.rs b/weaponry/src/weaponry_structs/equipement_scenario.rs index d228346..1722064 100644 --- a/weaponry/src/weaponry_structs/equipement_scenario.rs +++ b/weaponry/src/weaponry_structs/equipement_scenario.rs @@ -141,22 +141,16 @@ impl Scenario for CrossRecipeScenario { #[cfg(test)] mod test { - use more_fields::FieldsAsSlice; - use crate::{ interfaces::{RecipeDatabase, Scenario}, weaponry_structs::{ equipement_impl_database::EquipementDatabase, - equipement_structs::{DatabaseEquipementStats, EqFinishing, InnerEquipementStats}, + equipement_structs::{DatabaseEquipementStats, EqFinishing}, }, }; use super::{ConfigEquipementScenario, CrossRecipeScenario}; - struct TestStruct { - id: usize, - name: String, - } #[test] fn test_scenario() {} #[test] @@ -166,7 +160,7 @@ mod test { conn: rusqlite::Connection::open_in_memory().unwrap(), }, config: ConfigEquipementScenario { - table_name: "TestStruct".to_owned(), + table_name: "Finishing".to_owned(), db_path: "None".to_owned(), shape: None, materials: None, @@ -175,12 +169,19 @@ mod test { }, }; - b.get_database().conn.execute("CREATE TABLE IF NOT EXISTS Finishing (id INTEGER UNIQUE, name TEXT, PRIMARY KEY('id' AUTOINCREMENT));",[]).unwrap(); b.get_database() .initialize_empty_table::("Finishing") .unwrap(); - // let r = b - // .deserde_into_processed_representation::(None) - // .unwrap(); + b.get_database() + .insert_row::( + "Finishing", + DatabaseEquipementStats::random_new(), + ) + .unwrap(); + ::get_database(&b); + let r = b + .deserde_into_processed_representation::(None) + .unwrap(); + assert_eq!(r.inner.len(), 1); } } diff --git a/weaponry/src/weaponry_structs/equipement_thin_types.rs b/weaponry/src/weaponry_structs/equipement_thin_types.rs index 201ce45..ce834cf 100644 --- a/weaponry/src/weaponry_structs/equipement_thin_types.rs +++ b/weaponry/src/weaponry_structs/equipement_thin_types.rs @@ -102,3 +102,19 @@ impl Display for Dices { ::fmt(&self.0, f) } } + +#[cfg(test)] +mod test { + use crate::weaponry_structs::equipement_thin_types::{Actions, Attacks, Dices, Precision}; + + #[test] + fn test_displays() { + println!( + "{}, {}, {}, {}", + Attacks(0), + Actions(1), + Precision(2), + Dices(3) + ); + } +} diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml deleted file mode 100644 index 2cdf5a3..0000000 --- a/xtask/Cargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "xtask" -edition = "2024" -version.workspace = true - -[dependencies] -xtaskops = "0.4.2" diff --git a/xtask/src/main.rs b/xtask/src/main.rs deleted file mode 100644 index 90528e7..0000000 --- a/xtask/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - let _ = xtaskops::tasks::main(); -}