|
| 1 | +package repository |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/DATA-DOG/go-sqlmock" |
| 10 | + "github.com/Notifuse/notifuse/internal/domain/mocks" |
| 11 | + "github.com/golang/mock/gomock" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestTelemetryRepository_GetLastMessageAt(t *testing.T) { |
| 17 | + t.Run("returns last message timestamp", func(t *testing.T) { |
| 18 | + ctrl := gomock.NewController(t) |
| 19 | + defer ctrl.Finish() |
| 20 | + |
| 21 | + db, mock, err := sqlmock.New() |
| 22 | + require.NoError(t, err) |
| 23 | + defer db.Close() |
| 24 | + |
| 25 | + // Mock workspace repository |
| 26 | + mockWorkspaceRepo := mocks.NewMockWorkspaceRepository(ctrl) |
| 27 | + repo := NewTelemetryRepository(mockWorkspaceRepo) |
| 28 | + |
| 29 | + // Expected query uses ORDER BY with LIMIT for performance |
| 30 | + expectedQuery := `SELECT created_at FROM message_history |
| 31 | + WHERE created_at IS NOT NULL |
| 32 | + ORDER BY created_at DESC, id DESC |
| 33 | + LIMIT 1` |
| 34 | + |
| 35 | + expectedTime := time.Date(2023, 12, 25, 15, 30, 0, 0, time.UTC) |
| 36 | + mock.ExpectQuery(expectedQuery). |
| 37 | + WillReturnRows(sqlmock.NewRows([]string{"created_at"}). |
| 38 | + AddRow(expectedTime)) |
| 39 | + |
| 40 | + result, err := repo.GetLastMessageAt(context.Background(), db) |
| 41 | + |
| 42 | + require.NoError(t, err) |
| 43 | + assert.Equal(t, expectedTime.Format(time.RFC3339), result) |
| 44 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("returns empty string when no messages found", func(t *testing.T) { |
| 48 | + ctrl := gomock.NewController(t) |
| 49 | + defer ctrl.Finish() |
| 50 | + |
| 51 | + db, mock, err := sqlmock.New() |
| 52 | + require.NoError(t, err) |
| 53 | + defer db.Close() |
| 54 | + |
| 55 | + mockWorkspaceRepo := mocks.NewMockWorkspaceRepository(ctrl) |
| 56 | + repo := NewTelemetryRepository(mockWorkspaceRepo) |
| 57 | + |
| 58 | + expectedQuery := `SELECT created_at FROM message_history |
| 59 | + WHERE created_at IS NOT NULL |
| 60 | + ORDER BY created_at DESC, id DESC |
| 61 | + LIMIT 1` |
| 62 | + |
| 63 | + mock.ExpectQuery(expectedQuery). |
| 64 | + WillReturnError(sql.ErrNoRows) |
| 65 | + |
| 66 | + result, err := repo.GetLastMessageAt(context.Background(), db) |
| 67 | + |
| 68 | + require.NoError(t, err) |
| 69 | + assert.Equal(t, "", result) |
| 70 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 71 | + }) |
| 72 | + |
| 73 | + t.Run("handles database errors", func(t *testing.T) { |
| 74 | + ctrl := gomock.NewController(t) |
| 75 | + defer ctrl.Finish() |
| 76 | + |
| 77 | + db, mock, err := sqlmock.New() |
| 78 | + require.NoError(t, err) |
| 79 | + defer db.Close() |
| 80 | + |
| 81 | + mockWorkspaceRepo := mocks.NewMockWorkspaceRepository(ctrl) |
| 82 | + repo := NewTelemetryRepository(mockWorkspaceRepo) |
| 83 | + |
| 84 | + expectedQuery := `SELECT created_at FROM message_history |
| 85 | + WHERE created_at IS NOT NULL |
| 86 | + ORDER BY created_at DESC, id DESC |
| 87 | + LIMIT 1` |
| 88 | + |
| 89 | + mock.ExpectQuery(expectedQuery). |
| 90 | + WillReturnError(assert.AnError) |
| 91 | + |
| 92 | + result, err := repo.GetLastMessageAt(context.Background(), db) |
| 93 | + |
| 94 | + assert.Error(t, err) |
| 95 | + assert.Equal(t, "", result) |
| 96 | + assert.Contains(t, err.Error(), "failed to get last message timestamp") |
| 97 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 98 | + }) |
| 99 | +} |
0 commit comments