110 lines
3.3 KiB
Go
110 lines
3.3 KiB
Go
// 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 TestVoteFormFields(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db, cleanup := newTestDB(t)
|
|
ctx := context.Background()
|
|
|
|
store := NewVoteFormFieldsStore(db)
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
test func(t *testing.T, ctx context.Context, db *voteFormFields)
|
|
}{
|
|
{"Create", testVoteFormFieldsCreate},
|
|
{"BulkCreate", testVoteFormFieldsBulkCreate},
|
|
{"ListByVoteID", testVoteFormFieldsListByVoteID},
|
|
{"DeleteByID", testVoteFormFieldsDeleteByID},
|
|
{"DeleteByVoteID", testVoteFormFieldsDeleteByVoteID},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Cleanup(func() {
|
|
if err := cleanup("vote_form_fields"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
tc.test(t, ctx, store.(*voteFormFields))
|
|
})
|
|
}
|
|
}
|
|
|
|
func testVoteFormFieldsCreate(t *testing.T, ctx context.Context, db *voteFormFields) {
|
|
got, err := db.Create(ctx, CreateVoteFormFieldOptions{
|
|
VoteID: 1,
|
|
FieldType: VoteFormFieldTypeText,
|
|
Label: "你的昵称",
|
|
Required: true,
|
|
MaxLength: 50,
|
|
})
|
|
require.Nil(t, err)
|
|
require.Equal(t, uint(1), got.ID)
|
|
require.True(t, got.Required)
|
|
require.Equal(t, 50, got.MaxLength)
|
|
}
|
|
|
|
func testVoteFormFieldsBulkCreate(t *testing.T, ctx context.Context, db *voteFormFields) {
|
|
got, err := db.BulkCreate(ctx, 1, []CreateVoteFormFieldOptions{
|
|
{FieldType: VoteFormFieldTypeText, Label: "昵称", Required: true, MaxLength: 20, SortOrder: 0},
|
|
{FieldType: VoteFormFieldTypeTextarea, Label: "建议", Required: false, MaxLength: 500, SortOrder: 1},
|
|
{FieldType: VoteFormFieldTypeRadio, Label: "满意度", Options: `["满意","一般","不满意"]`, SortOrder: 2},
|
|
})
|
|
require.Nil(t, err)
|
|
require.Len(t, got, 3)
|
|
require.Equal(t, VoteFormFieldTypeTextarea, got[1].FieldType)
|
|
require.Equal(t, `["满意","一般","不满意"]`, got[2].Options)
|
|
}
|
|
|
|
func testVoteFormFieldsListByVoteID(t *testing.T, ctx context.Context, db *voteFormFields) {
|
|
_, err := db.BulkCreate(ctx, 1, []CreateVoteFormFieldOptions{
|
|
{FieldType: VoteFormFieldTypeText, Label: "B", SortOrder: 1},
|
|
{FieldType: VoteFormFieldTypeText, Label: "A", SortOrder: 0},
|
|
})
|
|
require.Nil(t, err)
|
|
|
|
got, err := db.ListByVoteID(ctx, 1)
|
|
require.Nil(t, err)
|
|
require.Len(t, got, 2)
|
|
require.Equal(t, "A", got[0].Label, "按 SortOrder 升序")
|
|
}
|
|
|
|
func testVoteFormFieldsDeleteByID(t *testing.T, ctx context.Context, db *voteFormFields) {
|
|
_, err := db.Create(ctx, CreateVoteFormFieldOptions{VoteID: 1, FieldType: VoteFormFieldTypeText, Label: "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, ErrVoteFormFieldNotExist, db.DeleteByID(ctx, 404))
|
|
})
|
|
}
|
|
|
|
func testVoteFormFieldsDeleteByVoteID(t *testing.T, ctx context.Context, db *voteFormFields) {
|
|
_, err := db.BulkCreate(ctx, 1, []CreateVoteFormFieldOptions{
|
|
{FieldType: VoteFormFieldTypeText, Label: "A"},
|
|
{FieldType: VoteFormFieldTypeText, Label: "B"},
|
|
})
|
|
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)
|
|
}
|