interstellar 0.2.0

A high-performance graph database with Gremlin-style traversals and GQL query language
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
//! Additional coverage tests for traversal/aggregate.rs
//!
//! This module covers edge cases and branches not covered by inline tests,
//! focusing on:
//! - value_to_map_key function for all value types
//! - List and Map keys being skipped
//! - GroupValue::Traversal returning multiple results
//! - Empty traversal results

#![allow(unused_variables)]
use interstellar::storage::Graph;
use interstellar::traversal::__;
use interstellar::value::{Value, VertexId};
use std::collections::HashMap;

// =============================================================================
// Helper Functions
// =============================================================================

fn create_basic_graph() -> Graph {
    let graph = Graph::new();

    let mut props = HashMap::new();
    props.insert("name".to_string(), Value::String("Alice".to_string()));
    props.insert("age".to_string(), Value::Int(30));
    graph.add_vertex("person", props);

    let mut props = HashMap::new();
    props.insert("name".to_string(), Value::String("Bob".to_string()));
    props.insert("age".to_string(), Value::Int(25));
    graph.add_vertex("person", props);

    graph
}

fn create_graph_with_edges() -> Graph {
    let graph = Graph::new();

    // Vertices
    let mut props = HashMap::new();
    props.insert("name".to_string(), Value::String("Alice".to_string()));
    graph.add_vertex("person", props);

    let mut props = HashMap::new();
    props.insert("name".to_string(), Value::String("Bob".to_string()));
    graph.add_vertex("person", props);

    let mut props = HashMap::new();
    props.insert("name".to_string(), Value::String("Charlie".to_string()));
    graph.add_vertex("person", props);

    // Edges
    let mut props = HashMap::new();
    props.insert("since".to_string(), Value::Int(2020));
    graph
        .add_edge(VertexId(0), VertexId(1), "knows", props)
        .unwrap();

    let mut props = HashMap::new();
    props.insert("since".to_string(), Value::Int(2021));
    graph
        .add_edge(VertexId(0), VertexId(2), "knows", props)
        .unwrap();

    let mut props = HashMap::new();
    props.insert("since".to_string(), Value::Int(2020));
    graph
        .add_edge(VertexId(1), VertexId(2), "knows", props)
        .unwrap();

    graph
}

// =============================================================================
// GroupKey Constructor Tests
// =============================================================================

mod group_key_constructors {
    use super::*;
    use interstellar::traversal::aggregate::{GroupKey, GroupValue};

    #[test]
    fn group_key_by_label() {
        let key = GroupKey::by_label();
        assert!(matches!(key, GroupKey::Label));
    }

    #[test]
    fn group_key_by_property() {
        let key = GroupKey::by_property("age");
        if let GroupKey::Property(prop) = key {
            assert_eq!(prop, "age");
        } else {
            panic!("Expected Property key");
        }
    }

    #[test]
    fn group_key_by_traversal() {
        let t = __.values("name");
        let key = GroupKey::by_traversal(t);
        assert!(matches!(key, GroupKey::Traversal(_)));
    }

    #[test]
    fn group_value_identity() {
        let value = GroupValue::identity();
        assert!(matches!(value, GroupValue::Identity));
    }

    #[test]
    fn group_value_by_property() {
        let value = GroupValue::by_property("name");
        if let GroupValue::Property(prop) = value {
            assert_eq!(prop, "name");
        } else {
            panic!("Expected Property value");
        }
    }

    #[test]
    fn group_value_by_traversal() {
        let t = __.values("name");
        let value = GroupValue::by_traversal(t);
        assert!(matches!(value, GroupValue::Traversal(_)));
    }
}

// =============================================================================
// GroupStep Key Type Tests (value_to_map_key coverage)
// =============================================================================

mod group_step_key_types {
    use super::*;

    #[test]
    fn group_by_bool_property() {
        let graph = Graph::new();

        let mut props = HashMap::new();
        props.insert("active".to_string(), Value::Bool(true));
        graph.add_vertex("user", props);

        let mut props = HashMap::new();
        props.insert("active".to_string(), Value::Bool(false));
        graph.add_vertex("user", props);

        let mut props = HashMap::new();
        props.insert("active".to_string(), Value::Bool(true));
        graph.add_vertex("user", props);

        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        let result = g
            .v()
            .has_label("user")
            .group()
            .by_key("active")
            .by_value()
            .build()
            .next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            // Bool keys become "true" and "false" strings
            assert!(map.contains_key("true") || map.contains_key("false"));
        }
    }

    #[test]
    fn group_by_float_property() {
        let graph = Graph::new();

        let mut props = HashMap::new();
        props.insert("score".to_string(), Value::Float(0.5));
        graph.add_vertex("item", props);

        let mut props = HashMap::new();
        props.insert("score".to_string(), Value::Float(0.5));
        graph.add_vertex("item", props);

        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        let result = g
            .v()
            .has_label("item")
            .group()
            .by_key("score")
            .by_value()
            .build()
            .next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            // Float key becomes string
            assert!(map.contains_key("0.5"));
        }
    }

    #[test]
    fn group_by_null_property() {
        let graph = Graph::new();

        let mut props = HashMap::new();
        props.insert("value".to_string(), Value::Null);
        graph.add_vertex("item", props);

        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        let result = g
            .v()
            .has_label("item")
            .group()
            .by_key("value")
            .by_value()
            .build()
            .next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            // Null key becomes "null" string
            assert!(map.contains_key("null"));
        }
    }
}

// =============================================================================
// GroupCountStep Key Type Tests
// =============================================================================

mod group_count_key_types {
    use super::*;

    #[test]
    fn group_count_by_bool_property() {
        let graph = Graph::new();

        let mut props = HashMap::new();
        props.insert("active".to_string(), Value::Bool(true));
        graph.add_vertex("user", props);

        let mut props = HashMap::new();
        props.insert("active".to_string(), Value::Bool(false));
        graph.add_vertex("user", props);

        let mut props = HashMap::new();
        props.insert("active".to_string(), Value::Bool(true));
        graph.add_vertex("user", props);

        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        let result = g
            .v()
            .has_label("user")
            .group_count()
            .by_key("active")
            .build()
            .next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            assert_eq!(map.get("true"), Some(&Value::Int(2)));
            assert_eq!(map.get("false"), Some(&Value::Int(1)));
        }
    }

    #[test]
    fn group_count_by_vertex_reference() {
        // This tests value_to_map_key for Vertex type
        let graph = create_graph_with_edges();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // Count edges grouped by target vertex
        let result = g.e().group_count().by_traversal(__.in_v()).build().next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            // Keys should be v[id] format
            assert!(map.keys().any(|k| k.starts_with("v[")));
        }
    }

    #[test]
    fn group_count_by_edge_reference() {
        // This tests value_to_map_key for Edge type
        let graph = create_graph_with_edges();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // Group vertices by outgoing edges (unusual but tests edge key handling)
        // First collect edges, then group by identity
        let result = g.e().group_count().by_label().build().next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            assert!(map.contains_key("knows"));
        }
    }
}

// =============================================================================
// GroupValue Traversal Tests (multiple results)
// =============================================================================

mod group_value_traversal_tests {
    use super::*;

    #[test]
    fn group_value_traversal_multiple_results() {
        let graph = create_graph_with_edges();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // Group by label, collect outgoing neighbor names
        let result = g
            .v()
            .has_label("person")
            .group()
            .by_label()
            .by_value_traversal(__.out().values("name"))
            .build()
            .next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            // Should have person key
            assert!(map.contains_key("person"));
            // Value should be list of collected values
            if let Some(Value::List(values)) = map.get("person") {
                // Alice has 2 outgoing edges, Bob has 1, Charlie has 0
                // So we should see some names collected
                assert!(!values.is_empty());
            }
        }
    }

    #[test]
    fn group_value_traversal_empty_results() {
        let graph = Graph::new();

        // Vertex with no outgoing edges
        let mut props = HashMap::new();
        props.insert("name".to_string(), Value::String("Isolated".to_string()));
        graph.add_vertex("person", props);

        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // Group by label, collect outgoing neighbor names (none exist)
        let result = g
            .v()
            .has_label("person")
            .group()
            .by_label()
            .by_value_traversal(__.out().values("name"))
            .build()
            .next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            // Person group exists but values list may be empty
            if let Some(Value::List(values)) = map.get("person") {
                assert!(values.is_empty());
            }
        }
    }

    #[test]
    fn group_value_traversal_single_result() {
        let graph = Graph::new();

        let mut props = HashMap::new();
        props.insert("name".to_string(), Value::String("Alice".to_string()));
        graph.add_vertex("person", props);

        let mut props = HashMap::new();
        props.insert("name".to_string(), Value::String("Bob".to_string()));
        graph.add_vertex("person", props);

        // Single edge from Alice to Bob
        graph
            .add_edge(VertexId(0), VertexId(1), "knows", HashMap::new())
            .unwrap();

        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // Use a traversal that returns single value (out-degree count)
        let result = g
            .v()
            .has_label("person")
            .group()
            .by_key("name")
            .by_value_traversal(__.out().values("name"))
            .build()
            .next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            // Alice has 1 outgoing edge
            if let Some(Value::List(values)) = map.get("Alice") {
                assert!(!values.is_empty());
            }
        }
    }
}

// =============================================================================
// Non-Element Input Tests
// =============================================================================

mod non_element_input_tests {
    use super::*;

    #[test]
    fn group_non_vertex_non_edge_by_label() {
        let graph = Graph::new();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // Inject integers and try to group by label (should skip them)
        let result = g
            .inject([Value::Int(1), Value::Int(2), Value::Int(3)])
            .group()
            .by_label()
            .by_value()
            .build()
            .next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            // Should be empty since integers don't have labels
            assert!(map.is_empty());
        }
    }

    #[test]
    fn group_count_non_vertex_non_edge_by_property() {
        let graph = Graph::new();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // Inject strings and try to group count by property (should skip them)
        let result = g
            .inject([
                Value::String("a".to_string()),
                Value::String("b".to_string()),
            ])
            .group_count()
            .by_key("nonexistent")
            .build()
            .next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            // Should be empty since strings don't have properties
            assert!(map.is_empty());
        }
    }
}

// =============================================================================
// Default Selector Tests
// =============================================================================

mod default_selector_tests {
    use super::*;

    #[test]
    fn group_step_default_key_is_label() {
        let graph = create_basic_graph();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // group() without by_key or by_label should default to label
        let result = g.v().group().build().next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            assert!(map.contains_key("person"));
        }
    }

    #[test]
    fn group_step_default_value_is_identity() {
        let graph = create_basic_graph();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // group().by_label() without by_value should default to identity
        let result = g.v().group().by_label().build().next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            if let Some(Value::List(values)) = map.get("person") {
                // Values should be vertices (identity)
                for v in values {
                    assert!(matches!(v, Value::Vertex(_)));
                }
            }
        }
    }

    #[test]
    fn group_count_step_default_is_label() {
        let graph = create_basic_graph();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // group_count() without by_key or by_label should default to label
        let result = g.v().group_count().build().next();

        assert!(result.is_some());
        if let Some(Value::Map(map)) = result {
            assert!(map.contains_key("person"));
            assert_eq!(map.get("person"), Some(&Value::Int(2)));
        }
    }
}

// =============================================================================
// Path Preservation Tests
// =============================================================================

mod path_preservation_tests {
    use super::*;

    #[test]
    fn group_preserves_last_path() {
        let graph = create_basic_graph();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // With path tracking enabled
        let result = g
            .v()
            .with_path()
            .as_("start")
            .group()
            .by_label()
            .by_value()
            .build()
            .next();

        // Result should exist (group doesn't fail with paths)
        assert!(result.is_some());
    }

    #[test]
    fn group_count_preserves_last_path() {
        let graph = create_basic_graph();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // With path tracking enabled
        let result = g
            .v()
            .with_path()
            .as_("start")
            .group_count()
            .by_label()
            .build()
            .next();

        // Result should exist
        assert!(result.is_some());
    }
}