diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 5661ad1..63aa4c0 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -5,29 +5,42 @@ import ( "net/http/httptest" "reflect" "testing" + "time" + "github.com/labstack/echo/v4" ) func TestMapQueryParameters(t *testing.T) { - e := echo.New() - req := httptest.NewRequest(http.MethodGet, "/", nil) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - + // setup type tests struct { name string qinputs map[string]string - qoutputs map[string][]string + qoutputs map[string]interface{} errmsg string } - var all_tests = []tests{ + now := time.Now().UTC().Truncate(time.Second) + firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC).Truncate(time.Second) + + startTime := time.Date(2023, 3, 23, 0, 0, 0, 0, time.UTC).Truncate(time.Second) + endTime := time.Date(2023, 3, 24, 0, 0, 0, 0, time.UTC).Truncate(time.Second) + + all_tests := []tests{ + { + name: "When start date and end date are not provided", + qinputs: map[string]string{"start_date": "", "end_date": ""}, + qoutputs: map[string]interface{}{ + "recommendation_sets.monitoring_end_time <= ?": now, + "recommendation_sets.monitoring_end_time >= ?": firstOfMonth, + }, + errmsg: `The startTime should be 1st of current month. The endTime should the current time.`, + }, { name: "When start date and end date are provided", - qinputs: map[string]string{"start_date": "2023-03-23", "end_date": "2023-03-24"}, - qoutputs: map[string][]string{ - "DATE(recommendation_sets.monitoring_end_time) <= ?": {"2023-03-24"}, - "DATE(recommendation_sets.monitoring_end_time) >= ?": {"2023-03-23"}, + qinputs: map[string]string{"start_date": startTime.Format("2006-01-02"), "end_date": endTime.Format("2006-01-02")}, + qoutputs: map[string]interface{}{ + "recommendation_sets.monitoring_end_time <= ?": endTime, + "recommendation_sets.monitoring_end_time >= ?": startTime, }, errmsg: `The recommendation_sets.monitoring_end_time should be less than or equal to end date! The recommendation_sets.monitoring_end_time should be greater than or equal to start date!`, @@ -35,17 +48,30 @@ func TestMapQueryParameters(t *testing.T) { } for _, tt := range all_tests { + tt := tt t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + // Create a fresh request and recorder for each parallel test + e := echo.New() + req := httptest.NewRequest(http.MethodGet, "/", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + for k, v := range tt.qinputs { c.QueryParams().Add(k, v) } + defer func() { + // Cleanup query params regardless of test result + for k := range c.QueryParams() { + delete(c.QueryParams(), k) + } + }() result, _ := MapQueryParameters(c) + t.Log(result, tt.qoutputs) if reflect.DeepEqual(result, tt.qoutputs) != true { t.Errorf("%s", tt.errmsg) } - for k := range c.QueryParams() { - delete(c.QueryParams(), k) - } }) } } diff --git a/internal/api/utils.go b/internal/api/utils.go index eb79d19..d958f8e 100644 --- a/internal/api/utils.go +++ b/internal/api/utils.go @@ -67,7 +67,7 @@ func CollectionResponse(collection []interface{}, req *http.Request, count, limi func MapQueryParameters(c echo.Context) (map[string]interface{}, error) { log := logging.GetLogger() queryParams := make(map[string]interface{}) - var startDate, endDate time.Time + var startTimestamp, endTimestamp time.Time var clusters, projects, workloadNames, workloadTypes, containers []string now := time.Now().UTC() @@ -76,29 +76,29 @@ func MapQueryParameters(c echo.Context) (map[string]interface{}, error) { startDateStr := c.QueryParam("start_date") if startDateStr == "" { - startDate = firstOfMonth + startTimestamp = firstOfMonth } else { var err error - startDate, err = time.Parse(timeLayout, startDateStr) + startTimestamp, err = time.Parse(timeLayout, startDateStr) if err != nil { log.Error("error parsing start_date:", err) return queryParams, err } } - queryParams["recommendation_sets.monitoring_end_time >= ?"] = startDate + queryParams["recommendation_sets.monitoring_end_time >= ?"] = startTimestamp.Truncate(time.Second) endDateStr := c.QueryParam("end_date") if endDateStr == "" { - endDate = now + endTimestamp = now } else { var err error - endDate, err = time.Parse(timeLayout, endDateStr) + endTimestamp, err = time.Parse(timeLayout, endDateStr) if err != nil { log.Error("error parsing end_date:", err) return queryParams, err } } - queryParams["recommendation_sets.monitoring_end_time <= ?"] = endDate + queryParams["recommendation_sets.monitoring_end_time <= ?"] = endTimestamp.Truncate(time.Second) clusters = c.QueryParams()["cluster"] if len(clusters) > 0 {