151 lines
5.7 KiB
Go
151 lines
5.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
func TestFolderRepo_Create(t *testing.T) {
|
|
db := setupTestDB(t, &model.Folder{}, &model.Portal{}, &model.Category{})
|
|
repo := NewFolderRepo(db)
|
|
|
|
account := &model.Account{Name: "FolderOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
portal := &model.Portal{AccountID: account.ID, Name: "Portal", Slug: "portal-slug"}
|
|
require.NoError(t, db.Create(portal).Error)
|
|
category := &model.Category{AccountID: account.ID, PortalID: portal.ID, Name: "Cat", Slug: "cat-slug", Locale: "en"}
|
|
require.NoError(t, db.Create(category).Error)
|
|
|
|
folder := &model.Folder{
|
|
AccountID: account.ID,
|
|
PortalID: portal.ID,
|
|
CategoryID: category.ID,
|
|
Name: "Test Folder",
|
|
}
|
|
err := repo.Create(context.Background(), folder)
|
|
assert.NoError(t, err)
|
|
assert.NotZero(t, folder.ID)
|
|
}
|
|
|
|
func TestFolderRepo_GetByID(t *testing.T) {
|
|
db := setupTestDB(t, &model.Folder{}, &model.Portal{}, &model.Category{})
|
|
repo := NewFolderRepo(db)
|
|
|
|
account := &model.Account{Name: "FolderGetOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
portal := &model.Portal{AccountID: account.ID, Name: "Portal", Slug: "portal-get"}
|
|
require.NoError(t, db.Create(portal).Error)
|
|
category := &model.Category{AccountID: account.ID, PortalID: portal.ID, Name: "Cat", Slug: "cat-get", Locale: "en"}
|
|
require.NoError(t, db.Create(category).Error)
|
|
|
|
folder := &model.Folder{AccountID: account.ID, PortalID: portal.ID, CategoryID: category.ID, Name: "Get Folder"}
|
|
require.NoError(t, repo.Create(context.Background(), folder))
|
|
|
|
found, err := repo.GetByID(context.Background(), folder.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, folder.ID, found.ID)
|
|
assert.Equal(t, "Get Folder", found.Name)
|
|
}
|
|
|
|
func TestFolderRepo_GetByID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.Folder{}, &model.Portal{})
|
|
repo := NewFolderRepo(db)
|
|
|
|
_, err := repo.GetByID(context.Background(), 9999)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestFolderRepo_Update(t *testing.T) {
|
|
db := setupTestDB(t, &model.Folder{}, &model.Portal{}, &model.Category{})
|
|
repo := NewFolderRepo(db)
|
|
|
|
account := &model.Account{Name: "FolderUpdOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
portal := &model.Portal{AccountID: account.ID, Name: "Portal", Slug: "portal-upd"}
|
|
require.NoError(t, db.Create(portal).Error)
|
|
category := &model.Category{AccountID: account.ID, PortalID: portal.ID, Name: "Cat", Slug: "cat-upd", Locale: "en"}
|
|
require.NoError(t, db.Create(category).Error)
|
|
|
|
folder := &model.Folder{AccountID: account.ID, PortalID: portal.ID, CategoryID: category.ID, Name: "Original"}
|
|
require.NoError(t, repo.Create(context.Background(), folder))
|
|
|
|
folder.Name = "Updated"
|
|
err := repo.Update(context.Background(), folder)
|
|
assert.NoError(t, err)
|
|
|
|
found, _ := repo.GetByID(context.Background(), folder.ID)
|
|
assert.Equal(t, "Updated", found.Name)
|
|
}
|
|
|
|
func TestFolderRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t, &model.Folder{}, &model.Portal{}, &model.Category{})
|
|
repo := NewFolderRepo(db)
|
|
|
|
account := &model.Account{Name: "FolderDelOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
portal := &model.Portal{AccountID: account.ID, Name: "Portal", Slug: "portal-del"}
|
|
require.NoError(t, db.Create(portal).Error)
|
|
category := &model.Category{AccountID: account.ID, PortalID: portal.ID, Name: "Cat", Slug: "cat-del", Locale: "en"}
|
|
require.NoError(t, db.Create(category).Error)
|
|
|
|
folder := &model.Folder{AccountID: account.ID, PortalID: portal.ID, CategoryID: category.ID, Name: "Delete"}
|
|
require.NoError(t, repo.Create(context.Background(), folder))
|
|
|
|
err := repo.Delete(context.Background(), folder.ID)
|
|
assert.NoError(t, err)
|
|
|
|
_, err = repo.GetByID(context.Background(), folder.ID)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestFolderRepo_FindByPortalID(t *testing.T) {
|
|
db := setupTestDB(t, &model.Folder{}, &model.Portal{}, &model.Category{})
|
|
repo := NewFolderRepo(db)
|
|
|
|
account := &model.Account{Name: "FolderFPIOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
portal := &model.Portal{AccountID: account.ID, Name: "Portal", Slug: "portal-fpi"}
|
|
require.NoError(t, db.Create(portal).Error)
|
|
category := &model.Category{AccountID: account.ID, PortalID: portal.ID, Name: "Cat", Slug: "cat-fpi", Locale: "en"}
|
|
require.NoError(t, db.Create(category).Error)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
require.NoError(t, repo.Create(context.Background(), &model.Folder{
|
|
AccountID: account.ID, PortalID: portal.ID, CategoryID: category.ID, Name: "Folder",
|
|
}))
|
|
}
|
|
|
|
folders, count, err := repo.FindByPortalID(context.Background(), portal.ID, 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(3), count)
|
|
assert.Len(t, folders, 3)
|
|
}
|
|
|
|
func TestFolderRepo_FindByCategoryID(t *testing.T) {
|
|
db := setupTestDB(t, &model.Folder{}, &model.Portal{}, &model.Category{})
|
|
repo := NewFolderRepo(db)
|
|
|
|
account := &model.Account{Name: "FolderFCIOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
portal := &model.Portal{AccountID: account.ID, Name: "Portal", Slug: "portal-fci"}
|
|
require.NoError(t, db.Create(portal).Error)
|
|
category := &model.Category{AccountID: account.ID, PortalID: portal.ID, Name: "Cat", Slug: "cat-fci", Locale: "en"}
|
|
require.NoError(t, db.Create(category).Error)
|
|
|
|
for i := 0; i < 2; i++ {
|
|
require.NoError(t, repo.Create(context.Background(), &model.Folder{
|
|
AccountID: account.ID, PortalID: portal.ID, CategoryID: category.ID, Name: "Folder",
|
|
}))
|
|
}
|
|
|
|
folders, count, err := repo.FindByCategoryID(context.Background(), category.ID, 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(2), count)
|
|
assert.Len(t, folders, 2)
|
|
}
|