From 49f943d7e718bd6f9799c5776a147ed19ec09565 Mon Sep 17 00:00:00 2001 From: imura81gt Date: Mon, 30 Dec 2019 00:17:59 +0900 Subject: [PATCH 1/2] [kadai4:fortuneAPI] first commit --- kadai4/imura81gt/fortune/README.md | 16 ++++++ kadai4/imura81gt/fortune/cmd/fortune/main.go | 28 ++++++++++ kadai4/imura81gt/fortune/fortune.go | 50 +++++++++++++++++ kadai4/imura81gt/fortune/fortune_test.go | 56 ++++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 kadai4/imura81gt/fortune/README.md create mode 100644 kadai4/imura81gt/fortune/cmd/fortune/main.go create mode 100644 kadai4/imura81gt/fortune/fortune.go create mode 100644 kadai4/imura81gt/fortune/fortune_test.go diff --git a/kadai4/imura81gt/fortune/README.md b/kadai4/imura81gt/fortune/README.md new file mode 100644 index 0000000..2677fe5 --- /dev/null +++ b/kadai4/imura81gt/fortune/README.md @@ -0,0 +1,16 @@ +おみくじAPIを作ってみよう +======================================== + +- [x] JSON形式でおみくじの結果を返す +- [x] 正月(1/1-1/3)だけ大吉にする +- [ ] ハンドラのテストを書いてみる + + + +構想 +------------------------------------------ + +- HTTPサーバーつくる +- おみくじ Middleware +- 特定日付大吉 Middleware +- handler test diff --git a/kadai4/imura81gt/fortune/cmd/fortune/main.go b/kadai4/imura81gt/fortune/cmd/fortune/main.go new file mode 100644 index 0000000..df7bede --- /dev/null +++ b/kadai4/imura81gt/fortune/cmd/fortune/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" + + "github.com/gopherdojo/dojo7/kadai4/imura81gt/fortune" +) + +func handler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + + f := &fortune.Fortune{} + v := struct { + Msg string `json:"msg"` + }{ + Msg: f.Do(), + } + if err := json.NewEncoder(w).Encode(v); err != nil { + log.Println("Error:", err) + } +} + +func main() { + http.HandleFunc("/", handler) + http.ListenAndServe(":8080", nil) +} diff --git a/kadai4/imura81gt/fortune/fortune.go b/kadai4/imura81gt/fortune/fortune.go new file mode 100644 index 0000000..48c3c12 --- /dev/null +++ b/kadai4/imura81gt/fortune/fortune.go @@ -0,0 +1,50 @@ +package fortune + +import ( + "math/rand" + "time" +) + +type Clock interface { + Now() time.Time +} + +type ClockFunc func() time.Time + +func (f ClockFunc) Now() time.Time { + return f() +} + +type Fortune struct { + Clock Clock +} + +func (f *Fortune) now() time.Time { + if f.Clock == nil { + return time.Now() + } + return f.Clock.Now() +} + +func (f *Fortune) Do() string { + fortunes := []string{"大吉", "中吉", "小吉", "末吉", "凶"} + + now := f.now() + year := now.Year() + month := now.Month() + day := now.Day() + hour := now.Hour() + minute := now.Minute() + second := now.Second() + nanosecond := now.Nanosecond() + + if month == 01 && (day == 1 || day == 2 || day == 3) { + return fortunes[0] + } + t := time.Date(year, month, day, hour, minute, second, nanosecond, time.Local) + rand.Seed(t.Unix()) + + i := rand.Intn(len(fortunes)) + + return fortunes[i] +} diff --git a/kadai4/imura81gt/fortune/fortune_test.go b/kadai4/imura81gt/fortune/fortune_test.go new file mode 100644 index 0000000..ec3b44b --- /dev/null +++ b/kadai4/imura81gt/fortune/fortune_test.go @@ -0,0 +1,56 @@ +package fortune_test + +import ( + "fmt" + "testing" + "time" + + "github.com/gopherdojo/dojo7/kadai4/imura81gt/fortune" +) + +func TestFortuneDo(t *testing.T) { + + testCases := []struct { + caseName string + clock fortune.Clock + expected string + }{ + {"2019-01-01", C(t, "2019-01-01 15:04:05"), "大吉"}, + {"2019-01-02", C(t, "2019-01-02 15:04:05"), "大吉"}, + {"2019-01-03", C(t, "2019-01-03 15:04:05"), "大吉"}, + {"2020-01-01", C(t, "2020-01-01 15:04:05"), "大吉"}, + {"2020-01-02", C(t, "2020-01-02 15:04:05"), "大吉"}, + {"2020-01-03", C(t, "2020-01-03 15:04:05"), "大吉"}, + {"2020-01-04", C(t, "2020-01-04 15:04:05"), "末吉"}, + {"2020-01-04", C(t, "2020-01-05 15:04:06"), "中吉"}, + {"2020-01-04", C(t, "2020-01-05 15:04:07"), "中吉"}, + {"2020-01-04", C(t, "2020-01-05 15:04:08"), "大吉"}, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.caseName, func(t *testing.T) { + t.Parallel() + f := &fortune.Fortune{Clock: tc.clock} + actual := f.Do() + + if tc.expected != actual { + t.Errorf("\nactual: %+v\nexpected: %+v\n", actual, tc.expected) + } + + }) + } + +} + +func C(t *testing.T, v string) fortune.Clock { + t.Helper() + str := fmt.Sprintf("%s", v) + now, err := time.Parse("2006-01-02 15:04:05", str) + if err != nil { + t.Fatal("unexpected error:", err) + } + return fortune.ClockFunc(func() time.Time { + return now + }) +} From 347a1c18d81bca067fb6844c584e850c09926cd2 Mon Sep 17 00:00:00 2001 From: imura81gt Date: Wed, 8 Jan 2020 22:11:16 +0900 Subject: [PATCH 2/2] [kadai4:fortuneAPI] add testing for handler --- .../fortune/cmd/fortune/main_test.go | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 kadai4/imura81gt/fortune/cmd/fortune/main_test.go diff --git a/kadai4/imura81gt/fortune/cmd/fortune/main_test.go b/kadai4/imura81gt/fortune/cmd/fortune/main_test.go new file mode 100644 index 0000000..0913c6f --- /dev/null +++ b/kadai4/imura81gt/fortune/cmd/fortune/main_test.go @@ -0,0 +1,44 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" +) + +func TestHandler(t *testing.T) { + + type Body struct { + Msg string `json:msg` + } + + var body Body + + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + handler(w, r) + rw := w.Result() + defer rw.Body.Close() + + if rw.StatusCode != http.StatusOK { + t.Fatal("unexpected status code") + } + + b, err := ioutil.ReadAll(rw.Body) + if err != nil { + t.Fatalf("unexpected err: %+v", err) + } + + err = json.Unmarshal(b, &body) + if err != nil { + t.Fatalf("unexpected err: %+v", err) + } + + t.Logf("body.Msg: %+v", body.Msg) + + if body.Msg == "" { + t.Errorf("cannot get the body msg: %+v", body.Msg) + } +}