Files
projectmycelium/tests/tests_archive/ux_suite/environment/test_data_manager.rs

314 lines
10 KiB
Rust

//! Test Data Management
//!
//! Manages test fixtures, user personas, and data isolation for UX testing
use serde::{Serialize, Deserialize};
use std::path::PathBuf;
use std::collections::HashMap;
/// Test user persona for different user types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestPersona {
pub email: String,
pub password: String,
pub name: String,
pub role: UserRole,
pub profile: UserProfile,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UserRole {
Consumer,
Farmer,
AppProvider,
ServiceProvider,
Admin,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserProfile {
pub country: String,
pub timezone: String,
pub currency_preference: String,
pub wallet_balance: f64,
}
/// Test marketplace data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestMarketplaceData {
pub products: Vec<TestProduct>,
pub services: Vec<TestService>,
pub nodes: Vec<TestNode>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestProduct {
pub id: String,
pub name: String,
pub category: String,
pub price: f64,
pub currency: String,
pub description: String,
pub provider_email: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestService {
pub id: String,
pub name: String,
pub description: String,
pub price: f64,
pub provider_email: String,
pub status: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestNode {
pub id: String,
pub farmer_email: String,
pub location: String,
pub specs: NodeSpecs,
pub available: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeSpecs {
pub cpu_cores: u32,
pub ram_gb: u32,
pub storage_gb: u32,
pub price_per_hour: f64,
}
/// Test data manager for UX testing
#[derive(Clone)]
pub struct TestDataManager {
test_data_dir: PathBuf,
personas: HashMap<UserRole, TestPersona>,
marketplace_data: TestMarketplaceData,
}
impl TestDataManager {
/// Create a new test data manager
pub fn new(test_data_dir: &PathBuf) -> Result<Self, Box<dyn std::error::Error>> {
let personas = Self::create_test_personas();
let marketplace_data = Self::create_test_marketplace_data(&personas);
let manager = Self {
test_data_dir: test_data_dir.clone(),
personas,
marketplace_data,
};
manager.setup_test_data()?;
Ok(manager)
}
/// Create test user personas
fn create_test_personas() -> HashMap<UserRole, TestPersona> {
let mut personas = HashMap::new();
personas.insert(UserRole::Consumer, TestPersona {
email: "user1@example.com".to_string(),
password: "testpass123".to_string(),
name: "Test Consumer".to_string(),
role: UserRole::Consumer,
profile: UserProfile {
country: "United States".to_string(),
timezone: "America/New_York".to_string(),
currency_preference: "USD".to_string(),
wallet_balance: 100.0,
},
});
personas.insert(UserRole::Farmer, TestPersona {
email: "farmer1@example.com".to_string(),
password: "testpass123".to_string(),
name: "Test Farmer".to_string(),
role: UserRole::Farmer,
profile: UserProfile {
country: "Canada".to_string(),
timezone: "America/Toronto".to_string(),
currency_preference: "CAD".to_string(),
wallet_balance: 500.0,
},
});
personas.insert(UserRole::AppProvider, TestPersona {
email: "appdev1@example.com".to_string(),
password: "testpass123".to_string(),
name: "Test App Developer".to_string(),
role: UserRole::AppProvider,
profile: UserProfile {
country: "Germany".to_string(),
timezone: "Europe/Berlin".to_string(),
currency_preference: "EUR".to_string(),
wallet_balance: 200.0,
},
});
personas.insert(UserRole::ServiceProvider, TestPersona {
email: "service1@example.com".to_string(),
password: "testpass123".to_string(),
name: "Test Service Provider".to_string(),
role: UserRole::ServiceProvider,
profile: UserProfile {
country: "United Kingdom".to_string(),
timezone: "Europe/London".to_string(),
currency_preference: "TFC".to_string(),
wallet_balance: 300.0,
},
});
personas
}
/// Create test marketplace data
fn create_test_marketplace_data(personas: &HashMap<UserRole, TestPersona>) -> TestMarketplaceData {
let farmer_email = personas.get(&UserRole::Farmer).unwrap().email.clone();
let application_provider_email = personas.get(&UserRole::AppProvider).unwrap().email.clone();
let service_provider_email = personas.get(&UserRole::ServiceProvider).unwrap().email.clone();
TestMarketplaceData {
products: vec![
TestProduct {
id: "test-vm-1".to_string(),
name: "Test VM Small".to_string(),
category: "compute".to_string(),
price: 10.0,
currency: "TFC".to_string(),
description: "Small virtual machine for testing".to_string(),
provider_email: farmer_email.clone(),
},
TestProduct {
id: "test-app-1".to_string(),
name: "Test Application".to_string(),
category: "applications".to_string(),
price: 25.0,
currency: "TFC".to_string(),
description: "Test application for UX testing".to_string(),
provider_email: application_provider_email.clone(),
},
],
services: vec![
TestService {
id: "test-service-1".to_string(),
name: "Test Consulting Service".to_string(),
description: "Professional consulting service for testing".to_string(),
price: 100.0,
provider_email: service_provider_email.clone(),
status: "available".to_string(),
},
],
nodes: vec![
TestNode {
id: "test-node-1".to_string(),
farmer_email: farmer_email.clone(),
location: "New York, USA".to_string(),
specs: NodeSpecs {
cpu_cores: 8,
ram_gb: 16,
storage_gb: 500,
price_per_hour: 2.0,
},
available: true,
},
],
}
}
/// Setup test data files
fn setup_test_data(&self) -> Result<(), Box<dyn std::error::Error>> {
// Create test data directory
std::fs::create_dir_all(&self.test_data_dir)?;
// Create user data files for each persona
for persona in self.personas.values() {
self.create_user_data_file(persona)?;
}
// Save marketplace data
let marketplace_file = self.test_data_dir.join("marketplace_data.json");
let marketplace_json = serde_json::to_string_pretty(&self.marketplace_data)?;
std::fs::write(marketplace_file, marketplace_json)?;
Ok(())
}
/// Create user data file for a persona
fn create_user_data_file(&self, persona: &TestPersona) -> Result<(), Box<dyn std::error::Error>> {
let encoded_email = persona.email.replace("@", "_at_").replace(".", "_dot_");
let user_file = self.test_data_dir.join(format!("{}.json", encoded_email));
let user_data = serde_json::json!({
"email": persona.email,
"name": persona.name,
"profile": persona.profile,
"role": persona.role,
"wallet": {
"balance": persona.profile.wallet_balance,
"currency": persona.profile.currency_preference,
"transactions": []
},
"cart": {
"items": [],
"total": 0.0
},
"orders": [],
"settings": {
"notifications": {
"security_alerts": true,
"billing_notifications": true,
"system_alerts": true,
"newsletter": false,
"dashboard_notifications": true
},
"ssh_keys": []
}
});
let user_json = serde_json::to_string_pretty(&user_data)?;
std::fs::write(user_file, user_json)?;
Ok(())
}
/// Get test persona by role
pub fn get_persona(&self, role: &UserRole) -> Option<&TestPersona> {
self.personas.get(role)
}
/// Get all test personas
pub fn get_all_personas(&self) -> &HashMap<UserRole, TestPersona> {
&self.personas
}
/// Get test marketplace data
pub fn get_marketplace_data(&self) -> &TestMarketplaceData {
&self.marketplace_data
}
/// Reset test data to clean state
pub fn reset_test_data(&self) -> Result<(), Box<dyn std::error::Error>> {
// Remove all user data files
if self.test_data_dir.exists() {
std::fs::remove_dir_all(&self.test_data_dir)?;
}
// Recreate test data
self.setup_test_data()?;
Ok(())
}
/// Cleanup test data
pub fn cleanup(&self) -> Result<(), Box<dyn std::error::Error>> {
if self.test_data_dir.exists() {
std::fs::remove_dir_all(&self.test_data_dir)?;
}
Ok(())
}
/// Get test user credentials
pub fn get_test_credentials(&self, role: &UserRole) -> Option<(String, String)> {
self.personas.get(role).map(|p| (p.email.clone(), p.password.clone()))
}
}