tests pass woo

This commit is contained in:
Bel LaPointe
2024-02-20 08:55:31 -07:00
parent 706522eeef
commit ce1c1e0205
2 changed files with 54 additions and 22 deletions

View File

@@ -2,8 +2,13 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path"
"strings"
"testing"
)
@@ -11,6 +16,15 @@ func TestRunHTTP(t *testing.T) {
cfg := Config{
fsDB: t.TempDir(),
}
if err := func() error {
b, _ := json.Marshal(Question{
Text: "QUESTION TEXT",
Options: []string{"X", "Y"},
})
return os.WriteFile(path.Join(string(cfg.fsDB), "0"), b, os.ModePerm)
}(); err != nil {
t.Fatal(err)
}
h := cfg.NewHandler()
@@ -62,10 +76,15 @@ func TestRunHTTP(t *testing.T) {
t.Logf("%s %s", r.Method, r.URL)
h.ServeHTTP(w, r)
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
if w.Code != http.StatusNotFound {
if w.Code != http.StatusOK {
t.Error(w.Code)
}
t.Errorf("not impl: %s", w.Body.Bytes())
var result []string
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Error(err)
} else if fmt.Sprint(result) != fmt.Sprint([]string{"0"}) {
t.Error(result)
}
})
t.Run("/api/v1/questions/0", func(t *testing.T) {
@@ -75,10 +94,27 @@ func TestRunHTTP(t *testing.T) {
t.Logf("%s %s", r.Method, r.URL)
h.ServeHTTP(w, r)
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
if w.Code != http.StatusNotFound {
if w.Code != http.StatusOK {
t.Error(w.Code)
}
var result Question
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Error(err)
} else if fmt.Sprint(result) != fmt.Sprint(Question{Text: "QUESTION TEXT", Options: []string{"X", "Y"}}) {
t.Error(result)
}
})
t.Run("POST /api/v1/questions/0/answers", func(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/api/v1/questions/0/answers", strings.NewReader(`{"Text": "teehee"}`))
r.SetBasicAuth("b", "b")
w := httptest.NewRecorder()
t.Logf("%s %s", r.Method, r.URL)
h.ServeHTTP(w, r)
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
if w.Code != http.StatusOK {
t.Error(w.Code)
}
t.Errorf("not impl: %s", w.Body.Bytes())
})
t.Run("GET /api/v1/questions/0/answers", func(t *testing.T) {
@@ -88,24 +124,17 @@ func TestRunHTTP(t *testing.T) {
t.Logf("%s %s", r.Method, r.URL)
h.ServeHTTP(w, r)
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
if w.Code != http.StatusNotFound {
if w.Code != http.StatusOK {
t.Error(w.Code)
}
t.Errorf("not impl: %s", w.Body.Bytes())
var result []Answer
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Error(err)
} else if fmt.Sprint(result) != fmt.Sprint([]Answer{{Text: "teehee"}}) {
t.Error(result)
}
})
t.Run("POST /api/v1/questions/0/answers", func(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/api/v1/questions/0/answers", nil)
r.SetBasicAuth("b", "b")
w := httptest.NewRecorder()
t.Logf("%s %s", r.Method, r.URL)
h.ServeHTTP(w, r)
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
if w.Code != http.StatusNotFound {
t.Error(w.Code)
}
t.Errorf("not impl: %s", w.Body.Bytes())
})
}
func TestPublic(t *testing.T) {