resource_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package teststore
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/lithammer/shortuuid/v4"
  6. "github.com/stretchr/testify/require"
  7. "github.com/usememos/memos/store"
  8. )
  9. func TestResourceStore(t *testing.T) {
  10. ctx := context.Background()
  11. ts := NewTestingStore(ctx, t)
  12. _, err := ts.CreateResource(ctx, &store.Resource{
  13. UID: shortuuid.New(),
  14. CreatorID: 101,
  15. Filename: "test.epub",
  16. Blob: []byte("test"),
  17. Type: "application/epub+zip",
  18. Size: 637607,
  19. })
  20. require.NoError(t, err)
  21. correctFilename := "test.epub"
  22. incorrectFilename := "test.png"
  23. resource, err := ts.GetResource(ctx, &store.FindResource{
  24. Filename: &correctFilename,
  25. })
  26. require.NoError(t, err)
  27. require.Equal(t, correctFilename, resource.Filename)
  28. require.Equal(t, int32(1), resource.ID)
  29. notFoundResource, err := ts.GetResource(ctx, &store.FindResource{
  30. Filename: &incorrectFilename,
  31. })
  32. require.NoError(t, err)
  33. require.Nil(t, notFoundResource)
  34. var correctCreatorID int32 = 101
  35. var incorrectCreatorID int32 = 102
  36. _, err = ts.GetResource(ctx, &store.FindResource{
  37. CreatorID: &correctCreatorID,
  38. })
  39. require.NoError(t, err)
  40. notFoundResource, err = ts.GetResource(ctx, &store.FindResource{
  41. CreatorID: &incorrectCreatorID,
  42. })
  43. require.NoError(t, err)
  44. require.Nil(t, notFoundResource)
  45. err = ts.DeleteResource(ctx, &store.DeleteResource{
  46. ID: 1,
  47. })
  48. require.NoError(t, err)
  49. err = ts.DeleteResource(ctx, &store.DeleteResource{
  50. ID: 2,
  51. })
  52. require.NoError(t, err)
  53. ts.Close()
  54. }