This commit is contained in:
@@ -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
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"kontor-api-gin/internal/helpers"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMultiFormatTimeUnmarshal(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
wantErr bool
|
||||
want string // Expected time string for comparison
|
||||
}{
|
||||
{
|
||||
name: "Date Only",
|
||||
input: `"2005-04-01"`,
|
||||
wantErr: false,
|
||||
want: "2005-04-01 00:00:00 +0000 UTC",
|
||||
},
|
||||
{
|
||||
name: "KontorTime Format",
|
||||
input: `"2026-04-30T13:54:26.334Z"`,
|
||||
wantErr: false,
|
||||
want: "2026-04-30 13:54:26.334 +0000 UTC",
|
||||
},
|
||||
{
|
||||
name: "Null Value",
|
||||
input: `null`,
|
||||
wantErr: false,
|
||||
want: "", // Zero time
|
||||
},
|
||||
{
|
||||
name: "Invalid Format",
|
||||
input: `"not-a-date"`,
|
||||
wantErr: true,
|
||||
want: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
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)
|
||||
return
|
||||
}
|
||||
if !tt.wantErr && !m.Time.IsZero() && m.Time.String() != tt.want {
|
||||
t.Errorf("UnmarshalJSON() = %v, want %v", m.Time.String(), tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user