Skip to content

Commit

Permalink
Ginへの移行
Browse files Browse the repository at this point in the history
  • Loading branch information
kynmh69 committed Sep 29, 2024
1 parent c007f85 commit 35615e3
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/api/handler/get_holidays_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package handler

import (
"github.com/gin-gonic/gin"
"github.com/kynmh69/go-ja-holidays/util"
"net/http/httptest"
"os"
"testing"
"time"
)

func TestGetHolidays(t *testing.T) {
_ = os.Setenv("DATABASE", "unittest")
_ = os.Setenv("PSQL_HOSTNAME", "localhost")
util.SetUp()
r := gin.Default()
r.GET("/holidays", GetHolidays)
tests := []struct {
name string
target string
}{
{
name: "TestGetHolidays",
target: "/holidays",
},
{
name: "TestGetHolidaysWithQuery",
target: "/holidays?start_day=2021-01-01&end_day=2021-12-31",
},
{
name: "TestGetHolidaysWithQueryStartDay",
target: "/holidays?start_day=2021-01-01",
},
{
name: "TestGetHolidaysWithQueryEndDay",
target: "/holidays?end_day=2021-12-31",
},
{
name: "TestGetHolidaysWithQueryInvalid",
target: "/holidays?start_day=2021-01",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", tt.target, nil)
context := gin.CreateTestContextOnly(w, r)
context.Request = req
GetHolidays(context)
})
}
}

func TestHolidaysRequest_String(t *testing.T) {
util.SetUp()
now := time.Now()
type fields struct {
StartDay time.Time
EndDay time.Time
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "TestHolidaysRequest_String",
fields: fields{
StartDay: now,
EndDay: now,
},
want: "StartDay: \"" + now.String() + "\", EndDay: \"" + now.String() + "\"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
receiver := HolidaysRequest{
StartDay: tt.fields.StartDay,
EndDay: tt.fields.EndDay,
}
if got := receiver.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions src/api/handler/is_holiday.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ func IsHoliday(c *gin.Context) {

var isHoliday model.IsHoliday
if ok {
// If the holiday data exists, return it.
isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: holiday}

Check warning on line 53 in src/api/handler/is_holiday.go

View check run for this annotation

Codecov / codecov/patch

src/api/handler/is_holiday.go#L53

Added line #L53 was not covered by tests
} else {
// If the holiday data does not exist, return the date.
isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: model.HolidayData{Date: request.Date}}
}
logger.Debug(isHoliday)
Expand Down
40 changes: 40 additions & 0 deletions src/api/handler/is_holiday_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package handler

import (
"github.com/gin-gonic/gin"
"github.com/kynmh69/go-ja-holidays/util"
"net/http/httptest"
"os"
"testing"
)

func TestIsHoliday(t *testing.T) {
_ = os.Setenv("DATABASE", "unittest")
_ = os.Setenv("PSQL_HOSTNAME", "localhost")
util.SetUp()
r := gin.Default()
r.GET("/holidays/:date", IsHoliday)

tests := []struct {
name string
target string
}{
{
name: "TestIsHoliday",
target: "/holidays/2021-01-01",
},
{
name: "TestIsHolidayInvalid",
target: "/holidays/2021-01",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", tt.target, nil)
context := gin.CreateTestContextOnly(w, r)
context.Request = req
IsHoliday(context)
})
}
}

0 comments on commit 35615e3

Please sign in to comment.