Skip to content

Commit

Permalink
Handle offset and limit for query users (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferhatelmas authored Jul 20, 2020
1 parent b26b5b3 commit 1ee2025
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
11 changes: 8 additions & 3 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stream_chat // nolint: golint

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -66,18 +67,22 @@ type queryUsersResponse struct {
}

// QueryUsers returns list of users that match QueryOption.
// If any number of SortOption are set, result will be sorted by field and direction in oder of sort options.
func (c *Client) QueryUsers(q *QueryOption, sort ...*SortOption) ([]*User, error) {
// If any number of SortOption are set, result will be sorted by field and direction in the order of sort options.
func (c *Client) QueryUsers(q *QueryOption, sorters ...*SortOption) ([]*User, error) {
qp := queryRequest{
FilterConditions: q.Filter,
Sort: sort,
Limit: q.Limit,
Offset: q.Offset,
Sort: sorters,
}

data, err := json.Marshal(&qp)
if err != nil {
return nil, err
}

fmt.Println(string(data))

values := make(url.Values)
values.Set("payload", string(data))

Expand Down
59 changes: 49 additions & 10 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stream_chat // nolint: golint

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -10,19 +11,57 @@ import (
func TestClient_QueryUsers(t *testing.T) {
c := initClient(t)

user := randomUser()
const n = 4
ids := make([]string, n)
defer func() {
for _, id := range ids {
if id != "" {
_ = c.DeleteUser(id, nil)
}
}
}()

for i := n - 1; i > -1; i-- {
u := &User{ID: randomString(30), ExtraData: map[string]interface{}{"order": n - i - 1}}
_, err := c.UpdateUser(u)
require.NoError(t, err)
ids[i] = u.ID
time.Sleep(200 * time.Millisecond)
}

users, err := c.QueryUsers(&QueryOption{Filter: map[string]interface{}{
"id": map[string]interface{}{
"$eq": user.ID,
}},
})
t.Parallel()
t.Run("Query all", func(tt *testing.T) {
results, err := c.QueryUsers(&QueryOption{Filter: map[string]interface{}{
"id": map[string]interface{}{
"$in": ids,
}},
})

require.NoError(t, err, "query users error")
require.NoError(tt, err)
require.Len(tt, results, len(ids))
})

if assert.NotEmpty(t, users, "query users exists") {
assert.Equal(t, user.ID, users[0].ID, "received user ID")
}
t.Run("Query with offset/limit", func(tt *testing.T) {
offset := 1

results, err := c.QueryUsers(
&QueryOption{
Filter: map[string]interface{}{
"id": map[string]interface{}{
"$in": ids,
},
},
Offset: offset,
Limit: 2,
},
)

require.NoError(tt, err)
require.Len(tt, results, 2)

require.Equal(tt, results[0].ID, ids[offset])
require.Equal(tt, results[1].ID, ids[offset+1])
})
}

func TestClient_QueryChannels(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (

//nolint: gochecknoinits
func init() {
rand.Seed(time.Now().Unix())
rand.Seed(time.Now().UnixNano())

serverUser = &User{ID: randomString(10), Name: "Gandalf the Grey", ExtraData: map[string]interface{}{"race": "Istari"}}

Expand Down

0 comments on commit 1ee2025

Please sign in to comment.