mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-24 13:48:49 +08:00

* Update WS tests Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Use require in all writer tests Refactor code. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Ignore Mainflux generated pb.go files Ignore *.pb.go files generated by Mainflux, but don't ignore vendored generated code. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Return an exported ErrNotFound instead of the unexported one Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update mocks to match the actual behaviour Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update mocks error message Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Add auth service unavailable error test Since this error is caused by gRPC server returning codes.Internal, this behaviour is simulated using specific token. When that token is passed as an auth header, the mock gRPC client returns aforementioned error. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Use require package for postgres tests Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove redundant error checks in tests Refactor tests. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Rename error flag token Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
167 lines
4.6 KiB
Go
167 lines
4.6 KiB
Go
package postgres_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mainflux/mainflux/things"
|
|
"github.com/mainflux/mainflux/things/postgres"
|
|
"github.com/mainflux/mainflux/things/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestThingSave(t *testing.T) {
|
|
email := "thing-save@example.com"
|
|
thingRepo := postgres.NewThingRepository(db, testLog)
|
|
|
|
thing := things.Thing{
|
|
Owner: email,
|
|
Key: uuid.New().ID(),
|
|
}
|
|
|
|
_, err := thingRepo.Save(thing)
|
|
assert.Nil(t, err, fmt.Sprintf("create new thing: expected no error got %s\n", err))
|
|
}
|
|
|
|
func TestThingUpdate(t *testing.T) {
|
|
email := "thing-update@example.com"
|
|
thingRepo := postgres.NewThingRepository(db, testLog)
|
|
|
|
thing := things.Thing{
|
|
Owner: email,
|
|
Key: uuid.New().ID(),
|
|
}
|
|
|
|
id, _ := thingRepo.Save(thing)
|
|
thing.ID = id
|
|
|
|
cases := map[string]struct {
|
|
thing things.Thing
|
|
err error
|
|
}{
|
|
"update existing thing": {thing: thing, err: nil},
|
|
"update non-existing thing with existing user": {thing: things.Thing{ID: wrongID, Owner: email}, err: things.ErrNotFound},
|
|
"update existing thing ID with non-existing user": {thing: things.Thing{ID: id, Owner: wrongValue}, err: things.ErrNotFound},
|
|
"update non-existing thing with non-existing user": {thing: things.Thing{ID: wrongID, Owner: wrongValue}, err: things.ErrNotFound},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
err := thingRepo.Update(tc.thing)
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestSingleThingRetrieval(t *testing.T) {
|
|
email := "thing-single-retrieval@example.com"
|
|
thingRepo := postgres.NewThingRepository(db, testLog)
|
|
|
|
thing := things.Thing{
|
|
Owner: email,
|
|
Key: uuid.New().ID(),
|
|
}
|
|
|
|
id, _ := thingRepo.Save(thing)
|
|
thing.ID = id
|
|
|
|
cases := map[string]struct {
|
|
owner string
|
|
ID uint64
|
|
err error
|
|
}{
|
|
"retrieve thing with existing user": {owner: thing.Owner, ID: thing.ID, err: nil},
|
|
"retrieve non-existing thing with existing user": {owner: thing.Owner, ID: wrongID, err: things.ErrNotFound},
|
|
"retrieve thing with non-existing owner": {owner: wrongValue, ID: thing.ID, err: things.ErrNotFound},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
_, err := thingRepo.RetrieveByID(tc.owner, tc.ID)
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestThingRetrieveByKey(t *testing.T) {
|
|
email := "thing-retrieved-by-key@example.com"
|
|
thingRepo := postgres.NewThingRepository(db, testLog)
|
|
|
|
thing := things.Thing{
|
|
Owner: email,
|
|
Key: uuid.New().ID(),
|
|
}
|
|
|
|
id, _ := thingRepo.Save(thing)
|
|
thing.ID = id
|
|
|
|
cases := map[string]struct {
|
|
key string
|
|
ID uint64
|
|
err error
|
|
}{
|
|
"retrieve existing thing by key": {key: thing.Key, ID: thing.ID, err: nil},
|
|
"retrieve non-existent thing by key": {key: wrongValue, ID: wrongID, err: things.ErrNotFound},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
id, err := thingRepo.RetrieveByKey(tc.key)
|
|
assert.Equal(t, tc.ID, id, fmt.Sprintf("%s: expected %d got %d\n", desc, tc.ID, id))
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestMultiThingRetrieval(t *testing.T) {
|
|
email := "thing-multi-retrieval@example.com"
|
|
idp := uuid.New()
|
|
thingRepo := postgres.NewThingRepository(db, testLog)
|
|
|
|
n := 10
|
|
|
|
for i := 0; i < n; i++ {
|
|
t := things.Thing{
|
|
Owner: email,
|
|
Key: idp.ID(),
|
|
}
|
|
|
|
thingRepo.Save(t)
|
|
}
|
|
|
|
cases := map[string]struct {
|
|
owner string
|
|
offset int
|
|
limit int
|
|
size int
|
|
}{
|
|
"retrieve all things with existing owner": {owner: email, offset: 0, limit: n, size: n},
|
|
"retrieve subset of things with existing owner": {owner: email, offset: n / 2, limit: n, size: n / 2},
|
|
"retrieve things with non-existing owner": {owner: wrongValue, offset: 0, limit: n, size: 0},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
n := len(thingRepo.RetrieveAll(tc.owner, tc.offset, tc.limit))
|
|
assert.Equal(t, tc.size, n, fmt.Sprintf("%s: expected %d got %d\n", desc, tc.size, n))
|
|
}
|
|
}
|
|
|
|
func TestThingRemoval(t *testing.T) {
|
|
email := "thing-removal@example.com"
|
|
thingRepo := postgres.NewThingRepository(db, testLog)
|
|
|
|
thing := things.Thing{
|
|
Owner: email,
|
|
Key: uuid.New().ID(),
|
|
}
|
|
|
|
id, _ := thingRepo.Save(thing)
|
|
thing.ID = id
|
|
|
|
// show that the removal works the same for both existing and non-existing
|
|
// (removed) thing
|
|
for i := 0; i < 2; i++ {
|
|
err := thingRepo.Remove(email, thing.ID)
|
|
require.Nil(t, err, fmt.Sprintf("#%d: failed to remove thing due to: %s", i, err))
|
|
|
|
_, err = thingRepo.RetrieveByID(email, thing.ID)
|
|
require.Equal(t, things.ErrNotFound, err, fmt.Sprintf("#%d: expected %s got %s", i, things.ErrNotFound, err))
|
|
}
|
|
}
|