Skip to content

Commit

Permalink
Reuse list data from fixtures where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
brandur committed Jun 23, 2017
1 parent 0fc3983 commit dfc785e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
22 changes: 17 additions & 5 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func (g *DataGenerator) generateResource(schema *JSONSchema) (interface{}, error
}

func (g *DataGenerator) Generate(schema *JSONSchema, requestPath string) (interface{}, error) {
return g.generateInternal(schema, requestPath, nil)
}

func (g *DataGenerator) generateInternal(schema *JSONSchema, requestPath string, existingData interface{}) (interface{}, error) {
schema, err := g.maybeDereference(schema)
if err != nil {
return nil, err
Expand All @@ -65,7 +69,8 @@ func (g *DataGenerator) Generate(schema *JSONSchema, requestPath string) (interf
}

if schema.Properties != nil {
listData, err := g.maybeGenerateList(schema.Properties, requestPath)
listData, err := g.maybeGenerateList(
schema.Properties, existingData, requestPath)
if err != nil {
return nil, err
}
Expand All @@ -74,21 +79,22 @@ func (g *DataGenerator) Generate(schema *JSONSchema, requestPath string) (interf
}

for key, property := range schema.Properties {
keyData, err := g.Generate(property, requestPath)
dataMap := data.(map[string]interface{})
keyData, err := g.generateInternal(property, requestPath, dataMap[key])
if err == notSupportedErr {
continue
}
if err != nil {
return nil, err
}
data.(map[string]interface{})[key] = keyData
dataMap[key] = keyData
}
}

return data, nil
}

func (g *DataGenerator) maybeGenerateList(properties map[string]*JSONSchema, requestPath string) (interface{}, error) {
func (g *DataGenerator) maybeGenerateList(properties map[string]*JSONSchema, existingData interface{}, requestPath string) (interface{}, error) {
object, ok := properties["object"]
if !ok {
return nil, nil
Expand Down Expand Up @@ -137,7 +143,13 @@ func (g *DataGenerator) maybeGenerateList(properties map[string]*JSONSchema, req
case "total_count":
val = 1
case "url":
val = requestPath
existingDataMap, ok := existingData.(map[string]interface{})
if ok {
// Reuse a URL that came from fixture data if one is available
val = existingDataMap["url"]
} else {
val = requestPath
}
default:
val = nil
}
Expand Down
19 changes: 16 additions & 3 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,30 @@ func TestGenerateResponseData(t *testing.T) {
data.(map[string]interface{})["data"].([]interface{})[0].(map[string]interface{})["id"])

// nested list
generator = DataGenerator{testSpec.Definitions, testFixtures}
generator = DataGenerator{
testSpec.Definitions,
&Fixtures{
Resources: map[ResourceID]interface{}{
ResourceID("charge"): map[string]interface{}{"id": "ch_123"},
ResourceID("with_charges_list"): map[string]interface{}{
"charges_list": map[string]interface{}{
"url": "/v1/from_charges_list",
},
},
},
},
}
data, err = generator.Generate(
&JSONSchema{
Properties: map[string]*JSONSchema{
"charges_list": listSchema,
},
}, "/v1/charges")
XResourceID: "with_charges_list",
}, "")
assert.Nil(t, err)
chargesList := data.(map[string]interface{})["charges_list"]
assert.Equal(t, "list", chargesList.(map[string]interface{})["object"])
assert.Equal(t, "/v1/charges", chargesList.(map[string]interface{})["url"])
assert.Equal(t, "/v1/from_charges_list", chargesList.(map[string]interface{})["url"])
assert.Equal(t,
testFixtures.Resources["charge"].(map[string]interface{})["id"],
chargesList.(map[string]interface{})["data"].([]interface{})[0].(map[string]interface{})["id"])
Expand Down

0 comments on commit dfc785e

Please sign in to comment.