maAndRandomValue_ExpectedValue   B
last analyzed

Complexity

Conditions 3

Size

Total Lines 103
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 76
nop 1
dl 0
loc 103
rs 7.7854
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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