2023-04-03 09:53:36 +08:00
|
|
|
package teststore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
2023-05-25 21:50:37 +08:00
|
|
|
"github.com/usememos/memos/store"
|
2023-04-03 09:53:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMemoStore(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2023-05-25 21:50:37 +08:00
|
|
|
ts := NewTestingStore(ctx, t)
|
|
|
|
user, err := createTestingHostUser(ctx, ts)
|
2023-04-03 09:53:36 +08:00
|
|
|
require.NoError(t, err)
|
2023-07-06 21:56:42 +08:00
|
|
|
memoCreate := &store.Memo{
|
2024-03-20 20:39:16 +08:00
|
|
|
UID: "test-resource-name",
|
|
|
|
CreatorID: user.ID,
|
|
|
|
Content: "test_content",
|
|
|
|
Visibility: store.Public,
|
2023-04-03 09:53:36 +08:00
|
|
|
}
|
2023-05-25 21:50:37 +08:00
|
|
|
memo, err := ts.CreateMemo(ctx, memoCreate)
|
2023-04-03 09:53:36 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, memoCreate.Content, memo.Content)
|
|
|
|
memoPatchContent := "test_content_2"
|
2023-07-06 21:56:42 +08:00
|
|
|
memoPatch := &store.UpdateMemo{
|
2023-04-03 09:53:36 +08:00
|
|
|
ID: memo.ID,
|
|
|
|
Content: &memoPatchContent,
|
|
|
|
}
|
2023-05-25 21:50:37 +08:00
|
|
|
err = ts.UpdateMemo(ctx, memoPatch)
|
2023-04-03 09:53:36 +08:00
|
|
|
require.NoError(t, err)
|
2023-07-06 21:56:42 +08:00
|
|
|
memo, err = ts.GetMemo(ctx, &store.FindMemo{
|
2023-05-25 21:50:37 +08:00
|
|
|
ID: &memo.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-07-06 22:53:38 +08:00
|
|
|
require.NotNil(t, memo)
|
2023-07-06 21:56:42 +08:00
|
|
|
memoList, err := ts.ListMemos(ctx, &store.FindMemo{
|
2023-04-03 09:53:36 +08:00
|
|
|
CreatorID: &user.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-06-26 23:06:53 +08:00
|
|
|
require.Equal(t, 1, len(memoList))
|
|
|
|
require.Equal(t, memo, memoList[0])
|
2023-07-06 21:56:42 +08:00
|
|
|
err = ts.DeleteMemo(ctx, &store.DeleteMemo{
|
2023-04-03 09:53:36 +08:00
|
|
|
ID: memo.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-10-03 00:47:34 +08:00
|
|
|
memoList, err = ts.ListMemos(ctx, &store.FindMemo{
|
|
|
|
CreatorID: &user.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(memoList))
|
2023-12-03 13:31:29 +08:00
|
|
|
|
|
|
|
memoList, err = ts.ListMemos(ctx, &store.FindMemo{
|
|
|
|
CreatorID: &user.ID,
|
|
|
|
VisibilityList: []store.Visibility{
|
|
|
|
store.Public,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(memoList))
|
2024-01-16 13:51:26 +08:00
|
|
|
ts.Close()
|
2023-04-03 09:53:36 +08:00
|
|
|
}
|
2024-01-05 21:27:16 +08:00
|
|
|
|
|
|
|
func TestDeleteMemoStore(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ts := NewTestingStore(ctx, t)
|
|
|
|
user, err := createTestingHostUser(ctx, ts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
memoCreate := &store.Memo{
|
2024-03-20 20:39:16 +08:00
|
|
|
UID: "test-resource-name",
|
|
|
|
CreatorID: user.ID,
|
|
|
|
Content: "test_content",
|
|
|
|
Visibility: store.Public,
|
2024-01-05 21:27:16 +08:00
|
|
|
}
|
|
|
|
memo, err := ts.CreateMemo(ctx, memoCreate)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, memoCreate.Content, memo.Content)
|
|
|
|
err = ts.DeleteMemo(ctx, &store.DeleteMemo{
|
|
|
|
ID: memo.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-01-16 13:51:26 +08:00
|
|
|
ts.Close()
|
2024-01-05 21:27:16 +08:00
|
|
|
}
|