2023-04-13 19:55:18 +08:00
|
|
|
package teststore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2024-01-21 10:49:30 +08:00
|
|
|
"github.com/lithammer/shortuuid/v4"
|
2023-04-13 19:55:18 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
2023-07-06 00:01:40 +08:00
|
|
|
"github.com/usememos/memos/store"
|
2023-04-13 19:55:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestResourceStore(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2023-07-06 00:01:40 +08:00
|
|
|
ts := NewTestingStore(ctx, t)
|
2023-07-06 21:56:42 +08:00
|
|
|
_, err := ts.CreateResource(ctx, &store.Resource{
|
2024-03-20 20:39:16 +08:00
|
|
|
UID: shortuuid.New(),
|
2023-04-13 19:55:18 +08:00
|
|
|
CreatorID: 101,
|
|
|
|
Filename: "test.epub",
|
|
|
|
Blob: []byte("test"),
|
|
|
|
InternalPath: "",
|
|
|
|
ExternalLink: "",
|
|
|
|
Type: "application/epub+zip",
|
|
|
|
Size: 637607,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
correctFilename := "test.epub"
|
|
|
|
incorrectFilename := "test.png"
|
2023-09-16 00:11:07 +08:00
|
|
|
resource, err := ts.GetResource(ctx, &store.FindResource{
|
2023-04-13 19:55:18 +08:00
|
|
|
Filename: &correctFilename,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-09-16 00:11:07 +08:00
|
|
|
require.Equal(t, correctFilename, resource.Filename)
|
|
|
|
require.Equal(t, int32(1), resource.ID)
|
|
|
|
|
2023-07-06 00:01:40 +08:00
|
|
|
notFoundResource, err := ts.GetResource(ctx, &store.FindResource{
|
2023-04-13 19:55:18 +08:00
|
|
|
Filename: &incorrectFilename,
|
|
|
|
})
|
2023-07-06 00:01:40 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, notFoundResource)
|
2023-04-13 19:55:18 +08:00
|
|
|
|
2023-08-04 21:55:07 +08:00
|
|
|
var correctCreatorID int32 = 101
|
|
|
|
var incorrectCreatorID int32 = 102
|
2023-07-06 00:01:40 +08:00
|
|
|
_, err = ts.GetResource(ctx, &store.FindResource{
|
2023-04-13 19:55:18 +08:00
|
|
|
CreatorID: &correctCreatorID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-09-16 00:11:07 +08:00
|
|
|
|
2023-07-06 00:01:40 +08:00
|
|
|
notFoundResource, err = ts.GetResource(ctx, &store.FindResource{
|
2023-04-13 19:55:18 +08:00
|
|
|
CreatorID: &incorrectCreatorID,
|
|
|
|
})
|
2023-07-06 00:01:40 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, notFoundResource)
|
2023-04-13 19:55:18 +08:00
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
err = ts.DeleteResource(ctx, &store.DeleteResource{
|
2023-04-13 19:55:18 +08:00
|
|
|
ID: 1,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-07-06 21:56:42 +08:00
|
|
|
err = ts.DeleteResource(ctx, &store.DeleteResource{
|
2023-04-13 19:55:18 +08:00
|
|
|
ID: 2,
|
|
|
|
})
|
2023-07-06 00:01:40 +08:00
|
|
|
require.NoError(t, err)
|
2024-01-16 13:51:26 +08:00
|
|
|
ts.Close()
|
2023-04-13 19:55:18 +08:00
|
|
|
}
|