rust-db-blueprint 0.1.0

A Rust code generator — reads YAML draft files and generates Axum + SQLx models, migrations, handlers, routes, requests, tests, and seeds
Documentation
use indexmap::IndexMap;

use crate::tree::Tree;
use crate::models::Statement;

pub struct TestGenerator;

impl TestGenerator {
    pub fn generate(tree: &Tree) -> IndexMap<String, String> {
        let mut files = IndexMap::new();

        for controller in tree.all_controllers() {
            let model_name = controller.name.trim_end_matches("Controller").to_string();
            let snake = model_name.chars().fold(String::new(), |mut acc, c| {
                if c.is_uppercase() && !acc.is_empty() { acc.push('_'); }
                acc.push(c.to_lowercase().next().unwrap());
                acc
            });
            let test_name = format!("{}_test", snake);
            let test_fn = snake.clone();

            let mut lines = vec![
                "use sqlx::PgPool;".to_string(),
                "use axum::http::StatusCode;".to_string(),
                format!("use crate::models::prelude::{};", model_name),
                format!("use crate::routes::app_router;"),
                String::new(),
            ];

            for (method_name, stmts) in &controller.methods {
                let stmt_line = format!(
                    "#[sqlx::test]\nasync fn test_{}_{}(pool: PgPool) {{\n    let app = app_router().with_state(pool.clone());\n\n    // TODO: implement test for {}::{}_{}\n    assert!(true);\n}}",
                    test_fn, method_name, model_name, method_name, method_name
                );
                lines.push(stmt_line);
                lines.push(String::new());
            }

            let path = format!("tests/{}_test.rs", test_name);
            files.insert(path, lines.join("\n"));
        }

        files
    }
}