108 lines
2.7 KiB
Go
108 lines
2.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func setupPreferenceTestDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{})
|
|
require.NoError(t, err)
|
|
require.NoError(t, db.AutoMigrate(&model.CaptainPreference{}))
|
|
return db
|
|
}
|
|
|
|
func TestCaptainPreferenceRepo_Create(t *testing.T) {
|
|
db := setupPreferenceTestDB(t)
|
|
repo := NewCaptainPreferenceRepo(db)
|
|
ctx := context.Background()
|
|
|
|
pref := &model.CaptainPreference{
|
|
AccountID: 1,
|
|
Tone: "professional",
|
|
Language: "en",
|
|
AutoLabelEnabled: true,
|
|
AutoFollowUpEnabled: false,
|
|
AutoReplyEnabled: false,
|
|
MaxResponseLength: 500,
|
|
CustomPromptSuffix: "Be concise.",
|
|
}
|
|
|
|
err := repo.Create(ctx, pref)
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, pref.ID)
|
|
assert.Equal(t, "professional", pref.Tone)
|
|
}
|
|
|
|
func TestCaptainPreferenceRepo_GetByAccountID(t *testing.T) {
|
|
db := setupPreferenceTestDB(t)
|
|
repo := NewCaptainPreferenceRepo(db)
|
|
ctx := context.Background()
|
|
|
|
pref := &model.CaptainPreference{
|
|
AccountID: 42,
|
|
Tone: "friendly",
|
|
Language: "es",
|
|
MaxResponseLength: 300,
|
|
}
|
|
require.NoError(t, repo.Create(ctx, pref))
|
|
|
|
found, err := repo.GetByAccountID(ctx, 42)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, uint(42), found.AccountID)
|
|
assert.Equal(t, "friendly", found.Tone)
|
|
assert.Equal(t, "es", found.Language)
|
|
}
|
|
|
|
func TestCaptainPreferenceRepo_GetByAccountID_NotFound(t *testing.T) {
|
|
db := setupPreferenceTestDB(t)
|
|
repo := NewCaptainPreferenceRepo(db)
|
|
ctx := context.Background()
|
|
|
|
_, err := repo.GetByAccountID(ctx, 999)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestCaptainPreferenceRepo_Update(t *testing.T) {
|
|
db := setupPreferenceTestDB(t)
|
|
repo := NewCaptainPreferenceRepo(db)
|
|
ctx := context.Background()
|
|
|
|
pref := &model.CaptainPreference{
|
|
AccountID: 1,
|
|
Tone: "professional",
|
|
MaxResponseLength: 500,
|
|
}
|
|
require.NoError(t, repo.Create(ctx, pref))
|
|
|
|
pref.Tone = "casual"
|
|
pref.MaxResponseLength = 200
|
|
require.NoError(t, repo.Update(ctx, pref))
|
|
|
|
found, err := repo.GetByAccountID(ctx, 1)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "casual", found.Tone)
|
|
assert.Equal(t, 200, found.MaxResponseLength)
|
|
}
|
|
|
|
func TestCaptainPreferenceRepo_Delete(t *testing.T) {
|
|
db := setupPreferenceTestDB(t)
|
|
repo := NewCaptainPreferenceRepo(db)
|
|
ctx := context.Background()
|
|
|
|
pref := &model.CaptainPreference{AccountID: 1, Tone: "professional"}
|
|
require.NoError(t, repo.Create(ctx, pref))
|
|
|
|
require.NoError(t, repo.Delete(ctx, pref.ID))
|
|
|
|
_, err := repo.GetByAccountID(ctx, 1)
|
|
assert.Error(t, err) // soft-deleted should not be found
|
|
}
|