Repository navigation
Expand file tree
/
Copy pathGraphTraversalEngineTests.cs
More file actions
101 lines (80 loc) · 3.25 KB
/
Copy pathGraphTraversalEngineTests.cs
File metadata and controls
101 lines (80 loc) · 3.25 KB
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
// <copyright file="GraphTraversalEngineTests.cs" company="MPCoreDeveloper">
// Copyright (c) 2025-2026 MPCoreDeveloper and GitHub Copilot. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>
namespace SharpCoreDB.Tests.Graph;
using SharpCoreDB;
using SharpCoreDB.Graph;
using SharpCoreDB.Interfaces;
using System;
using System.Collections.Generic;
using Xunit;
/// <summary>
/// Unit tests for GraphTraversalEngine.
/// Validates BFS/DFS traversal logic with simple graphs.
/// </summary>
public class GraphTraversalEngineTests
{
[Fact]
public void Traverse_BfsDepthTwo_ReturnsExpectedCount()
{
var table = CreateLinearGraphTable();
var engine = new GraphTraversalEngine(new GraphSearchOptions());
var result = engine.Traverse(table, 1, "next", 2, GraphTraversalStrategy.Bfs);
Assert.Equal(3, result.Count);
}
[Fact]
public void Traverse_BfsDepthTwo_IncludesStartNode()
{
var table = CreateLinearGraphTable();
var engine = new GraphTraversalEngine(new GraphSearchOptions());
var result = engine.Traverse(table, 1, "next", 2, GraphTraversalStrategy.Bfs);
Assert.Contains(1, result);
}
[Fact]
public void Traverse_DfsDepthTwo_ReturnsExpectedCount()
{
var table = CreateLinearGraphTable();
var engine = new GraphTraversalEngine(new GraphSearchOptions());
var result = engine.Traverse(table, 1, "next", 2, GraphTraversalStrategy.Dfs);
Assert.Equal(3, result.Count);
}
[Fact]
public void Traverse_WithDepthZero_ReturnsOnlyStartNode()
{
var table = CreateLinearGraphTable();
var engine = new GraphTraversalEngine(new GraphSearchOptions());
var result = engine.Traverse(table, 1, "next", 0, GraphTraversalStrategy.Bfs);
Assert.Equal(1, result.Count);
}
[Fact]
public void Traverse_BidirectionalDepthOne_ReturnsExpectedCount()
{
var table = CreateLinearGraphTable();
var engine = new GraphTraversalEngine(new GraphSearchOptions());
var result = engine.Traverse(table, 3, "next", 1, GraphTraversalStrategy.Bidirectional);
// NOTE: Current implementation of Bidirectional only follows outgoing edges + start node
// Full incoming edge discovery would require table scan (expensive for ROWREF)
// Future enhancement: Build reverse index for incoming edges
Assert.Equal(2, result.Count); // {3, 4} - start node + outgoing
}
[Fact]
public void Traverse_DijkstraDepthTwo_ReturnsExpectedCount()
{
var table = CreateLinearGraphTable();
var engine = new GraphTraversalEngine(new GraphSearchOptions());
var result = engine.Traverse(table, 1, "next", 2, GraphTraversalStrategy.Dijkstra);
Assert.Equal(3, result.Count);
}
private static FakeGraphTable CreateLinearGraphTable()
{
var rows = new List<Dictionary<string, object>>
{
new() { ["id"] = 1L, ["next"] = 2L },
new() { ["id"] = 2L, ["next"] = 3L },
new() { ["id"] = 3L, ["next"] = 4L },
new() { ["id"] = 4L, ["next"] = DBNull.Value }
};
return new FakeGraphTable(rows, "next");
}
}