internal/openapi/generator/data/objectGenerator_test.go   A
last analyzed

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 53
dl 0
loc 79
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A data.TestObjectGenerator_GenerateDataBySchema_ObjectWithOneProperty_PropertyGeneratedBySchemaGenerator 0 21 1
A data.TestObjectGenerator_GenerateDataBySchema_SchemaGeneratorError_ErrorReturned 0 21 1
A data.TestObjectGenerator_GenerateDataBySchema_ObjectWithOneWriteOnlyProperty_EmptyObject 0 21 1
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