AndGivenExample_ExpectedData   B
last analyzed

Complexity

Conditions 3

Size

Total Lines 107
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 76
nop 1
dl 0
loc 107
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
	"github.com/stretchr/testify/mock"
10
)
11
12
func TestCoordinatingMediaGenerator_GenerateData_DataSchemaOnly_DataGeneratedBySchema(t *testing.T) {
13
	schemaGeneratorMock := &mockSchemaGenerator{}
14
	mediaGenerator := &coordinatingMediaGenerator{
15
		schemaGenerator: schemaGeneratorMock,
16
	}
17
	schema := openapi3.NewSchema()
18
	mediaType := &openapi3.MediaType{
19
		Schema: openapi3.NewSchemaRef("", schema),
20
	}
21
	schemaGeneratorMock.On("GenerateDataBySchema", mock.Anything, schema).Return("data", nil).Once()
22
23
	data, err := mediaGenerator.GenerateData(context.Background(), mediaType)
24
25
	schemaGeneratorMock.AssertExpectations(t)
26
	assert.NoError(t, err)
27
	assert.Equal(t, "data", data)
28
}
29
30
func TestCoordinatingMediaGenerator_GenerateData_UseExamplesOptionAndGivenExample_ExpectedData(t *testing.T) {
31
	const randomData = "randomData"
32
	const exampleData = "exampleData"
33
34
	schemaGeneratorMock := &mockSchemaGenerator{}
35
	mediaGenerator := &coordinatingMediaGenerator{
36
		schemaGenerator: schemaGeneratorMock,
37
	}
38
	schema := openapi3.NewSchema()
39
	mediaType := &openapi3.MediaType{
40
		Schema: openapi3.NewSchemaRef("", schema),
41
	}
42
	schemaGeneratorMock.On("GenerateDataBySchema", mock.Anything, schema).Return(randomData, nil)
43
44
	tests := []struct {
45
		name         string
46
		useExamples  UseExamplesEnum
47
		example      interface{}
48
		examples     map[string]*openapi3.ExampleRef
49
		expectedData interface{}
50
	}{
51
		{
52
			"use examples disabled, no example, no examples",
53
			No,
54
			nil,
55
			nil,
56
			randomData,
57
		},
58
		{
59
			"use examples disabled, given example, no examples",
60
			No,
61
			exampleData,
62
			nil,
63
			randomData,
64
		},
65
		{
66
			"use examples disabled, no example, given examples",
67
			No,
68
			nil,
69
			map[string]*openapi3.ExampleRef{
70
				"example": {
71
					Value: &openapi3.Example{Value: exampleData},
72
				},
73
			},
74
			randomData,
75
		},
76
		{
77
			"use examples if present, no example, no examples",
78
			IfPresent,
79
			nil,
80
			nil,
81
			randomData,
82
		},
83
		{
84
			"use examples if present, given example, no examples",
85
			IfPresent,
86
			exampleData,
87
			nil,
88
			exampleData,
89
		},
90
		{
91
			"use examples if present, no example, given examples",
92
			IfPresent,
93
			nil,
94
			map[string]*openapi3.ExampleRef{
95
				"example": {
96
					Value: &openapi3.Example{Value: exampleData},
97
				},
98
			},
99
			exampleData,
100
		},
101
		{
102
			"use examples exclusively, no example, no examples",
103
			Exclusively,
104
			nil,
105
			nil,
106
			nil,
107
		},
108
		{
109
			"use examples exclusively, given example, no examples",
110
			Exclusively,
111
			exampleData,
112
			nil,
113
			exampleData,
114
		},
115
		{
116
			"use examples exclusively, no example, given examples",
117
			Exclusively,
118
			nil,
119
			map[string]*openapi3.ExampleRef{
120
				"example": {
121
					Value: &openapi3.Example{Value: exampleData},
122
				},
123
			},
124
			exampleData,
125
		},
126
	}
127
	for _, test := range tests {
128
		t.Run(test.name, func(t *testing.T) {
129
			mediaGenerator.useExamples = test.useExamples
130
			mediaType.Example = test.example
131
			mediaType.Examples = test.examples
132
133
			data, err := mediaGenerator.GenerateData(context.Background(), mediaType)
134
135
			assert.NoError(t, err)
136
			assert.Equal(t, test.expectedData, data)
137
		})
138
	}
139
}
140