From 86b559ce425b197ba34ba69c47ce2caf841c8cdf Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Tue, 13 Aug 2019 02:07:14 +0900 Subject: [PATCH 1/6] Add README --- kadai4/pei/README.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 kadai4/pei/README.md diff --git a/kadai4/pei/README.md b/kadai4/pei/README.md new file mode 100644 index 0000000..0b12fe7 --- /dev/null +++ b/kadai4/pei/README.md @@ -0,0 +1,5 @@ +## 課題4 おみくじAPIを作ってみよう + +- JSON形式でおみくじの結果を返す +- 正月(1/1-1/3)だけ大吉にする +- ハンドラのテストを書いてみる From 90084a2dc0d9780f134fb46d6cab896f7c45ca8e Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Fri, 16 Aug 2019 02:46:57 +0900 Subject: [PATCH 2/6] Add Makefile --- kadai4/pei/Makefile | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 kadai4/pei/Makefile diff --git a/kadai4/pei/Makefile b/kadai4/pei/Makefile new file mode 100644 index 0000000..011a05d --- /dev/null +++ b/kadai4/pei/Makefile @@ -0,0 +1,17 @@ +ROOT=github.com/gopherdojo/dojo6/kadai4/pei +BIN=fortune +MAIN=main.go +TEST=... + +.PHONY: build +build: ${MAIN} + go build -o ${BIN} ${GOPATH}/src/${ROOT}/$? + +.PHONY: test +test: + go test -v -cover ${ROOT}/${TEST} + +.PHONY: clean +clean: + rm ${BIN} + go clean From c5d214f26e36afa3a1e29bf299eb34f69f1579a6 Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Fri, 16 Aug 2019 02:47:29 +0900 Subject: [PATCH 3/6] Add pkg/fortune --- kadai4/pei/pkg/fortune/fortune.go | 66 ++++++++++++++++++++++++++ kadai4/pei/pkg/fortune/fortune_test.go | 36 ++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 kadai4/pei/pkg/fortune/fortune.go create mode 100644 kadai4/pei/pkg/fortune/fortune_test.go diff --git a/kadai4/pei/pkg/fortune/fortune.go b/kadai4/pei/pkg/fortune/fortune.go new file mode 100644 index 0000000..56cfea6 --- /dev/null +++ b/kadai4/pei/pkg/fortune/fortune.go @@ -0,0 +1,66 @@ +package fortune + +import ( + "fmt" + "net/http" + "math/rand" + "time" +) + +var fortuneList = []string{ + "大吉", + "中吉", + "吉", + "小吉", + "凶", +} + +// Clock +type Clock struct {} + +// Fortune +type Fortune struct { + clock Clock +} + +func init() { + rand.Seed(time.Now().UnixNano()) +} + +// GetCurrentTime return current time +func (c Clock) GetCurrentTime() time.Time { + return time.Now() +} + +// NewFortune return fortune instance +func NewFortune(c Clock) * Fortune { + return &Fortune{clock: c} +} + +func (f Fortune) drawingForNewYearDay() string { + return "大吉" +} + +func (f Fortune) defaultDrawing() string { + return fortuneList[rand.Intn(len(fortuneList))] +} + +func (f Fortune) isNewYearDay() bool { + c := f.clock.GetCurrentTime() + + return c.Month() == 1 && 1 <= c.Day() && c.Day() <= 3 +} + +// Drawing return drawing result +func (f Fortune) Drawing() string { + if f.isNewYearDay() { + return f.drawingForNewYearDay() + } + return f.defaultDrawing() +} + +// Handler +func (f Fortune) Handler(w http.ResponseWriter, r *http.Request) { + result := f.Drawing + fmt.Fprint(w, result) +} diff --git a/kadai4/pei/pkg/fortune/fortune_test.go b/kadai4/pei/pkg/fortune/fortune_test.go new file mode 100644 index 0000000..ca28532 --- /dev/null +++ b/kadai4/pei/pkg/fortune/fortune_test.go @@ -0,0 +1,36 @@ +package fortune + +import ( + "testing" + "time" +) + +type MockClock struct { + currentTime time.Time +} + +func (mc MockClock) GetCurrentTime() time.Time { + return mc.currentTime +} + +func TestFortune_Drawing(t *testing.T) { + cases := []struct { + clock time.Time + }{ + {time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)}, + {time.Date(2019, 1, 3, 23, 59, 59, 0, time.UTC)}, + } + + for _, c := range cases { + c := c + t.Run(c.clock, func(t *testing.T) { + t.Parallel() + + mc := &MockClock{c.clock} + f := fortune.NewFortune(mc) + if actual := f.Drawing(); actual != "大吉" { + t.Errorf("unexpected result: %s on %v", actual, mc.GetCurrentTime()) + } + }) + } +} From dbc2c4b2908a1e3d0fc95c8c51478e774a52f4b4 Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Fri, 16 Aug 2019 02:47:43 +0900 Subject: [PATCH 4/6] Add main --- kadai4/pei/main.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 kadai4/pei/main.go diff --git a/kadai4/pei/main.go b/kadai4/pei/main.go new file mode 100644 index 0000000..3b470d9 --- /dev/null +++ b/kadai4/pei/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "flag" + "fmt" + "net/http" + + "github.com/gopherdojo/dojo6/kadai4/pei/pkg/fortune" +) + +var port int + +func init() { + flag.IntVar(&port, "p", 8080, "server port") +} + +func main() { + flag.Parse() + f := fortune.NewFortune(fortune.Clock{}) + http.HandleFunc("/", f.Handler) + http.ListenAndServe(fmt.Sprintf(":%d", port), nil) +} From e8c83d72a1c70bb077434b44a094fe9ffcd1188c Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Fri, 16 Aug 2019 03:39:42 +0900 Subject: [PATCH 5/6] Confirm that TestFortune_Drawing can be executed --- kadai4/pei/main.go | 2 +- kadai4/pei/pkg/fortune/fortune.go | 27 +++++++++++++++++++++----- kadai4/pei/pkg/fortune/fortune_test.go | 19 ++++++++---------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/kadai4/pei/main.go b/kadai4/pei/main.go index 3b470d9..f12a73c 100644 --- a/kadai4/pei/main.go +++ b/kadai4/pei/main.go @@ -16,7 +16,7 @@ func init() { func main() { flag.Parse() - f := fortune.NewFortune(fortune.Clock{}) + f := fortune.NewFortune(fortune.DefaultClock{}) http.HandleFunc("/", f.Handler) http.ListenAndServe(fmt.Sprintf(":%d", port), nil) } diff --git a/kadai4/pei/pkg/fortune/fortune.go b/kadai4/pei/pkg/fortune/fortune.go index 56cfea6..3901354 100644 --- a/kadai4/pei/pkg/fortune/fortune.go +++ b/kadai4/pei/pkg/fortune/fortune.go @@ -1,9 +1,11 @@ package fortune import ( + "bytes" + "encoding/json" "fmt" - "net/http" "math/rand" + "net/http" "time" ) @@ -16,19 +18,29 @@ var fortuneList = []string{ } // Clock -type Clock struct {} +type Clock interface { + GetCurrentTime() time.Time +} + +// DefaultClock +type DefaultClock struct{} // Fortune type Fortune struct { clock Clock } +// DrawingResult +type DrawingResult struct { + Result string `json:"result"` +} + func init() { rand.Seed(time.Now().UnixNano()) } // GetCurrentTime return current time -func (c Clock) GetCurrentTime() time.Time { +func (d DefaultClock) GetCurrentTime() time.Time { return time.Now() } @@ -61,6 +73,11 @@ func (f Fortune) Drawing() string { // Handler func (f Fortune) Handler(w http.ResponseWriter, r *http.Request) { - result := f.Drawing - fmt.Fprint(w, result) + var buf bytes.Buffer + dr := DrawingResult{Result: f.Drawing()} + enc := json.NewEncoder(&buf) + if err := enc.Encode(dr); err != nil { + fmt.Errorf("error: %v", err) + } + fmt.Fprint(w, buf.String()) } diff --git a/kadai4/pei/pkg/fortune/fortune_test.go b/kadai4/pei/pkg/fortune/fortune_test.go index ca28532..8299f98 100644 --- a/kadai4/pei/pkg/fortune/fortune_test.go +++ b/kadai4/pei/pkg/fortune/fortune_test.go @@ -1,8 +1,10 @@ -package fortune +package fortune_test import ( "testing" "time" + + "github.com/gopherdojo/dojo6/kadai4/pei/pkg/fortune" ) type MockClock struct { @@ -22,15 +24,10 @@ func TestFortune_Drawing(t *testing.T) { } for _, c := range cases { - c := c - t.Run(c.clock, func(t *testing.T) { - t.Parallel() - - mc := &MockClock{c.clock} - f := fortune.NewFortune(mc) - if actual := f.Drawing(); actual != "大吉" { - t.Errorf("unexpected result: %s on %v", actual, mc.GetCurrentTime()) - } - }) + mc := &MockClock{c.clock} + f := fortune.NewFortune(mc) + if actual := f.Drawing(); actual != "大吉" { + t.Errorf("unexpected result: %s on %v", actual, mc.GetCurrentTime()) + } } } From a1097a290913bf07b320211b4fe6c354610d03f4 Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Fri, 16 Aug 2019 12:21:11 +0900 Subject: [PATCH 6/6] Update pkg/fortune --- kadai4/pei/pkg/fortune/fortune.go | 5 ++++ kadai4/pei/pkg/fortune/fortune_test.go | 40 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/kadai4/pei/pkg/fortune/fortune.go b/kadai4/pei/pkg/fortune/fortune.go index 3901354..bb3d927 100644 --- a/kadai4/pei/pkg/fortune/fortune.go +++ b/kadai4/pei/pkg/fortune/fortune.go @@ -71,6 +71,11 @@ func (f Fortune) Drawing() string { return f.defaultDrawing() } +// GetFortuneList return fortuneList +func (f Fortune) GetFortuneList() []string { + return fortuneList +} + // Handler func (f Fortune) Handler(w http.ResponseWriter, r *http.Request) { var buf bytes.Buffer diff --git a/kadai4/pei/pkg/fortune/fortune_test.go b/kadai4/pei/pkg/fortune/fortune_test.go index 8299f98..96362e6 100644 --- a/kadai4/pei/pkg/fortune/fortune_test.go +++ b/kadai4/pei/pkg/fortune/fortune_test.go @@ -1,12 +1,52 @@ package fortune_test import ( + "encoding/json" + "io/ioutil" + "net/http" + "net/http/httptest" "testing" "time" "github.com/gopherdojo/dojo6/kadai4/pei/pkg/fortune" ) +func TestFortune_Handler(t *testing.T) { + f := fortune.NewFortune(fortune.DefaultClock{}) + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + f.Handler(w, r) + + rw := w.Result() + defer rw.Body.Close() + if rw.StatusCode != http.StatusOK { + t.Error("unexpected status code") + } + + b, err := ioutil.ReadAll(rw.Body) + if err != nil { + t.Error("unexpected error") + } + + dr := &fortune.DrawingResult{} + if err := json.Unmarshal(b, &dr); err != nil { + t.Error("failed json unmarshal") + } + + fortuneList := f.GetFortuneList() + contain := false + for _, v := range fortuneList { + if v == dr.Result { + contain = true + break + } + } + + if !contain { + t.Errorf("unexpected response: %s", string(b)) + } +} + type MockClock struct { currentTime time.Time }