memo_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package teststore
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. "github.com/usememos/memos/store"
  7. )
  8. func TestMemoStore(t *testing.T) {
  9. ctx := context.Background()
  10. ts := NewTestingStore(ctx, t)
  11. user, err := createTestingHostUser(ctx, ts)
  12. require.NoError(t, err)
  13. memoCreate := &store.Memo{
  14. UID: "test-resource-name",
  15. CreatorID: user.ID,
  16. Content: "test_content",
  17. Visibility: store.Public,
  18. }
  19. memo, err := ts.CreateMemo(ctx, memoCreate)
  20. require.NoError(t, err)
  21. require.Equal(t, memoCreate.Content, memo.Content)
  22. memoPatchContent := "test_content_2"
  23. memoPatch := &store.UpdateMemo{
  24. ID: memo.ID,
  25. Content: &memoPatchContent,
  26. }
  27. err = ts.UpdateMemo(ctx, memoPatch)
  28. require.NoError(t, err)
  29. memo, err = ts.GetMemo(ctx, &store.FindMemo{
  30. ID: &memo.ID,
  31. })
  32. require.NoError(t, err)
  33. require.NotNil(t, memo)
  34. memoList, err := ts.ListMemos(ctx, &store.FindMemo{
  35. CreatorID: &user.ID,
  36. })
  37. require.NoError(t, err)
  38. require.Equal(t, 1, len(memoList))
  39. require.Equal(t, memo, memoList[0])
  40. err = ts.DeleteMemo(ctx, &store.DeleteMemo{
  41. ID: memo.ID,
  42. })
  43. require.NoError(t, err)
  44. memoList, err = ts.ListMemos(ctx, &store.FindMemo{
  45. CreatorID: &user.ID,
  46. })
  47. require.NoError(t, err)
  48. require.Equal(t, 0, len(memoList))
  49. memoList, err = ts.ListMemos(ctx, &store.FindMemo{
  50. CreatorID: &user.ID,
  51. VisibilityList: []store.Visibility{
  52. store.Public,
  53. },
  54. })
  55. require.NoError(t, err)
  56. require.Equal(t, 0, len(memoList))
  57. ts.Close()
  58. }
  59. func TestDeleteMemoStore(t *testing.T) {
  60. ctx := context.Background()
  61. ts := NewTestingStore(ctx, t)
  62. user, err := createTestingHostUser(ctx, ts)
  63. require.NoError(t, err)
  64. memoCreate := &store.Memo{
  65. UID: "test-resource-name",
  66. CreatorID: user.ID,
  67. Content: "test_content",
  68. Visibility: store.Public,
  69. }
  70. memo, err := ts.CreateMemo(ctx, memoCreate)
  71. require.NoError(t, err)
  72. require.Equal(t, memoCreate.Content, memo.Content)
  73. err = ts.DeleteMemo(ctx, &store.DeleteMemo{
  74. ID: memo.ID,
  75. })
  76. require.NoError(t, err)
  77. ts.Close()
  78. }