Skip to content

Commit

Permalink
fix: Soritng of the executions latest first, move fdatefilter to sepa…
Browse files Browse the repository at this point in the history
…rate package. (#539)
  • Loading branch information
nicufk authored Nov 19, 2021
1 parent 264abfd commit e254119
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/stretchr/testify v1.7.0
github.com/valyala/fasthttp v1.31.0
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
go.mongodb.org/mongo-driver v1.5.4
go.mongodb.org/mongo-driver v1.7.4
go.uber.org/zap v1.17.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.21.2
Expand Down
15 changes: 4 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hC
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/aws/aws-sdk-go v1.34.28 h1:sscPpn/Ns3i0F4HPEWAVcwdIRaZZCuL7llJ2/60yPIk=
github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48=
github.com/bclicn/color v0.0.0-20180711051946-108f2023dc84 h1:cutFptzj+ospnc1PETUqcSVTH3VQ44Bi0rpt3nE9gvo=
github.com/bclicn/color v0.0.0-20180711051946-108f2023dc84/go.mod h1:Va9ap1qxjAWkIVaW1E9rH0aNgE8SDI5A4n8Ds8P0fAA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -175,7 +173,6 @@ github.com/go-openapi/spec v0.19.3/go.mod h1:FpwSN1ksY1eteniUU7X0N/BgJ7a4WvBFVA8
github.com/go-openapi/spec v0.19.5/go.mod h1:Hm2Jr4jv8G1ciIAo+frC/Ft+rR2kQDh8JHKHb3gWUSk=
github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
Expand Down Expand Up @@ -333,10 +330,6 @@ github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
Expand All @@ -358,9 +351,9 @@ github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.9.5/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s=
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/cpuid v1.2.3/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid v1.3.1 h1:5JNjFYYQrZeKRJ0734q51WCEEn2huer72Dc7K+R/b6s=
github.com/klauspost/cpuid v1.3.1/go.mod h1:bYW4mA6ZgKPob1/Dlai2LviZJO7KGI3uoWLd42rAQw4=
Expand Down Expand Up @@ -584,8 +577,8 @@ go.etcd.io/etcd v0.5.0-alpha.5.0.20200910180754-dd1b699fc489/go.mod h1:yVHk9ub3C
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ=
go.mongodb.org/mongo-driver v1.5.4 h1:NPIBF/lxEcKNfWwoCJRX8+dMVwecWf9q3qUJkuh75oM=
go.mongodb.org/mongo-driver v1.5.4/go.mod h1:gRXCHX4Jo7J0IJ1oDQyUxF7jfy19UfxniMS4xxMmUqw=
go.mongodb.org/mongo-driver v1.7.4 h1:sllcioag8Mec0LYkftYWq+cKNPIR4Kqq3iv9ZXY0g/E=
go.mongodb.org/mongo-driver v1.7.4/go.mod h1:NqaYOwnXWr5Pm7AOpO5QFxKJ503nbMse/R79oO62zWg=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
Expand Down
3 changes: 2 additions & 1 deletion internal/app/api/v1/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kubeshop/testkube/internal/pkg/api"
"github.com/kubeshop/testkube/internal/pkg/api/datefilter"
"github.com/kubeshop/testkube/internal/pkg/api/repository/result"
)

Expand Down Expand Up @@ -342,7 +343,7 @@ func getFilterFromRequest(c *fiber.Ctx) result.Filter {
filter = filter.WithStatus(testkube.ExecutionStatus(status))
}

dFilter := NewDateFilter(c.Query("startDate", ""), c.Query("endDate", ""))
dFilter := datefilter.NewDateFilter(c.Query("startDate", ""), c.Query("endDate", ""))
if dFilter.IsStartValid {
filter = filter.WithStartDate(dFilter.Start)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package datefilter

import "time"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package datefilter

import (
"testing"
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/api/repository/result/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func composeQueryAndOpts(filter Filter) (bson.M, *options.FindOptions) {

opts.SetSkip(int64(filter.Page() * filter.PageSize()))
opts.SetLimit(int64(filter.PageSize()))
opts.SetSort(bson.D{{"starttime", -1}})

return query, opts
}
37 changes: 25 additions & 12 deletions internal/pkg/api/repository/result/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/kubeshop/testkube/internal/pkg/api/datefilter"
"github.com/kubeshop/testkube/internal/pkg/api/repository/storage"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/rand"
Expand Down Expand Up @@ -101,16 +102,19 @@ func TestFilters(t *testing.T) {
assert.Equal(int32(3), totals.Pending)
})

dateFilter := datefilter.NewDateFilter(oneDayAgo.Format(datefilter.DateFormatISO8601), "")
assert.True(dateFilter.IsStartValid)

t.Run("filter with startDate should return only executions after that day", func(t *testing.T) {

executions, err := repository.GetExecutions(context.Background(), NewExecutionsFilter().WithStartDate(oneDayAgo))
executions, err := repository.GetExecutions(context.Background(), NewExecutionsFilter().WithStartDate(dateFilter.Start))
assert.NoError(err)
assert.Len(executions, 14)
assert.True(executions[0].StartTime.After(oneDayAgo) || executions[0].StartTime.Equal(oneDayAgo))
assert.True(executions[0].StartTime.After(dateFilter.Start) || executions[0].StartTime.Equal(dateFilter.Start))
})

t.Run("getting totals with filter by date start date should return only the results after this date", func(t *testing.T) {
totals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithStartDate(oneDayAgo))
totals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithStartDate(dateFilter.Start))

assert.NoError(err)
assert.Equal(int32(14), totals.Results)
Expand All @@ -120,23 +124,26 @@ func TestFilters(t *testing.T) {
assert.Equal(int32(2), totals.Pending)
})

dateFilter = datefilter.NewDateFilter("", oneDayAgo.Format(datefilter.DateFormatISO8601))
assert.True(dateFilter.IsEndValid)

t.Run("filter with endDate should return only executions before that day", func(t *testing.T) {

executions, err := repository.GetExecutions(context.Background(), NewExecutionsFilter().WithEndDate(oneDayAgo))
executions, err := repository.GetExecutions(context.Background(), NewExecutionsFilter().WithEndDate(dateFilter.End))
assert.NoError(err)
assert.Len(executions, 14)
assert.True(executions[0].StartTime.Before(oneDayAgo) || executions[0].StartTime.Equal(oneDayAgo))
assert.Len(executions, 7)
assert.True(executions[0].StartTime.Before(dateFilter.End) || executions[0].StartTime.Equal(dateFilter.End))
})

t.Run("getting totals with filter by date start date should return only the results before this date", func(t *testing.T) {
totals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithEndDate(oneDayAgo))
totals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithEndDate(dateFilter.End))

assert.NoError(err)
assert.Equal(int32(14), totals.Results)
assert.Equal(int32(8), totals.Failed)
assert.Equal(int32(2), totals.Passed)
assert.Equal(int32(2), totals.Queued)
assert.Equal(int32(2), totals.Pending)
assert.Equal(int32(7), totals.Results)
assert.Equal(int32(4), totals.Failed)
assert.Equal(int32(1), totals.Passed)
assert.Equal(int32(1), totals.Queued)
assert.Equal(int32(1), totals.Pending)
})

t.Run("filter with script name that doesn't exist should return 0 results", func(t *testing.T) {
Expand Down Expand Up @@ -207,6 +214,12 @@ func TestFilters(t *testing.T) {
assert.Equal(int32(1), totals.Pending)
})

t.Run("scripts should be sorted with most recent first", func(t *testing.T) {
executions, err := repository.GetExecutions(context.Background(), NewExecutionsFilter())
assert.NoError(err)
assert.NotEmpty(executions)
assert.True(executions[0].StartTime.After(executions[len(executions)-1].StartTime), "executions are not sorted with the most recent first")
})
}

func getRepository() (*MongoRepository, error) {
Expand Down

0 comments on commit e254119

Please sign in to comment.