1
|
|
|
package data |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"math" |
6
|
|
|
"testing" |
7
|
|
|
|
8
|
|
|
"github.com/getkin/kin-openapi/openapi3" |
9
|
|
|
"github.com/stretchr/testify/assert" |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
const ( |
13
|
|
|
testDefaultMinFloat = -12345.0 |
14
|
|
|
testDefaultMaxFloat = 12345.0 |
15
|
|
|
) |
16
|
|
|
|
17
|
|
|
func TestNumberGenerator_GenerateDataBySchema_GivenSchemaAndRandomValue_ExpectedValue(t *testing.T) { |
18
|
|
|
min := 10.0 |
19
|
|
|
max := 100.0 |
20
|
|
|
|
21
|
|
|
tests := []struct { |
22
|
|
|
name string |
23
|
|
|
schema *openapi3.Schema |
24
|
|
|
randomValue int |
25
|
|
|
expectedMaxValue int |
26
|
|
|
expectedValue float64 |
27
|
|
|
}{ |
28
|
|
|
{ |
29
|
|
|
"no params, min random value", |
30
|
|
|
openapi3.NewSchema(), |
31
|
|
|
0, |
32
|
|
|
math.MaxInt64, |
33
|
|
|
testDefaultMinFloat, |
34
|
|
|
}, |
35
|
|
|
{ |
36
|
|
|
"no params, max random value", |
37
|
|
|
openapi3.NewSchema(), |
38
|
|
|
math.MaxInt64, |
39
|
|
|
math.MaxInt64, |
40
|
|
|
testDefaultMaxFloat, |
41
|
|
|
}, |
42
|
|
|
{ |
43
|
|
|
"given range, min random value", |
44
|
|
|
&openapi3.Schema{ |
45
|
|
|
Min: &min, |
46
|
|
|
Max: &max, |
47
|
|
|
}, |
48
|
|
|
0, |
49
|
|
|
math.MaxInt64, |
50
|
|
|
10, |
51
|
|
|
}, |
52
|
|
|
{ |
53
|
|
|
"given range, max random value", |
54
|
|
|
&openapi3.Schema{ |
55
|
|
|
Min: &min, |
56
|
|
|
Max: &max, |
57
|
|
|
}, |
58
|
|
|
math.MaxInt64, |
59
|
|
|
math.MaxInt64, |
60
|
|
|
100, |
61
|
|
|
}, |
62
|
|
|
{ |
63
|
|
|
"exclusive range, min random value", |
64
|
|
|
&openapi3.Schema{ |
65
|
|
|
Min: &min, |
66
|
|
|
Max: &max, |
67
|
|
|
ExclusiveMin: true, |
68
|
|
|
ExclusiveMax: true, |
69
|
|
|
}, |
70
|
|
|
0, |
71
|
|
|
math.MaxInt64 - 2, |
72
|
|
|
10, |
73
|
|
|
}, |
74
|
|
|
{ |
75
|
|
|
"exclusive range, max random value", |
76
|
|
|
&openapi3.Schema{ |
77
|
|
|
Min: &min, |
78
|
|
|
Max: &max, |
79
|
|
|
ExclusiveMin: true, |
80
|
|
|
ExclusiveMax: true, |
81
|
|
|
}, |
82
|
|
|
math.MaxInt64 - 2, |
83
|
|
|
math.MaxInt64 - 2, |
84
|
|
|
100, |
85
|
|
|
}, |
86
|
|
|
{ |
87
|
|
|
"given range and multiple of, middle random value", |
88
|
|
|
&openapi3.Schema{ |
89
|
|
|
Min: &min, |
90
|
|
|
Max: &max, |
91
|
|
|
MultipleOf: &min, |
92
|
|
|
}, |
93
|
|
|
math.MaxInt64 / 5, |
94
|
|
|
math.MaxInt64, |
95
|
|
|
10, |
96
|
|
|
}, |
97
|
|
|
} |
98
|
|
|
for _, test := range tests { |
99
|
|
|
t.Run(test.name, func(t *testing.T) { |
100
|
|
|
randomMock := &mockRandomGenerator{} |
101
|
|
|
numberGeneratorInstance := &numberGenerator{ |
102
|
|
|
random: randomMock, |
103
|
|
|
defaultMinimum: testDefaultMinFloat, |
104
|
|
|
defaultMaximum: testDefaultMaxFloat, |
105
|
|
|
} |
106
|
|
|
randomMock.On("Intn", test.expectedMaxValue).Return(test.randomValue).Twice() |
107
|
|
|
|
108
|
|
|
data, err := numberGeneratorInstance.GenerateDataBySchema(context.Background(), test.schema) |
109
|
|
|
|
110
|
|
|
randomMock.AssertExpectations(t) |
111
|
|
|
assert.NoError(t, err) |
112
|
|
|
assert.Equal(t, test.expectedValue, data) |
113
|
|
|
}) |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
func TestNumberGenerator_GenerateDataBySchema_MaxLessThanMin_Error(t *testing.T) { |
118
|
|
|
numberGeneratorInstance := &numberGenerator{} |
119
|
|
|
min := 11.0 |
120
|
|
|
max := 10.0 |
121
|
|
|
schema := openapi3.NewSchema() |
122
|
|
|
schema.Min = &min |
123
|
|
|
schema.Max = &max |
124
|
|
|
|
125
|
|
|
data, err := numberGeneratorInstance.GenerateDataBySchema(context.Background(), schema) |
126
|
|
|
|
127
|
|
|
assert.EqualError(t, err, "[numberGenerator] maximum cannot be less than minimum") |
128
|
|
|
assert.Equal(t, 0, data) |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
func TestNumberGenerator_GenerateDataBySchema_MaxEqualToMin_StaticValue(t *testing.T) { |
132
|
|
|
numberGeneratorInstance := &numberGenerator{} |
133
|
|
|
min := 10.0 |
134
|
|
|
max := 10.0 |
135
|
|
|
schema := openapi3.NewSchema() |
136
|
|
|
schema.Min = &min |
137
|
|
|
schema.Max = &max |
138
|
|
|
|
139
|
|
|
data, err := numberGeneratorInstance.GenerateDataBySchema(context.Background(), schema) |
140
|
|
|
|
141
|
|
|
assert.NoError(t, err) |
142
|
|
|
assert.Equal(t, 10.0, data) |
143
|
|
|
} |
144
|
|
|
|