chore: update store tests (#1449)

This commit is contained in:
boojack 2023-04-03 09:53:36 +08:00 committed by GitHub
parent 4f2adfef7b
commit c9a5df81ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 101 additions and 15 deletions

View File

@ -11,7 +11,7 @@ import (
type SystemSettingName string type SystemSettingName string
const ( const (
// SystemSettingServerID is the name of server id. // SystemSettingServerIDName is the name of server id.
SystemSettingServerIDName SystemSettingName = "server-id" SystemSettingServerIDName SystemSettingName = "server-id"
// SystemSettingSecretSessionName is the name of secret session. // SystemSettingSecretSessionName is the name of secret session.
SystemSettingSecretSessionName SystemSettingName = "secret-session" SystemSettingSecretSessionName SystemSettingName = "secret-session"

42
test/store/memo_test.go Normal file
View File

@ -0,0 +1,42 @@
package teststore
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/api"
)
func TestMemoStore(t *testing.T) {
ctx := context.Background()
store := NewTestingStore(ctx, t)
user, err := createTestingHostUser(ctx, store)
require.NoError(t, err)
memoCreate := &api.MemoCreate{
CreatorID: user.ID,
Content: "test_content",
Visibility: api.Public,
}
memo, err := store.CreateMemo(ctx, memoCreate)
require.NoError(t, err)
require.Equal(t, memoCreate.Content, memo.Content)
memoPatchContent := "test_content_2"
memoPatch := &api.MemoPatch{
ID: memo.ID,
Content: &memoPatchContent,
}
memo, err = store.PatchMemo(ctx, memoPatch)
require.NoError(t, err)
require.Equal(t, memoPatchContent, memo.Content)
memoList, err := store.FindMemoList(ctx, &api.MemoFind{
CreatorID: &user.ID,
})
require.NoError(t, err)
require.Equal(t, 1, len(memoList))
require.Equal(t, memo, memoList[0])
err = store.DeleteMemo(ctx, &api.MemoDelete{
ID: memo.ID,
})
require.NoError(t, err)
}

View File

@ -1,4 +1,4 @@
package store_test package teststore
import ( import (
"context" "context"

View File

@ -0,0 +1,35 @@
package teststore
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/api"
)
func TestSystemSettingStore(t *testing.T) {
ctx := context.Background()
store := NewTestingStore(ctx, t)
_, err := store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
Name: api.SystemSettingServerIDName,
Value: "test_server_id",
})
require.NoError(t, err)
_, err = store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
Name: api.SystemSettingSecretSessionName,
Value: "test_secret_session_name",
})
require.NoError(t, err)
_, err = store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
Name: api.SystemSettingAllowSignUpName,
Value: "true",
})
require.NoError(t, err)
_, err = store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
Name: api.SystemSettingLocalStoragePathName,
Value: "/tmp/memos",
})
require.NoError(t, err)
}

View File

@ -1,4 +1,4 @@
package store_test package teststore
import ( import (
"context" "context"
@ -6,28 +6,19 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/usememos/memos/api" "github.com/usememos/memos/api"
"github.com/usememos/memos/store"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
func TestUserStore(t *testing.T) { func TestUserStore(t *testing.T) {
ctx := context.Background() ctx := context.Background()
store := NewTestingStore(ctx, t) store := NewTestingStore(ctx, t)
userCreate := &api.UserCreate{ user, err := createTestingHostUser(ctx, store)
Username: "test",
Role: api.Host,
Email: "test@test.com",
Nickname: "test_nickname",
Password: "test_password",
OpenID: "test_open_id",
}
passwordHash, err := bcrypt.GenerateFromPassword([]byte(userCreate.Password), bcrypt.DefaultCost)
require.NoError(t, err)
userCreate.PasswordHash = string(passwordHash)
user, err := store.CreateUser(ctx, userCreate)
require.NoError(t, err) require.NoError(t, err)
users, err := store.FindUserList(ctx, &api.UserFind{}) users, err := store.FindUserList(ctx, &api.UserFind{})
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(users)) require.Equal(t, 1, len(users))
require.Equal(t, api.Host, users[0].Role)
require.Equal(t, user, users[0]) require.Equal(t, user, users[0])
userPatchNickname := "test_nickname_2" userPatchNickname := "test_nickname_2"
userPatch := &api.UserPatch{ userPatch := &api.UserPatch{
@ -45,3 +36,21 @@ func TestUserStore(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 0, len(users)) require.Equal(t, 0, len(users))
} }
func createTestingHostUser(ctx context.Context, store *store.Store) (*api.User, error) {
userCreate := &api.UserCreate{
Username: "test",
Role: api.Host,
Email: "test@test.com",
Nickname: "test_nickname",
Password: "test_password",
OpenID: "test_open_id",
}
passwordHash, err := bcrypt.GenerateFromPassword([]byte(userCreate.Password), bcrypt.DefaultCost)
if err != nil {
return nil, err
}
userCreate.PasswordHash = string(passwordHash)
user, err := store.CreateUser(ctx, userCreate)
return user, err
}