2023-04-25 23:27:38 +08:00
|
|
|
package teststore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
2023-04-25 23:27:38 +08:00
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMemoRelationStore(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ts := NewTestingStore(ctx, t)
|
|
|
|
user, err := createTestingHostUser(ctx, ts)
|
|
|
|
require.NoError(t, err)
|
2023-07-06 21:56:42 +08:00
|
|
|
memoCreate := &store.Memo{
|
2023-04-25 23:27:38 +08:00
|
|
|
CreatorID: user.ID,
|
|
|
|
Content: "test_content",
|
2023-05-25 21:50:37 +08:00
|
|
|
Visibility: store.Public,
|
2023-04-25 23:27:38 +08:00
|
|
|
}
|
|
|
|
memo, err := ts.CreateMemo(ctx, memoCreate)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, memoCreate.Content, memo.Content)
|
2023-07-06 21:56:42 +08:00
|
|
|
memo2Create := &store.Memo{
|
2023-04-25 23:27:38 +08:00
|
|
|
CreatorID: user.ID,
|
|
|
|
Content: "test_content_2",
|
2023-05-25 21:50:37 +08:00
|
|
|
Visibility: store.Public,
|
2023-04-25 23:27:38 +08:00
|
|
|
}
|
2023-05-25 21:50:37 +08:00
|
|
|
memo2, err := ts.CreateMemo(ctx, memo2Create)
|
2023-04-25 23:27:38 +08:00
|
|
|
require.NoError(t, err)
|
2023-05-25 21:50:37 +08:00
|
|
|
require.Equal(t, memo2Create.Content, memo2.Content)
|
2023-07-06 21:56:42 +08:00
|
|
|
memoRelationMessage := &store.MemoRelation{
|
2023-04-25 23:27:38 +08:00
|
|
|
MemoID: memo.ID,
|
|
|
|
RelatedMemoID: memo2.ID,
|
|
|
|
Type: store.MemoRelationReference,
|
|
|
|
}
|
|
|
|
_, err = ts.UpsertMemoRelation(ctx, memoRelationMessage)
|
|
|
|
require.NoError(t, err)
|
2023-07-06 21:56:42 +08:00
|
|
|
memoRelation, err := ts.ListMemoRelations(ctx, &store.FindMemoRelation{
|
2023-04-25 23:27:38 +08:00
|
|
|
MemoID: &memo.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(memoRelation))
|
|
|
|
require.Equal(t, memo2.ID, memoRelation[0].RelatedMemoID)
|
|
|
|
require.Equal(t, memo.ID, memoRelation[0].MemoID)
|
|
|
|
require.Equal(t, store.MemoRelationReference, memoRelation[0].Type)
|
2023-07-06 21:56:42 +08:00
|
|
|
err = ts.DeleteMemo(ctx, &store.DeleteMemo{
|
2023-04-25 23:27:38 +08:00
|
|
|
ID: memo2.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-07-06 21:56:42 +08:00
|
|
|
memoRelation, err = ts.ListMemoRelations(ctx, &store.FindMemoRelation{
|
2023-04-25 23:27:38 +08:00
|
|
|
MemoID: &memo.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(memoRelation))
|
|
|
|
}
|