156 lines
4.9 KiB
Go
156 lines
4.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: build a minimal valid CaptainInbox
|
|
func newTestCaptainInbox(assistantID, inboxID, accountID uint) *model.CaptainInbox {
|
|
return &model.CaptainInbox{
|
|
AssistantID: assistantID,
|
|
InboxID: inboxID,
|
|
AccountID: accountID,
|
|
}
|
|
}
|
|
|
|
// ========== Create ==========
|
|
|
|
func TestCaptainInboxRepo_Create(t *testing.T) {
|
|
db := setupTestDB(t, &model.CaptainInbox{})
|
|
repo := NewCaptainInboxRepo(db)
|
|
|
|
ci := newTestCaptainInbox(1, 10, 100)
|
|
err := repo.Create(context.Background(), ci)
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, ci.ID, "ID should be set after Create")
|
|
assert.Equal(t, uint(1), ci.AssistantID)
|
|
assert.Equal(t, uint(10), ci.InboxID)
|
|
assert.Equal(t, uint(100), ci.AccountID)
|
|
}
|
|
|
|
// ========== Delete ==========
|
|
|
|
func TestCaptainInboxRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t, &model.CaptainInbox{})
|
|
repo := NewCaptainInboxRepo(db)
|
|
|
|
ci := newTestCaptainInbox(2, 20, 200)
|
|
err := repo.Create(context.Background(), ci)
|
|
require.NoError(t, err)
|
|
|
|
err = repo.Delete(context.Background(), ci.AssistantID, ci.InboxID)
|
|
require.NoError(t, err)
|
|
|
|
// Verify deletion: FindByAssistantAndInbox should return error
|
|
found, err := repo.FindByAssistantAndInbox(context.Background(), ci.AssistantID, ci.InboxID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
func TestCaptainInboxRepo_Delete_NotExist(t *testing.T) {
|
|
db := setupTestDB(t, &model.CaptainInbox{})
|
|
repo := NewCaptainInboxRepo(db)
|
|
|
|
// Deleting a non-existent record should not error (GORM delete zero rows is not an error)
|
|
err := repo.Delete(context.Background(), 999, 888)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
// ========== ListByAssistant ==========
|
|
|
|
func TestCaptainInboxRepo_ListByAssistant(t *testing.T) {
|
|
db := setupTestDB(t, &model.CaptainInbox{})
|
|
repo := NewCaptainInboxRepo(db)
|
|
|
|
// Create multiple inboxes for the same assistant
|
|
ci1 := newTestCaptainInbox(3, 30, 300)
|
|
ci2 := newTestCaptainInbox(3, 31, 300)
|
|
ci3 := newTestCaptainInbox(4, 40, 400) // different assistant
|
|
require.NoError(t, repo.Create(context.Background(), ci1))
|
|
require.NoError(t, repo.Create(context.Background(), ci2))
|
|
require.NoError(t, repo.Create(context.Background(), ci3))
|
|
|
|
inboxes, err := repo.ListByAssistant(context.Background(), 3)
|
|
require.NoError(t, err)
|
|
assert.Len(t, inboxes, 2, "should return 2 inboxes for assistant 3")
|
|
|
|
// Verify the results belong to the correct assistant
|
|
for _, ci := range inboxes {
|
|
assert.Equal(t, uint(3), ci.AssistantID)
|
|
}
|
|
}
|
|
|
|
func TestCaptainInboxRepo_ListByAssistant_Empty(t *testing.T) {
|
|
db := setupTestDB(t, &model.CaptainInbox{})
|
|
repo := NewCaptainInboxRepo(db)
|
|
|
|
// No records exist for this assistant
|
|
inboxes, err := repo.ListByAssistant(context.Background(), 999)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, inboxes, "should return empty list for non-existent assistant")
|
|
}
|
|
|
|
// ========== FindByAssistantAndInbox ==========
|
|
|
|
func TestCaptainInboxRepo_FindByAssistantAndInbox(t *testing.T) {
|
|
db := setupTestDB(t, &model.CaptainInbox{})
|
|
repo := NewCaptainInboxRepo(db)
|
|
|
|
ci := newTestCaptainInbox(5, 50, 500)
|
|
require.NoError(t, repo.Create(context.Background(), ci))
|
|
|
|
found, err := repo.FindByAssistantAndInbox(context.Background(), 5, 50)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, ci.ID, found.ID)
|
|
assert.Equal(t, uint(5), found.AssistantID)
|
|
assert.Equal(t, uint(50), found.InboxID)
|
|
assert.Equal(t, uint(500), found.AccountID)
|
|
}
|
|
|
|
func TestCaptainInboxRepo_FindByAssistantAndInbox_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.CaptainInbox{})
|
|
repo := NewCaptainInboxRepo(db)
|
|
|
|
found, err := repo.FindByAssistantAndInbox(context.Background(), 999, 888)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
// ========== FindByInboxID ==========
|
|
|
|
func TestCaptainInboxRepo_FindByInboxID(t *testing.T) {
|
|
db := setupTestDB(t, &model.CaptainInbox{})
|
|
repo := NewCaptainInboxRepo(db)
|
|
|
|
// Create multiple assistants linked to the same inbox
|
|
ci1 := newTestCaptainInbox(6, 60, 600)
|
|
ci2 := newTestCaptainInbox(7, 60, 600) // same inbox, different assistant
|
|
ci3 := newTestCaptainInbox(8, 61, 600) // different inbox
|
|
require.NoError(t, repo.Create(context.Background(), ci1))
|
|
require.NoError(t, repo.Create(context.Background(), ci2))
|
|
require.NoError(t, repo.Create(context.Background(), ci3))
|
|
|
|
inboxes, err := repo.FindByInboxID(context.Background(), 60)
|
|
require.NoError(t, err)
|
|
assert.Len(t, inboxes, 2, "should return 2 entries for inbox 60")
|
|
|
|
// Verify all results belong to the correct inbox
|
|
for _, ci := range inboxes {
|
|
assert.Equal(t, uint(60), ci.InboxID)
|
|
}
|
|
}
|
|
|
|
func TestCaptainInboxRepo_FindByInboxID_Empty(t *testing.T) {
|
|
db := setupTestDB(t, &model.CaptainInbox{})
|
|
repo := NewCaptainInboxRepo(db)
|
|
|
|
inboxes, err := repo.FindByInboxID(context.Background(), 999)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, inboxes, "should return empty list for non-existent inbox")
|
|
} |