codegraph 0.1.1

A fast, reliable, and flexible graph database optimized for storing and querying code relationships
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
//! Unit tests for QueryBuilder
//!
//! These tests verify the fluent query interface for finding code patterns.

use codegraph::{helpers, CodeGraph, NodeType};

#[test]
fn test_query_builder_node_type_filter() {
    let mut graph = CodeGraph::in_memory().unwrap();

    // Add various node types
    let file_id = helpers::add_file(&mut graph, "src/main.rs", "rust").unwrap();
    let func1_id = helpers::add_function(&mut graph, file_id, "func1", 1, 10).unwrap();
    let func2_id = helpers::add_function(&mut graph, file_id, "func2", 12, 20).unwrap();
    let class_id = helpers::add_class(&mut graph, file_id, "MyClass", 22, 40).unwrap();

    // Query for only functions
    let results = graph
        .query()
        .node_type(NodeType::Function)
        .execute()
        .unwrap();

    assert_eq!(results.len(), 2);
    assert!(results.contains(&func1_id));
    assert!(results.contains(&func2_id));
    assert!(!results.contains(&file_id));
    assert!(!results.contains(&class_id));
}

#[test]
fn test_query_builder_in_file_filter() {
    let mut graph = CodeGraph::in_memory().unwrap();

    // Add two files with functions
    let file1_id = helpers::add_file(&mut graph, "src/main.rs", "rust").unwrap();
    let func1_id = helpers::add_function(&mut graph, file1_id, "main", 1, 10).unwrap();

    let file2_id = helpers::add_file(&mut graph, "src/lib.rs", "rust").unwrap();
    let func2_id = helpers::add_function(&mut graph, file2_id, "helper", 1, 10).unwrap();

    // Query for functions in main.rs
    let results = graph
        .query()
        .node_type(NodeType::Function)
        .in_file("src/main.rs")
        .execute()
        .unwrap();

    assert_eq!(results.len(), 1);
    assert_eq!(results[0], func1_id);

    // Query for functions in lib.rs
    let results = graph
        .query()
        .node_type(NodeType::Function)
        .in_file("src/lib.rs")
        .execute()
        .unwrap();

    assert_eq!(results.len(), 1);
    assert_eq!(results[0], func2_id);
}

#[test]
fn test_query_builder_file_pattern_filter() {
    let mut graph = CodeGraph::in_memory().unwrap();

    // Add files with different patterns
    helpers::add_file(&mut graph, "src/main.rs", "rust").unwrap();
    helpers::add_file(&mut graph, "src/lib.rs", "rust").unwrap();
    helpers::add_file(&mut graph, "tests/test_main.rs", "rust").unwrap();
    helpers::add_file(&mut graph, "src/utils.py", "python").unwrap();

    // Query for src/*.rs files
    let results = graph
        .query()
        .node_type(NodeType::CodeFile)
        .file_pattern("src/*.rs")
        .execute()
        .unwrap();

    assert_eq!(results.len(), 2);

    // Query for all .py files
    let results = graph
        .query()
        .node_type(NodeType::CodeFile)
        .file_pattern("**/*.py")
        .execute()
        .unwrap();

    assert_eq!(results.len(), 1);
}

#[test]
fn test_query_builder_property_filter() {
    let mut graph = CodeGraph::in_memory().unwrap();

    let file_id = helpers::add_file(&mut graph, "src/main.rs", "rust").unwrap();

    // Add functions with different properties
    helpers::add_function_with_metadata(
        &mut graph,
        file_id,
        helpers::FunctionMetadata {
            name: "pub_func",
            line_start: 1,
            line_end: 10,
            visibility: "public",
            signature: "fn pub_func()",
            is_async: false,
            is_test: false,
        },
    )
    .unwrap();

    helpers::add_function_with_metadata(
        &mut graph,
        file_id,
        helpers::FunctionMetadata {
            name: "priv_func",
            line_start: 12,
            line_end: 20,
            visibility: "private",
            signature: "fn priv_func()",
            is_async: false,
            is_test: false,
        },
    )
    .unwrap();

    helpers::add_function_with_metadata(
        &mut graph,
        file_id,
        helpers::FunctionMetadata {
            name: "async_func",
            line_start: 22,
            line_end: 30,
            visibility: "public",
            signature: "async fn async_func()",
            is_async: true,
            is_test: false,
        },
    )
    .unwrap();

    // Query for public functions
    let results = graph
        .query()
        .node_type(NodeType::Function)
        .property("visibility", "public")
        .execute()
        .unwrap();

    assert_eq!(results.len(), 2);

    // Query for async functions
    let results = graph
        .query()
        .node_type(NodeType::Function)
        .property("is_async", true)
        .execute()
        .unwrap();

    assert_eq!(results.len(), 1);
}

#[test]
fn test_query_builder_name_contains_filter() {
    let mut graph = CodeGraph::in_memory().unwrap();

    let file_id = helpers::add_file(&mut graph, "src/main.rs", "rust").unwrap();
    helpers::add_function(&mut graph, file_id, "get_user", 1, 10).unwrap();
    helpers::add_function(&mut graph, file_id, "set_user", 12, 20).unwrap();
    helpers::add_function(&mut graph, file_id, "delete_user", 22, 30).unwrap();
    helpers::add_function(&mut graph, file_id, "process_data", 32, 40).unwrap();

    // Query for functions with "user" in name (case insensitive)
    let results = graph
        .query()
        .node_type(NodeType::Function)
        .name_contains("user")
        .execute()
        .unwrap();

    assert_eq!(results.len(), 3);

    // Query for functions with "data" in name
    let results = graph
        .query()
        .node_type(NodeType::Function)
        .name_contains("data")
        .execute()
        .unwrap();

    assert_eq!(results.len(), 1);
}

#[test]
fn test_query_builder_custom_predicate() {
    let mut graph = CodeGraph::in_memory().unwrap();

    let file_id = helpers::add_file(&mut graph, "src/main.rs", "rust").unwrap();
    helpers::add_function(&mut graph, file_id, "short", 1, 5).unwrap();
    helpers::add_function(&mut graph, file_id, "medium", 10, 25).unwrap();
    helpers::add_function(&mut graph, file_id, "long", 30, 100).unwrap();

    // Query for functions longer than 20 lines
    let results = graph
        .query()
        .node_type(NodeType::Function)
        .custom(|node| {
            if let (Some(start), Some(end)) = (
                node.properties.get_int("line_start"),
                node.properties.get_int("line_end"),
            ) {
                (end - start) > 20
            } else {
                false
            }
        })
        .execute()
        .unwrap();

    assert_eq!(results.len(), 1);
}

#[test]
fn test_query_builder_chaining_multiple_filters() {
    let mut graph = CodeGraph::in_memory().unwrap();

    let file1_id = helpers::add_file(&mut graph, "src/main.rs", "rust").unwrap();
    let file2_id = helpers::add_file(&mut graph, "src/lib.rs", "rust").unwrap();

    // Add various functions
    helpers::add_function_with_metadata(
        &mut graph,
        file1_id,
        helpers::FunctionMetadata {
            name: "pub_main",
            line_start: 1,
            line_end: 10,
            visibility: "public",
            signature: "fn pub_main()",
            is_async: false,
            is_test: false,
        },
    )
    .unwrap();

    helpers::add_function_with_metadata(
        &mut graph,
        file1_id,
        helpers::FunctionMetadata {
            name: "priv_helper",
            line_start: 12,
            line_end: 20,
            visibility: "private",
            signature: "fn priv_helper()",
            is_async: false,
            is_test: false,
        },
    )
    .unwrap();

    helpers::add_function_with_metadata(
        &mut graph,
        file2_id,
        helpers::FunctionMetadata {
            name: "pub_lib",
            line_start: 1,
            line_end: 10,
            visibility: "public",
            signature: "fn pub_lib()",
            is_async: false,
            is_test: false,
        },
    )
    .unwrap();

    // Query with multiple filters: public functions in main.rs
    let results = graph
        .query()
        .node_type(NodeType::Function)
        .in_file("src/main.rs")
        .property("visibility", "public")
        .execute()
        .unwrap();

    assert_eq!(results.len(), 1);

    // Query with name filter and property filter
    let results = graph
        .query()
        .node_type(NodeType::Function)
        .name_contains("lib")
        .property("visibility", "public")
        .execute()
        .unwrap();

    assert_eq!(results.len(), 1);
}

#[test]
fn test_query_builder_limit() {
    let mut graph = CodeGraph::in_memory().unwrap();

    let file_id = helpers::add_file(&mut graph, "src/main.rs", "rust").unwrap();
    for i in 0..10 {
        helpers::add_function(&mut graph, file_id, &format!("func{i}"), i * 10, i * 10 + 5)
            .unwrap();
    }

    // Query with limit
    let results = graph
        .query()
        .node_type(NodeType::Function)
        .limit(3)
        .execute()
        .unwrap();

    assert_eq!(results.len(), 3);

    // Query with limit larger than results
    let results = graph
        .query()
        .node_type(NodeType::Function)
        .limit(100)
        .execute()
        .unwrap();

    assert_eq!(results.len(), 10);
}

#[test]
fn test_query_builder_count_and_exists() {
    let mut graph = CodeGraph::in_memory().unwrap();

    let file_id = helpers::add_file(&mut graph, "src/main.rs", "rust").unwrap();
    helpers::add_function(&mut graph, file_id, "func1", 1, 10).unwrap();
    helpers::add_function(&mut graph, file_id, "func2", 12, 20).unwrap();
    helpers::add_function(&mut graph, file_id, "func3", 22, 30).unwrap();

    // Test count
    let count = graph.query().node_type(NodeType::Function).count().unwrap();

    assert_eq!(count, 3);

    // Test exists (should be true)
    let exists = graph
        .query()
        .node_type(NodeType::Function)
        .name_contains("func2")
        .exists()
        .unwrap();

    assert!(exists);

    // Test exists (should be false)
    let exists = graph
        .query()
        .node_type(NodeType::Function)
        .name_contains("nonexistent")
        .exists()
        .unwrap();

    assert!(!exists);
}

#[test]
fn test_query_builder_performance_10k_nodes() {
    let mut graph = CodeGraph::in_memory().unwrap();

    // Add 10K functions across multiple files
    let file_id = helpers::add_file(&mut graph, "src/large.rs", "rust").unwrap();
    for i in 0..10000 {
        let name = if i % 3 == 0 {
            format!("public_func_{i}")
        } else {
            format!("func_{i}")
        };

        let visibility = if i % 3 == 0 { "public" } else { "private" };
        let signature = format!("fn {name}()");

        helpers::add_function_with_metadata(
            &mut graph,
            file_id,
            helpers::FunctionMetadata {
                name: &name,
                line_start: i * 10,
                line_end: i * 10 + 5,
                visibility,
                signature: &signature,
                is_async: false,
                is_test: false,
            },
        )
        .unwrap();
    }

    // Query for public functions with name filter
    let results = graph
        .query()
        .node_type(NodeType::Function)
        .property("visibility", "public")
        .name_contains("public")
        .execute()
        .unwrap();

    // Should find ~3333 functions (every 3rd one)
    assert!(results.len() > 3300 && results.len() < 3400);

    // Test count is faster than execute
    let count = graph
        .query()
        .node_type(NodeType::Function)
        .property("visibility", "public")
        .count()
        .unwrap();

    assert!(count > 3300 && count < 3400);

    // Test exists short-circuits
    let exists = graph
        .query()
        .node_type(NodeType::Function)
        .property("visibility", "public")
        .exists()
        .unwrap();

    assert!(exists);
}