Files
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

66 lines
1.7 KiB
Go

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")
}