Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Yuri Shkuro <[email protected]>
  • Loading branch information
Yuri Shkuro committed Oct 24, 2017
1 parent e241533 commit 171e303
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/query/app/fixture/ui-config-malformed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"x" == "y"}
1 change: 1 addition & 0 deletions cmd/query/app/fixture/ui-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"x": "y"}
4 changes: 4 additions & 0 deletions cmd/query/app/fixture/ui-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x: abcd
z:
- a
- b
4 changes: 4 additions & 0 deletions cmd/query/app/fixture/ui-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x: abcd
z:
- a
- b
4 changes: 4 additions & 0 deletions cmd/query/app/fixture/ui-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x: abcd
z:
- a
- b
14 changes: 6 additions & 8 deletions cmd/query/app/static_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,20 @@ func loadUIConfig(uiConfig string) (map[string]interface{}, error) {
}

var c map[string]interface{}
var unmarshall func([]byte, interface{}) error

switch strings.ToLower(ext) {
case ".yaml", ".yml":
if err := yaml.Unmarshal(bytes, &c); err != nil {
return nil, errors.Wrapf(err, "Cannot parse UI config file %v as YAML", uiConfig)
}

unmarshall = yaml.Unmarshal
case ".json":
if err := json.Unmarshal(bytes, &c); err != nil {
return nil, errors.Wrapf(err, "Cannot parse UI config file %v as JSON", uiConfig)
}

unmarshall = json.Unmarshal
default:
return nil, fmt.Errorf("Unrecognized UI config file format %v", uiConfig)
}

if err := unmarshall(bytes, &c); err != nil {
return nil, errors.Wrapf(err, "Cannot parse UI config file %v", uiConfig)
}
return c, nil
}

Expand Down
52 changes: 52 additions & 0 deletions cmd/query/app/static_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,55 @@ func TestRegisterRoutesHandler(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, expectedRespString, respString)
}

func TestLoadUIConfig(t *testing.T) {
type testCase struct {
configFile string
expected map[string]interface{}
expectedError string
}

run := func(description string, testCase testCase) {
t.Run(description, func(t *testing.T) {
config, err := loadUIConfig(testCase.configFile)
if testCase.expectedError != "" {
assert.EqualError(t, err, testCase.expectedError)
} else {
assert.NoError(t, err)
}
assert.EqualValues(t, testCase.expected, config)
})
}

run("no config", testCase{})
run("invalid config", testCase{
configFile: "invalid",
expectedError: "Cannot read UI config file invalid: open invalid: no such file or directory",
})
run("unsupported type", testCase{
configFile: "fixture/ui-config.toml",
expectedError: "Unrecognized UI config file format fixture/ui-config.toml",
})
run("malformed", testCase{
configFile: "fixture/ui-config-malformed.json",
expectedError: "Cannot parse UI config file fixture/ui-config-malformed.json: invalid character '=' after object key",
})
run("yaml", testCase{
configFile: "fixture/ui-config.yaml",
expected: map[string]interface{}{
"x": "abcd",
"z": []interface{}{"a", "b"},
},
})
run("yml", testCase{
configFile: "fixture/ui-config.yml",
expected: map[string]interface{}{
"x": "abcd",
"z": []interface{}{"a", "b"},
},
})
run("json", testCase{
configFile: "fixture/ui-config.json",
expected: map[string]interface{}{"x": "y"},
})
}

0 comments on commit 171e303

Please sign in to comment.