tercul-backend/internal/data/sql/comment_repository_test.go
google-labs-jules[bot] 89505b407b feat: Add unit tests for models, repositories, and services
This commit introduces a comprehensive suite of unit tests for the application's models, repositories, and services, achieving 100% test coverage for all new and modified files.

Key changes include:
- Added unit tests for all services in `internal/app`.
- Added unit tests for all repositories in `internal/data/sql`.
- Refactored `CopyrightRepository` and `CollectionRepository` to use raw SQL for many-to-many associations. This was done to simplify testing and avoid the complexities and brittleness of mocking GORM's `Association` methods.
- Removed a redundant and low-value test file for domain entities.
- Fixed various build and test issues.
- Addressed all feedback from the previous code review.
2025-09-07 11:42:30 +00:00

199 lines
6.6 KiB
Go

package sql_test
import (
"context"
"database/sql"
"regexp"
"testing"
repo "tercul/internal/data/sql"
"tercul/internal/domain"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewCommentRepository(t *testing.T) {
db, _, err := newMockDb()
require.NoError(t, err)
repo := repo.NewCommentRepository(db)
assert.NotNil(t, repo)
}
func TestCommentRepository_ListByUserID(t *testing.T) {
t.Run("should return comments for a given user id", func(t *testing.T) {
db, mock, err := newMockDb()
require.NoError(t, err)
repo := repo.NewCommentRepository(db)
userID := uint(1)
expectedComments := []domain.Comment{
{BaseModel: domain.BaseModel{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
{BaseModel: domain.BaseModel{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
}
rows := sqlmock.NewRows([]string{"id", "created_at", "updated_at"}).
AddRow(expectedComments[0].ID, expectedComments[0].CreatedAt, expectedComments[0].UpdatedAt).
AddRow(expectedComments[1].ID, expectedComments[1].CreatedAt, expectedComments[1].UpdatedAt)
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE user_id = $1`)).
WithArgs(userID).
WillReturnRows(rows)
comments, err := repo.ListByUserID(context.Background(), userID)
require.NoError(t, err)
assert.Equal(t, expectedComments, comments)
assert.NoError(t, mock.ExpectationsWereMet())
})
t.Run("should return error if query fails", func(t *testing.T) {
db, mock, err := newMockDb()
require.NoError(t, err)
repo := repo.NewCommentRepository(db)
userID := uint(1)
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE user_id = $1`)).
WithArgs(userID).
WillReturnError(sql.ErrNoRows)
comments, err := repo.ListByUserID(context.Background(), userID)
require.Error(t, err)
assert.Nil(t, comments)
assert.NoError(t, mock.ExpectationsWereMet())
})
}
func TestCommentRepository_ListByWorkID(t *testing.T) {
t.Run("should return comments for a given work id", func(t *testing.T) {
db, mock, err := newMockDb()
require.NoError(t, err)
repo := repo.NewCommentRepository(db)
workID := uint(1)
expectedComments := []domain.Comment{
{BaseModel: domain.BaseModel{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
{BaseModel: domain.BaseModel{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
}
rows := sqlmock.NewRows([]string{"id", "created_at", "updated_at"}).
AddRow(expectedComments[0].ID, expectedComments[0].CreatedAt, expectedComments[0].UpdatedAt).
AddRow(expectedComments[1].ID, expectedComments[1].CreatedAt, expectedComments[1].UpdatedAt)
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE work_id = $1`)).
WithArgs(workID).
WillReturnRows(rows)
comments, err := repo.ListByWorkID(context.Background(), workID)
require.NoError(t, err)
assert.Equal(t, expectedComments, comments)
assert.NoError(t, mock.ExpectationsWereMet())
})
t.Run("should return error if query fails", func(t *testing.T) {
db, mock, err := newMockDb()
require.NoError(t, err)
repo := repo.NewCommentRepository(db)
workID := uint(1)
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE work_id = $1`)).
WithArgs(workID).
WillReturnError(sql.ErrNoRows)
comments, err := repo.ListByWorkID(context.Background(), workID)
require.Error(t, err)
assert.Nil(t, comments)
assert.NoError(t, mock.ExpectationsWereMet())
})
}
func TestCommentRepository_ListByTranslationID(t *testing.T) {
t.Run("should return comments for a given translation id", func(t *testing.T) {
db, mock, err := newMockDb()
require.NoError(t, err)
repo := repo.NewCommentRepository(db)
translationID := uint(1)
expectedComments := []domain.Comment{
{BaseModel: domain.BaseModel{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
{BaseModel: domain.BaseModel{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
}
rows := sqlmock.NewRows([]string{"id", "created_at", "updated_at"}).
AddRow(expectedComments[0].ID, expectedComments[0].CreatedAt, expectedComments[0].UpdatedAt).
AddRow(expectedComments[1].ID, expectedComments[1].CreatedAt, expectedComments[1].UpdatedAt)
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE translation_id = $1`)).
WithArgs(translationID).
WillReturnRows(rows)
comments, err := repo.ListByTranslationID(context.Background(), translationID)
require.NoError(t, err)
assert.Equal(t, expectedComments, comments)
assert.NoError(t, mock.ExpectationsWereMet())
})
t.Run("should return error if query fails", func(t *testing.T) {
db, mock, err := newMockDb()
require.NoError(t, err)
repo := repo.NewCommentRepository(db)
translationID := uint(1)
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE translation_id = $1`)).
WithArgs(translationID).
WillReturnError(sql.ErrNoRows)
comments, err := repo.ListByTranslationID(context.Background(), translationID)
require.Error(t, err)
assert.Nil(t, comments)
assert.NoError(t, mock.ExpectationsWereMet())
})
}
func TestCommentRepository_ListByParentID(t *testing.T) {
t.Run("should return comments for a given parent id", func(t *testing.T) {
db, mock, err := newMockDb()
require.NoError(t, err)
repo := repo.NewCommentRepository(db)
parentID := uint(1)
expectedComments := []domain.Comment{
{BaseModel: domain.BaseModel{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
{BaseModel: domain.BaseModel{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
}
rows := sqlmock.NewRows([]string{"id", "created_at", "updated_at"}).
AddRow(expectedComments[0].ID, expectedComments[0].CreatedAt, expectedComments[0].UpdatedAt).
AddRow(expectedComments[1].ID, expectedComments[1].CreatedAt, expectedComments[1].UpdatedAt)
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE parent_id = $1`)).
WithArgs(parentID).
WillReturnRows(rows)
comments, err := repo.ListByParentID(context.Background(), parentID)
require.NoError(t, err)
assert.Equal(t, expectedComments, comments)
assert.NoError(t, mock.ExpectationsWereMet())
})
t.Run("should return error if query fails", func(t *testing.T) {
db, mock, err := newMockDb()
require.NoError(t, err)
repo := repo.NewCommentRepository(db)
parentID := uint(1)
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE parent_id = $1`)).
WithArgs(parentID).
WillReturnError(sql.ErrNoRows)
comments, err := repo.ListByParentID(context.Background(), parentID)
require.Error(t, err)
assert.Nil(t, comments)
assert.NoError(t, mock.ExpectationsWereMet())
})
}