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,
|
2023-10-03 00:47:34 +08:00
|
|
|
Content: "main memo 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-10-03 00:47:34 +08:00
|
|
|
relatedMemoCreate := &store.Memo{
|
2023-04-25 23:27:38 +08:00
|
|
|
CreatorID: user.ID,
|
2023-10-03 00:47:34 +08:00
|
|
|
Content: "related memo content",
|
2023-05-25 21:50:37 +08:00
|
|
|
Visibility: store.Public,
|
2023-04-25 23:27:38 +08:00
|
|
|
}
|
2023-10-03 00:47:34 +08:00
|
|
|
relatedMemo, err := ts.CreateMemo(ctx, relatedMemoCreate)
|
2023-04-25 23:27:38 +08:00
|
|
|
require.NoError(t, err)
|
2023-10-03 00:47:34 +08:00
|
|
|
require.Equal(t, relatedMemoCreate.Content, relatedMemo.Content)
|
|
|
|
commentMemoCreate := &store.Memo{
|
|
|
|
CreatorID: user.ID,
|
|
|
|
Content: "comment memo content",
|
|
|
|
Visibility: store.Public,
|
|
|
|
}
|
|
|
|
commentMemo, err := ts.CreateMemo(ctx, commentMemoCreate)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, commentMemoCreate.Content, commentMemo.Content)
|
|
|
|
|
|
|
|
// Reference relation.
|
|
|
|
referenceRelation := &store.MemoRelation{
|
2023-04-25 23:27:38 +08:00
|
|
|
MemoID: memo.ID,
|
2023-10-03 00:47:34 +08:00
|
|
|
RelatedMemoID: relatedMemo.ID,
|
2023-04-25 23:27:38 +08:00
|
|
|
Type: store.MemoRelationReference,
|
|
|
|
}
|
2023-10-03 00:47:34 +08:00
|
|
|
_, err = ts.UpsertMemoRelation(ctx, referenceRelation)
|
2023-04-25 23:27:38 +08:00
|
|
|
require.NoError(t, err)
|
2023-10-03 00:47:34 +08:00
|
|
|
// Comment relation.
|
|
|
|
commentRelation := &store.MemoRelation{
|
|
|
|
MemoID: memo.ID,
|
|
|
|
RelatedMemoID: commentMemo.ID,
|
|
|
|
Type: store.MemoRelationComment,
|
|
|
|
}
|
|
|
|
_, err = ts.UpsertMemoRelation(ctx, commentRelation)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
memo, err = ts.GetMemo(ctx, &store.FindMemo{
|
|
|
|
ID: &memo.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(memo.RelationList))
|
|
|
|
require.Equal(t, referenceRelation, memo.RelationList[0])
|
|
|
|
require.Equal(t, commentRelation, memo.RelationList[1])
|
|
|
|
relatedMemo, err = ts.GetMemo(ctx, &store.FindMemo{
|
|
|
|
ID: &relatedMemo.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(relatedMemo.RelationList))
|
|
|
|
require.Equal(t, referenceRelation, relatedMemo.RelationList[0])
|
|
|
|
commentMemo, err = ts.GetMemo(ctx, &store.FindMemo{
|
|
|
|
ID: &commentMemo.ID,
|
2023-04-25 23:27:38 +08:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-10-03 00:47:34 +08:00
|
|
|
require.Equal(t, 1, len(commentMemo.RelationList))
|
|
|
|
require.Equal(t, commentRelation, commentMemo.RelationList[0])
|
2023-07-06 21:56:42 +08:00
|
|
|
err = ts.DeleteMemo(ctx, &store.DeleteMemo{
|
2023-10-03 00:47:34 +08:00
|
|
|
ID: relatedMemo.ID,
|
2023-04-25 23:27:38 +08:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-10-03 00:47:34 +08:00
|
|
|
err = ts.DeleteMemo(ctx, &store.DeleteMemo{
|
|
|
|
ID: commentMemo.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
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))
|
|
|
|
}
|