300 lines
9.2 KiB
Go
300 lines
9.2 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 PlatformApp
|
|
func newTestPlatformApp(accountID uint, name string) *model.PlatformApp {
|
|
uid := &accountID // *uint
|
|
active := true
|
|
return &model.PlatformApp{
|
|
Name: name,
|
|
AccountID: uid,
|
|
Description: "Test platform app",
|
|
Type: "api",
|
|
Status: "active",
|
|
Active: &active,
|
|
}
|
|
}
|
|
|
|
// ========== Create ==========
|
|
|
|
func TestPlatformAppRepo_Create(t *testing.T) {
|
|
db := setupTestDB(t, &model.PlatformApp{}, &model.Account{})
|
|
repo := NewPlatformAppRepo(db)
|
|
|
|
// Create a test account first (for FK constraint if PG)
|
|
account := &model.Account{Name: "TestAccount"}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
app := newTestPlatformApp(account.ID, "MyApp")
|
|
err := repo.Create(context.Background(), app)
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, app.ID, "ID should be set after Create")
|
|
assert.Equal(t, "MyApp", app.Name)
|
|
}
|
|
|
|
// ========== GetByID ==========
|
|
|
|
func TestPlatformAppRepo_GetByID(t *testing.T) {
|
|
db := setupTestDB(t, &model.PlatformApp{}, &model.Account{})
|
|
repo := NewPlatformAppRepo(db)
|
|
|
|
account := &model.Account{Name: "TestAccount"}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
app := newTestPlatformApp(account.ID, "FindByIDApp")
|
|
err := repo.Create(context.Background(), app)
|
|
require.NoError(t, err)
|
|
|
|
found, err := repo.GetByID(context.Background(), app.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, app.ID, found.ID)
|
|
assert.Equal(t, app.Name, found.Name)
|
|
}
|
|
|
|
func TestPlatformAppRepo_GetByID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.PlatformApp{})
|
|
repo := NewPlatformAppRepo(db)
|
|
|
|
_, err := repo.GetByID(context.Background(), 99999)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
// ========== Update ==========
|
|
|
|
func TestPlatformAppRepo_Update(t *testing.T) {
|
|
db := setupTestDB(t, &model.PlatformApp{}, &model.Account{})
|
|
repo := NewPlatformAppRepo(db)
|
|
|
|
account := &model.Account{Name: "TestAccount"}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
app := newTestPlatformApp(account.ID, "OriginalName")
|
|
err := repo.Create(context.Background(), app)
|
|
require.NoError(t, err)
|
|
|
|
app.Name = "UpdatedName"
|
|
app.Description = "Updated description"
|
|
app.Status = "disabled"
|
|
err = repo.Update(context.Background(), app)
|
|
require.NoError(t, err)
|
|
|
|
found, err := repo.GetByID(context.Background(), app.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "UpdatedName", found.Name)
|
|
assert.Equal(t, "Updated description", found.Description)
|
|
assert.Equal(t, "disabled", found.Status)
|
|
}
|
|
|
|
// ========== Delete ==========
|
|
|
|
func TestPlatformAppRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t, &model.PlatformApp{}, &model.Account{})
|
|
repo := NewPlatformAppRepo(db)
|
|
|
|
account := &model.Account{Name: "TestAccount"}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
app := newTestPlatformApp(account.ID, "DeleteApp")
|
|
err := repo.Create(context.Background(), app)
|
|
require.NoError(t, err)
|
|
|
|
err = repo.Delete(context.Background(), app.ID)
|
|
require.NoError(t, err)
|
|
|
|
// Soft delete — should not be findable via normal query
|
|
_, err = repo.GetByID(context.Background(), app.ID)
|
|
assert.Error(t, err, "soft-deleted record should not be found")
|
|
}
|
|
|
|
func TestPlatformAppRepo_Delete_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.PlatformApp{})
|
|
repo := NewPlatformAppRepo(db)
|
|
|
|
err := repo.Delete(context.Background(), 99999)
|
|
// GORM soft delete on non-existent record doesn't error (0 rows affected)
|
|
// but query-based may return error — behavior depends on DB dialect
|
|
// We just verify no panic
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
// ========== FindAll ==========
|
|
|
|
func TestPlatformAppRepo_FindAll(t *testing.T) {
|
|
db := setupTestDB(t, &model.PlatformApp{}, &model.Account{})
|
|
repo := NewPlatformAppRepo(db)
|
|
|
|
account := &model.Account{Name: "TestAccount"}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
// Create multiple apps
|
|
for i := 0; i < 5; i++ {
|
|
app := newTestPlatformApp(account.ID, "App"+string(rune('A'+i)))
|
|
require.NoError(t, repo.Create(context.Background(), app))
|
|
}
|
|
|
|
apps, count, err := repo.FindAll(context.Background(), 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(5), count)
|
|
assert.Len(t, apps, 5)
|
|
}
|
|
|
|
func TestPlatformAppRepo_FindAll_Pagination(t *testing.T) {
|
|
db := setupTestDB(t, &model.PlatformApp{}, &model.Account{})
|
|
repo := NewPlatformAppRepo(db)
|
|
|
|
account := &model.Account{Name: "TestAccount"}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
app := newTestPlatformApp(account.ID, "PagedApp"+string(rune('0'+i)))
|
|
require.NoError(t, repo.Create(context.Background(), app))
|
|
}
|
|
|
|
// Page 1: offset 0, limit 3
|
|
apps, count, err := repo.FindAll(context.Background(), 0, 3)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(10), count)
|
|
assert.Len(t, apps, 3)
|
|
|
|
// Page 2: offset 3, limit 3
|
|
apps, count, err = repo.FindAll(context.Background(), 3, 3)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(10), count)
|
|
assert.Len(t, apps, 3)
|
|
}
|
|
|
|
// ========== FindByAccountID ==========
|
|
|
|
func TestPlatformAppRepo_FindByAccountID(t *testing.T) {
|
|
db := setupTestDB(t, &model.PlatformApp{}, &model.Account{})
|
|
repo := NewPlatformAppRepo(db)
|
|
|
|
account1 := &model.Account{Name: "Account1"}
|
|
require.NoError(t, db.Create(account1).Error)
|
|
account2 := &model.Account{Name: "Account2"}
|
|
require.NoError(t, db.Create(account2).Error)
|
|
|
|
// Create apps for account1
|
|
for i := 0; i < 3; i++ {
|
|
app := newTestPlatformApp(account1.ID, "Acc1App"+string(rune('A'+i)))
|
|
require.NoError(t, repo.Create(context.Background(), app))
|
|
}
|
|
|
|
// Create apps for account2
|
|
for i := 0; i < 2; i++ {
|
|
app := newTestPlatformApp(account2.ID, "Acc2App"+string(rune('A'+i)))
|
|
require.NoError(t, repo.Create(context.Background(), app))
|
|
}
|
|
|
|
// Find for account1
|
|
apps, count, err := repo.FindByAccountID(context.Background(), account1.ID, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(3), count)
|
|
assert.Len(t, apps, 3)
|
|
|
|
// Find for account2
|
|
apps, count, err = repo.FindByAccountID(context.Background(), account2.ID, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(2), count)
|
|
assert.Len(t, apps, 2)
|
|
}
|
|
|
|
// ========== FindActive ==========
|
|
|
|
func TestPlatformAppRepo_FindActive(t *testing.T) {
|
|
db := setupTestDB(t, &model.PlatformApp{}, &model.Account{})
|
|
repo := NewPlatformAppRepo(db)
|
|
|
|
account := &model.Account{Name: "TestAccount"}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
// Create active apps
|
|
for i := 0; i < 3; i++ {
|
|
app := newTestPlatformApp(account.ID, "ActiveApp"+string(rune('A'+i)))
|
|
require.NoError(t, repo.Create(context.Background(), app))
|
|
}
|
|
|
|
// Create a disabled app
|
|
disabled := newTestPlatformApp(account.ID, "DisabledApp")
|
|
disabled.Status = "disabled"
|
|
disabledVal := false
|
|
disabled.Active = &disabledVal
|
|
require.NoError(t, repo.Create(context.Background(), disabled))
|
|
|
|
apps, count, err := repo.FindActive(context.Background(), 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(3), count)
|
|
assert.Len(t, apps, 3)
|
|
}
|
|
|
|
// ========== SearchByName ==========
|
|
|
|
func TestPlatformAppRepo_SearchByName_WithQuery(t *testing.T) {
|
|
db := setupTestDB(t, &model.PlatformApp{}, &model.Account{})
|
|
repo := NewPlatformAppRepo(db)
|
|
|
|
account := &model.Account{Name: "TestAccount"}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
app1 := newTestPlatformApp(account.ID, "AlphaIntegration")
|
|
require.NoError(t, repo.Create(context.Background(), app1))
|
|
app2 := newTestPlatformApp(account.ID, "BetaService")
|
|
require.NoError(t, repo.Create(context.Background(), app2))
|
|
app3 := newTestPlatformApp(account.ID, "GammaAlpha")
|
|
require.NoError(t, repo.Create(context.Background(), app3))
|
|
|
|
apps, count, err := repo.SearchByName(context.Background(), "Alpha", 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(2), count)
|
|
assert.Len(t, apps, 2)
|
|
}
|
|
|
|
func TestPlatformAppRepo_SearchByName_NoMatch(t *testing.T) {
|
|
db := setupTestDB(t, &model.PlatformApp{}, &model.Account{})
|
|
repo := NewPlatformAppRepo(db)
|
|
|
|
account := &model.Account{Name: "TestAccount"}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
app := newTestPlatformApp(account.ID, "SomeApp")
|
|
require.NoError(t, repo.Create(context.Background(), app))
|
|
|
|
apps, count, err := repo.SearchByName(context.Background(), "NonExistent", 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(0), count)
|
|
assert.Len(t, apps, 0)
|
|
}
|
|
|
|
// ========== SearchByNameByAccount ==========
|
|
|
|
func TestPlatformAppRepo_SearchByNameByAccount_WithQuery(t *testing.T) {
|
|
db := setupTestDB(t, &model.PlatformApp{}, &model.Account{})
|
|
repo := NewPlatformAppRepo(db)
|
|
|
|
account1 := &model.Account{Name: "Account1"}
|
|
require.NoError(t, db.Create(account1).Error)
|
|
account2 := &model.Account{Name: "Account2"}
|
|
require.NoError(t, db.Create(account2).Error)
|
|
|
|
app1 := newTestPlatformApp(account1.ID, "SearchAlpha")
|
|
require.NoError(t, repo.Create(context.Background(), app1))
|
|
app2 := newTestPlatformApp(account1.ID, "SearchBeta")
|
|
require.NoError(t, repo.Create(context.Background(), app2))
|
|
app3 := newTestPlatformApp(account2.ID, "OtherAlpha")
|
|
require.NoError(t, repo.Create(context.Background(), app3))
|
|
|
|
apps, count, err := repo.SearchByNameByAccount(context.Background(), account1.ID, "Alpha", 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(1), count)
|
|
assert.Len(t, apps, 1)
|
|
assert.Equal(t, "SearchAlpha", apps[0].Name)
|
|
} |