ggen-cli-lib 26.7.2

CLI interface for ggen
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
#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::needless_raw_string_hashes,
    clippy::duration_suboptimal_units,
    clippy::branches_sharing_code,
    clippy::used_underscore_binding,
    clippy::single_char_pattern,
    clippy::ignore_without_reason,
    clippy::cloned_ref_to_slice_refs,
    clippy::doc_overindented_list_items,
    clippy::match_wildcard_for_single_variants,
    clippy::ignored_unit_patterns,
    clippy::needless_collect,
    clippy::unnecessary_map_or,
    clippy::manual_flatten,
    clippy::manual_strip,
    clippy::future_not_send,
    clippy::unnested_or_patterns,
    clippy::no_effect_underscore_binding,
    clippy::literal_string_with_formatting_args
)]

//! End-to-end integration tests for RDF graph operations
//!
//! **Chicago TDD Principles**:
//! - REAL RDF graph operations with Oxigraph
//! - REAL SPARQL query execution
//! - REAL file system operations for graph storage
//! - REAL state verification of graph contents
//! - NO mocking of RDF engine
//!
//! **Critical User Workflows (80/20)**:
//! 1. Load RDF data into graph
//! 2. Query graph with SPARQL
//! 3. Export graph data
//! 4. Validate graph structure
//! 5. Generate code from graph queries

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

/// Helper to create ggen command
fn ggen() -> Command {
    Command::cargo_bin("ggen").expect("Failed to find ggen binary")
}

/// Helper to create test RDF data
fn create_test_rdf(temp_dir: &TempDir) -> std::path::PathBuf {
    let rdf_file = temp_dir.path().join("test.ttl");

    let rdf_content = r#"
@prefix ex: <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

ex:Person a rdfs:Class ;
    rdfs:label "Person" ;
    rdfs:comment "A human being" .

ex:name a rdf:Property ;
    rdfs:domain ex:Person ;
    rdfs:range rdfs:Literal ;
    rdfs:label "name" .

ex:age a rdf:Property ;
    rdfs:domain ex:Person ;
    rdfs:range rdfs:Literal ;
    rdfs:label "age" .

ex:john a ex:Person ;
    ex:name "John Doe" ;
    ex:age "30" .

ex:jane a ex:Person ;
    ex:name "Jane Smith" ;
    ex:age "28" .
"#;

    fs::write(&rdf_file, rdf_content).expect("Failed to write RDF file");
    rdf_file
}

#[test]
#[ignore]
fn test_graph_load_turtle_format() {
    // Chicago TDD: Verify real RDF loading into graph store
    let temp_dir = TempDir::new().unwrap();
    let rdf_file = create_test_rdf(&temp_dir);

    ggen()
        .arg("graph")
        .arg("load")
        .arg(rdf_file.to_str().unwrap())
        .current_dir(&temp_dir)
        .assert()
        .success();

    // Verify state: Graph file created
    let _graph_path = temp_dir.path().join(".ggen/graph");
    // Note: Graph may be stored in memory or file, implementation-dependent
}

#[test]
#[ignore]
fn test_graph_load_invalid_format() {
    // Chicago TDD: Verify error state for invalid RDF
    let temp_dir = TempDir::new().unwrap();
    let invalid_file = temp_dir.path().join("invalid.ttl");
    fs::write(&invalid_file, "not valid RDF @!#$%").unwrap();

    ggen()
        .arg("graph")
        .arg("load")
        .arg(invalid_file.to_str().unwrap())
        .current_dir(&temp_dir)
        .assert()
        .failure()
        .stderr(predicate::str::contains("error").or(predicate::str::contains("invalid")));
}

#[test]
#[ignore]
fn test_graph_query_sparql_select() {
    // Chicago TDD: Verify real SPARQL query execution
    let temp_dir = TempDir::new().unwrap();
    let rdf_file = create_test_rdf(&temp_dir);

    // Load graph first
    ggen()
        .arg("graph")
        .arg("load")
        .arg(rdf_file.to_str().unwrap())
        .current_dir(&temp_dir)
        .assert()
        .success();

    // Query for all persons
    let query = "SELECT ?person WHERE { ?person a <http://example.org/Person> }";

    ggen()
        .arg("graph")
        .arg("query")
        .arg(query)
        .current_dir(&temp_dir)
        .assert()
        .success()
        .stdout(predicate::str::contains("person").or(predicate::str::contains("result")));
}

#[test]
#[ignore]
fn test_graph_query_invalid_sparql() {
    // Chicago TDD: Verify error handling for invalid SPARQL
    let temp_dir = TempDir::new().unwrap();

    ggen()
        .arg("graph")
        .arg("query")
        .arg("INVALID SPARQL SYNTAX")
        .current_dir(&temp_dir)
        .assert()
        .failure()
        .stderr(predicate::str::contains("error").or(predicate::str::contains("invalid")));
}

#[test]
#[ignore]
fn test_graph_export_turtle() {
    // Chicago TDD: Verify graph export to file
    let temp_dir = TempDir::new().unwrap();
    let rdf_file = create_test_rdf(&temp_dir);

    // Load graph
    ggen()
        .arg("graph")
        .arg("load")
        .arg(rdf_file.to_str().unwrap())
        .current_dir(&temp_dir)
        .assert()
        .success();

    // Export graph
    let export_file = temp_dir.path().join("export.ttl");

    ggen()
        .arg("graph")
        .arg("export")
        .arg(export_file.to_str().unwrap())
        .arg("--format")
        .arg("turtle")
        .current_dir(&temp_dir)
        .assert()
        .success();

    // Verify state: Export file created
    assert!(export_file.exists(), "Export file should be created");

    // Verify state: Export contains RDF data
    let content = fs::read_to_string(&export_file).unwrap();
    assert!(
        content.contains("@prefix") || !content.is_empty(),
        "Export should contain RDF data"
    );
}

#[test]
#[ignore]
fn test_graph_validate_structure() {
    // Chicago TDD: Verify graph validation
    let temp_dir = TempDir::new().unwrap();
    let rdf_file = create_test_rdf(&temp_dir);

    // Load graph
    ggen()
        .arg("graph")
        .arg("load")
        .arg(rdf_file.to_str().unwrap())
        .current_dir(&temp_dir)
        .assert()
        .success();

    // Validate graph
    ggen()
        .arg("graph")
        .arg("validate")
        .current_dir(&temp_dir)
        .assert()
        .success();
}

#[test]
#[ignore]
fn test_graph_stats_shows_metrics() {
    // Chicago TDD: Verify graph statistics state
    let temp_dir = TempDir::new().unwrap();
    let rdf_file = create_test_rdf(&temp_dir);

    // Load graph
    ggen()
        .arg("graph")
        .arg("load")
        .arg(rdf_file.to_str().unwrap())
        .current_dir(&temp_dir)
        .assert()
        .success();

    // Get stats
    ggen()
        .arg("graph")
        .arg("stats")
        .current_dir(&temp_dir)
        .assert()
        .success()
        .stdout(
            predicate::str::contains("triples")
                .or(predicate::str::contains("subjects"))
                .or(predicate::str::contains("stats")),
        );
}

#[test]
#[ignore]
fn test_graph_snapshot_create() {
    // Chicago TDD: Verify graph snapshot state creation
    let temp_dir = TempDir::new().unwrap();
    let rdf_file = create_test_rdf(&temp_dir);

    // Load graph
    ggen()
        .arg("graph")
        .arg("load")
        .arg(rdf_file.to_str().unwrap())
        .current_dir(&temp_dir)
        .assert()
        .success();

    // Create snapshot
    ggen()
        .arg("graph")
        .arg("snapshot")
        .arg("create")
        .arg("v1.0")
        .current_dir(&temp_dir)
        .assert()
        .success();
}

#[test]
#[ignore]
fn test_graph_snapshot_list() {
    // Chicago TDD: Verify snapshot listing state
    let temp_dir = TempDir::new().unwrap();

    ggen()
        .arg("graph")
        .arg("snapshot")
        .arg("list")
        .current_dir(&temp_dir)
        .assert()
        .success()
        .stdout(
            predicate::str::contains("snapshot")
                .or(predicate::str::contains("No snapshots"))
                .or(predicate::str::contains("Snapshots")),
        );
}

#[test]
#[ignore]
fn test_graph_diff_snapshots() {
    // Chicago TDD: Verify snapshot diff functionality
    let temp_dir = TempDir::new().unwrap();
    let rdf_file = create_test_rdf(&temp_dir);

    // Load and create first snapshot
    ggen()
        .arg("graph")
        .arg("load")
        .arg(rdf_file.to_str().unwrap())
        .current_dir(&temp_dir)
        .assert()
        .success();

    ggen()
        .arg("graph")
        .arg("snapshot")
        .arg("create")
        .arg("v1")
        .current_dir(&temp_dir)
        .assert()
        .success();

    // Diff command (may fail if only one snapshot exists)
    let _ = ggen()
        .arg("graph")
        .arg("diff")
        .arg("v1")
        .arg("v1")
        .current_dir(&temp_dir)
        .output();
}

#[test]
#[ignore]
fn test_graph_help_output() {
    // Chicago TDD: Verify help state is comprehensive
    ggen()
        .arg("graph")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("RDF graph operations"))
        .stdout(predicate::str::contains("load"))
        .stdout(predicate::str::contains("query"))
        .stdout(predicate::str::contains("export"));
}

#[test]
#[ignore]
fn test_graph_query_help() {
    // Chicago TDD: Verify verb-specific help
    ggen()
        .arg("graph")
        .arg("query")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("SPARQL").or(predicate::str::contains("query")));
}

#[test]
#[ignore]
fn test_graph_invalid_verb() {
    // Chicago TDD: Verify error handling for invalid verbs
    ggen()
        .arg("graph")
        .arg("invalid-verb")
        .assert()
        .failure()
        .stderr(predicate::str::contains("error").or(predicate::str::contains("invalid")));
}

#[test]
#[ignore]
fn test_graph_load_missing_file() {
    // Chicago TDD: Verify error state for missing RDF file
    let temp_dir = TempDir::new().unwrap();

    ggen()
        .arg("graph")
        .arg("load")
        .arg("/nonexistent/file.ttl")
        .current_dir(&temp_dir)
        .assert()
        .failure()
        .stderr(predicate::str::contains("not found").or(predicate::str::contains("error")));
}

#[test]
#[ignore]
fn test_graph_export_missing_graph() {
    // Chicago TDD: Verify error state when no graph loaded
    let temp_dir = TempDir::new().unwrap();

    let export_file = temp_dir.path().join("export.ttl");

    // Try to export without loading
    let _ = ggen()
        .arg("graph")
        .arg("export")
        .arg(export_file.to_str().unwrap())
        .current_dir(&temp_dir)
        .output();

    // Command may succeed with empty graph or fail - both valid
}

#[test]
#[ignore]
fn test_graph_performance_large_query() {
    // Chicago TDD: Verify performance for complex queries
    let temp_dir = TempDir::new().unwrap();
    let rdf_file = create_test_rdf(&temp_dir);

    ggen()
        .arg("graph")
        .arg("load")
        .arg(rdf_file.to_str().unwrap())
        .current_dir(&temp_dir)
        .assert()
        .success();

    let start = std::time::Instant::now();

    let query = r#"
        SELECT ?person ?name ?age WHERE {
            ?person a <http://example.org/Person> .
            ?person <http://example.org/name> ?name .
            ?person <http://example.org/age> ?age .
        }
    "#;

    ggen()
        .arg("graph")
        .arg("query")
        .arg(query)
        .current_dir(&temp_dir)
        .assert()
        .success();

    let duration = start.elapsed();

    // Query should complete quickly
    assert!(
        duration.as_secs() < 5,
        "SPARQL query should be fast: {:?}",
        duration
    );
}