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

* Move messaging to pkg Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Move errors to pkg Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Move Transformers to pkg Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Move SDK to pkg Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Remove Transformers from root Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix make proto Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add copyrights header Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix CI Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Move Auth client to pkg Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix dependencies Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Update dependencies and vendors Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix CI Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
77 lines
1.7 KiB
Go
77 lines
1.7 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package postgres_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
"github.com/mainflux/mainflux/users"
|
|
"github.com/mainflux/mainflux/users/postgres"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestUserSave(t *testing.T) {
|
|
email := "user-save@example.com"
|
|
|
|
cases := []struct {
|
|
desc string
|
|
user users.User
|
|
err error
|
|
}{
|
|
{
|
|
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,
|
|
},
|
|
}
|
|
|
|
dbMiddleware := postgres.NewDatabase(db)
|
|
repo := postgres.New(dbMiddleware)
|
|
|
|
for _, tc := range cases {
|
|
err := repo.Save(context.Background(), tc.user)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestSingleUserRetrieval(t *testing.T) {
|
|
email := "user-retrieval@example.com"
|
|
|
|
dbMiddleware := postgres.NewDatabase(db)
|
|
repo := postgres.New(dbMiddleware)
|
|
err := repo.Save(context.Background(), users.User{
|
|
Email: email,
|
|
Password: "pass",
|
|
})
|
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
|
|
|
|
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 {
|
|
_, err := repo.RetrieveByID(context.Background(), tc.email)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|