146 lines
3.9 KiB
Go
146 lines
3.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// helper to create a minimal test pre-chat form
|
|
func newTestPreChatForm(inboxID uint) *model.PreChatForm {
|
|
return &model.PreChatForm{
|
|
InboxID: inboxID,
|
|
Enabled: true,
|
|
Message: "Please fill the form below",
|
|
RequireName: true,
|
|
RequireEmail: true,
|
|
RequirePhone: false,
|
|
ShowCompany: false,
|
|
ShowCity: false,
|
|
ShowCountry: false,
|
|
}
|
|
}
|
|
|
|
// --- Test: Create ---
|
|
func TestPreChatFormRepo_Create(t *testing.T) {
|
|
db := setupTestDB(t, &model.PreChatForm{})
|
|
repo := NewPreChatFormRepo(db)
|
|
|
|
form := newTestPreChatForm(1)
|
|
err := repo.Create(context.Background(), form)
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, form.ID)
|
|
assert.Equal(t, uint(1), form.InboxID)
|
|
assert.True(t, form.Enabled)
|
|
assert.True(t, form.RequireName)
|
|
assert.True(t, form.RequireEmail)
|
|
}
|
|
|
|
// --- Test: FindByID ---
|
|
func TestPreChatFormRepo_FindByID(t *testing.T) {
|
|
db := setupTestDB(t, &model.PreChatForm{})
|
|
repo := NewPreChatFormRepo(db)
|
|
|
|
form := newTestPreChatForm(10)
|
|
require.NoError(t, repo.Create(context.Background(), form))
|
|
|
|
found, err := repo.FindByID(context.Background(), form.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, form.ID, found.ID)
|
|
assert.Equal(t, uint(10), found.InboxID)
|
|
assert.Equal(t, "Please fill the form below", found.Message)
|
|
}
|
|
|
|
// --- Test: FindByID Not Found ---
|
|
func TestPreChatFormRepo_FindByID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.PreChatForm{})
|
|
repo := NewPreChatFormRepo(db)
|
|
|
|
found, err := repo.FindByID(context.Background(), 9999)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
// --- Test: FindByInboxID ---
|
|
func TestPreChatFormRepo_FindByInboxID(t *testing.T) {
|
|
db := setupTestDB(t, &model.PreChatForm{})
|
|
repo := NewPreChatFormRepo(db)
|
|
|
|
form := newTestPreChatForm(42)
|
|
require.NoError(t, repo.Create(context.Background(), form))
|
|
|
|
found, err := repo.FindByInboxID(context.Background(), 42)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, form.ID, found.ID)
|
|
assert.Equal(t, uint(42), found.InboxID)
|
|
}
|
|
|
|
// --- Test: FindByInboxID Not Found ---
|
|
func TestPreChatFormRepo_FindByInboxID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.PreChatForm{})
|
|
repo := NewPreChatFormRepo(db)
|
|
|
|
found, err := repo.FindByInboxID(context.Background(), 9999)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
// --- Test: Update ---
|
|
func TestPreChatFormRepo_Update(t *testing.T) {
|
|
db := setupTestDB(t, &model.PreChatForm{})
|
|
repo := NewPreChatFormRepo(db)
|
|
|
|
form := newTestPreChatForm(10)
|
|
require.NoError(t, repo.Create(context.Background(), form))
|
|
|
|
form.Enabled = false
|
|
form.RequirePhone = true
|
|
form.ShowCompany = true
|
|
form.Message = "Updated intro message"
|
|
err := repo.Update(context.Background(), form)
|
|
require.NoError(t, err)
|
|
|
|
found, err := repo.FindByID(context.Background(), form.ID)
|
|
require.NoError(t, err)
|
|
assert.False(t, found.Enabled)
|
|
assert.True(t, found.RequirePhone)
|
|
assert.True(t, found.ShowCompany)
|
|
assert.Equal(t, "Updated intro message", found.Message)
|
|
}
|
|
|
|
// --- Test: Delete ---
|
|
func TestPreChatFormRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t, &model.PreChatForm{})
|
|
repo := NewPreChatFormRepo(db)
|
|
|
|
form := newTestPreChatForm(10)
|
|
require.NoError(t, repo.Create(context.Background(), form))
|
|
|
|
err := repo.Delete(context.Background(), form.ID)
|
|
require.NoError(t, err)
|
|
|
|
// After soft-delete, FindByID should not find it
|
|
found, err := repo.FindByID(context.Background(), form.ID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
// --- Test: DeleteByInboxID ---
|
|
func TestPreChatFormRepo_DeleteByInboxID(t *testing.T) {
|
|
db := setupTestDB(t, &model.PreChatForm{})
|
|
repo := NewPreChatFormRepo(db)
|
|
|
|
form := newTestPreChatForm(55)
|
|
require.NoError(t, repo.Create(context.Background(), form))
|
|
|
|
err := repo.DeleteByInboxID(context.Background(), 55)
|
|
require.NoError(t, err)
|
|
|
|
found, err := repo.FindByInboxID(context.Background(), 55)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
} |