2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-05-11 01:00:10 +02:00
|
|
|
package postgres_test
|
|
|
|
|
|
|
|
import (
|
2019-07-18 15:01:09 +02:00
|
|
|
"context"
|
2018-05-11 01:00:10 +02:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux/users"
|
|
|
|
"github.com/mainflux/mainflux/users/postgres"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-04-16 14:58:56 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-05-11 01:00:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserSave(t *testing.T) {
|
|
|
|
email := "user-save@example.com"
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
user users.User
|
|
|
|
err error
|
|
|
|
}{
|
2019-04-16 14:58:56 +02:00
|
|
|
{
|
|
|
|
desc: "new user",
|
|
|
|
user: users.User{
|
|
|
|
Email: email,
|
|
|
|
Password: "pass",
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "duplicate user",
|
|
|
|
user: users.User{
|
|
|
|
Email: email,
|
|
|
|
Password: "pass",
|
|
|
|
},
|
|
|
|
err: users.ErrConflict,
|
|
|
|
},
|
2018-05-11 01:00:10 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
dbMiddleware := postgres.NewDatabase(db)
|
|
|
|
repo := postgres.New(dbMiddleware)
|
2018-05-11 01:00:10 +02:00
|
|
|
|
|
|
|
for _, tc := range cases {
|
2019-07-18 15:01:09 +02:00
|
|
|
err := repo.Save(context.Background(), tc.user)
|
2018-05-11 01:00:10 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSingleUserRetrieval(t *testing.T) {
|
|
|
|
email := "user-retrieval@example.com"
|
|
|
|
|
2019-10-07 05:32:09 -06:00
|
|
|
dbMiddleware := postgres.NewDatabase(db)
|
|
|
|
repo := postgres.New(dbMiddleware)
|
2019-07-18 15:01:09 +02:00
|
|
|
err := repo.Save(context.Background(), users.User{
|
2019-04-16 14:58:56 +02:00
|
|
|
Email: email,
|
|
|
|
Password: "pass",
|
|
|
|
})
|
|
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
2018-05-11 01:00:10 +02:00
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
email string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
"existing user": {email, nil},
|
|
|
|
"non-existing user": {"unknown@example.com", users.ErrNotFound},
|
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
2019-07-18 15:01:09 +02:00
|
|
|
_, err := repo.RetrieveByID(context.Background(), tc.email)
|
2018-05-11 01:00:10 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
|
|
}
|