Added correct handling of schema example rendering

reported in https://github.com/pb33f/wiretap/issues/84

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2024-01-27 13:17:13 -05:00
parent 8039927eb5
commit 8825f5e328
3 changed files with 114 additions and 1 deletions

View File

@@ -323,3 +323,94 @@ func TestMockGenerator_GenerateMock_YamlNode_Nil(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "hello", strings.TrimSpace(string(mock)))
}
func TestMockGenerator_GenerateJSONMock_Object_SchemaExamples(t *testing.T) {
yml := `type: object
examples:
- name: happy days
description: a terrible show from a time that never existed.
- name: robocop
description: perhaps the best cyberpunk movie ever made.
properties:
name:
type: string
example: nameExample
description:
type: string
example: descriptionExample`
fake := createFakeMock(yml, nil, nil)
mg := NewMockGenerator(YAML)
mock, err := mg.GenerateMock(fake, "")
assert.NoError(t, err)
// re-serialize back into a map and check the values
var m map[string]any
err = yaml.Unmarshal(mock, &m)
assert.NoError(t, err)
assert.Len(t, m, 2)
assert.Equal(t, "happy days", m["name"].(string))
assert.Equal(t, "a terrible show from a time that never existed.", m["description"].(string))
}
func TestMockGenerator_GenerateJSONMock_Object_SchemaExamples_Preferred(t *testing.T) {
yml := `type: object
examples:
- name: happy days
description: a terrible show from a time that never existed.
- name: robocop
description: perhaps the best cyberpunk movie ever made.
properties:
name:
type: string
example: nameExample
description:
type: string
example: descriptionExample`
fake := createFakeMock(yml, nil, nil)
mg := NewMockGenerator(YAML)
mock, err := mg.GenerateMock(fake, "1")
assert.NoError(t, err)
// re-serialize back into a map and check the values
var m map[string]any
err = yaml.Unmarshal(mock, &m)
assert.NoError(t, err)
assert.Len(t, m, 2)
assert.Equal(t, "robocop", m["name"].(string))
assert.Equal(t, "perhaps the best cyberpunk movie ever made.", m["description"].(string))
}
func TestMockGenerator_GenerateJSONMock_Object_SchemaExample(t *testing.T) {
yml := `type: object
example:
name: robocop
description: perhaps the best cyberpunk movie ever made.
properties:
name:
type: string
example: nameExample
description:
type: string
example: descriptionExample`
fake := createFakeMock(yml, nil, nil)
mg := NewMockGenerator(YAML)
mock, err := mg.GenerateMock(fake, "")
assert.NoError(t, err)
// re-serialize back into a map and check the values
var m map[string]any
err = yaml.Unmarshal(mock, &m)
assert.NoError(t, err)
assert.Len(t, m, 2)
assert.Equal(t, "robocop", m["name"].(string))
assert.Equal(t, "perhaps the best cyberpunk movie ever made.", m["description"].(string))
}