add tests for repository
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s

This commit is contained in:
Thomas Peetz
2026-10-06 17:11:42 +02:00
parent 32f3f034eb
commit 980eda29af
9 changed files with 154 additions and 17 deletions
+45
View File
@@ -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
}