Rewrite of get data, creation of set_up func for testing
This commit is contained in:
parent
26d22037ae
commit
1505a6c6f7
|
|
@ -12,6 +12,7 @@ probability = "0.20.3"
|
|||
rayon = "1.10.0"
|
||||
rusqlite = { version = "0.33.0", features = ["bundled"] }
|
||||
serde = { version = "1.0.217", features = ["derive"] }
|
||||
serde_rusqlite = "0.37.0"
|
||||
struct-field-names-as-array = "0.3.0"
|
||||
strum = { version = "0.27.0", features = ["derive"] }
|
||||
|
||||
|
|
|
|||
|
|
@ -5,8 +5,11 @@
|
|||
//! Should dump whole database in a vec of structs from weaponry_structs
|
||||
#![allow(dead_code)]
|
||||
|
||||
use crate::weaponry_structs::equipement_structs::InnerEquipementStats;
|
||||
//use crate::weaponry_structs::equipement_structs::InnerEquipementStats;
|
||||
use rusqlite::{self, Connection, Result};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde_rusqlite::*;
|
||||
use struct_field_names_as_array::FieldNamesAsArray;
|
||||
|
||||
/// Generate a SELECT query with fields generated from a struct. Returns a String
|
||||
fn generate_query(fields: &[&str], table_name: &str) -> String {
|
||||
|
|
@ -22,38 +25,54 @@ fn generate_query(fields: &[&str], table_name: &str) -> String {
|
|||
query
|
||||
}
|
||||
|
||||
/// Checks if database exists, and returns a rusqlite::Connection. If not, panics while insulting you. You deserved it.
|
||||
/// Checks if database exists, and returns a rusqlite::Connection. If not, opens database in memory.
|
||||
fn initialize_database_conn(filename: Option<String>) -> Result<Connection> {
|
||||
if let Some(text) = filename {
|
||||
let conn = Connection::open(text)?;
|
||||
Ok(conn)
|
||||
} else {
|
||||
panic!("No filename, you big oaf.");
|
||||
//let conn = Connection::open_in_memory()?;
|
||||
//Ok(conn)
|
||||
let conn = Connection::open_in_memory()?;
|
||||
Ok(conn)
|
||||
}
|
||||
}
|
||||
|
||||
/// Public function to get data from db
|
||||
pub fn get_data() -> Vec<InnerEquipementStats> {
|
||||
/* let gen_query = generate_query(&InnerStats::FIELD_NAMES_AS_ARRAY, "InnerStats");
|
||||
let db_conn = initialize_database_conn(Some("/tmp/data.sqlite3".to_owned())).unwrap();
|
||||
let mut query_result = db_conn.prepare(&gen_query);*/
|
||||
todo!()
|
||||
pub fn get_data<T>() -> Result<Vec<T>>
|
||||
where
|
||||
T: FieldNamesAsArray<2> + DeserializeOwned,
|
||||
{
|
||||
let gen_query = generate_query(&(T::FIELD_NAMES_AS_ARRAY), "InnerEquipementStats");
|
||||
let db_conn = initialize_database_conn(None)?;
|
||||
let mut query_result = db_conn.prepare(&gen_query)?;
|
||||
let res = from_rows::<T>(query_result.query([])?)
|
||||
.filter_map(|s| s.ok())
|
||||
.collect();
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde::Deserialize;
|
||||
use struct_field_names_as_array::FieldNamesAsArray;
|
||||
|
||||
use super::*;
|
||||
#[derive(FieldNamesAsArray)]
|
||||
#[derive(FieldNamesAsArray, Deserialize)]
|
||||
struct TestStruct {
|
||||
id: usize,
|
||||
name: String,
|
||||
}
|
||||
|
||||
fn set_up() -> Result<Connection> {
|
||||
let conn = initialize_database_conn(None)?;
|
||||
conn.execute("CREATE TABLE IF NOT EXISTS InnerEquipementStats (id INTEGER UNIQUE, name TEXT, PRIMARY KEY('id' AUTOINCREMENT));",())?;
|
||||
conn.execute(
|
||||
"INSERT INTO TestStuct (id, name) VALUES (1, 'dupont'), (2, 'durand');",
|
||||
(),
|
||||
)?;
|
||||
Ok(conn)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_query() {
|
||||
assert_eq!(
|
||||
|
|
@ -74,9 +93,11 @@ mod test {
|
|||
todo!()
|
||||
}
|
||||
|
||||
#[ignore]
|
||||
//#[ignore]
|
||||
#[test]
|
||||
fn test_get_data() {
|
||||
todo!()
|
||||
let _conn = set_up();
|
||||
let result = get_data::<TestStruct>();
|
||||
assert!(result.unwrap().is_empty())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
//! Weaponry module, handling all weaponry creation and analysis
|
||||
|
||||
#![warn(missing_docs, clippy::missing_docs_in_private_items)]
|
||||
#![feature(iter_intersperse)]
|
||||
#![feature(iter_intersperse, generic_arg_infer)]
|
||||
|
||||
mod database;
|
||||
mod export;
|
||||
|
|
|
|||
Loading…
Reference in New Issue