tests pass woo
This commit is contained in:
@@ -35,7 +35,7 @@ type Handler struct {
|
|||||||
type DB interface {
|
type DB interface {
|
||||||
GetQuestions() ([]string, error)
|
GetQuestions() ([]string, error)
|
||||||
GetQuestion(string) (Question, error)
|
GetQuestion(string) (Question, error)
|
||||||
PutAnswer(string, string, Answer) error
|
InsertAnswer(string, string, Answer) error
|
||||||
GetAnswers(string) ([]Answer, error)
|
GetAnswers(string) ([]Answer, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,7 +220,7 @@ func (h Handler) handleAPIV1QuestionsAnswersPost(session Session, w http.Respons
|
|||||||
if err := json.NewDecoder(r.Body).Decode(&a); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&a); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return h.db.PutAnswer(qid, uid, a)
|
return h.db.InsertAnswer(qid, uid, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db fsDB) GetQuestion(qid string) (Question, error) {
|
func (db fsDB) GetQuestion(qid string) (Question, error) {
|
||||||
@@ -237,13 +237,16 @@ func (db fsDB) GetQuestion(qid string) (Question, error) {
|
|||||||
return q, nil
|
return q, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db fsDB) PutAnswer(qid, uid string, a Answer) error {
|
func (db fsDB) InsertAnswer(qid, uid string, a Answer) error {
|
||||||
p := path.Join(db.path(qid)+".d", uid)
|
p := path.Join(db.path(qid)+".d", uid)
|
||||||
os.MkdirAll(path.Dir(p), os.ModePerm)
|
os.MkdirAll(path.Dir(p), os.ModePerm)
|
||||||
b, err := json.Marshal(a)
|
b, err := json.Marshal(a)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if _, err := os.Stat(p); !os.IsNotExist(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return os.WriteFile(p, b, os.ModePerm)
|
return os.WriteFile(p, b, os.ModePerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,7 +271,7 @@ func (db fsDB) GetAnswers(qid string) ([]Answer, error) {
|
|||||||
}
|
}
|
||||||
results := []Answer{}
|
results := []Answer{}
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
b, err := os.ReadFile(entry.Name())
|
b, err := os.ReadFile(path.Join(p, entry.Name()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -11,6 +16,15 @@ func TestRunHTTP(t *testing.T) {
|
|||||||
cfg := Config{
|
cfg := Config{
|
||||||
fsDB: t.TempDir(),
|
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()
|
h := cfg.NewHandler()
|
||||||
|
|
||||||
@@ -62,10 +76,15 @@ func TestRunHTTP(t *testing.T) {
|
|||||||
t.Logf("%s %s", r.Method, r.URL)
|
t.Logf("%s %s", r.Method, r.URL)
|
||||||
h.ServeHTTP(w, r)
|
h.ServeHTTP(w, r)
|
||||||
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
|
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
|
||||||
if w.Code != http.StatusNotFound {
|
if w.Code != http.StatusOK {
|
||||||
t.Error(w.Code)
|
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) {
|
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)
|
t.Logf("%s %s", r.Method, r.URL)
|
||||||
h.ServeHTTP(w, r)
|
h.ServeHTTP(w, r)
|
||||||
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
|
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.Error(w.Code)
|
||||||
}
|
}
|
||||||
t.Errorf("not impl: %s", w.Body.Bytes())
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("GET /api/v1/questions/0/answers", func(t *testing.T) {
|
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)
|
t.Logf("%s %s", r.Method, r.URL)
|
||||||
h.ServeHTTP(w, r)
|
h.ServeHTTP(w, r)
|
||||||
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
|
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
|
||||||
if w.Code != http.StatusNotFound {
|
if w.Code != http.StatusOK {
|
||||||
t.Error(w.Code)
|
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) {
|
func TestPublic(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user