tercul-backend/test/fixtures/authors.go

97 lines
2.3 KiB
Go

package fixtures
import (
"context"
"tercul/internal/domain"
"time"
"gorm.io/gorm"
)
// Author fixtures for testing
var (
// TolstoyAuthor is a fixture for Leo Tolstoy
TolstoyAuthor = domain.Author{
TranslatableModel: domain.TranslatableModel{
BaseModel: domain.BaseModel{
ID: 1,
CreatedAt: time.Now().Add(-365 * 24 * time.Hour),
UpdatedAt: time.Now().Add(-30 * 24 * time.Hour),
},
Language: "en",
},
Name: "Leo Tolstoy",
BirthDate: timePtr("1828-09-09T00:00:00Z"),
DeathDate: timePtr("1910-11-20T00:00:00Z"),
}
// EliotAuthor is a fixture for T.S. Eliot
EliotAuthor = domain.Author{
TranslatableModel: domain.TranslatableModel{
BaseModel: domain.BaseModel{
ID: 2,
CreatedAt: time.Now().Add(-180 * 24 * time.Hour),
UpdatedAt: time.Now().Add(-10 * 24 * time.Hour),
},
Language: "en",
},
Name: "T.S. Eliot",
BirthDate: timePtr("1888-09-26T00:00:00Z"),
DeathDate: timePtr("1965-01-04T00:00:00Z"),
}
// KafkaAuthor is a fixture for Franz Kafka
KafkaAuthor = domain.Author{
TranslatableModel: domain.TranslatableModel{
BaseModel: domain.BaseModel{
ID: 3,
CreatedAt: time.Now().Add(-90 * 24 * time.Hour),
UpdatedAt: time.Now().Add(-5 * 24 * time.Hour),
},
Language: "en",
},
Name: "Franz Kafka",
BirthDate: timePtr("1883-07-03T00:00:00Z"),
DeathDate: timePtr("1924-06-03T00:00:00Z"),
}
// ContemporaryAuthor is a fixture for a contemporary author
ContemporaryAuthor = domain.Author{
TranslatableModel: domain.TranslatableModel{
BaseModel: domain.BaseModel{
ID: 4,
CreatedAt: time.Now().Add(-30 * 24 * time.Hour),
UpdatedAt: time.Now(),
},
Language: "en",
},
Name: "Jane Modern",
BirthDate: timePtr("1985-03-15T00:00:00Z"),
}
)
// AllAuthors returns all author fixtures
func AllAuthors() []domain.Author {
return []domain.Author{
TolstoyAuthor,
EliotAuthor,
KafkaAuthor,
ContemporaryAuthor,
}
}
// LoadAuthors loads author fixtures into the database
func LoadAuthors(ctx context.Context, db *gorm.DB) error {
for _, author := range AllAuthors() {
if err := db.Create(&author).Error; err != nil {
return err
}
}
return nil
}
func timePtr(s string) *time.Time {
t, _ := time.Parse(time.RFC3339, s)
return &t
}