|
1
|
|
|
package data |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"errors" |
|
6
|
|
|
"testing" |
|
7
|
|
|
|
|
8
|
|
|
"github.com/getkin/kin-openapi/openapi3" |
|
9
|
|
|
"github.com/stretchr/testify/assert" |
|
10
|
|
|
"github.com/stretchr/testify/mock" |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
func TestObjectGenerator_GenerateDataBySchema_ObjectWithOneProperty_PropertyGeneratedBySchemaGenerator(t *testing.T) { |
|
14
|
|
|
schemaGeneratorMock := &mockSchemaGenerator{} |
|
15
|
|
|
objectGeneratorInstance := &objectGenerator{ |
|
16
|
|
|
schemaGenerator: schemaGeneratorMock, |
|
17
|
|
|
} |
|
18
|
|
|
propertySchema := &openapi3.Schema{} |
|
19
|
|
|
schema := &openapi3.Schema{ |
|
20
|
|
|
Type: "object", |
|
21
|
|
|
Properties: map[string]*openapi3.SchemaRef{ |
|
22
|
|
|
"propertyName": { |
|
23
|
|
|
Value: propertySchema, |
|
24
|
|
|
}, |
|
25
|
|
|
}, |
|
26
|
|
|
} |
|
27
|
|
|
schemaGeneratorMock.On("GenerateDataBySchema", mock.Anything, propertySchema).Return("propertyValue", nil).Once() |
|
28
|
|
|
|
|
29
|
|
|
data, err := objectGeneratorInstance.GenerateDataBySchema(context.Background(), schema) |
|
30
|
|
|
|
|
31
|
|
|
schemaGeneratorMock.AssertExpectations(t) |
|
32
|
|
|
assert.NoError(t, err) |
|
33
|
|
|
assert.Equal(t, "propertyValue", data.(map[string]interface{})["propertyName"]) |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
func TestObjectGenerator_GenerateDataBySchema_ObjectWithOneWriteOnlyProperty_EmptyObject(t *testing.T) { |
|
37
|
|
|
schemaGeneratorMock := &mockSchemaGenerator{} |
|
38
|
|
|
objectGeneratorInstance := &objectGenerator{ |
|
39
|
|
|
schemaGenerator: schemaGeneratorMock, |
|
40
|
|
|
} |
|
41
|
|
|
propertySchema := &openapi3.Schema{} |
|
42
|
|
|
propertySchema.WriteOnly = true |
|
43
|
|
|
schema := &openapi3.Schema{ |
|
44
|
|
|
Type: "object", |
|
45
|
|
|
Properties: map[string]*openapi3.SchemaRef{ |
|
46
|
|
|
"propertyName": { |
|
47
|
|
|
Value: propertySchema, |
|
48
|
|
|
}, |
|
49
|
|
|
}, |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
data, err := objectGeneratorInstance.GenerateDataBySchema(context.Background(), schema) |
|
53
|
|
|
|
|
54
|
|
|
schemaGeneratorMock.AssertExpectations(t) |
|
55
|
|
|
assert.NoError(t, err) |
|
56
|
|
|
assert.Len(t, data, 0) |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
func TestObjectGenerator_GenerateDataBySchema_SchemaGeneratorError_ErrorReturned(t *testing.T) { |
|
60
|
|
|
schemaGeneratorMock := &mockSchemaGenerator{} |
|
61
|
|
|
objectGeneratorInstance := &objectGenerator{ |
|
62
|
|
|
schemaGenerator: schemaGeneratorMock, |
|
63
|
|
|
} |
|
64
|
|
|
propertySchema := &openapi3.Schema{} |
|
65
|
|
|
schema := &openapi3.Schema{ |
|
66
|
|
|
Type: "object", |
|
67
|
|
|
Properties: map[string]*openapi3.SchemaRef{ |
|
68
|
|
|
"propertyName": { |
|
69
|
|
|
Value: propertySchema, |
|
70
|
|
|
}, |
|
71
|
|
|
}, |
|
72
|
|
|
} |
|
73
|
|
|
schemaGeneratorMock.On("GenerateDataBySchema", mock.Anything, propertySchema).Return(nil, errors.New("error")).Once() |
|
74
|
|
|
|
|
75
|
|
|
data, err := objectGeneratorInstance.GenerateDataBySchema(context.Background(), schema) |
|
76
|
|
|
|
|
77
|
|
|
schemaGeneratorMock.AssertExpectations(t) |
|
78
|
|
|
assert.EqualError(t, err, "[objectGenerator] failed to generate object property 'propertyName': error") |
|
79
|
|
|
assert.Nil(t, data) |
|
80
|
|
|
} |
|
81
|
|
|
|