mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 04:20:24 +00:00
Hardening mock generator with a few more tests
Signed-off-by: Quobix <dave@quobix.com>
This commit is contained in:
@@ -92,8 +92,6 @@ func (mg *MockGenerator) GenerateMock(mock any, name string) ([]byte, error) {
|
||||
// check if this is a SchemaProxy, if not, then see if it has a Schema, if not, then we can't generate a mock.
|
||||
var schemaValue *highbase.Schema
|
||||
switch reflect.TypeOf(mock) {
|
||||
case reflect.TypeOf(&highbase.SchemaProxy{}):
|
||||
schemaValue = mock.(*highbase.SchemaProxy).Schema()
|
||||
case reflect.TypeOf(&highbase.Schema{}):
|
||||
schemaValue = mock.(*highbase.Schema)
|
||||
default:
|
||||
|
||||
@@ -20,6 +20,12 @@ type fakeMockable struct {
|
||||
Examples map[string]*highbase.Example
|
||||
}
|
||||
|
||||
type fakeMockableButWithASchemaNotAProxy struct {
|
||||
Schema *highbase.Schema
|
||||
Example any
|
||||
Examples map[string]*highbase.Example
|
||||
}
|
||||
|
||||
var simpleFakeMockSchema = `type: string
|
||||
enum: [magic-herbs]`
|
||||
|
||||
@@ -56,6 +62,29 @@ func createFakeMock(mock string, values map[string]any, example any) *fakeMockab
|
||||
}
|
||||
}
|
||||
|
||||
func createFakeMockWithoutProxy(mock string, values map[string]any, example any) *fakeMockableButWithASchemaNotAProxy {
|
||||
var root yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(mock), &root)
|
||||
var lowProxy lowbase.SchemaProxy
|
||||
_ = lowProxy.Build(&root, root.Content[0], nil)
|
||||
lowRef := low.NodeReference[*lowbase.SchemaProxy]{
|
||||
Value: &lowProxy,
|
||||
}
|
||||
highSchema := highbase.NewSchemaProxy(&lowRef)
|
||||
examples := make(map[string]*highbase.Example)
|
||||
|
||||
for k, v := range values {
|
||||
examples[k] = &highbase.Example{
|
||||
Value: v,
|
||||
}
|
||||
}
|
||||
return &fakeMockableButWithASchemaNotAProxy{
|
||||
Schema: highSchema.Schema(),
|
||||
Example: example,
|
||||
Examples: examples,
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewMockGenerator(t *testing.T) {
|
||||
mg := NewMockGenerator(MockJSON)
|
||||
assert.NotNil(t, mg)
|
||||
@@ -210,3 +239,23 @@ func TestMockGenerator_GenerateJSONMock_Object_NoExamples_YAML(t *testing.T) {
|
||||
assert.GreaterOrEqual(t, m["herbs"].(int), 350)
|
||||
assert.LessOrEqual(t, m["herbs"].(int), 400)
|
||||
}
|
||||
|
||||
// should result in the exact same output as the above test
|
||||
func TestMockGenerator_GenerateJSONMock_Object_RawSchema(t *testing.T) {
|
||||
|
||||
fake := createFakeMockWithoutProxy(objectFakeMockSchema, nil, nil)
|
||||
|
||||
mg := NewMockGenerator(MockYAML)
|
||||
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.GreaterOrEqual(t, len(m["coffee"].(string)), 6)
|
||||
assert.GreaterOrEqual(t, m["herbs"].(int), 350)
|
||||
assert.LessOrEqual(t, m["herbs"].(int), 400)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user