-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_cmd_test.go
More file actions
117 lines (111 loc) · 3.03 KB
/
graph_cmd_test.go
File metadata and controls
117 lines (111 loc) · 3.03 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cli
import (
"bytes"
"encoding/json"
"path/filepath"
"strings"
"testing"
)
// TestGraphCommandJSON asserts the default JSON export has `nodes`,
// `edges`, and `stats` keys.
func TestGraphCommandJSON(t *testing.T) {
dir := statsFixtureDir(t)
root := NewRootCommand()
root.SetArgs([]string{
"graph", "--format", "json",
"--graph-dir", filepath.Join(dir, "graph.kuzu"),
dir,
})
var out bytes.Buffer
root.SetOut(&out)
root.SetErr(&out)
if err := root.Execute(); err != nil {
t.Fatalf("graph: %v\n%s", err, out.String())
}
var got map[string]any
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
t.Fatalf("graph JSON invalid: %v\n%s", err, out.String())
}
for _, k := range []string{"nodes", "edges", "stats"} {
if _, ok := got[k]; !ok {
t.Errorf("graph JSON missing %q", k)
}
}
}
// TestGraphCommandYAML asserts the YAML export is parseable and contains
// the canonical top-level keys.
func TestGraphCommandYAML(t *testing.T) {
dir := statsFixtureDir(t)
root := NewRootCommand()
root.SetArgs([]string{
"graph", "-f", "yaml",
"--graph-dir", filepath.Join(dir, "graph.kuzu"),
dir,
})
var out bytes.Buffer
root.SetOut(&out)
root.SetErr(&out)
if err := root.Execute(); err != nil {
t.Fatalf("graph yaml: %v\n%s", err, out.String())
}
for _, k := range []string{"nodes:", "edges:", "stats:"} {
if !strings.Contains(out.String(), k) {
t.Errorf("graph yaml missing %q\n%s", k, out.String())
}
}
}
// TestGraphCommandMermaid asserts the mermaid export starts with `graph LR`.
func TestGraphCommandMermaid(t *testing.T) {
dir := statsFixtureDir(t)
root := NewRootCommand()
root.SetArgs([]string{
"graph", "-f", "mermaid",
"--graph-dir", filepath.Join(dir, "graph.kuzu"),
dir,
})
var out bytes.Buffer
root.SetOut(&out)
root.SetErr(&out)
if err := root.Execute(); err != nil {
t.Fatalf("graph mermaid: %v\n%s", err, out.String())
}
if !strings.HasPrefix(out.String(), "graph LR\n") {
t.Fatalf("graph mermaid must start with `graph LR`, got:\n%s", out.String())
}
}
// TestGraphCommandDOT asserts the dot export is well-formed.
func TestGraphCommandDOT(t *testing.T) {
dir := statsFixtureDir(t)
root := NewRootCommand()
root.SetArgs([]string{
"graph", "-f", "dot",
"--graph-dir", filepath.Join(dir, "graph.kuzu"),
dir,
})
var out bytes.Buffer
root.SetOut(&out)
root.SetErr(&out)
if err := root.Execute(); err != nil {
t.Fatalf("graph dot: %v\n%s", err, out.String())
}
if !strings.HasPrefix(out.String(), "digraph G {") {
t.Fatalf("graph dot must start with `digraph G {`, got:\n%s", out.String())
}
}
// TestGraphCommandUnknownFormat asserts an unknown format is surfaced as
// an error.
func TestGraphCommandUnknownFormat(t *testing.T) {
dir := statsFixtureDir(t)
root := NewRootCommand()
root.SetArgs([]string{
"graph", "-f", "bogus",
"--graph-dir", filepath.Join(dir, "graph.kuzu"),
dir,
})
var out bytes.Buffer
root.SetOut(&out)
root.SetErr(&out)
if err := root.Execute(); err == nil {
t.Fatalf("expected error for unknown format, got success:\n%s", out.String())
}
}