Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MF-1357 - Add new endpoint for searching things #1383

Merged
merged 13 commits into from
Mar 11, 2021
25 changes: 8 additions & 17 deletions pkg/sdk/go/channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package sdk_test
import (
"fmt"
"net/http"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -16,7 +15,7 @@ import (
)

var (
channel = sdk.Channel{ID: "1", Name: "test"}
channel = sdk.Channel{ID: "001", Name: "test"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use UUID?

emptyChannel = sdk.Channel{}
)

Expand Down Expand Up @@ -99,8 +98,8 @@ func TestCreateChannels(t *testing.T) {
mainfluxSDK := sdk.NewSDK(sdkConf)

channels := []sdk.Channel{
sdk.Channel{ID: "1", Name: "1"},
sdk.Channel{ID: "2", Name: "2"},
sdk.Channel{ID: "001", Name: "1"},
sdk.Channel{ID: "002", Name: "2"},
}

cases := []struct {
Expand Down Expand Up @@ -221,7 +220,7 @@ func TestChannels(t *testing.T) {
var channels []sdk.Channel
mainfluxSDK := sdk.NewSDK(sdkConf)
for i := 1; i < 101; i++ {
ch := sdk.Channel{ID: strconv.Itoa(i), Name: "test"}
ch := sdk.Channel{ID: fmt.Sprintf("%03d", i), Name: "test"}
mainfluxSDK.CreateChannel(ch, token)
channels = append(channels, ch)
}
Expand Down Expand Up @@ -260,12 +259,12 @@ func TestChannels(t *testing.T) {
response: nil,
},
{
desc: "get a list of channels with zero limit",
desc: "get a list of channels without limit, default 10",
token: token,
offset: 0,
limit: 0,
err: createError(sdk.ErrFailedFetch, http.StatusBadRequest),
response: nil,
err: nil,
response: channels[0:10],
},
{
desc: "get a list of channels with limit greater than max",
Expand All @@ -283,14 +282,6 @@ func TestChannels(t *testing.T) {
err: nil,
response: []sdk.Channel{},
},
{
desc: "get a list of channels with invalid args (zero limit) and invalid token",
token: wrongValue,
offset: 0,
limit: 0,
err: createError(sdk.ErrFailedFetch, http.StatusBadRequest),
response: nil,
},
}
for _, tc := range cases {
page, err := mainfluxSDK.Channels(tc.token, tc.offset, tc.limit, tc.name)
Expand Down Expand Up @@ -323,7 +314,7 @@ func TestChannelsByThing(t *testing.T) {
var channels []sdk.Channel
for i := 1; i < n+1; i++ {
ch := sdk.Channel{
ID: strconv.Itoa(i),
ID: fmt.Sprintf("%03d", i),
Name: "test",
}
cid, err := mainfluxSDK.CreateChannel(ch, token)
Expand Down
27 changes: 9 additions & 18 deletions pkg/sdk/go/things_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"testing"

sdk "github.com/mainflux/mainflux/pkg/sdk/go"
Expand Down Expand Up @@ -36,7 +35,7 @@ const (
var (
metadata = map[string]interface{}{"meta": "data"}
metadata2 = map[string]interface{}{"meta": "data2"}
thing = sdk.Thing{ID: "1", Name: "test_device", Metadata: metadata}
thing = sdk.Thing{ID: "001", Name: "test_device", Metadata: metadata}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to use UUID

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point is that here we mock id so we can test "order by id" functionalities.
That is the reason why I have change mock IDs to use three chars instead one, because ordering with just one char, like it is now, is 1, 10, 100, 2, 20...
With this change order would be 001, 002, 003... and you can write tests that check order by id functionality.

emptyThing = sdk.Thing{}
)

Expand Down Expand Up @@ -86,14 +85,14 @@ func TestCreateThing(t *testing.T) {
thing: thing,
token: token,
err: nil,
location: "1",
location: "001",
},
{
desc: "create new empty thing",
thing: emptyThing,
token: token,
err: nil,
location: "2",
location: "002",
},
{
desc: "create new thing with empty token",
Expand Down Expand Up @@ -136,8 +135,8 @@ func TestCreateThings(t *testing.T) {
mainfluxSDK := sdk.NewSDK(sdkConf)

things := []sdk.Thing{
sdk.Thing{ID: "1", Name: "1", Key: "1"},
sdk.Thing{ID: "2", Name: "2", Key: "2"},
sdk.Thing{ID: "001", Name: "1", Key: "1"},
sdk.Thing{ID: "002", Name: "2", Key: "2"},
}

cases := []struct {
Expand Down Expand Up @@ -262,7 +261,7 @@ func TestThings(t *testing.T) {
mainfluxSDK := sdk.NewSDK(sdkConf)
for i := 1; i < 101; i++ {

th := sdk.Thing{ID: strconv.Itoa(i), Name: "test_device", Metadata: metadata}
th := sdk.Thing{ID: fmt.Sprintf("%03d", i), Name: "test_device", Metadata: metadata}
mainfluxSDK.CreateThing(th, token)
th.Key = fmt.Sprintf("%s%012d", keyPrefix, 2*i)
things = append(things, th)
Expand Down Expand Up @@ -306,8 +305,8 @@ func TestThings(t *testing.T) {
token: token,
offset: 0,
limit: 0,
err: createError(sdk.ErrFailedFetch, http.StatusBadRequest),
response: nil,
err: nil,
response: things[0:10],
},
{
desc: "get a list of things with limit greater than max",
Expand All @@ -325,14 +324,6 @@ func TestThings(t *testing.T) {
err: nil,
response: []sdk.Thing{},
},
{
desc: "get a list of things with invalid args (zero limit) and invalid token",
token: wrongValue,
offset: 0,
limit: 0,
err: createError(sdk.ErrFailedFetch, http.StatusBadRequest),
response: nil,
},
}
for _, tc := range cases {
page, err := mainfluxSDK.Things(tc.token, tc.offset, tc.limit, tc.name)
Expand Down Expand Up @@ -366,7 +357,7 @@ func TestThingsByChannel(t *testing.T) {
var things []sdk.Thing
for i := 1; i < n+1; i++ {
th := sdk.Thing{
ID: strconv.Itoa(i),
ID: fmt.Sprintf("%03d", i),
Name: "test_device",
Metadata: metadata,
Key: fmt.Sprintf("%s%012d", keyPrefix, 2*i+1),
Expand Down
Loading