46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package collector
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.tornberg.me/go-otel/internal/todos"
|
|
)
|
|
|
|
func TestTodoCreateAndList(t *testing.T) {
|
|
coll := New(nil, 10, nil)
|
|
st, err := todos.NewFileStore(filepath.Join(t.TempDir(), "todos.jsonl"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
coll.SetTodoStore(st)
|
|
|
|
post := func(body string) *httptest.ResponseRecorder {
|
|
w := httptest.NewRecorder()
|
|
coll.HandleTodoCreate(w, httptest.NewRequest(http.MethodPost, "/v1/todos", strings.NewReader(body)))
|
|
return w
|
|
}
|
|
if w := post(`{"project":"a","text":"fix cart","component":"Cart","path":"/cart","hash":"#x"}`); w.Code != http.StatusCreated {
|
|
t.Fatalf("create: %d %s", w.Code, w.Body)
|
|
}
|
|
post(`{"project":"b","text":"other"}`)
|
|
if w := post(`{"project":"a","text":" "}`); w.Code != http.StatusBadRequest {
|
|
t.Fatalf("empty text: %d", w.Code)
|
|
}
|
|
|
|
list := func(q string) string {
|
|
w := httptest.NewRecorder()
|
|
coll.HandleTodoList(w, httptest.NewRequest(http.MethodGet, "/api/todos"+q, nil))
|
|
return w.Body.String()
|
|
}
|
|
if got := list("?project=a"); !strings.Contains(got, "fix cart") || strings.Contains(got, "other") || !strings.Contains(got, `"hash":"#x"`) {
|
|
t.Fatalf("project filter: %s", got)
|
|
}
|
|
if got := list(""); !strings.Contains(got, "fix cart") || !strings.Contains(got, "other") {
|
|
t.Fatalf("all: %s", got)
|
|
}
|
|
}
|