Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kadai4 imura81gt #51

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions kadai4/imura81gt/fortune/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
おみくじAPIを作ってみよう
========================================

- [x] JSON形式でおみくじの結果を返す
- [x] 正月(1/1-1/3)だけ大吉にする
- [ ] ハンドラのテストを書いてみる



構想
------------------------------------------

- HTTPサーバーつくる
- おみくじ Middleware
- 特定日付大吉 Middleware
- handler test
28 changes: 28 additions & 0 deletions kadai4/imura81gt/fortune/cmd/fortune/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
44 changes: 44 additions & 0 deletions kadai4/imura81gt/fortune/cmd/fortune/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
50 changes: 50 additions & 0 deletions kadai4/imura81gt/fortune/fortune.go
Original file line number Diff line number Diff line change
@@ -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]
}
56 changes: 56 additions & 0 deletions kadai4/imura81gt/fortune/fortune_test.go
Original file line number Diff line number Diff line change
@@ -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
})
}