Skip to content

Commit bd74ec2

Browse files
committed
add mermaid exporter test
1 parent 0ac21be commit bd74ec2

1 file changed

Lines changed: 72 additions & 3 deletions

File tree

exporters/mermaid_test.go

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
"github.com/spdeepak/flowtracker"
1515
)
1616

17-
func NewServer() http.Handler {
17+
func NewServer(orientation Orientation, tags []string) http.Handler {
1818
mux := http.NewServeMux()
1919
mux.HandleFunc("/", Handler)
20-
mw := flowtracker.NewMiddleware(flowtracker.WithExporter(&MermaidExporter{}))
20+
mw := flowtracker.NewMiddleware(flowtracker.WithExporter(&MermaidExporter{Orientation: orientation, IncludeTags: tags}))
2121
return mw(mux)
2222
}
2323

@@ -27,7 +27,7 @@ func TestMermaidExporter_OK(t *testing.T) {
2727
r, w, _ := os.Pipe()
2828
os.Stdout = w
2929

30-
server := NewServer()
30+
server := NewServer(TopDown, []string{})
3131

3232
req := httptest.NewRequest(http.MethodGet, "/", nil)
3333
rr := httptest.NewRecorder()
@@ -90,6 +90,75 @@ func TestMermaidExporter_OK(t *testing.T) {
9090
}
9191
}
9292

93+
func TestMermaidExporter_OK_IncludeTags(t *testing.T) {
94+
// capture stdout
95+
old := os.Stdout
96+
r, w, _ := os.Pipe()
97+
os.Stdout = w
98+
99+
server := NewServer(LeftRight, []string{"db.query"})
100+
101+
req := httptest.NewRequest(http.MethodGet, "/", nil)
102+
rr := httptest.NewRecorder()
103+
104+
server.ServeHTTP(rr, req)
105+
106+
time.Sleep(1 * time.Second)
107+
108+
// restore stdout
109+
w.Close()
110+
os.Stdout = old
111+
112+
// read logs
113+
var buf bytes.Buffer
114+
_, err := io.Copy(&buf, r)
115+
if err != nil {
116+
t.Error("Error should be nil")
117+
}
118+
119+
logs := buf.String()
120+
output := strings.Split(logs, "\n")
121+
if !strings.Contains(output[1], "----- MERMAID OUTPUT -----") {
122+
t.Fatalf("expected log not found: %s", logs)
123+
}
124+
if !strings.EqualFold(output[2], "```mermaid") {
125+
t.Fatalf("expected log not found: %s", logs)
126+
}
127+
if !strings.EqualFold(output[3], "graph LR") {
128+
t.Fatalf("expected log not found: %s", logs)
129+
}
130+
if !strings.Contains(output[4], "[\"GET /\"] -->|") {
131+
t.Fatalf("expected log not found: %s", logs)
132+
}
133+
if !strings.Contains(output[4], "[\"Process Payment\"]") {
134+
t.Fatalf("expected log not found: %s", logs)
135+
}
136+
if !strings.Contains(output[5], "[\"GET /\"] -->|") {
137+
t.Fatalf("expected log not found: %s", logs)
138+
}
139+
if !strings.Contains(output[5], "[\"DB: Select User (db.query:SELECT * FROM users...)\"]") {
140+
t.Fatalf("expected log not found: %s", logs)
141+
}
142+
if !strings.Contains(output[6], "[\"GET /\"] -->|") {
143+
t.Fatalf("expected log not found: %s", logs)
144+
}
145+
if !strings.Contains(output[6], "[\"HTTP: Shipping Service\"]") {
146+
t.Fatalf("expected log not found: %s", logs)
147+
}
148+
if !strings.Contains(output[7], "[\"HTTP: Shipping Service\"] -->|") {
149+
t.Fatalf("expected log not found: %s", logs)
150+
}
151+
if !strings.Contains(output[7], "[\"Calculate Weight\"]") {
152+
t.Fatalf("expected log not found: %s", logs)
153+
}
154+
if !strings.EqualFold(output[8], "```") {
155+
t.Fatalf("expected log not found: %s", logs)
156+
}
157+
if !strings.EqualFold(output[9], "--------------------------") {
158+
t.Fatalf("expected log not found: %s", logs)
159+
}
160+
}
161+
93162
func Handler(w http.ResponseWriter, r *http.Request) {
94163
ctx := r.Context()
95164

0 commit comments

Comments
 (0)