memo_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package teststore
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. "github.com/usememos/memos/store"
  7. storepb "github.com/usememos/memos/proto/gen/store"
  8. )
  9. func TestMemoStore(t *testing.T) {
  10. ctx := context.Background()
  11. ts := NewTestingStore(ctx, t)
  12. user, err := createTestingHostUser(ctx, ts)
  13. require.NoError(t, err)
  14. memoCreate := &store.Memo{
  15. UID: "test-resource-name",
  16. CreatorID: user.ID,
  17. Content: "test_content",
  18. Visibility: store.Public,
  19. }
  20. memo, err := ts.CreateMemo(ctx, memoCreate)
  21. require.NoError(t, err)
  22. require.Equal(t, memoCreate.Content, memo.Content)
  23. memoPatchContent := "test_content_2"
  24. memoPatch := &store.UpdateMemo{
  25. ID: memo.ID,
  26. Content: &memoPatchContent,
  27. }
  28. err = ts.UpdateMemo(ctx, memoPatch)
  29. require.NoError(t, err)
  30. memo, err = ts.GetMemo(ctx, &store.FindMemo{
  31. ID: &memo.ID,
  32. })
  33. require.NoError(t, err)
  34. require.NotNil(t, memo)
  35. memoList, err := ts.ListMemos(ctx, &store.FindMemo{
  36. CreatorID: &user.ID,
  37. })
  38. require.NoError(t, err)
  39. require.Equal(t, 1, len(memoList))
  40. require.Equal(t, memo, memoList[0])
  41. err = ts.DeleteMemo(ctx, &store.DeleteMemo{
  42. ID: memo.ID,
  43. })
  44. require.NoError(t, err)
  45. memoList, err = ts.ListMemos(ctx, &store.FindMemo{
  46. CreatorID: &user.ID,
  47. })
  48. require.NoError(t, err)
  49. require.Equal(t, 0, len(memoList))
  50. memoList, err = ts.ListMemos(ctx, &store.FindMemo{
  51. CreatorID: &user.ID,
  52. VisibilityList: []store.Visibility{store.Public},
  53. })
  54. require.NoError(t, err)
  55. require.Equal(t, 0, len(memoList))
  56. ts.Close()
  57. }
  58. func TestMemoListByTags(t *testing.T) {
  59. ctx := context.Background()
  60. ts := NewTestingStore(ctx, t)
  61. user, err := createTestingHostUser(ctx, ts)
  62. require.NoError(t, err)
  63. memoCreate := &store.Memo{
  64. UID: "test-resource-name",
  65. CreatorID: user.ID,
  66. Content: "test_content",
  67. Visibility: store.Public,
  68. Payload: &storepb.MemoPayload{
  69. Tags: []string{"test_tag"},
  70. },
  71. }
  72. memo, err := ts.CreateMemo(ctx, memoCreate)
  73. require.NoError(t, err)
  74. require.Equal(t, memoCreate.Content, memo.Content)
  75. memo, err = ts.GetMemo(ctx, &store.FindMemo{
  76. ID: &memo.ID,
  77. })
  78. require.NoError(t, err)
  79. require.NotNil(t, memo)
  80. memoList, err := ts.ListMemos(ctx, &store.FindMemo{
  81. PayloadFind: &store.FindMemoPayload{
  82. TagSearch: []string{"test_tag"},
  83. },
  84. })
  85. require.NoError(t, err)
  86. require.Equal(t, 1, len(memoList))
  87. require.Equal(t, memo, memoList[0])
  88. ts.Close()
  89. }
  90. func TestDeleteMemoStore(t *testing.T) {
  91. ctx := context.Background()
  92. ts := NewTestingStore(ctx, t)
  93. user, err := createTestingHostUser(ctx, ts)
  94. require.NoError(t, err)
  95. memoCreate := &store.Memo{
  96. UID: "test-resource-name",
  97. CreatorID: user.ID,
  98. Content: "test_content",
  99. Visibility: store.Public,
  100. }
  101. memo, err := ts.CreateMemo(ctx, memoCreate)
  102. require.NoError(t, err)
  103. require.Equal(t, memoCreate.Content, memo.Content)
  104. err = ts.DeleteMemo(ctx, &store.DeleteMemo{
  105. ID: memo.ID,
  106. })
  107. require.NoError(t, err)
  108. ts.Close()
  109. }