-
Notifications
You must be signed in to change notification settings - Fork 3
/
schema_test.go
98 lines (88 loc) · 1.76 KB
/
schema_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
package main
import (
"fmt"
"os"
"testing"
srvConfig "github.com/CHESSComputing/golib/config"
)
// TestSchemaYaml tests schema yaml file
func TestSchemaYaml(t *testing.T) {
srvConfig.Init()
tmpFile, err := os.CreateTemp(os.TempDir(), "*.yaml")
if err != nil {
t.Fatal(err)
}
yamlData := `
- key: Pi
optional: true
type: string
- key: BeamEnergy
optional: false
type: int
`
tmpFile.Write([]byte(yamlData))
tmpFile.Close()
defer os.Remove(tmpFile.Name())
// load json data
fname := tmpFile.Name()
s := &Schema{FileName: fname}
err = s.Load()
if err != nil {
t.Fatal(err)
}
keys, err := s.Keys()
if err != nil {
t.Fatal(err)
}
fmt.Println("Schema keys", keys)
okeys, err := s.OptionalKeys()
if err != nil {
t.Fatal(err)
}
fmt.Println("Schema optional keys", okeys)
rec := make(map[string]any)
rec["Pi"] = "person"
rec["BeamEnergy"] = 123
err = s.Validate(rec)
if err != nil {
t.Fatal(err)
}
}
// TestSchemaJson tests schema json file
func TestSchemaJson(t *testing.T) {
tmpFile, err := os.CreateTemp(os.TempDir(), "*.json")
if err != nil {
t.Fatal(err)
}
jsonData := `[
{"key": "Pi", "type": "string", "optional": true},
{"key": "BeamEnergy", "type": "int", "optional": false}
]`
tmpFile.Write([]byte(jsonData))
tmpFile.Close()
defer os.Remove(tmpFile.Name())
// load json data
fname := tmpFile.Name()
s := &Schema{FileName: fname}
err = s.Load()
if err != nil {
t.Fatal(err)
}
keys, err := s.Keys()
if err != nil {
t.Fatal(err)
}
fmt.Println("Schema keys", keys)
okeys, err := s.OptionalKeys()
if err != nil {
t.Fatal(err)
}
fmt.Println("Schema optional keys", okeys)
rec := make(map[string]any)
rec["Pi"] = "person"
rec["BeamEnergy"] = 123
err = s.Validate(rec)
if err != nil {
t.Fatal(err)
}
}