0904
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
// Copyright 2026 tamakyi. 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 TestVoteOptions(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db, cleanup := newTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
store := NewVoteOptionsStore(db)
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
test func(t *testing.T, ctx context.Context, db *voteOptions)
|
||||
}{
|
||||
{"Create", testVoteOptionsCreate},
|
||||
{"BulkCreate", testVoteOptionsBulkCreate},
|
||||
{"ListByVoteID", testVoteOptionsListByVoteID},
|
||||
{"DeleteByID", testVoteOptionsDeleteByID},
|
||||
{"DeleteByVoteID", testVoteOptionsDeleteByVoteID},
|
||||
{"CountByVoteID", testVoteOptionsCountByVoteID},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Cleanup(func() {
|
||||
if err := cleanup("vote_options"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
tc.test(t, ctx, store.(*voteOptions))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func testVoteOptionsCreate(t *testing.T, ctx context.Context, db *voteOptions) {
|
||||
got, err := db.Create(ctx, CreateVoteOptionOptions{
|
||||
VoteID: 1,
|
||||
Content: "选项 A",
|
||||
SortOrder: 0,
|
||||
})
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, uint(1), got.ID)
|
||||
require.Equal(t, "选项 A", got.Content)
|
||||
}
|
||||
|
||||
func testVoteOptionsBulkCreate(t *testing.T, ctx context.Context, db *voteOptions) {
|
||||
got, err := db.BulkCreate(ctx, 1, []string{"A", "B", "C"})
|
||||
require.Nil(t, err)
|
||||
require.Len(t, got, 3)
|
||||
|
||||
// SortOrder 应按传入顺序从 0 递增
|
||||
require.Equal(t, 0, got[0].SortOrder)
|
||||
require.Equal(t, 2, got[2].SortOrder)
|
||||
require.Equal(t, "C", got[2].Content)
|
||||
}
|
||||
|
||||
func testVoteOptionsListByVoteID(t *testing.T, ctx context.Context, db *voteOptions) {
|
||||
_, err := db.BulkCreate(ctx, 1, []string{"B", "A", "C"})
|
||||
require.Nil(t, err)
|
||||
_, err = db.BulkCreate(ctx, 2, []string{"X"})
|
||||
require.Nil(t, err)
|
||||
|
||||
got, err := db.ListByVoteID(ctx, 1)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, got, 3)
|
||||
require.Equal(t, "B", got[0].Content, "按 SortOrder 升序:B(sort 0), A(sort 1), C(sort 2)")
|
||||
require.Equal(t, "C", got[2].Content)
|
||||
}
|
||||
|
||||
func testVoteOptionsDeleteByID(t *testing.T, ctx context.Context, db *voteOptions) {
|
||||
_, err := db.Create(ctx, CreateVoteOptionOptions{VoteID: 1, Content: "A"})
|
||||
require.Nil(t, err)
|
||||
|
||||
t.Run("normal", func(t *testing.T) {
|
||||
require.Nil(t, db.DeleteByID(ctx, 1))
|
||||
got, err := db.ListByVoteID(ctx, 1)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, got, 0)
|
||||
})
|
||||
|
||||
t.Run("not found", func(t *testing.T) {
|
||||
require.Equal(t, ErrVoteOptionNotExist, db.DeleteByID(ctx, 404))
|
||||
})
|
||||
}
|
||||
|
||||
func testVoteOptionsDeleteByVoteID(t *testing.T, ctx context.Context, db *voteOptions) {
|
||||
_, err := db.BulkCreate(ctx, 1, []string{"A", "B", "C"})
|
||||
require.Nil(t, err)
|
||||
|
||||
require.Nil(t, db.DeleteByVoteID(ctx, 1))
|
||||
got, err := db.ListByVoteID(ctx, 1)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, got, 0)
|
||||
}
|
||||
|
||||
func testVoteOptionsCountByVoteID(t *testing.T, ctx context.Context, db *voteOptions) {
|
||||
_, err := db.BulkCreate(ctx, 1, []string{"A", "B"})
|
||||
require.Nil(t, err)
|
||||
|
||||
got, err := db.CountByVoteID(ctx, 1)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(2), got)
|
||||
|
||||
got, err = db.CountByVoteID(ctx, 404)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(0), got)
|
||||
}
|
||||
Reference in New Issue
Block a user