|
| 1 | +package migrations |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/DATA-DOG/go-sqlmock" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/Notifuse/notifuse/config" |
| 12 | + "github.com/Notifuse/notifuse/internal/domain" |
| 13 | +) |
| 14 | + |
| 15 | +func TestV31Migration_GetMajorVersion(t *testing.T) { |
| 16 | + m := &V31Migration{} |
| 17 | + assert.Equal(t, 31.0, m.GetMajorVersion()) |
| 18 | +} |
| 19 | + |
| 20 | +func TestV31Migration_HasSystemUpdate(t *testing.T) { |
| 21 | + m := &V31Migration{} |
| 22 | + assert.False(t, m.HasSystemUpdate()) |
| 23 | +} |
| 24 | + |
| 25 | +func TestV31Migration_HasWorkspaceUpdate(t *testing.T) { |
| 26 | + m := &V31Migration{} |
| 27 | + assert.True(t, m.HasWorkspaceUpdate()) |
| 28 | +} |
| 29 | + |
| 30 | +func TestV31Migration_ShouldRestartServer(t *testing.T) { |
| 31 | + m := &V31Migration{} |
| 32 | + assert.False(t, m.ShouldRestartServer()) |
| 33 | +} |
| 34 | + |
| 35 | +func TestV31Migration_UpdateSystem_NoOp(t *testing.T) { |
| 36 | + m := &V31Migration{} |
| 37 | + // System-side has nothing to do — just verify it returns nil cleanly. |
| 38 | + assert.NoError(t, m.UpdateSystem(context.Background(), &config.Config{}, nil)) |
| 39 | +} |
| 40 | + |
| 41 | +func TestV31Migration_UpdateWorkspace_Success(t *testing.T) { |
| 42 | + db, mock, err := sqlmock.New() |
| 43 | + require.NoError(t, err) |
| 44 | + defer db.Close() |
| 45 | + |
| 46 | + mock.ExpectExec(`CREATE OR REPLACE FUNCTION queue_contact_for_segment_recomputation`). |
| 47 | + WillReturnResult(sqlmock.NewResult(0, 0)) |
| 48 | + |
| 49 | + m := &V31Migration{} |
| 50 | + err = m.UpdateWorkspace(context.Background(), &config.Config{}, |
| 51 | + &domain.Workspace{ID: "ws_test"}, db) |
| 52 | + assert.NoError(t, err) |
| 53 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 54 | +} |
| 55 | + |
| 56 | +func TestV31Migration_UpdateWorkspace_Error(t *testing.T) { |
| 57 | + db, mock, err := sqlmock.New() |
| 58 | + require.NoError(t, err) |
| 59 | + defer db.Close() |
| 60 | + |
| 61 | + mock.ExpectExec(`CREATE OR REPLACE FUNCTION queue_contact_for_segment_recomputation`). |
| 62 | + WillReturnError(assert.AnError) |
| 63 | + |
| 64 | + m := &V31Migration{} |
| 65 | + err = m.UpdateWorkspace(context.Background(), &config.Config{}, |
| 66 | + &domain.Workspace{ID: "ws_test"}, db) |
| 67 | + require.Error(t, err) |
| 68 | + assert.Contains(t, err.Error(), "failed to update queue_contact_for_segment_recomputation") |
| 69 | + assert.Contains(t, err.Error(), "ws_test") |
| 70 | +} |
| 71 | + |
| 72 | +func TestV31Migration_Registered(t *testing.T) { |
| 73 | + // Verify init() registered the migration so the runner picks it up. |
| 74 | + for _, m := range GetRegisteredMigrations() { |
| 75 | + if m.GetMajorVersion() == 31.0 { |
| 76 | + return |
| 77 | + } |
| 78 | + } |
| 79 | + t.Fatal("V31Migration not registered") |
| 80 | +} |
0 commit comments