-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
78 lines (66 loc) · 1.63 KB
/
json_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
package csm
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/suite"
)
func TestMigratorJSONSuite(t *testing.T) {
suite.Run(t, new(MigratorJSONSuite))
}
type MigratorJSONSuite struct {
suite.Suite
}
func jsonEntryV1ToV2(data []byte) ([]byte, error) {
return WrapperJSON(data, func(v1 EntryV1) (EntryV2, error) {
return EntryV2{
FullName: v1.FirstName + " " + v1.LastName,
}, nil
})
}
func jsonEntryV2ToV3(data []byte) ([]byte, error) {
return WrapperJSON(data, func(v2 EntryV2) (EntryV3, error) {
return EntryV3{
FullName: v2.FullName,
}, nil
})
}
var (
testJSONMigrations = []MigrationJSON{
jsonEntryV1ToV2,
jsonEntryV2ToV3,
}
)
func (suite *MigratorJSONSuite) TestFromJSON() {
input := struct {
FirstName, LastName string
Version int `json:"__schema_version"`
}{
FirstName: "first",
LastName: "last",
Version: 1,
}
inputByte, err := json.Marshal(input)
suite.Require().NoError(err)
expectedOutput := EntryV3{
FullName: "first last",
}
mig := NewMigratorJSON[EntryV3](testJSONMigrations)
entry, err := mig.Import(inputByte)
suite.Require().NoError(err)
suite.Require().IsType(expectedOutput, entry)
suite.Require().Equal(expectedOutput, entry)
}
func (suite *MigratorJSONSuite) TestToJSON() {
input := EntryV3{
FullName: "first last",
Age: 34,
}
expectedOutput := []byte(
fmt.Sprintf("{%q:%q,%q:%d,%q:%d}", "FullName", "first last", "Age", 34, VersionFieldKey, 3),
)
mig := NewMigratorJSON[EntryV3](testJSONMigrations)
output, err := mig.Export(input)
suite.Require().NoError(err)
suite.Require().Equal(string(expectedOutput), string(output))
}