use serde_json::{json, Value};
use uuid::Uuid;
use super::crud_test_base::{CrudTestConfig, GenericCrudTest, TestDataGenerator};
use crate::integration::helpers::CommonUtils;
pub struct AttributeTestData;
impl TestDataGenerator for AttributeTestData {
fn generate_create_payload(&self, _utils: &CommonUtils) -> Value {
json!({
"id": Uuid::new_v4().to_string(),
"code": format!("TEST_{}", Uuid::new_v4().to_string().split('-').next().unwrap()),
"name": format!("Test {}", Uuid::new_v4().to_string().split('-').next().unwrap()),
"attribute_type": "color",
"status": "active",
"metadata": json!({}),
})
}
fn generate_update_payload(&self, id: &str, _utils: &CommonUtils) -> Value {
json!({
"id": id,
"code": format!("TEST_{}", Uuid::new_v4().to_string().split('-').next().unwrap()),
"name": format!("Test {}", Uuid::new_v4().to_string().split('-').next().unwrap()),
"attribute_type": "color",
"status": "active",
"metadata": json!({}),
})
}
fn generate_invalid_payload(&self) -> Value {
json!({
})
}
}
pub struct AttributeApiTest {
inner: GenericCrudTest<AttributeTestData>,
}
impl AttributeApiTest {
pub fn new() -> Self {
let mut config = CrudTestConfig::new("/api/v1/attributes", "Attribute");
config.supports_soft_delete = true;
Self {
inner: GenericCrudTest::new(config, AttributeTestData),
}
}
pub async fn run_all(&mut self) -> Vec<crate::integration::framework::TestResult> {
self.inner.run_all().await
}
}
impl Default for AttributeApiTest {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_attribute_crud() {
let mut test = AttributeApiTest::new();
let results = test.run_all().await;
for result in &results {
println!("{:?}", result);
}
let failed: Vec<_> = results.iter().filter(|r| !r.success).collect();
assert!(failed.is_empty(), "Some tests failed: {:?}", failed);
}
}