169 lines
5.8 KiB
Go
169 lines
5.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// --- DraftMessageRepo Tests ---
|
|
|
|
func createDraftTestUser(t *testing.T, db *gorm.DB) *model.User {
|
|
t.Helper()
|
|
user := &model.User{Name: "DraftUser", Email: "draft@test.com", Password: "hashed", Role: "agent", Active: true}
|
|
require.NoError(t, db.Create(user).Error)
|
|
return user
|
|
}
|
|
|
|
func TestDraftMessageRepo_Create(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewDraftMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "DraftTestOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "DraftTestInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "DraftTestContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := createTestConversation(t, db, account.ID, inbox.ID, contact.ID, "open")
|
|
user := createDraftTestUser(t, db)
|
|
|
|
draft := &model.DraftMessage{
|
|
ConversationID: conv.ID,
|
|
UserID: user.ID,
|
|
Content: "Hello, this is a draft",
|
|
}
|
|
err := repo.Create(context.Background(), draft)
|
|
assert.NoError(t, err)
|
|
assert.NotZero(t, draft.ID)
|
|
}
|
|
|
|
func TestDraftMessageRepo_FindByConversationID(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewDraftMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "DraftFindOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "DraftFindInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "DraftFindContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := createTestConversation(t, db, account.ID, inbox.ID, contact.ID, "open")
|
|
user1 := createDraftTestUser(t, db)
|
|
user2 := &model.User{Name: "DraftUser2", Email: "draft2@test.com", Password: "hashed", Role: "agent", Active: true}
|
|
require.NoError(t, db.Create(user2).Error)
|
|
|
|
require.NoError(t, repo.Create(context.Background(), &model.DraftMessage{ConversationID: conv.ID, UserID: user1.ID, Content: "draft 1"}))
|
|
require.NoError(t, repo.Create(context.Background(), &model.DraftMessage{ConversationID: conv.ID, UserID: user2.ID, Content: "draft 2"}))
|
|
|
|
// Find all drafts for conversation
|
|
drafts, err := repo.FindByConversationID(context.Background(), conv.ID, 0)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, drafts, 2)
|
|
|
|
// Find drafts filtered by user1
|
|
drafts, err = repo.FindByConversationID(context.Background(), conv.ID, user1.ID)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, drafts, 1)
|
|
assert.Equal(t, "draft 1", drafts[0].Content)
|
|
}
|
|
|
|
func TestDraftMessageRepo_FindByID(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewDraftMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "DraftFindByIDOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "DraftFindByIDInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "DraftFindByIDContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := createTestConversation(t, db, account.ID, inbox.ID, contact.ID, "open")
|
|
user := createDraftTestUser(t, db)
|
|
|
|
draft := &model.DraftMessage{ConversationID: conv.ID, UserID: user.ID, Content: "find me"}
|
|
require.NoError(t, repo.Create(context.Background(), draft))
|
|
|
|
found, err := repo.FindByID(context.Background(), draft.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, draft.ID, found.ID)
|
|
assert.Equal(t, "find me", found.Content)
|
|
}
|
|
|
|
func TestDraftMessageRepo_FindByID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewDraftMessageRepo(db)
|
|
|
|
found, err := repo.FindByID(context.Background(), 99999)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
func TestDraftMessageRepo_Update(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewDraftMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "DraftUpdateOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "DraftUpdateInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "DraftUpdateContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := createTestConversation(t, db, account.ID, inbox.ID, contact.ID, "open")
|
|
user := createDraftTestUser(t, db)
|
|
|
|
draft := &model.DraftMessage{ConversationID: conv.ID, UserID: user.ID, Content: "original"}
|
|
require.NoError(t, repo.Create(context.Background(), draft))
|
|
|
|
draft.Content = "updated"
|
|
err := repo.Update(context.Background(), draft)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.FindByID(context.Background(), draft.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "updated", found.Content)
|
|
}
|
|
|
|
func TestDraftMessageRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewDraftMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "DraftDelOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "DraftDelInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "DraftDelContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := createTestConversation(t, db, account.ID, inbox.ID, contact.ID, "open")
|
|
user := createDraftTestUser(t, db)
|
|
|
|
draft := &model.DraftMessage{ConversationID: conv.ID, UserID: user.ID, Content: "to delete"}
|
|
require.NoError(t, repo.Create(context.Background(), draft))
|
|
|
|
err := repo.Delete(context.Background(), draft.ID)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.FindByID(context.Background(), draft.ID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
} |