use std::collections::HashMap;
#[allow(unused_imports)]
use njord_derive::Table;
pub trait Table {
fn get_name(&self) -> &str;
fn get_columns(&self) -> HashMap<String, String>;
fn get_column_fields(&self) -> Vec<String>;
fn get_column_values(&self) -> Vec<String>;
fn set_column_value(&mut self, column: &str, value: &str);
fn is_auto_increment_primary_key(&self, value: &str) -> bool;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn create_tables() {
#[derive(Table)]
#[table_name = "table_a"]
struct TableA {
title: String,
desc: String,
amount: u32,
}
let table_a = TableA {
title: "Table A".to_string(),
desc: "Some description for Table A".to_string(),
amount: 0,
};
#[derive(Table)]
#[table_name = "table_b"]
struct TableB {
name: String,
age: u32,
email: String,
}
let table_b = TableB {
name: "John Doe".to_string(),
age: 30,
email: "john.doe@example.com".to_string(),
};
#[derive(Table)]
#[table_name = "table_c"]
struct TableC {
product_id: i64,
product_name: String,
price: f64,
in_stock: bool,
}
let table_c = TableC {
product_id: 1001,
product_name: "Example Product".to_string(),
price: 29.99,
in_stock: true,
};
let expected_columns_a: HashMap<String, String> = vec![
("title".to_string(), "TEXT".to_string()),
("desc".to_string(), "TEXT".to_string()),
("amount".to_string(), "INTEGER".to_string()),
]
.into_iter()
.collect();
let expected_columns_b: HashMap<String, String> = vec![
("name".to_string(), "TEXT".to_string()),
("age".to_string(), "INTEGER".to_string()),
("email".to_string(), "TEXT".to_string()),
]
.into_iter()
.collect();
let expected_columns_c: HashMap<String, String> = vec![
("product_id".to_string(), "INTEGER".to_string()),
("product_name".to_string(), "TEXT".to_string()),
("price".to_string(), "REAL".to_string()),
("in_stock".to_string(), "TEXT".to_string()),
]
.into_iter()
.collect();
let expected_fields_a: Vec<String> = vec![
"title".to_string(),
"desc".to_string(),
"amount".to_string(),
];
let expected_fields_b: Vec<String> =
vec!["name".to_string(), "age".to_string(), "email".to_string()];
let expected_fields_c: Vec<String> = vec![
"product_id".to_string(),
"product_name".to_string(),
"price".to_string(),
"in_stock".to_string(),
];
let columns_a = table_a.get_columns();
for (key, value) in &expected_columns_a {
assert_eq!(columns_a.get(key), Some(value));
}
let columns_b = table_b.get_columns();
for (key, value) in &expected_columns_b {
assert_eq!(columns_b.get(key), Some(value));
}
let columns_c = table_c.get_columns();
for (key, value) in &expected_columns_c {
assert_eq!(columns_c.get(key), Some(value));
}
assert_eq!(table_a.get_column_fields(), expected_fields_a);
assert_eq!(table_b.get_column_fields(), expected_fields_b);
assert_eq!(table_c.get_column_fields(), expected_fields_c);
assert_eq!(table_a.get_name(), "table_a");
assert_eq!(table_b.get_name(), "table_b");
assert_eq!(table_c.get_name(), "table_c");
}
}