clicktype-transport 0.2.0

Transport layer for ClickType - HTTP client for ClickHouse
Documentation
use crate::traits::ClickTypeTransport;
use crate::error::Result;
use async_trait::async_trait;
use std::sync::{Arc, Mutex};

/// A mock client for testing ClickType applications without a real ClickHouse server.
#[derive(Clone, Default)]
pub struct MockClient {
    state: Arc<Mutex<MockState>>,
}

#[derive(Default)]
struct MockState {
    inserted_batches: Vec<(String, Vec<u8>)>,
    schema_validations: Vec<String>,
}

impl MockClient {
    /// Create a new mock client
    pub fn new() -> Self {
        Self::default()
    }

    /// Get the number of batches inserted
    pub fn inserted_count(&self) -> usize {
        self.state.lock().unwrap().inserted_batches.len()
    }

    /// Get the data of the last inserted batch
    pub fn last_inserted_data(&self) -> Option<Vec<u8>> {
        self.state.lock().unwrap().inserted_batches.last().map(|(_, data)| data.clone())
    }

    /// Check if a table schema was validated
    pub fn was_schema_validated(&self, table: &str) -> bool {
        self.state.lock().unwrap().schema_validations.contains(&table.to_string())
    }
}

#[async_trait]
impl ClickTypeTransport for MockClient {
    async fn insert_binary(&self, table_name: &str, data: &[u8]) -> Result<()> {
        let mut state = self.state.lock().unwrap();
        state.inserted_batches.push((table_name.to_string(), data.to_vec()));
        Ok(())
    }

    async fn validate_schema(&self, table_name: &str, _expected_columns: &[(&str, String)]) -> Result<()> {
        let mut state = self.state.lock().unwrap();
        state.schema_validations.push(table_name.to_string());
        Ok(())
    }
}