forked from garetht/amanar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal_config_test.go
92 lines (87 loc) · 2.57 KB
/
unmarshal_config_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
package amanar
import (
"reflect"
"testing"
)
func TestLoadConfiguration(t *testing.T) {
type args struct {
configFilepath string
schemaAssetPath string
}
tests := []struct {
name string
args args
hasConfiguration bool
wantErr error
hasErrors bool
errors []string
}{
{
name: "Valid JSON can be loaded",
args: args{
configFilepath: "./example/example_configuration.json",
},
hasConfiguration: true,
wantErr: nil,
hasErrors: false,
},
{
name: "Valid YAML can be loaded",
args: args{
configFilepath: "./example/example_configuration.yml",
},
hasConfiguration: true,
wantErr: nil,
hasErrors: false,
},
{
name: "Invalid YAML cannot be loaded: top-level property that do not begin with x-",
args: args{
configFilepath: "./fixtures/invalid_properties.yml",
},
hasConfiguration: true,
wantErr: nil,
hasErrors: true,
errors: []string{"(root): Additional property invalid_prop is not allowed"},
},
{
name: "Invalid YAML cannot be loaded: anchor resolves to invalid type",
args: args{
configFilepath: "./fixtures/invalid_resolved_anchor.yml",
},
hasConfiguration: true,
wantErr: nil,
hasErrors: true,
errors: []string{"amanar_configuration.0.vault_configuration.0.vault_path: Invalid type. Expected: string, given: integer"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotC, gotErr, gotRe := LoadConfiguration(tt.args.configFilepath)
if (gotC != nil) != tt.hasConfiguration {
t.Errorf("ValidateConfiguration() gotC = %v, hasConfiguration %t", gotC, tt.hasConfiguration)
}
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("ValidateConfiguration() gotErr = %v, want %v", gotErr, tt.wantErr)
}
if (len(gotRe) > 0) != tt.hasErrors {
t.Errorf("ValidateConfiguration() got errors = %v, but wanted has errors %t", gotRe, tt.hasErrors)
}
if tt.hasErrors {
for i, actualError := range gotRe {
assertErr := tt.errors[i]
if actualError.String() != assertErr {
t.Errorf("ResultErrors got error = %s, but wanted error to be %s", actualError.String(), assertErr)
}
}
}
})
}
}
func TestJsonYamlCompatibility(t *testing.T) {
jsonC, _, _ := LoadConfiguration("./example/example_configuration.json")
yamlC, _, _ := LoadConfiguration("./example/example_configuration.yml")
if !reflect.DeepEqual(jsonC, yamlC) {
t.Errorf("deep equality jsonC = %+v, yamlC %+v", jsonC, yamlC)
}
}