mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-05 16:15:56 +00:00
Add more prettify tests
This commit is contained in:
parent
356f1cfd17
commit
44a738afde
1 changed files with 66 additions and 31 deletions
|
|
@ -11,48 +11,83 @@ import (
|
|||
"github.com/pilosa/pilosa/bench"
|
||||
)
|
||||
|
||||
func TestPrettify(t *testing.T) {
|
||||
res := make(map[string]interface{}, 1)
|
||||
res["0"] = map[string]interface{}{
|
||||
"avg": time.Duration(12345),
|
||||
"max": time.Duration(23456),
|
||||
"int": 3456,
|
||||
"slice": []time.Duration{123, 234},
|
||||
}
|
||||
res["runtimes"] = map[string]interface{}{
|
||||
"0": time.Duration(10e9),
|
||||
"total": time.Duration(10e9),
|
||||
}
|
||||
resPretty := bench.Prettify(res)
|
||||
|
||||
func prettyEncode(data map[string]interface{}) string {
|
||||
pretty := bench.Prettify(data)
|
||||
jsonString := new(bytes.Buffer)
|
||||
enc := json.NewEncoder(jsonString)
|
||||
enc.SetIndent("", " ")
|
||||
|
||||
err := enc.Encode(resPretty)
|
||||
err := enc.Encode(pretty)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
|
||||
return jsonString.String()
|
||||
}
|
||||
|
||||
func TestPrettifyString(t *testing.T) {
|
||||
res := make(map[string]interface{}, 1)
|
||||
res["0"] = "foobar"
|
||||
pretty := prettyEncode(res)
|
||||
|
||||
expected := `
|
||||
{
|
||||
"0": {
|
||||
"avg": "12.345µs",
|
||||
"max": "23.456µs",
|
||||
"slice": [
|
||||
"123ns",
|
||||
"234ns"
|
||||
],
|
||||
"int": 3456
|
||||
},
|
||||
"runtimes": {
|
||||
"0": "10s",
|
||||
"total": "10s"
|
||||
}
|
||||
"0": "foobar"
|
||||
}
|
||||
`[1:]
|
||||
|
||||
if jsonString.String() != expected {
|
||||
t.Fatalf("failure")
|
||||
if pretty != expected {
|
||||
t.Fatalf("Pretty string doesn't match")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrettifyInt(t *testing.T) {
|
||||
res := make(map[string]interface{}, 1)
|
||||
res["0"] = 234567
|
||||
pretty := prettyEncode(res)
|
||||
|
||||
expected := `
|
||||
{
|
||||
"0": 234567
|
||||
}
|
||||
`[1:]
|
||||
|
||||
if pretty != expected {
|
||||
t.Fatalf("Pretty int doesn't match")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrettifyDuration(t *testing.T) {
|
||||
res := make(map[string]interface{}, 1)
|
||||
res["0"] = time.Duration(234567)
|
||||
pretty := prettyEncode(res)
|
||||
|
||||
expected := `
|
||||
{
|
||||
"0": "234.567µs"
|
||||
}
|
||||
`[1:]
|
||||
|
||||
if pretty != expected {
|
||||
t.Fatalf("Pretty duration doesn't match")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrettifyDurationSlice(t *testing.T) {
|
||||
res := make(map[string]interface{}, 1)
|
||||
res["0"] = []time.Duration{123, 234567, 34567890}
|
||||
pretty := prettyEncode(res)
|
||||
|
||||
expected := `
|
||||
{
|
||||
"0": [
|
||||
"123ns",
|
||||
"234.567µs",
|
||||
"34.56789ms"
|
||||
]
|
||||
}
|
||||
`[1:]
|
||||
|
||||
if pretty != expected {
|
||||
t.Fatalf("Pretty duration slice doesn't match")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue