Files
madbase/storage/tests/test_utils.rs
Vlad Durnea cffdf8af86
Some checks failed
CI/CD Pipeline / unit-tests (push) Failing after 1m16s
CI/CD Pipeline / integration-tests (push) Failing after 2m32s
CI/CD Pipeline / lint (push) Successful in 5m22s
CI/CD Pipeline / e2e-tests (push) Has been skipped
CI/CD Pipeline / build (push) Has been skipped
wip:milestone 0 fixes
2026-03-15 12:35:42 +02:00

61 lines
2.1 KiB
Rust

// Test utilities for storage module
use aws_sdk_s3::{Client, config::Region};
use aws_config::BehaviorVersion;
use aws_sdk_s3::config::Credentials;
use bytes::Bytes;
use std::env;
use sqlx::PgPool;
pub struct TestConfig {
pub database_url: String,
pub s3_endpoint: String,
pub s3_access_key: String,
pub s3_secret_key: String,
pub s3_bucket: String,
}
impl Default for TestConfig {
fn default() -> Self {
Self {
database_url: env::var("TEST_DATABASE_URL").unwrap_or_else(|_| "postgresql://postgres:postgres@localhost:5432/madbase_test".to_string()),
s3_endpoint: env::var("TEST_S3_ENDPOINT").unwrap_or_else(|_| "http://localhost:9000".to_string()),
s3_access_key: env::var("TEST_S3_ACCESS_KEY").unwrap_or_else(|_| "minioadmin".to_string()),
s3_secret_key: env::var("TEST_S3_SECRET_KEY").unwrap_or_else(|_| "minioadmin".to_string()),
s3_bucket: env::var("TEST_S3_BUCKET").unwrap_or_else(|_| "madbase-test".to_string()),
}
}
}
pub fn generate_test_data(size: usize) -> Bytes {
let data: Vec<u8> = (0..size).map(|i| (i % 256) as u8).collect();
Bytes::from(data)
}
pub async fn create_test_bucket(pool: &PgPool, bucket_id: &str, public: bool) {
sqlx::query("INSERT INTO storage.buckets (id, name, public) VALUES ($1, $2, $3) ON CONFLICT (id) DO UPDATE SET name = $2, public = $3")
.bind(bucket_id)
.bind(format!("test_bucket_{}", bucket_id))
.bind(public)
.execute(pool)
.await
.expect("Failed to create test bucket");
}
pub async fn cleanup_test_data(pool: &PgPool, bucket_id: &str) {
sqlx::query("DELETE FROM storage.objects WHERE bucket_id = $1").bind(bucket_id).execute(pool).await.ok();
sqlx::query("DELETE FROM storage.buckets WHERE id = $1").bind(bucket_id).execute(pool).await.ok();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_generate_test_data() {
let data = generate_test_data(1024);
assert_eq!(data.len(), 1024);
assert_eq!(data[0], 0);
assert_eq!(data[255], 255);
}
}