TestDeleteOldAlertsHistory tests the deleteOldAlertsHistory function
(t *testing.T)
| 196 | |
| 197 | // TestDeleteOldAlertsHistory tests the deleteOldAlertsHistory function |
| 198 | func TestDeleteOldAlertsHistory(t *testing.T) { |
| 199 | hub, err := tests.NewTestHub(t.TempDir()) |
| 200 | require.NoError(t, err) |
| 201 | defer hub.Cleanup() |
| 202 | |
| 203 | // Create test users |
| 204 | user1, err := tests.CreateUser(hub, "user1@example.com", "testtesttest") |
| 205 | require.NoError(t, err) |
| 206 | |
| 207 | user2, err := tests.CreateUser(hub, "user2@example.com", "testtesttest") |
| 208 | require.NoError(t, err) |
| 209 | |
| 210 | system, err := tests.CreateRecord(hub, "systems", map[string]any{ |
| 211 | "name": "test-system", |
| 212 | "host": "localhost", |
| 213 | "port": "45876", |
| 214 | "status": "up", |
| 215 | "users": []string{user1.Id, user2.Id}, |
| 216 | }) |
| 217 | require.NoError(t, err) |
| 218 | now := time.Now().UTC() |
| 219 | |
| 220 | testCases := []struct { |
| 221 | name string |
| 222 | user *core.Record |
| 223 | alertCount int |
| 224 | countToKeep int |
| 225 | countBeforeDeletion int |
| 226 | expectedAfterDeletion int |
| 227 | description string |
| 228 | }{ |
| 229 | { |
| 230 | name: "User with few alerts (below threshold)", |
| 231 | user: user1, |
| 232 | alertCount: 100, |
| 233 | countToKeep: 50, |
| 234 | countBeforeDeletion: 150, |
| 235 | expectedAfterDeletion: 100, // No deletion because below threshold |
| 236 | description: "User with alerts below countBeforeDeletion should not have any deleted", |
| 237 | }, |
| 238 | { |
| 239 | name: "User with many alerts (above threshold)", |
| 240 | user: user2, |
| 241 | alertCount: 300, |
| 242 | countToKeep: 100, |
| 243 | countBeforeDeletion: 200, |
| 244 | expectedAfterDeletion: 100, // Should be trimmed to countToKeep |
| 245 | description: "User with alerts above countBeforeDeletion should be trimmed to countToKeep", |
| 246 | }, |
| 247 | } |
| 248 | |
| 249 | for _, tc := range testCases { |
| 250 | t.Run(tc.name, func(t *testing.T) { |
| 251 | // Create alerts for this user |
| 252 | for i := 0; i < tc.alertCount; i++ { |
| 253 | _, err := tests.CreateRecord(hub, "alerts_history", map[string]any{ |
| 254 | "user": tc.user.Id, |
| 255 | "name": "CPU", |
nothing calls this directly
no test coverage detected