refactor to use WithOrderByCreateTime(...) (#1058)

This commit is contained in:
Jim authored and GitHub committed 2021-04-02 19:13:48 -04:00
1 parent f21996d8ad
commit c281e808d6
6 files changed
+32 -22

No files matched your search

+13 -11
View File
@@ -1,6 +1,7 @@
package session
import (
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/db/timestamp"
)
@@ -18,14 +19,14 @@ type Option func(*options)
// options = how options are represented
type options struct {
withLimit int
withOrder string
withScopeIds []string
withUserId string
withExpirationTime *timestamp.Timestamp
withTestTofu []byte
withListingConvert bool
withSessionIds []string
withLimit int
withOrderByCreateTime db.OrderBy
withScopeIds []string
withUserId string
withExpirationTime *timestamp.Timestamp
withTestTofu []byte
withListingConvert bool
withSessionIds []string
}
func getDefaultOptions() options {
@@ -41,10 +42,11 @@ func WithLimit(limit int) Option {
}
}
// WithOrder allows specifying an order for returned values
func WithOrder(order string) Option {
// WithOrderByCreateTime provides an option to specify ordering by the
// CreateTime field.
func WithOrderByCreateTime(orderBy db.OrderBy) Option {
return func(o *options) {
o.withOrder = order
o.withOrderByCreateTime = orderBy
}
}
+4 -3
View File
@@ -5,6 +5,7 @@ import (
"time"
"github.com/golang/protobuf/ptypes"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/db/timestamp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -38,11 +39,11 @@ func Test_GetOpts(t *testing.T) {
testOpts.withScopeIds = []string{"o_1234"}
assert.Equal(opts, testOpts)
})
t.Run("WithOrder", func(t *testing.T) {
t.Run("WithOrderByCreateTime", func(t *testing.T) {
assert := assert.New(t)
opts := getOpts(WithOrder("create_time asc"))
opts := getOpts(WithOrderByCreateTime(db.AscendingOrderBy))
testOpts := getDefaultOptions()
testOpts.withOrder = "create_time asc"
testOpts.withOrderByCreateTime = db.AscendingOrderBy
assert.Equal(opts, testOpts)
})
t.Run("WithUserId", func(t *testing.T) {
+5 -2
View File
@@ -61,8 +61,11 @@ func (r *Repository) list(ctx context.Context, resources interface{}, where stri
limit = opts.withLimit
}
dbOpts = append(dbOpts, db.WithLimit(limit))
if opts.withOrder != "" {
dbOpts = append(dbOpts, db.WithOrder(opts.withOrder))
switch opts.withOrderByCreateTime {
case db.AscendingOrderBy:
dbOpts = append(dbOpts, db.WithOrder("create_time asc"))
case db.DescendingOrderBy:
dbOpts = append(dbOpts, db.WithOrder("create_time"))
}
if err := r.reader.SearchWhere(ctx, resources, where, args, dbOpts...); err != nil {
return errors.Wrap(err, op)
@@ -115,7 +115,7 @@ func TestRepository_ListConnection(t *testing.T) {
2222,
)
}
got, err := repo.ListConnections(context.Background(), session.PublicId, WithOrder("create_time asc"))
got, err := repo.ListConnections(context.Background(), session.PublicId, WithOrderByCreateTime(db.AscendingOrderBy))
require.NoError(err)
assert.Equal(wantCnt, len(got))
+7 -3
View File
@@ -210,8 +210,12 @@ func (r *Repository) ListSessions(ctx context.Context, opt ...Option) ([]*Sessio
limit = fmt.Sprintf("limit %d", opts.withLimit)
}
if opts.withOrder != "" {
opts.withOrder = fmt.Sprintf("order by %s", opts.withOrder)
var withOrder string
switch opts.withOrderByCreateTime {
case db.AscendingOrderBy:
withOrder = "order by create_time asc"
case db.DescendingOrderBy:
withOrder = "order by create_time"
}
var whereClause string
@@ -219,7 +223,7 @@ func (r *Repository) ListSessions(ctx context.Context, opt ...Option) ([]*Sessio
whereClause = " and " + strings.Join(where, " and ")
}
q := sessionList
query := fmt.Sprintf(q, limit, whereClause, opts.withOrder)
query := fmt.Sprintf(q, limit, whereClause, withOrder)
rows, err := r.reader.Query(ctx, query, args)
if err != nil {
+2 -2
View File
@@ -124,7 +124,7 @@ func TestRepository_ListSession(t *testing.T) {
for i := 0; i < wantCnt; i++ {
_ = TestSession(t, conn, wrapper, composedOf)
}
got, err := repo.ListSessions(context.Background(), WithOrder("create_time asc"))
got, err := repo.ListSessions(context.Background(), WithOrderByCreateTime(db.AscendingOrderBy))
require.NoError(err)
assert.Equal(wantCnt, len(got))
@@ -183,7 +183,7 @@ func TestRepository_ListSession(t *testing.T) {
}
assert.Equal(10, len(testSessions))
withIds := []string{testSessions[0].PublicId, testSessions[1].PublicId}
got, err := repo.ListSessions(context.Background(), WithSessionIds(withIds...), WithOrder("create_time asc"))
got, err := repo.ListSessions(context.Background(), WithSessionIds(withIds...), WithOrderByCreateTime(db.AscendingOrderBy))
require.NoError(err)
assert.Equal(2, len(got))
assert.Equal(StatusActive, got[0].States[0].Status)