Files
tamabox/internal/db/upload_images_test.go
T

138 lines
4.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Copyright 2023 E99p1ant. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package db
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func TestUploadImages(t *testing.T) {
// 注意:各子测试都创建 question_id=1 的上传图,cleanup 会 truncate 并 RESTART IDENTITY,
// 并行子测试会拿到相同自增 id,撞 upload_image_questions 的复合主键
// (upload_image_id, question_id);故顺序执行,使每个子测试独占全表。
db, cleanup := newTestDB(t)
ctx := context.Background()
uploadImagesStore := NewUploadImagesStore(db)
for _, tc := range []struct {
name string
test func(t *testing.T, ctx context.Context, db *uploadImages)
}{
{"Create", testUploadImagesCreate},
{"GetByQuestionID", testUploadImagesGetByQuestionID},
{"GetByTypeQuestionID", testUploadImagesGetByTypeQuestionID},
} {
t.Run(tc.name, func(t *testing.T) {
t.Cleanup(func() {
// 必须一并清链接表:upload_image_questions 无 FK 级联依赖 upload_images,
// 仅 truncate upload_images 会残留 (upload_image_id, question_id) 链接行,
// 下一个子测试因身份重启拿到相同 id 而撞复合主键。
if err := cleanup("upload_images", "upload_image_questions"); err != nil {
t.Fatal(err)
}
})
tc.test(t, ctx, uploadImagesStore.(*uploadImages))
})
}
}
func testUploadImagesCreate(t *testing.T, ctx context.Context, db *uploadImages) {
got, err := db.Create(ctx, CreateUploadImageOptions{
Type: UploadImageQuestionTypeAsk,
QuestionID: 1,
UploaderUserID: 1,
Name: "test.png",
FileSize: 12345,
Md5: "d41d8cd98f00b204e9800998ecf8427e",
Key: "uploads/test.png",
IsDeletingPrevious: false,
})
require.Nil(t, err)
want := &UploadImage{
Model: got.Model,
UploaderUserID: 1,
Name: "test.png",
FileSize: 12345,
Md5: "d41d8cd98f00b204e9800998ecf8427e",
Key: "uploads/test.png",
}
require.Equal(t, want, got)
}
func testUploadImagesGetByQuestionID(t *testing.T, ctx context.Context, db *uploadImages) {
_, err := db.Create(ctx, CreateUploadImageOptions{
Type: UploadImageQuestionTypeAsk,
QuestionID: 1,
UploaderUserID: 1,
Name: "test.png",
FileSize: 12345,
Md5: "d41d8cd98f00b204e9800998ecf8427e",
Key: "uploads/test.png",
IsDeletingPrevious: false,
})
require.Nil(t, err)
t.Run("normal", func(t *testing.T) {
got, err := db.GetByQuestionID(ctx, 1)
require.Nil(t, err)
want := []*UploadImage{{
Model: got[0].Model,
UploaderUserID: 1,
Name: "test.png",
FileSize: 12345,
Md5: "d41d8cd98f00b204e9800998ecf8427e",
Key: "uploads/test.png",
}}
require.Equal(t, got, want)
})
t.Run("not found", func(t *testing.T) {
got, err := db.GetByQuestionID(ctx, 2)
require.Nil(t, err)
require.Equal(t, 0, len(got))
})
}
func testUploadImagesGetByTypeQuestionID(t *testing.T, ctx context.Context, db *uploadImages) {
_, err := db.Create(ctx, CreateUploadImageOptions{
Type: UploadImageQuestionTypeAsk,
QuestionID: 1,
UploaderUserID: 1,
Name: "test.png",
FileSize: 12345,
Md5: "d41d8cd98f00b204e9800998ecf8427e",
Key: "uploads/test.png",
IsDeletingPrevious: false,
})
require.Nil(t, err)
t.Run("normal", func(t *testing.T) {
got, err := db.GetByTypeQuestionID(ctx, UploadImageQuestionTypeAsk, 1)
require.Nil(t, err)
want := []*UploadImage{{
Model: got[0].Model,
UploaderUserID: 1,
Name: "test.png",
FileSize: 12345,
Md5: "d41d8cd98f00b204e9800998ecf8427e",
Key: "uploads/test.png",
}}
require.Equal(t, got, want)
})
t.Run("not found", func(t *testing.T) {
got, err := db.GetByTypeQuestionID(ctx, UploadImageQuestionTypeAsk, 2)
require.Nil(t, err)
require.Equal(t, 0, len(got))
})
}