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
//! Integration tests for script execution (-f/--file flag)
//!
//! Tests the M2 CLI feature for executing CQL scripts from files
//! as specified in Issue #122.

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;

/// Helper to create a test script file
fn create_test_script(dir: &TempDir, filename: &str, content: &str) -> std::path::PathBuf {
    let script_path = dir.path().join(filename);
    fs::write(&script_path, content).expect("Failed to write test script");
    script_path
}

#[test]
fn test_execute_simple_script() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = r#"
-- Simple query script
SELECT * FROM users;
SELECT * FROM orders;
"#;
    let script_path = create_test_script(&temp_dir, "simple.cql", script_content);

    // Note: This test will fail without state_machine feature enabled
    // and without actual database setup. The test validates the CLI interface.
    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/Users/patrick/local_projects/cqlite/test-data/datasets/sstables/test_basic");

    // For now, we just test that the CLI accepts the -f flag
    // Full integration requires state_machine feature and proper database setup
    let output = cmd.output().unwrap();

    // Check that the flag was recognized (not an unknown argument error)
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        // Should not be "unknown argument" error
        assert!(
            !stderr.contains("unexpected argument") && !stderr.contains("unrecognized"),
            "CLI should recognize -f flag"
        );
    }
}

#[test]
fn test_script_file_not_found() {
    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg("/nonexistent/path/script.cql")
        .arg("--data-dir")
        .arg("/tmp");

    let output = cmd.output().unwrap();

    // Should fail with file not found error
    assert!(!output.status.success());

    let stderr = String::from_utf8_lossy(&output.stderr);
    // Should contain error about missing file
    assert!(
        stderr.contains("Failed to read script file")
            || stderr.contains("No such file")
            || stderr.contains("not found"),
        "Should report file not found error. Stderr: {}",
        stderr
    );
}

#[test]
fn test_script_with_unterminated_statement() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = r#"
SELECT * FROM users;
SELECT * FROM orders WHERE id = 1
"#;
    let script_path = create_test_script(&temp_dir, "unterminated.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp");

    let output = cmd.output().unwrap();

    // Should fail with parse error
    assert!(!output.status.success());

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Unterminated statement") || stderr.contains("semicolon"),
        "Should report unterminated statement. Stderr: {}",
        stderr
    );
}

#[test]
fn test_script_with_comments() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = r#"
-- This is a line comment
/* This is a block comment */
SELECT * FROM users;

/* Multi-line
   block comment */
SELECT * FROM orders; -- inline comment
"#;
    let script_path = create_test_script(&temp_dir, "comments.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp");

    // Test that comments are properly handled during parsing
    let output = cmd.output().unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    // Should not have comment-related parse errors
    assert!(
        !stderr.contains("comment") || stderr.contains("Unterminated"),
        "Comments should be parsed correctly. Stderr: {}",
        stderr
    );
}

#[test]
fn test_script_with_strings_containing_semicolons() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = r#"
SELECT * FROM users WHERE name = 'foo;bar';
SELECT * FROM orders WHERE note = 'Order; pending';
"#;
    let script_path = create_test_script(&temp_dir, "strings.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp");

    let output = cmd.output().unwrap();

    // Should parse correctly (semicolons in strings should not break statements)
    let stderr = String::from_utf8_lossy(&output.stderr);
    // Should not have parse errors related to statement termination
    assert!(
        !stderr.contains("Unterminated statement found"),
        "String semicolons should not break parsing. Stderr: {}",
        stderr
    );
}

#[test]
fn test_empty_script_file() {
    let temp_dir = TempDir::new().unwrap();
    let script_path = create_test_script(&temp_dir, "empty.cql", "");

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp");

    let output = cmd.output().unwrap();

    // Empty file should not error, just a warning
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    // Should indicate no statements to execute
    assert!(
        stdout.contains("no statements") || stderr.contains("no statements"),
        "Should handle empty file gracefully. Stdout: {}, Stderr: {}",
        stdout,
        stderr
    );
}

#[test]
fn test_script_comments_only() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = r#"
-- Just comments
/* No actual
   statements here */
"#;
    let script_path = create_test_script(&temp_dir, "comments_only.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp");

    let output = cmd.output().unwrap();

    // Comments-only file should behave like empty file
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stdout.contains("no statements") || stderr.contains("no statements"),
        "Should handle comments-only file gracefully. Stdout: {}, Stderr: {}",
        stdout,
        stderr
    );
}

#[test]
fn test_script_output_format_table() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = "SELECT * FROM users;";
    let script_path = create_test_script(&temp_dir, "query.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp")
        .arg("--out")
        .arg("table");

    // Just verify the flag combination is accepted
    let output = cmd.output().unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should not have argument parsing errors
    assert!(
        !stderr.contains("unexpected argument"),
        "Should accept --out table with -f. Stderr: {}",
        stderr
    );
}

#[test]
fn test_script_output_format_json() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = "SELECT * FROM users;";
    let script_path = create_test_script(&temp_dir, "query.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp")
        .arg("--out")
        .arg("json");

    let output = cmd.output().unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should accept JSON format
    assert!(
        !stderr.contains("unexpected argument"),
        "Should accept --out json with -f. Stderr: {}",
        stderr
    );
}

#[test]
fn test_script_output_format_csv() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = "SELECT * FROM users;";
    let script_path = create_test_script(&temp_dir, "query.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp")
        .arg("--out")
        .arg("csv");

    let output = cmd.output().unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should accept CSV format
    assert!(
        !stderr.contains("unexpected argument"),
        "Should accept --out csv with -f. Stderr: {}",
        stderr
    );
}

#[test]
fn test_script_with_multiline_statements() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = r#"
SELECT *
FROM users
WHERE status = 'active'
  AND created_at > '2025-01-01';

SELECT user_id,
       order_id,
       total
FROM orders;
"#;
    let script_path = create_test_script(&temp_dir, "multiline.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp");

    let output = cmd.output().unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should parse multi-line statements correctly
    assert!(
        !stderr.contains("Unterminated statement"),
        "Multi-line statements should parse correctly. Stderr: {}",
        stderr
    );
}

#[test]
fn test_script_with_long_file_alias() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = "SELECT * FROM users;";
    let script_path = create_test_script(&temp_dir, "query.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("--file")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp");

    let output = cmd.output().unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should accept --file as well as -f
    assert!(
        !stderr.contains("unexpected argument"),
        "Should accept --file alias. Stderr: {}",
        stderr
    );
}

#[test]
fn test_script_complex_real_world() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = r#"
-- Database exploration script
-- Generated: 2025-10-07

/* ==========================
   User Queries
   ========================== */

-- Get active users
SELECT id, name, email
FROM users
WHERE status = 'active'
LIMIT 100;

-- Get user orders
SELECT user_id, order_id, total
FROM orders
WHERE created_at > '2025-01-01'
  AND status IN ('pending', 'shipped')
ORDER BY created_at DESC;

/* Query with string containing special chars */
SELECT COUNT(*) FROM events WHERE type = 'login;logout';
"#;
    let script_path = create_test_script(&temp_dir, "complex.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp");

    let output = cmd.output().unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should parse complex script without parse errors
    assert!(
        !stderr.contains("Unterminated statement"),
        "Complex script should parse correctly. Stderr: {}",
        stderr
    );
}

#[test]
fn test_script_unterminated_block_comment() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = r#"
SELECT * FROM users;
/* This comment is not closed
SELECT * FROM orders;
"#;
    let script_path = create_test_script(&temp_dir, "bad_comment.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp");

    let output = cmd.output().unwrap();

    // Should fail with unterminated comment error
    assert!(!output.status.success());

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Unterminated block comment") || stderr.contains("comment"),
        "Should report unterminated block comment. Stderr: {}",
        stderr
    );
}

#[test]
fn test_script_unterminated_string() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = r#"
SELECT * FROM users WHERE name = 'unterminated;
"#;
    let script_path = create_test_script(&temp_dir, "bad_string.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp");

    let output = cmd.output().unwrap();

    // Should fail with unterminated string error
    assert!(!output.status.success());

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Unterminated string") || stderr.contains("string"),
        "Should report unterminated string. Stderr: {}",
        stderr
    );
}

#[test]
fn test_script_with_schema_flag() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = "SELECT * FROM users;";
    let script_path = create_test_script(&temp_dir, "query.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp")
        .arg("--schema")
        .arg("/tmp/schema.cql");

    let output = cmd.output().unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should accept --schema with -f
    assert!(
        !stderr.contains("unexpected argument"),
        "Should accept --schema with -f. Stderr: {}",
        stderr
    );
}

#[test]
fn test_conflicting_execute_and_file() {
    let temp_dir = TempDir::new().unwrap();

    let script_content = "SELECT * FROM users;";
    let script_path = create_test_script(&temp_dir, "query.cql", script_content);

    let mut cmd = Command::cargo_bin("cqlite").unwrap();
    cmd.arg("-e")
        .arg("SELECT * FROM orders;")
        .arg("-f")
        .arg(script_path.to_str().unwrap())
        .arg("--data-dir")
        .arg("/tmp");

    let output = cmd.output().unwrap();

    // May error or prioritize one over the other - depends on implementation
    // This test documents the expected behavior once implemented
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should either execute successfully or report conflict
    assert!(
        output.status.success()
            || stderr.contains("conflict")
            || stderr.contains("cannot use both"),
        "Should handle -e and -f flags appropriately. Stderr: {}",
        stderr
    );
}