Skip to content

kadai4 yoheimiyamoto #59

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions kadai4/yoheimiyamoto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 概要
* JSON形式でおみくじの結果を返す
* 正月(1/1-1/3)だけ大吉にする
* ハンドラのテストを書いてみる
12 changes: 12 additions & 0 deletions kadai4/yoheimiyamoto/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"net/http"

"github.com/YoheiMiyamoto/dojo4/kadai4/yoheimiyamoto/omikuji"
)

func main() {
http.HandleFunc("/", omikuji.Handler)
http.ListenAndServe(":8080", nil)
}
46 changes: 46 additions & 0 deletions kadai4/yoheimiyamoto/omikuji/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package omikuji

import (
"math/rand"
"time"
)

type client struct {
now now
}

type now func() time.Time

func NewClient() *client {
return &client{
now(func() time.Time {
return time.Now()
}),
}
}

func (c *client) play() *Result {
t := getType(c.now())
return &Result{t}
}

// 吉凶を取得(大吉,中吉,小吉,凶,大凶)
func getType(now time.Time) string {
// 三が日は大吉にする
if now.Month() == 1 {
if now.Day() == 1 || now.Day() == 2 || now.Day() == 3 {
return "大吉"
}
}

// 三が日以外はランダム
types := []string{
"大吉",
"中吉",
"小吉",
"凶",
"大凶",
}
rand.Seed(time.Now().UnixNano())
return types[rand.Intn(len(types))]
}
32 changes: 32 additions & 0 deletions kadai4/yoheimiyamoto/omikuji/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package omikuji

import (
"testing"
"time"
)

func TestGetType(t *testing.T) {
t.Run("", func(t *testing.T) {
testGetType(t, time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local), "大吉")
})
}

// TestGetTypeのテストヘルパー
func testGetType(t *testing.T, in time.Time, expected string) {
t.Helper()
r := getType(in)
if r != expected {
t.Errorf("expected: %s, actual: %s", expected, r)
}
}

func TestPlay(t *testing.T) {
c := &client{
now(func() time.Time { return time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local) }),
}
actual := *c.play()
expected := Result{"大吉"}
if actual != expected {
t.Fatalf("actual: %v, expected: %v", actual, expected)
}
}
17 changes: 17 additions & 0 deletions kadai4/yoheimiyamoto/omikuji/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package omikuji

import (
"encoding/json"
"log"
"net/http"
)

func Handler(w http.ResponseWriter, r *http.Request) {
c := NewClient()
result := c.play()
err := json.NewEncoder(w).Encode(result)
if err != nil {
log.Fatal(err)
return
}
}
35 changes: 35 additions & 0 deletions kadai4/yoheimiyamoto/omikuji/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package omikuji

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)

func TestHandler(t *testing.T) {
// どうにかして、現在時刻を三が日に変更する必要がある

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.Fatal("unexpected error")
}
var actual Result
err = json.Unmarshal(b, &actual)
if err != nil {
t.Fatal(err)
}
expected := Result{"大吉"}
if actual != expected {
t.Fatalf("actual: %v, expected: %v", actual, expected)
}
}
7 changes: 7 additions & 0 deletions kadai4/yoheimiyamoto/omikuji/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package omikuji

// Result ...
type Result struct {
Type string `json:"type"` // 吉凶(大吉,中吉,小吉,凶,大凶)
// Message string `json:"message`
}