This commit is contained in:
@@ -15,7 +15,7 @@ type SportHandler struct {
|
||||
}
|
||||
|
||||
func (handler *SportHandler) List(c *gin.Context) {
|
||||
sports, err := handler.Service.List(c.Request.Context())
|
||||
sports, _, err := handler.Service.List(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
||||
@@ -3,9 +3,23 @@ package internal
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"kontor-api-gin/internal/test/testutils"
|
||||
"kontor-api-gin/internal/tests"
|
||||
"kontor-api-gin/internal/tests/testutils"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var TestDB *gorm.DB
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
testutils.InitializeTestSuite(m)
|
||||
}
|
||||
|
||||
func TestRepos(t *testing.T) {
|
||||
suite.Run(t, new(tests.RepositoryTestSuite))
|
||||
}
|
||||
|
||||
func TestServices(t *testing.T) {
|
||||
suite.Run(t, new(tests.ServiceTestSuite))
|
||||
}
|
||||
|
||||
@@ -17,13 +17,21 @@ func NewSportRepository(db *gorm.DB) *SportRepository {
|
||||
return repo
|
||||
}
|
||||
|
||||
func (repository *SportRepository) FindAll(context context.Context) ([]models.Sport, error) {
|
||||
var sports []models.Sport
|
||||
func (repository *SportRepository) FindAll(page int, limit int, context context.Context) (sports []models.Sport, total int64, err error) {
|
||||
offset := (page - 1) * limit
|
||||
|
||||
result := repository.db.Find(&sports)
|
||||
slog.Info("SportRepository#FindAll", "count", result.RowsAffected)
|
||||
|
||||
return sports, nil
|
||||
err = repository.db.
|
||||
Model(&models.Sport{}).
|
||||
Count(&total).
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
Order("id asc").
|
||||
Find(&sports).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
slog.Info("SportRepository#FindAll", "total", total)
|
||||
return
|
||||
}
|
||||
|
||||
func (repository *SportRepository) GetById(context context.Context, id string) (models.Sport, error) {
|
||||
|
||||
@@ -16,9 +16,9 @@ func NewSportService(repo *repository.SportRepository) *SportService {
|
||||
return service
|
||||
}
|
||||
|
||||
func (service *SportService) List(context context.Context) ([]models.Sport, error) {
|
||||
func (service *SportService) List(context context.Context) ([]models.Sport, int64, error) {
|
||||
slog.Info("SportService#List")
|
||||
return service.repository.FindAll(context)
|
||||
return service.repository.FindAll(1, 10, context)
|
||||
}
|
||||
|
||||
func (service *SportService) Get(context context.Context, id string) (models.Sport, error) {
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"kontor-api-gin/internal/models"
|
||||
"kontor-api-gin/internal/repository"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var testDB *gorm.DB
|
||||
|
||||
type RepositoryTestSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func (suite *RepositoryTestSuite) TestSportFindAll() {
|
||||
sportRepository := repository.NewSportRepository(testDB)
|
||||
_, total, err := sportRepository.FindAll(1, 10, context.Background())
|
||||
suite.Nil(err)
|
||||
suite.Equal(int64(0), total)
|
||||
}
|
||||
|
||||
// This will run right before the test starts
|
||||
// and receives the suite and test names as input
|
||||
func (suite *RepositoryTestSuite) BeforeTest(suiteName, testName string) {
|
||||
slog.Info("RepositoryTestSuite#BeforeTest", "suiteName", suiteName, "testName", testName)
|
||||
}
|
||||
|
||||
// This will run after test finishes
|
||||
// and receives the suite and test names as input
|
||||
func (suite *RepositoryTestSuite) AfterTest(suiteName, testName string) {
|
||||
slog.Info("RepositoryTestSuite#AfterTest", "suiteName", suiteName, "testName", testName)
|
||||
}
|
||||
|
||||
// This will run before before the tests in the suite are run
|
||||
func (suite *RepositoryTestSuite) SetupSuite() {
|
||||
slog.Info("RepositoryTestSuite#SetupSuite")
|
||||
// Remove existing test db to ensure clean state
|
||||
os.Remove("test.db")
|
||||
|
||||
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("failed to connect test database")
|
||||
}
|
||||
|
||||
// Auto-migrate models to ensure schema matches
|
||||
db.AutoMigrate(&models.Sport{})
|
||||
|
||||
testDB = db
|
||||
}
|
||||
|
||||
// This will run before before the tests in the suite are run
|
||||
func (suite *RepositoryTestSuite) TearDownSuite() {
|
||||
slog.Info("RepositoryTestSuite#TeardownSuite")
|
||||
}
|
||||
|
||||
// This will run before each test in the suite
|
||||
func (suite *RepositoryTestSuite) SetupTest() {
|
||||
slog.Info("RepositoryTestSuite#SetupTest")
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"kontor-api-gin/internal/models"
|
||||
"kontor-api-gin/internal/repository"
|
||||
"kontor-api-gin/internal/service"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var testServiceDB *gorm.DB
|
||||
|
||||
type ServiceTestSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func (suite *ServiceTestSuite) TestSportServiceList() {
|
||||
sportService := service.NewSportService(repository.NewSportRepository(testServiceDB))
|
||||
sports, total, err := sportService.List(context.Background())
|
||||
suite.Nil(err)
|
||||
suite.Equal(int64(0), total)
|
||||
suite.Equal(0, len(sports))
|
||||
}
|
||||
|
||||
// This will run before before the tests in the suite are run
|
||||
func (suite *ServiceTestSuite) SetupSuite() {
|
||||
slog.Info("RepositoryTestSuite#SetupSuite")
|
||||
// Remove existing test db to ensure clean state
|
||||
os.Remove("test.db")
|
||||
|
||||
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("failed to connect test database")
|
||||
}
|
||||
|
||||
// Auto-migrate models to ensure schema matches
|
||||
db.AutoMigrate(&models.Sport{})
|
||||
|
||||
testServiceDB = db
|
||||
}
|
||||
@@ -1,27 +1,29 @@
|
||||
package testutils
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func SetupTestDatabase() {
|
||||
|
||||
slog.Info("SetupTestDatabase")
|
||||
}
|
||||
|
||||
func PatchDatabase() {
|
||||
|
||||
slog.Info("PatchDatabase")
|
||||
}
|
||||
|
||||
func TearDownTestDatabase() {
|
||||
|
||||
slog.Info("TearDownTestDatabase")
|
||||
}
|
||||
|
||||
func UnpatchDatabase() {
|
||||
|
||||
slog.Info("UnpatchDatabase")
|
||||
}
|
||||
|
||||
func InitializeTestSuite(m *testing.M) {
|
||||
slog.Info("InitializeTestSuite")
|
||||
// Initialize the test database
|
||||
SetupTestDatabase()
|
||||
// Patch the global database.DB to use the TestDB
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package helpers
|
||||
package tests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"kontor-api-gin/internal/helpers"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -40,7 +41,7 @@ func TestMultiFormatTimeUnmarshal(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var m KontorTime
|
||||
var m helpers.KontorTime
|
||||
err := json.Unmarshal([]byte(tt.input), &m)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
|
||||
Reference in New Issue
Block a user