-
Notifications
You must be signed in to change notification settings - Fork 12
/
instance_test.go
184 lines (153 loc) · 4.32 KB
/
instance_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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package thema
import (
"encoding/json"
"fmt"
"io"
"strings"
"testing"
"cuelang.org/go/cue"
"github.com/grafana/thema/internal/txtartest/vanilla"
"cuelang.org/go/cue/cuecontext"
"github.com/stretchr/testify/require"
)
func TestInstance_Translate(t *testing.T) {
test := vanilla.TxTarTest{
Root: "./testdata/lineage",
Name: "core/instance/translate",
ThemaFS: CueJointFS,
}
ctx := cuecontext.New()
rt := NewRuntime(ctx)
test.Run(t, func(tc *vanilla.Test) {
if !tc.HasTag("multiversion") {
tc.Skip("case not tagged with 'multiversion'")
}
lin, lerr := bindTxtarLineage(tc, rt, "lineagePath")
require.NoError(tc, lerr)
for from := lin.First(); from != nil; from = from.Successor() {
for _, example := range from.Examples() {
for to := lin.First(); to != nil; to = to.Successor() {
tinst, lacunas, err := example.Translate(to.Version())
require.NoError(t, err)
require.NotNil(t, tinst)
result := tinst.Underlying()
require.True(t, result.Exists())
require.NoError(t, result.Err())
writeGolden(tc, to.Version(), example, result, lacunas)
}
}
}
})
}
func writeGolden(tc *vanilla.Test, to SyntacticVersion, example *Instance, result cue.Value, lacunas TranslationLacunas) {
tc.Helper()
fromStr := example.Schema().Version().String()
toStr := to.String()
exName := example.name
tName := strings.Replace(tc.Name(), tc.Name()+"/", "", -1)
wName := fmt.Sprintf("%s-%s-%s->%s.json", tName, fromStr, exName, toStr)
w := tc.Writer(wName)
// From (example)
marshalAndWrite(tc, w, example.Underlying())
// To (result)
marshalAndWrite(tc, w, result)
// Lacunas
marshalAndWrite(tc, w, lacunas)
}
func marshalAndWrite(tc *vanilla.Test, w io.Writer, any interface{}) {
tc.Helper()
bytes, err := json.Marshal(any)
require.NoError(tc, err)
_, err = w.Write(append(bytes, '\n'))
require.NoError(tc, err)
}
// TestInstance_LookupPathOnTranslatedInstance is a regression test
// specifically written for https://github.com/grafana/thema/issues/155.
//
// Caused because [Instance.Translate] results were always non-concrete,
// when the result evaluates to something concrete.
//
// So, this test checks that [cue.Value.LookupPath] behaves as expected
// when used over an [Instance.Translate] result.
func TestInstance_LookupPathOnTranslatedInstance(t *testing.T) {
ctx := cuecontext.New()
rt := NewRuntime(ctx)
// Initialize lineage for testing
linstr := `name: "simple"
schemas: [
{
version: [0, 0]
schema:
{
title: string
},
},
{
version: [0, 1]
schema:
{
title: string
header?: string
},
},
]`
linval := rt.Context().CompileString(linstr)
lin, err := BindLineage(linval, rt)
require.NoError(t, err)
// Initialize cue.Value
expected := "foo"
val := ctx.CompileString(fmt.Sprintf(`{"title": "%s"}`, expected))
// Validate cue.Value
inst := lin.ValidateAny(val)
require.Equal(t, SV(0, 0), inst.Schema().Version())
got, err := inst.Underlying().LookupPath(cue.ParsePath("title")).String()
require.NoError(t, err)
require.Equal(t, expected, got)
// Translate cue.Value (no lacunas)
tinst, _, err := inst.Translate(SV(0, 1))
require.NoError(t, err)
require.Equal(t, SV(0, 0), inst.Schema().Version())
got, err = tinst.Underlying().LookupPath(cue.ParsePath("title")).String()
require.NoError(t, err)
require.Equal(t, expected, got)
}
func BenchmarkBasicTranslate(b *testing.B) {
test := vanilla.TxTarTest{
Root: "./testdata/lineage",
Name: "core/instance/translate",
ThemaFS: CueJointFS,
}
ctx := cuecontext.New()
rt := NewRuntime(ctx)
test.RunBenchmark(b, func(bc *vanilla.Benchmark) {
if !bc.HasTag("multiversion") {
bc.Skip()
}
lval := ctx.BuildInstance(bc.Instance())
lin, err := BindLineage(lval, rt)
require.NoError(b, err)
first, second := lin.First(), lin.First().Successor()
bc.Run("forward", func(b *testing.B) {
for name, iexample := range first.Examples() {
example := iexample
b.Run(name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
example.Translate(second.Version())
}
})
}
})
bc.Run("reverse", func(b *testing.B) {
for name, iexample := range second.Examples() {
example := iexample
b.Run(name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
example.Translate(first.Version())
}
})
}
})
})
}