|
| 1 | +package backups |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "postgresus-backend/internal/features/databases" |
| 6 | + "postgresus-backend/internal/features/notifiers" |
| 7 | + "postgresus-backend/internal/features/storages" |
| 8 | + "postgresus-backend/internal/features/users" |
| 9 | + "postgresus-backend/internal/util/logger" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/google/uuid" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/mock" |
| 16 | +) |
| 17 | + |
| 18 | +func Test_BackupExecuted_NotificationSent(t *testing.T) { |
| 19 | + user := users.GetTestUser() |
| 20 | + storage := storages.CreateTestStorage(user.UserID) |
| 21 | + notifier := notifiers.CreateTestNotifier(user.UserID) |
| 22 | + database := databases.CreateTestDatabase(user.UserID, storage, notifier) |
| 23 | + |
| 24 | + t.Run("BackupFailed_FailNotificationSent", func(t *testing.T) { |
| 25 | + mockNotificationSender := &MockNotificationSender{} |
| 26 | + backupService := &BackupService{ |
| 27 | + databases.GetDatabaseService(), |
| 28 | + storages.GetStorageService(), |
| 29 | + backupRepository, |
| 30 | + notifiers.GetNotifierService(), |
| 31 | + mockNotificationSender, |
| 32 | + &CreateFailedBackupUsecase{}, |
| 33 | + logger.GetLogger(), |
| 34 | + } |
| 35 | + |
| 36 | + // Set up expectations |
| 37 | + mockNotificationSender.On("SendNotification", |
| 38 | + mock.Anything, |
| 39 | + mock.MatchedBy(func(title string) bool { |
| 40 | + return strings.Contains(title, "❌ Backup failed") |
| 41 | + }), |
| 42 | + mock.MatchedBy(func(message string) bool { |
| 43 | + return strings.Contains(message, "backup failed") |
| 44 | + }), |
| 45 | + ).Once() |
| 46 | + |
| 47 | + backupService.MakeBackup(database.ID) |
| 48 | + |
| 49 | + // Verify all expectations were met |
| 50 | + mockNotificationSender.AssertExpectations(t) |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("BackupSuccess_SuccessNotificationSent", func(t *testing.T) { |
| 54 | + mockNotificationSender := &MockNotificationSender{} |
| 55 | + |
| 56 | + // Set up expectations |
| 57 | + mockNotificationSender.On("SendNotification", |
| 58 | + mock.Anything, |
| 59 | + mock.MatchedBy(func(title string) bool { |
| 60 | + return strings.Contains(title, "✅ Backup completed") |
| 61 | + }), |
| 62 | + mock.MatchedBy(func(message string) bool { |
| 63 | + return strings.Contains(message, "Backup completed successfully") |
| 64 | + }), |
| 65 | + ).Once() |
| 66 | + |
| 67 | + backupService := &BackupService{ |
| 68 | + databases.GetDatabaseService(), |
| 69 | + storages.GetStorageService(), |
| 70 | + backupRepository, |
| 71 | + notifiers.GetNotifierService(), |
| 72 | + mockNotificationSender, |
| 73 | + &CreateSuccessBackupUsecase{}, |
| 74 | + logger.GetLogger(), |
| 75 | + } |
| 76 | + |
| 77 | + backupService.MakeBackup(database.ID) |
| 78 | + |
| 79 | + // Verify all expectations were met |
| 80 | + mockNotificationSender.AssertExpectations(t) |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("BackupSuccess_VerifyNotificationContent", func(t *testing.T) { |
| 84 | + mockNotificationSender := &MockNotificationSender{} |
| 85 | + backupService := &BackupService{ |
| 86 | + databases.GetDatabaseService(), |
| 87 | + storages.GetStorageService(), |
| 88 | + backupRepository, |
| 89 | + notifiers.GetNotifierService(), |
| 90 | + mockNotificationSender, |
| 91 | + &CreateSuccessBackupUsecase{}, |
| 92 | + logger.GetLogger(), |
| 93 | + } |
| 94 | + |
| 95 | + // capture arguments |
| 96 | + var capturedNotifier *notifiers.Notifier |
| 97 | + var capturedTitle string |
| 98 | + var capturedMessage string |
| 99 | + |
| 100 | + mockNotificationSender.On("SendNotification", |
| 101 | + mock.Anything, |
| 102 | + mock.AnythingOfType("string"), |
| 103 | + mock.AnythingOfType("string"), |
| 104 | + ).Run(func(args mock.Arguments) { |
| 105 | + capturedNotifier = args.Get(0).(*notifiers.Notifier) |
| 106 | + capturedTitle = args.Get(1).(string) |
| 107 | + capturedMessage = args.Get(2).(string) |
| 108 | + }).Once() |
| 109 | + |
| 110 | + backupService.MakeBackup(database.ID) |
| 111 | + |
| 112 | + // Verify expectations were met |
| 113 | + mockNotificationSender.AssertExpectations(t) |
| 114 | + |
| 115 | + // Additional detailed assertions |
| 116 | + assert.Contains(t, capturedTitle, "✅ Backup completed") |
| 117 | + assert.Contains(t, capturedTitle, database.Name) |
| 118 | + assert.Contains(t, capturedMessage, "Backup completed successfully") |
| 119 | + assert.Contains(t, capturedMessage, "10.00 MB") |
| 120 | + assert.Equal(t, notifier.ID, capturedNotifier.ID) |
| 121 | + }) |
| 122 | +} |
| 123 | + |
| 124 | +type CreateFailedBackupUsecase struct { |
| 125 | +} |
| 126 | + |
| 127 | +func (uc *CreateFailedBackupUsecase) Execute( |
| 128 | + backupID uuid.UUID, |
| 129 | + database *databases.Database, |
| 130 | + storage *storages.Storage, |
| 131 | + backupProgressListener func( |
| 132 | + completedMBs float64, |
| 133 | + ), |
| 134 | +) error { |
| 135 | + backupProgressListener(10) // Assume we completed 10MB |
| 136 | + return errors.New("backup failed") |
| 137 | +} |
| 138 | + |
| 139 | +type CreateSuccessBackupUsecase struct { |
| 140 | +} |
| 141 | + |
| 142 | +func (uc *CreateSuccessBackupUsecase) Execute( |
| 143 | + backupID uuid.UUID, |
| 144 | + database *databases.Database, |
| 145 | + storage *storages.Storage, |
| 146 | + backupProgressListener func( |
| 147 | + completedMBs float64, |
| 148 | + ), |
| 149 | +) error { |
| 150 | + backupProgressListener(10) // Assume we completed 10MB |
| 151 | + return nil |
| 152 | +} |
0 commit comments