resource_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package teststore
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. "github.com/usememos/memos/api"
  7. )
  8. func TestResourceStore(t *testing.T) {
  9. ctx := context.Background()
  10. store := NewTestingStore(ctx, t)
  11. _, err := store.CreateResource(ctx, &api.ResourceCreate{
  12. CreatorID: 101,
  13. Filename: "test.epub",
  14. Blob: []byte("test"),
  15. InternalPath: "",
  16. ExternalLink: "",
  17. Type: "application/epub+zip",
  18. Size: 637607,
  19. PublicID: "a02748e2-9b56-46b2-8b1f-72d686d52f77",
  20. })
  21. require.NoError(t, err)
  22. correctFilename := "test.epub"
  23. incorrectFilename := "test.png"
  24. res, err := store.FindResource(ctx, &api.ResourceFind{
  25. Filename: &correctFilename,
  26. })
  27. require.NoError(t, err)
  28. require.Equal(t, correctFilename, res.Filename)
  29. require.Equal(t, 1, res.ID)
  30. _, err = store.FindResource(ctx, &api.ResourceFind{
  31. Filename: &incorrectFilename,
  32. })
  33. require.Error(t, err)
  34. correctCreatorID := 101
  35. incorrectCreatorID := 102
  36. _, err = store.FindResource(ctx, &api.ResourceFind{
  37. CreatorID: &correctCreatorID,
  38. })
  39. require.NoError(t, err)
  40. _, err = store.FindResource(ctx, &api.ResourceFind{
  41. CreatorID: &incorrectCreatorID,
  42. })
  43. require.Error(t, err)
  44. err = store.DeleteResource(ctx, &api.ResourceDelete{
  45. ID: 1,
  46. })
  47. require.NoError(t, err)
  48. err = store.DeleteResource(ctx, &api.ResourceDelete{
  49. ID: 2,
  50. })
  51. require.Error(t, err)
  52. }