From ce1c1e020599c8121b1a55c07dcbaa09d00c8e50 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Tue, 20 Feb 2024 08:55:31 -0700 Subject: [PATCH] tests pass woo --- cmd/server/main.go | 11 ++++--- cmd/server/main_test.go | 65 +++++++++++++++++++++++++++++------------ 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 97aca75..344e3de 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -35,7 +35,7 @@ type Handler struct { type DB interface { GetQuestions() ([]string, error) GetQuestion(string) (Question, error) - PutAnswer(string, string, Answer) error + InsertAnswer(string, 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 { return err } - return h.db.PutAnswer(qid, uid, a) + return h.db.InsertAnswer(qid, uid, a) } func (db fsDB) GetQuestion(qid string) (Question, error) { @@ -237,13 +237,16 @@ func (db fsDB) GetQuestion(qid string) (Question, error) { 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) os.MkdirAll(path.Dir(p), os.ModePerm) b, err := json.Marshal(a) if err != nil { return err } + if _, err := os.Stat(p); !os.IsNotExist(err) { + return nil + } return os.WriteFile(p, b, os.ModePerm) } @@ -268,7 +271,7 @@ func (db fsDB) GetAnswers(qid string) ([]Answer, error) { } results := []Answer{} for _, entry := range entries { - b, err := os.ReadFile(entry.Name()) + b, err := os.ReadFile(path.Join(p, entry.Name())) if err != nil { return nil, err } diff --git a/cmd/server/main_test.go b/cmd/server/main_test.go index 178bf30..93ea7c6 100644 --- a/cmd/server/main_test.go +++ b/cmd/server/main_test.go @@ -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) {