185 lines
5.7 KiB
Rust
185 lines
5.7 KiB
Rust
//! Tests for the new model-DB architecture
|
|
|
|
use crate::core::{DB, DBBuilder, SledDBResult, Storable, SledModel};
|
|
use crate::zaz::factory::create_zaz_db;
|
|
use crate::zaz::models::*;
|
|
use chrono::Utc;
|
|
use std::path::Path;
|
|
use tempfile::tempdir;
|
|
|
|
#[test]
|
|
fn test_zaz_db_factory() {
|
|
// Create a temporary directory for testing
|
|
let temp_dir = tempdir().expect("Failed to create temp directory");
|
|
println!("Created temporary directory at: {:?}", temp_dir.path());
|
|
|
|
// Create a DB with all zaz models registered using the factory
|
|
let db = create_zaz_db(temp_dir.path()).expect("Failed to create zaz DB");
|
|
|
|
// Test with a user model
|
|
let user = User::new(
|
|
1,
|
|
"Factory Test User".to_string(),
|
|
"factory@example.com".to_string(),
|
|
"password".to_string(),
|
|
"Test Company".to_string(),
|
|
"User".to_string(),
|
|
);
|
|
|
|
// Insert the user
|
|
db.set(&user).expect("Failed to insert user");
|
|
|
|
// Retrieve the user
|
|
let retrieved: User = db.get(&user.id.to_string()).expect("Failed to retrieve user");
|
|
|
|
// Verify the user is correct
|
|
assert_eq!(user.name, retrieved.name);
|
|
assert_eq!(user.email, retrieved.email);
|
|
|
|
// Test with a company model
|
|
let company = Company::new(
|
|
1,
|
|
"Factory Test Corp".to_string(),
|
|
"FTC-123".to_string(),
|
|
Utc::now(),
|
|
"12-31".to_string(),
|
|
"info@ftc.com".to_string(),
|
|
"123-456-7890".to_string(),
|
|
"www.ftc.com".to_string(),
|
|
"123 Factory St".to_string(),
|
|
BusinessType::Global,
|
|
"Technology".to_string(),
|
|
"A test company for the factory pattern".to_string(),
|
|
CompanyStatus::Active,
|
|
);
|
|
|
|
// Insert the company
|
|
db.set(&company).expect("Failed to insert company");
|
|
|
|
// Retrieve the company
|
|
let retrieved: Company = db.get(&company.id.to_string()).expect("Failed to retrieve company");
|
|
|
|
// Verify the company is correct
|
|
assert_eq!(company.name, retrieved.name);
|
|
assert_eq!(company.registration_number, retrieved.registration_number);
|
|
|
|
println!("All zaz DB factory tests passed successfully!");
|
|
}
|
|
|
|
#[test]
|
|
fn test_db_builder() {
|
|
// Create a temporary directory for testing
|
|
let temp_dir = tempdir().expect("Failed to create temp directory");
|
|
println!("Created temporary directory at: {:?}", temp_dir.path());
|
|
|
|
// Create a DB with selectively registered models using the builder pattern
|
|
let db = DBBuilder::new(temp_dir.path())
|
|
.register_model::<User>()
|
|
.register_model::<Company>()
|
|
.build()
|
|
.expect("Failed to build DB");
|
|
|
|
// Test with a user model
|
|
let user = User::new(
|
|
2,
|
|
"Builder Test User".to_string(),
|
|
"builder@example.com".to_string(),
|
|
"password".to_string(),
|
|
"Test Company".to_string(),
|
|
"User".to_string(),
|
|
);
|
|
|
|
// Insert the user
|
|
db.set(&user).expect("Failed to insert user");
|
|
|
|
// Retrieve the user
|
|
let retrieved: User = db.get(&user.id.to_string()).expect("Failed to retrieve user");
|
|
|
|
// Verify the user is correct
|
|
assert_eq!(user.name, retrieved.name);
|
|
assert_eq!(user.email, retrieved.email);
|
|
|
|
// Test that unregistered models cause an error
|
|
let product = Product::new(
|
|
1,
|
|
"Unregistered Product".to_string(),
|
|
"PROD-123".to_string(),
|
|
"A test product".to_string(),
|
|
"Test".to_string(),
|
|
ProductType::Standard,
|
|
ProductStatus::Available,
|
|
Currency::USD,
|
|
100.0,
|
|
vec![],
|
|
);
|
|
|
|
// This should fail because Product was not registered
|
|
let result = db.set(&product);
|
|
assert!(result.is_err(), "Setting unregistered model should fail");
|
|
|
|
println!("All DB builder tests passed successfully!");
|
|
}
|
|
|
|
#[test]
|
|
fn test_dynamic_registration() {
|
|
// Create a temporary directory for testing
|
|
let temp_dir = tempdir().expect("Failed to create temp directory");
|
|
println!("Created temporary directory at: {:?}", temp_dir.path());
|
|
|
|
// Create an empty DB
|
|
let mut db = DB::new(temp_dir.path()).expect("Failed to create empty DB");
|
|
|
|
// Register User model dynamically
|
|
db.register::<User>().expect("Failed to register User");
|
|
|
|
// Test with a user model
|
|
let user = User::new(
|
|
3,
|
|
"Dynamic Test User".to_string(),
|
|
"dynamic@example.com".to_string(),
|
|
"password".to_string(),
|
|
"Test Company".to_string(),
|
|
"User".to_string(),
|
|
);
|
|
|
|
// Insert the user
|
|
db.set(&user).expect("Failed to insert user");
|
|
|
|
// Retrieve the user
|
|
let retrieved: User = db.get(&user.id.to_string()).expect("Failed to retrieve user");
|
|
|
|
// Verify the user is correct
|
|
assert_eq!(user.name, retrieved.name);
|
|
assert_eq!(user.email, retrieved.email);
|
|
|
|
// Now dynamically register Company
|
|
db.register::<Company>().expect("Failed to register Company");
|
|
|
|
// Test with a company model
|
|
let company = Company::new(
|
|
3,
|
|
"Dynamic Test Corp".to_string(),
|
|
"DTC-123".to_string(),
|
|
Utc::now(),
|
|
"12-31".to_string(),
|
|
"info@dtc.com".to_string(),
|
|
"123-456-7890".to_string(),
|
|
"www.dtc.com".to_string(),
|
|
"123 Dynamic St".to_string(),
|
|
BusinessType::Global,
|
|
"Technology".to_string(),
|
|
"A test company for dynamic registration".to_string(),
|
|
CompanyStatus::Active,
|
|
);
|
|
|
|
// Insert the company
|
|
db.set(&company).expect("Failed to insert company");
|
|
|
|
// Retrieve the company
|
|
let retrieved: Company = db.get(&company.id.to_string()).expect("Failed to retrieve company");
|
|
|
|
// Verify the company is correct
|
|
assert_eq!(company.name, retrieved.name);
|
|
|
|
println!("All dynamic registration tests passed successfully!");
|
|
} |