mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
Some checks failed
- Updated database models and repositories to replace uint IDs with UUIDs. - Modified test fixtures to generate and use UUIDs for authors, translations, users, and works. - Adjusted mock implementations to align with the new UUID structure. - Ensured all relevant functions and methods are updated to handle UUIDs correctly. - Added necessary imports for UUID handling in various files.
74 lines
2.4 KiB
Go
74 lines
2.4 KiB
Go
package sql_test
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"regexp"
|
|
repo "tercul/internal/data/sql"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/platform/config"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestNewCityRepository(t *testing.T) {
|
|
db, _, err := newMockDb()
|
|
require.NoError(t, err)
|
|
cfg, err := config.LoadConfig()
|
|
require.NoError(t, err)
|
|
repo := repo.NewCityRepository(db, cfg)
|
|
assert.NotNil(t, repo)
|
|
}
|
|
|
|
func TestCityRepository_ListByCountryID(t *testing.T) {
|
|
t.Run("should return cities for a given country id", func(t *testing.T) {
|
|
db, mock, err := newMockDb()
|
|
require.NoError(t, err)
|
|
cfg, err := config.LoadConfig()
|
|
require.NoError(t, err)
|
|
repo := repo.NewCityRepository(db, cfg)
|
|
|
|
countryID := uint(1)
|
|
expectedCities := []domain.City{
|
|
{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}}, Name: "City 1", CountryID: countryID},
|
|
{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}}, Name: "City 2", CountryID: countryID},
|
|
}
|
|
|
|
rows := sqlmock.NewRows([]string{"id", "created_at", "updated_at", "name", "country_id"}).
|
|
AddRow(expectedCities[0].ID, expectedCities[0].CreatedAt, expectedCities[0].UpdatedAt, expectedCities[0].Name, expectedCities[0].CountryID).
|
|
AddRow(expectedCities[1].ID, expectedCities[1].CreatedAt, expectedCities[1].UpdatedAt, expectedCities[1].Name, expectedCities[1].CountryID)
|
|
|
|
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "cities" WHERE country_id = $1`)).
|
|
WithArgs(countryID).
|
|
WillReturnRows(rows)
|
|
|
|
cities, err := repo.ListByCountryID(context.Background(), countryID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedCities, cities)
|
|
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)
|
|
cfg, err := config.LoadConfig()
|
|
require.NoError(t, err)
|
|
repo := repo.NewCityRepository(db, cfg)
|
|
|
|
countryID := uint(1)
|
|
|
|
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "cities" WHERE country_id = $1`)).
|
|
WithArgs(countryID).
|
|
WillReturnError(sql.ErrNoRows)
|
|
|
|
cities, err := repo.ListByCountryID(context.Background(), countryID)
|
|
require.Error(t, err)
|
|
assert.Nil(t, cities)
|
|
assert.NoError(t, mock.ExpectationsWereMet())
|
|
})
|
|
}
|