-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
74 lines (55 loc) · 1.54 KB
/
main_test.go
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
package main
import (
"io"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/maxmind/xgb2code/gen"
)
func TestGenerateAndRunModels(t *testing.T) {
tests := []struct {
model string
}{
{model: "small-model"},
{model: "breast-cancer"},
}
for _, test := range tests {
t.Run(test.model, func(t *testing.T) {
// Generate the code.
modelDir := filepath.Join("gen", "testdata", test.model)
modelFile := filepath.Join(modelDir, "model.json")
packageName := "main"
functionName := "predict"
outputDir := t.TempDir()
funcFile := filepath.Join(outputDir, "predict.go")
err := gen.GenerateFile(modelFile, packageName, functionName, funcFile)
require.NoError(t, err)
// Copy the test program and test data into place.
files := []string{
filepath.Join("gen", "testdata", "main.go"),
filepath.Join(modelDir, "xtest.csv"),
filepath.Join(modelDir, "preds.csv"),
}
for _, file := range files {
ifh, err := os.Open(filepath.Clean(file))
require.NoError(t, err)
tempFile := filepath.Join(outputDir, filepath.Base(file))
ofh, err := os.Create(filepath.Clean(tempFile))
require.NoError(t, err)
_, err = io.Copy(ofh, ifh)
require.NoError(t, err)
err = ifh.Close()
require.NoError(t, err)
err = ofh.Close()
require.NoError(t, err)
}
// Run the program.
cmd := exec.Command("go", "run", "main.go", "predict.go")
cmd.Dir = outputDir
buf, err := cmd.CombinedOutput()
require.NoError(t, err, string(buf))
})
}
}