cqlite-cli 0.11.0

Command-line interface for CQLite — read Apache Cassandra 5.0 SSTables without a cluster
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! CLI testing utilities and helpers
//!
//! This module provides utilities for testing CLI commands, including
//! command builders, assertion helpers, and output validation.

use super::{TestContainer, TestResult};
use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::path::PathBuf;
use std::process::Command;

/// CLI test runner for executing and validating CLI commands
#[derive(Debug)]
pub struct CliTestRunner {
    container: TestContainer,
    binary_name: String,
    timeout: std::time::Duration,
}

/// Builder for constructing CLI test commands
#[derive(Debug)]
pub struct CliTestBuilder {
    command: String,
    args: Vec<String>,
    env_vars: Vec<(String, String)>,
    working_dir: Option<PathBuf>,
    stdin_data: Option<String>,
    timeout: Option<std::time::Duration>,
}

/// Assertion helper for CLI command results
#[derive(Debug)]
pub struct CommandAssertion {
    cmd: assert_cmd::Command,
}

impl CliTestRunner {
    /// Create a new CLI test runner
    pub fn new(container: TestContainer) -> Self {
        Self {
            container,
            binary_name: "cqlite".to_string(),
            timeout: std::time::Duration::from_secs(30),
        }
    }

    /// Set the binary name to test
    pub fn with_binary<S: Into<String>>(mut self, binary_name: S) -> Self {
        self.binary_name = binary_name.into();
        self
    }

    /// Set command timeout
    pub fn with_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Create a new command builder
    pub fn command<S: Into<String>>(&self, command: S) -> CliTestBuilder {
        CliTestBuilder::new(command)
            .with_timeout(self.timeout)
            .with_working_dir(self.container.environment().temp_dir.clone())
    }

    /// Execute a simple command with arguments
    pub fn run(&self, args: &[&str]) -> TestResult<CommandAssertion> {
        let mut cmd = Command::cargo_bin(&self.binary_name)?;
        cmd.args(args);
        cmd.current_dir(&self.container.environment().temp_dir);

        // Set up test environment variables
        let env = self.container.environment();
        cmd.env("CQLITE_CONFIG", env.config_path);
        cmd.env("CQLITE_DB", env.db_path);

        Ok(CommandAssertion {
            cmd: assert_cmd::Command::from(cmd),
        })
    }

    /// Test basic CLI functionality
    pub fn test_help(&self) -> TestResult<()> {
        self.run(&["--help"])?
            .assert_success()?
            .stdout_contains("CQLite - High-performance embedded database")?;
        Ok(())
    }

    /// Test version command
    pub fn test_version(&self) -> TestResult<()> {
        self.run(&["--version"])?
            .assert_success()?
            .stdout_contains(env!("CARGO_PKG_VERSION"))?;
        Ok(())
    }

    /// Test database info command
    pub fn test_info(&self) -> TestResult<()> {
        let sstable_fixture = self
            .container
            .environment()
            .fixtures_dir
            .join("test.sstable");
        let schema_fixture = self
            .container
            .environment()
            .fixtures_dir
            .join("test.schema");

        // Create dummy fixtures for testing
        std::fs::write(&sstable_fixture, "dummy sstable data")?;
        std::fs::write(&schema_fixture, "{\"tables\": []}")?;

        self.run(&["info", sstable_fixture.to_str().unwrap()])?
            .assert_success()?;

        Ok(())
    }
}

impl CliTestBuilder {
    /// Create a new command builder
    pub fn new<S: Into<String>>(command: S) -> Self {
        Self {
            command: command.into(),
            args: Vec::new(),
            env_vars: Vec::new(),
            working_dir: None,
            stdin_data: None,
            timeout: None,
        }
    }

    /// Add command argument
    pub fn arg<S: Into<String>>(mut self, arg: S) -> Self {
        self.args.push(arg.into());
        self
    }

    /// Add multiple arguments
    pub fn args<I, S>(mut self, args: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.args.extend(args.into_iter().map(|s| s.into()));
        self
    }

    /// Set environment variable
    pub fn env<K, V>(mut self, key: K, value: V) -> Self
    where
        K: Into<String>,
        V: Into<String>,
    {
        self.env_vars.push((key.into(), value.into()));
        self
    }

    /// Set working directory
    pub fn with_working_dir<P: Into<PathBuf>>(mut self, dir: P) -> Self {
        self.working_dir = Some(dir.into());
        self
    }

    /// Set stdin data
    pub fn with_stdin<S: Into<String>>(mut self, data: S) -> Self {
        self.stdin_data = Some(data.into());
        self
    }

    /// Set command timeout
    pub fn with_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Execute the command and return assertion helper
    pub fn execute(self) -> TestResult<CommandAssertion> {
        let mut cmd = Command::cargo_bin("cqlite")?;

        // Add command as first argument
        cmd.arg(&self.command);

        // Add all arguments
        cmd.args(&self.args);

        // Set environment variables
        for (key, value) in &self.env_vars {
            cmd.env(key, value);
        }

        // Set working directory
        if let Some(ref dir) = self.working_dir {
            cmd.current_dir(dir);
        }

        let mut assert_cmd = assert_cmd::Command::from(cmd);

        // Set stdin data if provided
        if let Some(ref stdin) = self.stdin_data {
            assert_cmd.write_stdin(stdin.as_str());
        }

        // Set timeout if provided
        if let Some(timeout) = self.timeout {
            assert_cmd.timeout(timeout);
        }

        Ok(CommandAssertion { cmd: assert_cmd })
    }
}

impl CommandAssertion {
    /// Assert command succeeds
    pub fn assert_success(mut self) -> TestResult<Self> {
        self.cmd.assert().success();
        Ok(self)
    }

    /// Assert command fails
    pub fn assert_failure(mut self) -> TestResult<Self> {
        self.cmd.assert().failure();
        Ok(self)
    }

    /// Assert stdout contains text
    pub fn stdout_contains<S: AsRef<str>>(mut self, expected: S) -> TestResult<Self> {
        self.cmd
            .assert()
            .stdout(predicate::str::contains(expected.as_ref()));
        Ok(self)
    }

    /// Assert stderr contains text
    pub fn stderr_contains<S: AsRef<str>>(mut self, expected: S) -> TestResult<Self> {
        self.cmd
            .assert()
            .stderr(predicate::str::contains(expected.as_ref()));
        Ok(self)
    }

    /// Assert stdout matches exactly
    pub fn stdout_equals<S: AsRef<str>>(mut self, expected: S) -> TestResult<Self> {
        let expected_string = expected.as_ref().to_string();
        self.cmd.assert().stdout(expected_string);
        Ok(self)
    }

    /// Assert stderr matches exactly
    pub fn stderr_equals<S: AsRef<str>>(mut self, expected: S) -> TestResult<Self> {
        let expected_string = expected.as_ref().to_string();
        self.cmd.assert().stderr(expected_string);
        Ok(self)
    }

    /// Assert stdout is empty
    pub fn stdout_empty(mut self) -> TestResult<Self> {
        self.cmd.assert().stdout(predicate::str::is_empty());
        Ok(self)
    }

    /// Assert stderr is empty
    pub fn stderr_empty(mut self) -> TestResult<Self> {
        self.cmd.assert().stderr(predicate::str::is_empty());
        Ok(self)
    }

    /// Assert exit code
    pub fn exit_code(mut self, code: i32) -> TestResult<Self> {
        self.cmd.assert().code(code);
        Ok(self)
    }

    /// Get the raw assert_cmd::Command for custom assertions
    pub fn raw_command(self) -> assert_cmd::Command {
        self.cmd
    }
}

/// Common CLI test scenarios
pub struct CliTestScenarios;

impl CliTestScenarios {
    /// Test all basic CLI commands
    pub fn test_basic_commands(runner: &CliTestRunner) -> TestResult<()> {
        // Test help command
        runner.test_help()?;

        // Test version command
        runner.test_version()?;

        println!("✅ Basic CLI commands test passed");
        Ok(())
    }

    /// Test query execution commands
    pub fn test_query_commands(runner: &CliTestRunner) -> TestResult<()> {
        let env = runner.container.environment();
        let schema_file = env.fixtures_dir.join("test_schema.json");
        let sstable_file = env.fixtures_dir.join("test_sstable");

        // Create test fixtures
        std::fs::write(
            &schema_file,
            r#"{"tables": [{"name": "users", "columns": []}]}"#,
        )?;
        std::fs::write(&sstable_file, "test sstable data")?;

        // Test read command
        runner
            .run(&[
                "read",
                sstable_file.to_str().unwrap(),
                "--schema",
                schema_file.to_str().unwrap(),
            ])?
            .assert_success()?;

        // Test info command
        runner
            .run(&["info", sstable_file.to_str().unwrap()])?
            .assert_success()?;

        println!("✅ Query commands test passed");
        Ok(())
    }

    /// Test error handling
    pub fn test_error_handling(runner: &CliTestRunner) -> TestResult<()> {
        // Test invalid command
        runner
            .run(&["invalid_command"])?
            .assert_failure()?
            .stderr_contains("error:")?;

        // Test missing required argument
        runner
            .run(&["read-sstable"])?
            .assert_failure()?
            .stderr_contains("required")?;

        // Test non-existent file
        runner
            .run(&[
                "read-sstable",
                "/non/existent/file",
                "--schema",
                "/non/existent/schema",
            ])?
            .assert_failure()?;

        println!("✅ Error handling test passed");
        Ok(())
    }

    /// Test configuration handling
    pub fn test_config_handling(runner: &CliTestRunner) -> TestResult<()> {
        let env = runner.container.environment();

        // Test with custom config file
        runner
            .run(&["--config", env.config_path.to_str().unwrap(), "--help"])?
            .assert_success()?;

        // Test verbose flag
        runner.run(&["-v", "--help"])?.assert_success()?;

        // Test quiet flag
        runner.run(&["-q", "--help"])?.assert_success()?;

        println!("✅ Configuration handling test passed");
        Ok(())
    }

    /// Run all CLI test scenarios
    pub fn run_all(runner: &CliTestRunner) -> TestResult<()> {
        Self::test_basic_commands(runner)?;
        Self::test_query_commands(runner)?;
        Self::test_error_handling(runner)?;
        Self::test_config_handling(runner)?;

        println!("🎉 All CLI test scenarios passed!");
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_infrastructure::TestContainer;

    #[tokio::test]
    async fn test_cli_runner_creation() {
        let container = TestContainer::new().unwrap();
        let runner = CliTestRunner::new(container)
            .with_binary("cqlite")
            .with_timeout(std::time::Duration::from_secs(10));

        assert_eq!(runner.binary_name, "cqlite");
        assert_eq!(runner.timeout, std::time::Duration::from_secs(10));
    }

    #[test]
    fn test_command_builder() {
        let builder = CliTestBuilder::new("read")
            .arg("test.sstable")
            .arg("--schema")
            .arg("test.schema")
            .env("TEST_VAR", "test_value")
            .with_timeout(std::time::Duration::from_secs(30));

        assert_eq!(builder.command, "read");
        assert_eq!(builder.args.len(), 3);
        assert_eq!(builder.env_vars.len(), 1);
        assert_eq!(builder.timeout, Some(std::time::Duration::from_secs(30)));
    }
}