87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
package generatedtext
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidateHourlyNormalizesJSON(t *testing.T) {
|
|
value, normalized, err := ValidateHourly([]byte(`{
|
|
"timing": " main window late morning ",
|
|
"summary": " Storm chances increase. ",
|
|
"confidence": " Medium ",
|
|
"impacts": " Brief downpours. "
|
|
}`))
|
|
if err != nil {
|
|
t.Fatalf("ValidateHourly() error = %v", err)
|
|
}
|
|
if value.Summary != "Storm chances increase." {
|
|
t.Fatalf("Summary = %q, want trimmed summary", value.Summary)
|
|
}
|
|
want := `{"summary":"Storm chances increase.","timing":"main window late morning","impacts":"Brief downpours.","confidence":"Medium"}`
|
|
if string(normalized) != want {
|
|
t.Fatalf("normalized = %s, want %s", normalized, want)
|
|
}
|
|
}
|
|
|
|
func TestValidateHourlyOmitsEmptyConfidence(t *testing.T) {
|
|
_, normalized, err := ValidateHourly([]byte(`{
|
|
"summary": "Storm chances increase.",
|
|
"timing": "Late morning.",
|
|
"impacts": "Brief downpours.",
|
|
"confidence": " "
|
|
}`))
|
|
if err != nil {
|
|
t.Fatalf("ValidateHourly() error = %v", err)
|
|
}
|
|
want := `{"summary":"Storm chances increase.","timing":"Late morning.","impacts":"Brief downpours."}`
|
|
if string(normalized) != want {
|
|
t.Fatalf("normalized = %s, want %s", normalized, want)
|
|
}
|
|
}
|
|
|
|
func TestValidateHourlyRejectsInvalidInput(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
}{
|
|
{
|
|
name: "malformed",
|
|
in: `{`,
|
|
want: "decode hourly generated text",
|
|
},
|
|
{
|
|
name: "unknown field",
|
|
in: `{"summary":"Storm chances increase.","timing":"Late morning.","impacts":"Brief downpours.","extra":"value"}`,
|
|
want: `unknown field "extra"`,
|
|
},
|
|
{
|
|
name: "missing summary",
|
|
in: `{"timing":"Late morning.","impacts":"Brief downpours."}`,
|
|
want: "summary is required",
|
|
},
|
|
{
|
|
name: "blank timing",
|
|
in: `{"summary":"Storm chances increase.","timing":" ","impacts":"Brief downpours."}`,
|
|
want: "timing is required",
|
|
},
|
|
{
|
|
name: "multiple values",
|
|
in: `{"summary":"Storm chances increase.","timing":"Late morning.","impacts":"Brief downpours."} {}`,
|
|
want: "multiple JSON values",
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
_, _, err := ValidateHourly([]byte(test.in))
|
|
if err == nil {
|
|
t.Fatal("ValidateHourly() error = nil, want error")
|
|
}
|
|
if !strings.Contains(err.Error(), test.want) {
|
|
t.Fatalf("ValidateHourly() error = %v, want %q", err, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|