63 lines
2.1 KiB
Go
63 lines
2.1 KiB
Go
package contracts
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/santhosh-tekuri/jsonschema/v6"
|
|
)
|
|
|
|
func TestTaskTrunkConcurrencySchema(t *testing.T) {
|
|
schema, err := CompileCurrent("http-task-detail.schema.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fixture, err := Files.ReadFile("schema/examples/config-read-task-asr.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
name string
|
|
value string
|
|
valid bool
|
|
}{
|
|
{"multiple lines", `[{"trunk_id":"12","concurrency":5},{"trunk_id":"15","concurrency":5}]`, true},
|
|
{"zero concurrency", `[{"trunk_id":"12","concurrency":0}]`, true},
|
|
{"old string array", `["12","15"]`, false},
|
|
{"empty array", `[]`, false},
|
|
{"missing trunk ID", `[{"concurrency":5}]`, false},
|
|
{"empty trunk ID", `[{"trunk_id":"","concurrency":5}]`, false},
|
|
{"numeric trunk ID", `[{"trunk_id":12,"concurrency":5}]`, false},
|
|
{"missing concurrency", `[{"trunk_id":"12"}]`, false},
|
|
{"negative concurrency", `[{"trunk_id":"12","concurrency":-1}]`, false},
|
|
{"fractional concurrency", `[{"trunk_id":"12","concurrency":1.5}]`, false},
|
|
{"string concurrency", `[{"trunk_id":"12","concurrency":"5"}]`, false},
|
|
{"null concurrency", `[{"trunk_id":"12","concurrency":null}]`, false},
|
|
{"boolean concurrency", `[{"trunk_id":"12","concurrency":false}]`, false},
|
|
{"unknown property", `[{"trunk_id":"12","concurrency":5,"unknown":true}]`, false},
|
|
{"duplicate entry", `[{"trunk_id":"12","concurrency":5},{"trunk_id":"12","concurrency":5}]`, false},
|
|
{"null entry", `[null]`, false},
|
|
{"null array", `null`, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var doc map[string]json.RawMessage
|
|
if err := json.Unmarshal(fixture, &doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
doc["allowed_trunk_ids"] = json.RawMessage(tc.value)
|
|
data, err := json.Marshal(doc)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
instance, err := jsonschema.UnmarshalJSON(bytes.NewReader(data))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := schema.Validate(instance); (err == nil) != tc.valid {
|
|
t.Fatalf("valid=%t, want %t: %v", err == nil, tc.valid, err)
|
|
}
|
|
})
|
|
}
|
|
}
|