Files
Charon/backend/internal/models/notification_test.go
T
GitHub Actions e299aa6b52 feat(tests): enhance test coverage and error handling across various components
- Added a test case in CrowdSecConfig to show improved error message when preset is not cached.
- Introduced a new test suite for the Dashboard component, verifying counts and health status.
- Updated SMTPSettings tests to utilize a shared render function and added tests for backend validation errors.
- Modified Security.audit tests to improve input handling and removed redundant export failure test.
- Refactored Security tests to remove export functionality and ensure correct rendering of components.
- Enhanced UsersPage tests with new scenarios for updating user permissions and manual invite link flow.
- Created a new utility for rendering components with a QueryClient and MemoryRouter for better test isolation.
- Updated go-test-coverage script to improve error handling and coverage reporting.
2025-12-11 00:26:07 +00:00

48 lines
1.3 KiB
Go

package models
import (
"testing"
"github.com/stretchr/testify/assert"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func TestNotification_BeforeCreate(t *testing.T) {
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
assert.NoError(t, err)
db.AutoMigrate(&Notification{})
// Case 1: ID is empty, should be generated
n1 := &Notification{Title: "Test", Message: "Test Message"}
err = db.Create(n1).Error
assert.NoError(t, err)
assert.NotEmpty(t, n1.ID)
// Case 2: ID is provided, should be kept
id := "123e4567-e89b-12d3-a456-426614174000"
n2 := &Notification{ID: id, Title: "Test 2", Message: "Test Message 2"}
err = db.Create(n2).Error
assert.NoError(t, err)
assert.Equal(t, id, n2.ID)
}
func TestNotificationConfig_BeforeCreate(t *testing.T) {
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
assert.NoError(t, err)
db.AutoMigrate(&NotificationConfig{})
// Case 1: ID is empty, should be generated
nc1 := &NotificationConfig{Enabled: true, MinLogLevel: "error"}
err = db.Create(nc1).Error
assert.NoError(t, err)
assert.NotEmpty(t, nc1.ID)
// Case 2: ID is provided, should be kept
id := "custom-config-id"
nc2 := &NotificationConfig{ID: id, Enabled: false, MinLogLevel: "warn"}
err = db.Create(nc2).Error
assert.NoError(t, err)
assert.Equal(t, id, nc2.ID)
}