Files
gochat/backend/internal/repository/note_repo_test.go
T

203 lines
6.4 KiB
Go

package repository
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/gochat/gochat/internal/model"
)
func TestNoteRepo_Create(t *testing.T) {
db := setupTestDB(t, &model.Note{})
repo := NewNoteRepo(db)
account := &model.Account{Name: "NoteCreateOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
contact := createTestContact(t, db, account.ID, "Note Contact", "note@example.com")
note := &model.Note{
AccountID: account.ID,
ContactID: contact.ID,
Content: "Test note content",
}
err := repo.Create(context.Background(), note)
assert.NoError(t, err)
assert.NotZero(t, note.ID)
}
func TestNoteRepo_Create_WithUserID(t *testing.T) {
db := setupTestDB(t, &model.Note{})
repo := NewNoteRepo(db)
account := &model.Account{Name: "NoteUserOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
contact := createTestContact(t, db, account.ID, "Note Contact2", "note2@example.com")
user := &model.User{Name: "NoteUser", Email: "noteuser@test.com", AccountID: account.ID}
require.NoError(t, db.Create(user).Error)
note := &model.Note{
AccountID: account.ID,
ContactID: contact.ID,
UserID: &user.ID,
Content: "Note with user",
}
err := repo.Create(context.Background(), note)
assert.NoError(t, err)
assert.NotZero(t, note.ID)
}
func TestNoteRepo_FindByContact(t *testing.T) {
db := setupTestDB(t, &model.Note{})
repo := NewNoteRepo(db)
account := &model.Account{Name: "NoteFindOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
contact := createTestContact(t, db, account.ID, "Find Contact", "find@example.com")
for i := 0; i < 3; i++ {
require.NoError(t, repo.Create(context.Background(), &model.Note{
AccountID: account.ID,
ContactID: contact.ID,
Content: "note",
}))
}
notes, err := repo.FindByContact(context.Background(), account.ID, contact.ID)
assert.NoError(t, err)
assert.Len(t, notes, 3)
}
func TestNoteRepo_ListByContact(t *testing.T) {
db := setupTestDB(t, &model.Note{})
repo := NewNoteRepo(db)
account := &model.Account{Name: "NoteListOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
contact := createTestContact(t, db, account.ID, "List Contact", "list@example.com")
require.NoError(t, repo.Create(context.Background(), &model.Note{
AccountID: account.ID, ContactID: contact.ID, Content: "note1",
}))
require.NoError(t, repo.Create(context.Background(), &model.Note{
AccountID: account.ID, ContactID: contact.ID, Content: "note2",
}))
notes, err := repo.ListByContact(account.ID, contact.ID)
assert.NoError(t, err)
assert.Len(t, notes, 2)
}
func TestNoteRepo_GetByID(t *testing.T) {
db := setupTestDB(t, &model.Note{})
repo := NewNoteRepo(db)
account := &model.Account{Name: "NoteGetOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
contact := createTestContact(t, db, account.ID, "Get Contact", "get@example.com")
note := &model.Note{
AccountID: account.ID,
ContactID: contact.ID,
Content: "get me",
}
require.NoError(t, repo.Create(context.Background(), note))
found, err := repo.GetByID(account.ID, contact.ID, note.ID)
assert.NoError(t, err)
assert.Equal(t, note.ID, found.ID)
assert.Equal(t, "get me", found.Content)
}
func TestNoteRepo_GetByID_NotFound(t *testing.T) {
db := setupTestDB(t, &model.Note{})
repo := NewNoteRepo(db)
found, err := repo.GetByID(1, 1, 9999)
assert.Error(t, err)
assert.Nil(t, found)
}
func TestNoteRepo_GetByIDContext(t *testing.T) {
db := setupTestDB(t, &model.Note{})
repo := NewNoteRepo(db)
account := &model.Account{Name: "NoteCtxOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
contact := createTestContact(t, db, account.ID, "Ctx Contact", "ctx@example.com")
note := &model.Note{AccountID: account.ID, ContactID: contact.ID, Content: "ctx"}
require.NoError(t, repo.Create(context.Background(), note))
found, err := repo.GetByIDContext(context.Background(), account.ID, contact.ID, note.ID)
assert.NoError(t, err)
assert.Equal(t, note.ID, found.ID)
}
func TestNoteRepo_CreateNote(t *testing.T) {
db := setupTestDB(t, &model.Note{})
repo := NewNoteRepo(db)
account := &model.Account{Name: "NoteCreateNoteOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
contact := createTestContact(t, db, account.ID, "CreateNote Contact", "createnote@example.com")
note := &model.Note{AccountID: account.ID, ContactID: contact.ID, Content: "created"}
created, err := repo.CreateNote(note)
assert.NoError(t, err)
assert.NotZero(t, created.ID)
assert.Equal(t, "created", created.Content)
}
func TestNoteRepo_Update(t *testing.T) {
db := setupTestDB(t, &model.Note{})
repo := NewNoteRepo(db)
account := &model.Account{Name: "NoteUpdateOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
contact := createTestContact(t, db, account.ID, "Update Contact", "update@example.com")
note := &model.Note{AccountID: account.ID, ContactID: contact.ID, Content: "original"}
require.NoError(t, repo.Create(context.Background(), note))
note.Content = "updated"
updated, err := repo.Update(note)
assert.NoError(t, err)
assert.Equal(t, "updated", updated.Content)
}
func TestNoteRepo_Delete(t *testing.T) {
db := setupTestDB(t, &model.Note{})
repo := NewNoteRepo(db)
account := &model.Account{Name: "NoteDeleteOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
contact := createTestContact(t, db, account.ID, "Delete Contact", "delete@example.com")
note := &model.Note{AccountID: account.ID, ContactID: contact.ID, Content: "delete me"}
require.NoError(t, repo.Create(context.Background(), note))
err := repo.Delete(account.ID, contact.ID, note.ID)
assert.NoError(t, err)
_, err = repo.GetByID(account.ID, contact.ID, note.ID)
assert.Error(t, err)
}
func TestNoteRepo_DeleteContext(t *testing.T) {
db := setupTestDB(t, &model.Note{})
repo := NewNoteRepo(db)
account := &model.Account{Name: "NoteDeleteCtxOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
contact := createTestContact(t, db, account.ID, "DelCtx Contact", "delctx@example.com")
note := &model.Note{AccountID: account.ID, ContactID: contact.ID, Content: "delete ctx"}
require.NoError(t, repo.Create(context.Background(), note))
err := repo.DeleteContext(context.Background(), account.ID, contact.ID, note.ID)
assert.NoError(t, err)
}