Skip to content

Commit 2a289a4

Browse files
committed
test(snapshot): generate the svgs as for golden
1 parent 2653b72 commit 2a289a4

4 files changed

Lines changed: 173 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*.so
66
*.dylib
77
app
8+
main
89

910
# Test binary, built with `go test -c`
1011
*.test

adapter/render_to_string.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package adapter
2+
3+
import (
4+
"bytes"
5+
"context"
6+
7+
svg "github.com/ajstarks/svgo"
8+
"github.com/jodi-ivan/numbered-notation-xml/svc/usecase"
9+
"github.com/jodi-ivan/numbered-notation-xml/utils/canvas"
10+
)
11+
12+
type RenderStringCanvasDelegator struct{}
13+
14+
func (rscd *RenderStringCanvasDelegator) OnBeforeStartWrite() {}
15+
func (rscd *RenderStringCanvasDelegator) OnError(err error) canvas.DelegatorErrorFlowControl {
16+
return canvas.DelegatorErrorFlowControlStop
17+
}
18+
19+
type RenderString struct {
20+
usecase usecase.Usecase
21+
}
22+
23+
func NewRenderString(u usecase.Usecase) *RenderString {
24+
25+
return &RenderString{
26+
usecase: u,
27+
}
28+
}
29+
30+
func (rs *RenderString) RenderHymn(ctx context.Context, buf *bytes.Buffer, number int) (string, error) {
31+
canv := canvas.NewCanvasWithDelegator(svg.New(buf), &RenderStringCanvasDelegator{})
32+
err := rs.usecase.RenderHymn(ctx, canv, number)
33+
if err != nil {
34+
return "", err
35+
}
36+
return buf.String(), nil
37+
}

cmd/lab/test/snapshot/main.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"flag"
7+
"fmt"
8+
"log"
9+
"os"
10+
"strings"
11+
12+
"github.com/jodi-ivan/numbered-notation-xml/adapter"
13+
"github.com/jodi-ivan/numbered-notation-xml/internal/renderer"
14+
"github.com/jodi-ivan/numbered-notation-xml/svc/repository"
15+
"github.com/jodi-ivan/numbered-notation-xml/svc/usecase"
16+
"github.com/jodi-ivan/numbered-notation-xml/utils/config"
17+
"github.com/jodi-ivan/numbered-notation-xml/utils/storage"
18+
)
19+
20+
// TODO: the snapshot testing
21+
/*
22+
- generate golden file
23+
- generate the svg
24+
- sampling
25+
- assert
26+
*/
27+
28+
var defaultxml = "~/go/src/github.com/jodi-ivan/numbered-notation-xml/files/scores/musicxml/"
29+
var defaultdb = "~/go/src/github.com/jodi-ivan/numbered-notation-xml/files/database/kidung-jemaat.db"
30+
31+
func main() {
32+
env := os.Environ()
33+
34+
// Define a string flag
35+
method := flag.String("snapshot", "gen", "Operation, 'gen' generate golden. 'assert' assert snapshot")
36+
goldenPath := flag.String("path", "", "Path of the golden files to be generated, only when snapshot=gen")
37+
38+
// Parse the command-line arguments into the defined flags
39+
flag.Parse()
40+
41+
log.Println("method:", *method)
42+
xmlFlag := defaultxml
43+
dbPath := defaultdb
44+
45+
for _, e := range env {
46+
keyval := strings.Split(e, "=")
47+
switch keyval[0] {
48+
case "KJ_TEST_MUSICXML_PATH":
49+
xmlFlag = keyval[1]
50+
case "KJ_TEST_DB_PATH":
51+
dbPath = keyval[1]
52+
}
53+
}
54+
55+
// Parse the flags from the command line
56+
57+
cfg := config.Config{
58+
MusicXML: config.MusicXMLConfig{
59+
Path: xmlFlag,
60+
FilePrefix: "kj",
61+
},
62+
SQLite: config.SQLiteConfig{
63+
DBPath: dbPath,
64+
},
65+
}
66+
67+
db, err := storage.NewStorage(context.Background(), cfg.SQLite.DBPath)
68+
if err != nil {
69+
log.Fatalf("Failed to connect to storage: %s", err.Error())
70+
return
71+
}
72+
73+
defer db.Close()
74+
75+
repo := repository.New(context.Background(), db)
76+
usecaseMod := usecase.New(cfg, repo, renderer.NewRenderer())
77+
stringRender := adapter.NewRenderString(usecaseMod)
78+
79+
switch *method {
80+
case "gen":
81+
if *goldenPath == "" {
82+
fmt.Printf("Goldenpath -path cannot be empty")
83+
os.Exit(2)
84+
return
85+
}
86+
87+
fmt.Printf("Read the musicxml files on %s.\n", defaultxml)
88+
fmt.Printf("Generate golden files. Generated on %s. \n\n", *goldenPath)
89+
90+
err = GenerateGolden(context.Background(), stringRender, *goldenPath)
91+
if err != nil {
92+
fmt.Printf("Failed to Generate Snapshot: %s\n", err.Error())
93+
os.Exit(2)
94+
return
95+
}
96+
97+
}
98+
99+
os.Exit(0)
100+
101+
}
102+
103+
func GenerateGolden(ctx context.Context, stringRenderer *adapter.RenderString, path string) error {
104+
numFiles := 22
105+
for i := 1; i <= numFiles; i++ {
106+
buff := bytes.NewBuffer(nil)
107+
content, err := stringRenderer.RenderHymn(context.Background(), buff, i)
108+
if err != nil {
109+
log.Fatalf("Problem creating file: %v", err)
110+
return err
111+
}
112+
113+
fileName := fmt.Sprintf("%s/kj-%03d.svg", path, i)
114+
fmt.Println("Creating golden for", fileName)
115+
file, err := os.Create(fileName)
116+
if err != nil {
117+
log.Fatalf("Problem creating file: %v", err)
118+
return err
119+
}
120+
fmt.Fprintf(file, "%s", content)
121+
file.Close()
122+
}
123+
return nil
124+
}

cmd/lab/test/snapshot/makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Simple Makefile to build and run the snapshot test
2+
3+
.PHONY: build run
4+
5+
# Target to build the executable
6+
build:
7+
go build -o cmd/lab/test/snapshot-test/main cmd/lab/test/snapshot-test/main.go
8+
9+
# Target to run the executable with specified environment variables and flags
10+
run: build
11+
KJ_TEST_DB_PATH=./files/database/kidung-jemaat.db KJ_TEST_MUSICXML_PATH=./files/scores/musicxml/ ./cmd/lab/test/snapshot-test/main -snapshot=gen -path=./cmd/lab/test/snapshot-test/goldenfiles

0 commit comments

Comments
 (0)