use crate::common::setup; use reqwest::Client; use serde_json::json; use uuid::Uuid; async fn create_test_{{ resource_name | snake_case }}(client: &Client) -> (Uuid, serde_json::Value) { let {{ resource_name | snake_case }}_data = json!({ {% for field in fields %} {% if field.field_type != "DateTime" and field.name != "uuid" %} "{{ field.name }}": {% if field.field_type == "String" %}"test_{{ field.name }}" {% elif field.field_type == "i32" %}42 {% elif field.field_type == "bool" %}true {% elif field.field_type == "f32" %}42.42 {% else %}"test_{{ field.name }}"{% endif %} {% if not loop.last %},{% endif %} {% endif %} {% endfor %} }); let res = client .post("http://0.0.0.0:8085/api/{{ resource_name | snake_case_to_kebab_case }}s") .json(&{{ resource_name | snake_case }}_data) .send() .await .expect("Failed to send request"); assert_eq!(res.status().as_u16(), 201); let {{ resource_name | snake_case }}: serde_json::Value = res.json().await.expect("Failed to read response"); let uuid = Uuid::parse_str({{ resource_name | snake_case }}["uuid"].as_str().unwrap()).unwrap(); (uuid, {{ resource_name | snake_case }}) } #[tokio::test] async fn test_crud_operations() { setup().await; let client = Client::new(); // Test CREATE let (uuid, created_{{ resource_name | snake_case }}) = create_test_{{ resource_name | snake_case }}(&client).await; {% for field in fields %} {% if field.field_type != "DateTime" and field.name != "uuid" %} assert_eq!(created_{{ resource_name | snake_case }}["{{ field.name }}"], {% if field.field_type == "String" %}"test_{{ field.name }}" {% elif field.field_type == "i32" %}42 {% elif field.field_type == "bool" %}true {% elif field.field_type == "f32" %}42.42 {% endif %}); {% endif %} {% endfor %} // Test READ (Get by UUID) let res = client .get(format!("http://0.0.0.0:8085/api/{{ resource_name | snake_case_to_kebab_case }}s/{}", uuid)) .send() .await .expect("Failed to send request"); assert_eq!(res.status().as_u16(), 200); let {{ resource_name | snake_case }}: serde_json::Value = res.json().await.expect("Failed to read response"); assert_eq!({{ resource_name | snake_case }}["uuid"], uuid.to_string()); // Test READ (List all) let res = client .get("http://0.0.0.0:8085/api/{{ resource_name | snake_case_to_kebab_case }}s") .send() .await .expect("Failed to send request"); assert_eq!(res.status().as_u16(), 200); let {{ resource_name | snake_case }}s: Vec = res.json().await.expect("Failed to read response"); assert!(!{{ resource_name | snake_case }}s.is_empty()); // Test UPDATE let update_data = json!({ "uuid": uuid, {% for field in fields %} {% if field.field_type != "DateTime" and field.name != "uuid" %} "{{ field.name }}": {% if field.field_type == "String" %}"updated_{{ field.name }}" {% elif field.field_type == "i32" %}100 {% elif field.field_type == "bool" %}false {% elif field.field_type == "f32" %}100.100 {% endif %} {% if not loop.last %},{% endif %} {% endif %} {% endfor %} }); let res = client .put(format!("http://0.0.0.0:8085/api/{{ resource_name | snake_case_to_kebab_case }}s/{}", uuid)) .json(&update_data) .send() .await .expect("Failed to send request"); assert_eq!(res.status().as_u16(), 200); let updated_{{ resource_name | snake_case }}: serde_json::Value = res.json().await.expect("Failed to read response"); {% for field in fields %} {% if field.field_type != "DateTime" and field.name != "uuid" %} assert_eq!(updated_{{ resource_name | snake_case }}["{{ field.name }}"], {% if field.field_type == "String" %}"updated_{{ field.name }}" {% elif field.field_type == "i32" %}100 {% elif field.field_type == "bool" %}false {% elif field.field_type == "f32" %}100.100 {% endif %}); {% endif %} {% endfor %} // Test DELETE let res = client .delete(format!("http://0.0.0.0:8085/api/{{ resource_name | snake_case_to_kebab_case }}s/{}", uuid)) .send() .await .expect("Failed to send request"); assert_eq!(res.status().as_u16(), 200); // Verify deletion let res = client .get(format!("http://0.0.0.0:8085/api/{{ resource_name | snake_case_to_kebab_case }}s/{}", uuid)) .send() .await .expect("Failed to send request"); assert_eq!(res.status().as_u16(), 404); }