Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- queryparam now uses truncated ts
- Added test case for start,end date
- Parallelized tests
  • Loading branch information
saltgen committed Dec 18, 2024
1 parent 620f225 commit cb6be27
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
54 changes: 40 additions & 14 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,73 @@ 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!`,
},
}

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)
}
})
}
}
14 changes: 7 additions & 7 deletions internal/api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand Down

0 comments on commit cb6be27

Please sign in to comment.