Files
kontor/kontor-gin/internal/tests/service.go
T
Thomas Peetz 980eda29af
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
add tests for repository
2026-10-06 17:11:42 +02:00

46 lines
1.0 KiB
Go

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
}