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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Test assertion utilities
//!
//! This module provides specialized assertion helpers for validating
//! CLI output, database results, and error conditions.

use super::TestResult;
use serde_json::Value;
use std::collections::HashMap;

/// Assertion helper for validating command results
#[derive(Debug)]
pub struct ResultAssertion {
    actual: CommandResult,
}

/// Assertion helper for validating CLI output
#[derive(Debug)]
pub struct OutputAssertion {
    stdout: String,
    stderr: String,
    exit_code: i32,
}

/// Assertion helper for validating error conditions
#[derive(Debug)]
pub struct ErrorAssertion {
    error: Box<dyn std::error::Error + Send + Sync>,
}

/// Represents a command execution result
#[derive(Debug, Clone)]
pub struct CommandResult {
    pub success: bool,
    pub stdout: String,
    pub stderr: String,
    pub exit_code: i32,
    pub execution_time: std::time::Duration,
}

/// Represents database query results
#[derive(Debug, Clone)]
pub struct QueryResult {
    pub rows: Vec<HashMap<String, Value>>,
    pub column_names: Vec<String>,
    pub row_count: usize,
    pub execution_time: std::time::Duration,
}

/// Represents parsed JSON output
#[derive(Debug, Clone)]
pub struct JsonOutput {
    pub data: Value,
    pub is_array: bool,
    pub is_object: bool,
}

impl ResultAssertion {
    /// Create a new result assertion
    pub fn new(result: CommandResult) -> Self {
        Self { actual: result }
    }

    /// Assert that the command succeeded
    pub fn success(self) -> TestResult<Self> {
        if !self.actual.success {
            return Err(format!(
                "Expected command to succeed, but it failed with exit code {}. stderr: {}",
                self.actual.exit_code, self.actual.stderr
            )
            .into());
        }
        Ok(self)
    }

    /// Assert that the command failed
    pub fn failure(self) -> TestResult<Self> {
        if self.actual.success {
            return Err(format!(
                "Expected command to fail, but it succeeded. stdout: {}",
                self.actual.stdout
            )
            .into());
        }
        Ok(self)
    }

    /// Assert specific exit code
    pub fn exit_code(self, expected_code: i32) -> TestResult<Self> {
        if self.actual.exit_code != expected_code {
            return Err(format!(
                "Expected exit code {}, but got {}",
                expected_code, self.actual.exit_code
            )
            .into());
        }
        Ok(self)
    }

    /// Check execution time
    pub fn execution_time_less_than(self, max_time: std::time::Duration) -> TestResult<Self> {
        if self.actual.execution_time > max_time {
            return Err(format!(
                "Expected execution time to be less than {:?}, but got {:?}",
                max_time, self.actual.execution_time
            )
            .into());
        }
        Ok(self)
    }

    /// Get output assertion
    pub fn output(self) -> OutputAssertion {
        OutputAssertion {
            stdout: self.actual.stdout,
            stderr: self.actual.stderr,
            exit_code: self.actual.exit_code,
        }
    }
}

impl OutputAssertion {
    /// Create a new output assertion
    pub fn new(stdout: String, stderr: String, exit_code: i32) -> Self {
        Self {
            stdout,
            stderr,
            exit_code,
        }
    }

    /// Assert stdout contains text
    pub fn stdout_contains<S: AsRef<str>>(&self, expected: S) -> TestResult<Self> {
        let expected = expected.as_ref();
        if !self.stdout.contains(expected) {
            return Err(format!(
                "Expected stdout to contain '{}', but got: '{}'",
                expected, self.stdout
            )
            .into());
        }
        Ok(Self {
            stdout: self.stdout.clone(),
            stderr: self.stderr.clone(),
            exit_code: self.exit_code,
        })
    }

    /// Assert stderr contains text
    pub fn stderr_contains<S: AsRef<str>>(&self, expected: S) -> TestResult<Self> {
        let expected = expected.as_ref();
        if !self.stderr.contains(expected) {
            return Err(format!(
                "Expected stderr to contain '{}', but got: '{}'",
                expected, self.stderr
            )
            .into());
        }
        Ok(Self {
            stdout: self.stdout.clone(),
            stderr: self.stderr.clone(),
            exit_code: self.exit_code,
        })
    }

    /// Assert stdout matches exactly
    pub fn stdout_equals<S: AsRef<str>>(&self, expected: S) -> TestResult<Self> {
        let expected = expected.as_ref();
        if self.stdout.trim() != expected {
            return Err(format!(
                "Expected stdout to equal '{}', but got: '{}'",
                expected, self.stdout
            )
            .into());
        }
        Ok(Self {
            stdout: self.stdout.clone(),
            stderr: self.stderr.clone(),
            exit_code: self.exit_code,
        })
    }

    /// Assert stderr is empty
    pub fn stderr_empty(self) -> TestResult<Self> {
        if !self.stderr.trim().is_empty() {
            return Err(format!("Expected stderr to be empty, but got: '{}'", self.stderr).into());
        }
        Ok(self)
    }

    /// Assert stdout is empty
    pub fn stdout_empty(self) -> TestResult<Self> {
        if !self.stdout.trim().is_empty() {
            return Err(format!("Expected stdout to be empty, but got: '{}'", self.stdout).into());
        }
        Ok(self)
    }

    /// Parse stdout as JSON and return JSON assertion
    pub fn parse_json(self) -> TestResult<JsonAssertion> {
        let json: Value = serde_json::from_str(&self.stdout)
            .map_err(|e| format!("Failed to parse stdout as JSON: {}", e))?;

        Ok(JsonAssertion::new(json))
    }

    /// Assert stdout contains valid JSON
    pub fn stdout_is_json(self) -> TestResult<JsonAssertion> {
        self.parse_json()
    }

    /// Parse stdout as table output and validate structure
    pub fn stdout_is_table(self) -> TestResult<TableAssertion> {
        TableAssertion::parse_from_string(&self.stdout)
    }

    /// Assert stdout matches regex pattern
    pub fn stdout_matches_regex<S: AsRef<str>>(self, pattern: S) -> TestResult<Self> {
        let regex = regex::Regex::new(pattern.as_ref())
            .map_err(|e| format!("Invalid regex pattern: {}", e))?;

        if !regex.is_match(&self.stdout) {
            return Err(format!(
                "Expected stdout to match regex '{}', but got: '{}'",
                pattern.as_ref(),
                self.stdout
            )
            .into());
        }
        Ok(self)
    }

    /// Count lines in stdout
    pub fn stdout_line_count(self, expected_count: usize) -> TestResult<Self> {
        let actual_count = self.stdout.lines().count();
        if actual_count != expected_count {
            return Err(format!(
                "Expected {} lines in stdout, but got {}. Content: '{}'",
                expected_count, actual_count, self.stdout
            )
            .into());
        }
        Ok(self)
    }
}

/// Assertion helper for JSON data
#[derive(Debug)]
pub struct JsonAssertion {
    data: Value,
}

impl JsonAssertion {
    /// Create a new JSON assertion
    pub fn new(data: Value) -> Self {
        Self { data }
    }

    /// Assert JSON is an object
    pub fn is_object(&self) -> TestResult<Self> {
        if !self.data.is_object() {
            return Err(format!(
                "Expected JSON to be an object, but got: {}",
                serde_json::to_string(&self.data).unwrap_or_default()
            )
            .into());
        }
        Ok(Self {
            data: self.data.clone(),
        })
    }

    /// Assert JSON is an array
    pub fn is_array(&self) -> TestResult<Self> {
        if !self.data.is_array() {
            return Err(format!(
                "Expected JSON to be an array, but got: {}",
                serde_json::to_string(&self.data).unwrap_or_default()
            )
            .into());
        }
        Ok(Self {
            data: self.data.clone(),
        })
    }

    /// Assert JSON has specific field
    pub fn has_field<S: AsRef<str>>(&self, field_name: S) -> TestResult<Self> {
        let field_name = field_name.as_ref();
        if let Value::Object(ref obj) = self.data {
            if !obj.contains_key(field_name) {
                return Err(format!(
                    "Expected JSON to have field '{}', but it was missing. Available fields: {:?}",
                    field_name,
                    obj.keys().collect::<Vec<_>>()
                )
                .into());
            }
        } else {
            return Err("Cannot check field on non-object JSON".into());
        }
        Ok(Self {
            data: self.data.clone(),
        })
    }

    /// Assert field equals specific value
    pub fn field_equals<S: AsRef<str>>(self, field_name: S, expected: Value) -> TestResult<Self> {
        let field_name = field_name.as_ref();
        if let Value::Object(ref obj) = self.data {
            if let Some(actual) = obj.get(field_name) {
                if actual != &expected {
                    return Err(format!(
                        "Expected field '{}' to equal {}, but got {}",
                        field_name,
                        serde_json::to_string(&expected).unwrap_or_default(),
                        serde_json::to_string(actual).unwrap_or_default()
                    )
                    .into());
                }
            } else {
                return Err(format!("Field '{}' not found in JSON object", field_name).into());
            }
        } else {
            return Err("Cannot check field on non-object JSON".into());
        }
        Ok(self)
    }

    /// Assert array has specific length
    pub fn array_length(self, expected_length: usize) -> TestResult<Self> {
        if let Value::Array(ref arr) = self.data {
            if arr.len() != expected_length {
                return Err(format!(
                    "Expected array to have length {}, but got {}",
                    expected_length,
                    arr.len()
                )
                .into());
            }
        } else {
            return Err("Cannot check array length on non-array JSON".into());
        }
        Ok(self)
    }

    /// Get the underlying JSON value
    pub fn value(&self) -> &Value {
        &self.data
    }
}

/// Assertion helper for table output
#[derive(Debug, Clone)]
pub struct TableAssertion {
    headers: Vec<String>,
    rows: Vec<Vec<String>>,
}

impl TableAssertion {
    /// Parse table from string output
    pub fn parse_from_string(output: &str) -> TestResult<Self> {
        let lines: Vec<&str> = output.lines().collect();

        if lines.is_empty() {
            return Err("Empty table output".into());
        }

        // Simple table parsing - assumes first line is headers
        // This is a basic implementation; you might want to improve it
        let headers: Vec<String> = lines[0]
            .split('|')
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect();

        let mut rows = Vec::new();
        for line in &lines[2..] {
            // Skip header and separator line
            if line.trim().is_empty() {
                continue;
            }

            let row: Vec<String> = line
                .split('|')
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect();

            if !row.is_empty() {
                rows.push(row);
            }
        }

        Ok(Self { headers, rows })
    }

    /// Assert table has specific number of columns
    pub fn column_count(&self, expected_count: usize) -> TestResult<Self> {
        if self.headers.len() != expected_count {
            return Err(format!(
                "Expected {} columns, but got {}. Headers: {:?}",
                expected_count,
                self.headers.len(),
                self.headers
            )
            .into());
        }
        Ok(Self {
            headers: self.headers.clone(),
            rows: self.rows.clone(),
        })
    }

    /// Assert table has specific number of rows
    pub fn row_count(&self, expected_count: usize) -> TestResult<Self> {
        if self.rows.len() != expected_count {
            return Err(format!(
                "Expected {} rows, but got {}",
                expected_count,
                self.rows.len()
            )
            .into());
        }
        Ok(self.clone())
    }

    /// Assert table has specific header
    pub fn has_header<S: AsRef<str>>(&self, header_name: S) -> TestResult<&Self> {
        let header_name = header_name.as_ref();
        if !self.headers.contains(&header_name.to_string()) {
            return Err(format!(
                "Expected header '{}', but headers are: {:?}",
                header_name, self.headers
            )
            .into());
        }
        Ok(self)
    }

    /// Assert cell value at specific position
    pub fn cell_equals<S: AsRef<str>>(
        &self,
        row: usize,
        col: usize,
        expected: S,
    ) -> TestResult<&Self> {
        let expected = expected.as_ref();

        if row >= self.rows.len() {
            return Err(
                format!("Row {} does not exist (only {} rows)", row, self.rows.len()).into(),
            );
        }

        if col >= self.rows[row].len() {
            return Err(format!(
                "Column {} does not exist in row {} (only {} columns)",
                col,
                row,
                self.rows[row].len()
            )
            .into());
        }

        let actual = &self.rows[row][col];
        if actual != expected {
            return Err(format!(
                "Expected cell at ({}, {}) to be '{}', but got '{}'",
                row, col, expected, actual
            )
            .into());
        }

        Ok(self)
    }
}

impl ErrorAssertion {
    /// Create a new error assertion
    pub fn new(error: Box<dyn std::error::Error + Send + Sync>) -> Self {
        Self { error }
    }

    /// Assert error message contains text
    pub fn message_contains<S: AsRef<str>>(self, expected: S) -> TestResult<Self> {
        let expected = expected.as_ref();
        let actual = self.error.to_string();

        if !actual.contains(expected) {
            return Err(format!(
                "Expected error message to contain '{}', but got: '{}'",
                expected, actual
            )
            .into());
        }
        Ok(self)
    }

    /// Assert error is of specific type
    pub fn is_type<T: std::error::Error + 'static>(self) -> TestResult<Self> {
        if self.error.downcast_ref::<T>().is_none() {
            return Err(format!(
                "Expected error to be of type {}, but got different type",
                std::any::type_name::<T>()
            )
            .into());
        }
        Ok(self)
    }
}

/// Convenient macros for assertions
#[macro_export]
macro_rules! assert_json_field {
    ($json:expr, $field:expr, $expected:expr) => {
        $json.has_field($field)?.field_equals($field, $expected)?
    };
}

#[macro_export]
macro_rules! assert_table_cell {
    ($table:expr, $row:expr, $col:expr, $expected:expr) => {
        $table.cell_equals($row, $col, $expected)?
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_command_result_assertion() {
        let result = CommandResult {
            success: true,
            stdout: "Hello, World!".to_string(),
            stderr: "".to_string(),
            exit_code: 0,
            execution_time: std::time::Duration::from_millis(100),
        };

        let assertion = ResultAssertion::new(result);
        assert!(assertion.success().is_ok());
    }

    #[test]
    fn test_output_assertion() {
        let assertion = OutputAssertion::new("Hello, World!".to_string(), "".to_string(), 0);

        assert!(assertion.stdout_contains("Hello").is_ok());
        assert!(assertion.stdout_contains("Missing").is_err());
    }

    #[test]
    fn test_json_assertion() {
        let json_data = json!({
            "name": "test",
            "count": 42,
            "items": [1, 2, 3]
        });

        let assertion = JsonAssertion::new(json_data);
        assert!(assertion.is_object().is_ok());
        assert!(assertion.has_field("name").is_ok());
        assert!(assertion.has_field("missing").is_err());
    }

    #[test]
    fn test_table_assertion() {
        let table_output = "| Name | Age | Email |\n|------|-----|-------|\n| John | 30  | john@example.com |\n| Jane | 25  | jane@example.com |";

        let assertion = TableAssertion::parse_from_string(table_output).unwrap();
        assert!(assertion.column_count(3).is_ok());
        assert!(assertion.row_count(2).is_ok());
        assert!(assertion.has_header("Name").is_ok());
        assert!(assertion.cell_equals(0, 0, "John").is_ok());
    }
}