77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package v01
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
func TestPatchConfig(t *testing.T) {
|
|
dir := t.TempDir()
|
|
p := path.Join(dir, t.Name()+".yaml")
|
|
cases := map[string]struct {
|
|
was config
|
|
patch string
|
|
want config
|
|
}{
|
|
"replace entire doc": {
|
|
was: config{
|
|
Feedback: configFeedback{Addr: "a", TTSURL: "a"},
|
|
Users: map[string]configUser{"a": configUser{Player: 1, Message: "a"}},
|
|
Players: []configPlayer{configPlayer{Transformation: transformation{"a": "a"}}},
|
|
Quiet: true,
|
|
},
|
|
patch: `[{"op": "replace", "path": "", "value": {
|
|
"Feedback": {"Addr": "b", "TTSURL": "b"},
|
|
"Users": {"b": {"Player": 2, "Message": "b"}},
|
|
"Players": [{"Transformation": {"b": "b"}}],
|
|
"Quiet": false
|
|
}}]`,
|
|
want: config{
|
|
Feedback: configFeedback{Addr: "b", TTSURL: "b"},
|
|
Users: map[string]configUser{"b": configUser{Player: 2, Message: "b"}},
|
|
Players: []configPlayer{configPlayer{Transformation: transformation{"b": "b"}}},
|
|
Quiet: false,
|
|
},
|
|
},
|
|
}
|
|
|
|
for name, d := range cases {
|
|
c := d
|
|
for _, usesdisk := range []bool{false, true} {
|
|
t.Run(fmt.Sprintf("%s disk=%v", name, usesdisk), func(t *testing.T) {
|
|
b, _ := yaml.Marshal(c.was)
|
|
os.WriteFile(p, b, os.ModePerm)
|
|
FlagParseV01Config = ""
|
|
if usesdisk {
|
|
FlagParseV01Config = p
|
|
}
|
|
v01 := &V01{cfg: c.was}
|
|
v01.cfg.lock = &sync.Mutex{}
|
|
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest(http.MethodPatch, "/config", strings.NewReader(c.patch))
|
|
v01.servePatchConfig(w, r)
|
|
if fmt.Sprintf("%+v", c.want) != fmt.Sprintf("%+v", v01.cfg) {
|
|
t.Errorf("want \n\t%+v, got \n\t%+v", c.want, v01.cfg)
|
|
}
|
|
if usesdisk {
|
|
b, _ := os.ReadFile(p)
|
|
var got config
|
|
yaml.Unmarshal(b, &got)
|
|
if fmt.Sprintf("%+v", c.want) != fmt.Sprintf("%+v", v01.cfg) {
|
|
t.Errorf("want \n\t%+v, got \n\t%+v", c.want, v01.cfg)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|