/
assertion.go
172 lines (136 loc) · 4.16 KB
/
assertion.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
package assertion
import (
"encoding/json"
"fmt"
"sort"
"strconv"
"time"
"github.com/vincoll/vigie/pkg/probe"
"github.com/vincoll/vigie/pkg/utils"
)
type AssertionsApplied struct {
Ok bool
Failures []string
}
type Assert struct {
Key string // Key in wich to pick the value from a probe result
Method AssertMethod // Assert method (equal, sup, contains ...)
Value interface{} // value to assert
Values []string // values to assert
ResultStatus int8 //TODO: à typer en teststruct.Status like
ResultAssert string // ok, or assert fail msg
}
type AssertResult struct {
Assertion string
ResultStatus int8 //TODO: à typer en teststruct.Status like
ResultAssert string // ok, or assert fail msg
}
type AssertShortJSON struct {
Key string `json:"key"`
Method string `json:"method"`
Value string `json:"value"`
}
type AssertDesc struct {
LongAssert string `json:"assertion"`
ResultStatus int8 `json:"resultstatus"`
ResultAssert string `json:"resultassert"`
}
// ToAssertJSON returns a struct with content easily readable
func (a Assert) ToAssertJSON() *AssertShortJSON {
assertjson := &AssertShortJSON{
Key: a.Key,
Method: a.Method.LongName,
}
if a.Values == nil {
assertjson.Value = fmt.Sprintf("%v", a.Value)
} else {
jsonValues, _ := json.Marshal(a.Values)
assertjson.Value = string(jsonValues)
}
return assertjson
}
func (a Assert) ToAssertDesc() AssertDesc {
return AssertDesc{
LongAssert: a.AssertConditionsLong(),
ResultStatus: a.ResultStatus,
ResultAssert: a.ResultAssert,
}
}
func (a Assert) AssertConditionsLong() string {
if a.Values == nil {
return fmt.Sprintf("%s %s %v", a.Key, a.Method.LongName, a.Value)
} else {
jsonValues, _ := json.Marshal(a.Values)
return fmt.Sprintf("%s %s %s", a.Key, a.Method.LongName, string(jsonValues))
}
}
// ApplyAssert find the corresponding value to assert in probeAnswer
// then asserts the probe value with the expected value.
func ApplyAssert(probeAnswer *probe.ProbeAnswer, tAssert *Assert) (assertRes bool, failCause string) {
// Looking for the key value assertion in the probe result
probeValueToAssert, found := browse(tAssert.Key, *probeAnswer)
if !found {
return false, fmt.Sprintf("key '%q' does not exist in result of probe: %+v", tAssert.Key, probeAnswer)
}
probValueFmt, probValuesFmt := formatProbeVal(probeValueToAssert, tAssert)
// Assertion de Probe ResultValue sur l'attendu
_, assertResult := tAssert.Method.AssertFunc(probValueFmt, probValuesFmt, tAssert.Value, tAssert.Values)
if assertResult != "" {
var failCause string
if tAssert.Method.IsDuration {
// If Duration, the formating need precise
numActualVal, _ := utils.GetFloat(probValueFmt)
failCause = fmt.Sprintf("assertion '%s' failed: probe result is '%s'", tAssert.AssertConditionsLong(), time.Duration(numActualVal))
} else {
failCause = fmt.Sprintf("assertion '%s' failed: probe result is '%v'", tAssert.AssertConditionsLong(), probValueFmt)
}
return false, failCause
}
return true, ""
}
func formatProbeVal(probeValue string, tAssert *Assert) (value interface{}, values []string) {
// Define the input (String, Array, Nested Array)
// String
if utils.IsJSONString(probeValue) {
// Simple String
// Simple Array
err := json.Unmarshal([]byte(probeValue), &value)
if err != nil {
panic(err)
}
return value, nil
}
// Float64
// Numerical
if utils.IsNumeric(probeValue) {
num, _ := strconv.ParseFloat(probeValue, 64)
return num, nil
}
if utils.IsBool(probeValue) {
return fmt.Sprint(probeValue), nil
}
// Array
if utils.IsArray(probeValue) && !utils.IsNestedArray(probeValue) {
var fmtProbeValues []string
// Simple Array
err := json.Unmarshal([]byte(probeValue), &fmtProbeValues)
if err != nil {
panic(err)
}
if tAssert.Method.IsEqualType && !tAssert.Method.IsOrdered {
sort.Strings(fmtProbeValues)
}
// Is an Array
return nil, fmtProbeValues
}
// Nested Array
if utils.IsNestedArray(probeValue) && utils.IsArray(probeValue) {
// TODO: NestedArray
/* err := json.Unmarshal([]byte(v), &aNestedArrayValue)
if err != nil {
panic(err)
}
*/
}
return nil, nil
}