From 35615e355c91324165a42fd840450ab4679c5618 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 20:52:56 +0900 Subject: [PATCH] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/handler/get_holidays_test.go | 86 ++++++++++++++++++++++++++++ src/api/handler/is_holiday.go | 2 + src/api/handler/is_holiday_test.go | 40 +++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 src/api/handler/get_holidays_test.go create mode 100644 src/api/handler/is_holiday_test.go diff --git a/src/api/handler/get_holidays_test.go b/src/api/handler/get_holidays_test.go new file mode 100644 index 0000000..053e3c2 --- /dev/null +++ b/src/api/handler/get_holidays_test.go @@ -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) + } + }) + } +} diff --git a/src/api/handler/is_holiday.go b/src/api/handler/is_holiday.go index 32b7c90..4b8d109 100644 --- a/src/api/handler/is_holiday.go +++ b/src/api/handler/is_holiday.go @@ -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} } 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) diff --git a/src/api/handler/is_holiday_test.go b/src/api/handler/is_holiday_test.go new file mode 100644 index 0000000..0518260 --- /dev/null +++ b/src/api/handler/is_holiday_test.go @@ -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) + }) + } +}