Skip to content

Commit

Permalink
Update test - disable required check to render all examples
Browse files Browse the repository at this point in the history
Test default behaviour when required check is not disabled
  • Loading branch information
martinsirbe authored and daveshanley committed May 24, 2024
1 parent 3eaa7bf commit f27d57a
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions renderer/mock_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,8 @@ properties:
assert.Equal(t, "perhaps the best cyberpunk movie ever made.", m["description"].(string))
}

// Test schema rendering for property examples, regardless of whether the property
// is marked as required or not.
func TestMockGenerator_GenerateExamplesForAllProperties(t *testing.T) {

yml := `type: object
func TestMockGenerator_GeneratePropertyExamples(t *testing.T) {
fake := createFakeMock(`type: object
required:
- id
- name
Expand All @@ -439,13 +436,40 @@ properties:
items:
type: string
example: ["tag1", "tag2", "tag3"]
`
`, nil, nil)

for name, tc := range map[string]struct {
mockGen func() *MockGenerator
expectedMock string
}{
"OnlyRequired": {
mockGen: func() *MockGenerator {
mg := NewMockGenerator(JSON)
return mg
},
expectedMock: `{"id":123,"name":"John Doe"}`,
},
"All": {
mockGen: func() *MockGenerator {
mg := NewMockGenerator(JSON)

fake := createFakeMock(yml, nil, nil)
mg := NewMockGenerator(JSON)
// Test schema rendering for property examples, regardless of
// whether the property is marked as required or not.
mg.DisableRequiredCheck()

mock, err := mg.GenerateMock(fake, "")
require.NoError(t, err)
return mg
},
expectedMock: `{"active":true,"balance":99.99,"id":123,"name":"John Doe","tags":["tag1","tag2","tag3"]}`,
},
} {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()

mock, err := tc.mockGen().GenerateMock(fake, "")
require.NoError(t, err)

assert.Equal(t, `{"active":true,"balance":99.99,"id":123,"name":"John Doe","tags":["tag1","tag2","tag3"]}`, string(mock))
assert.Equal(t, tc.expectedMock, string(mock))
})
}
}

0 comments on commit f27d57a

Please sign in to comment.