refactor: move testing data to custom package faker (interna/faker)
This commit is contained in:
parent
bd135bcfe6
commit
18d1c98b76
3 changed files with 262 additions and 169 deletions
|
|
@ -5,6 +5,7 @@ import (
|
|||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"gitlab.mareshq.com/hq/yggdrasil/internal/faker"
|
||||
"gitlab.mareshq.com/hq/yggdrasil/internal/server"
|
||||
"gitlab.mareshq.com/hq/yggdrasil/pkg/training"
|
||||
"go.uber.org/zap"
|
||||
|
|
@ -23,6 +24,12 @@ func main() {
|
|||
|
||||
trainingRepository := training.NewInMemoryTrainingRepository()
|
||||
trainingDateRepository := training.NewInMemoryTrainingDateRepository()
|
||||
|
||||
f := faker.NewFaker(trainingRepository, trainingDateRepository)
|
||||
if err := f.GenerateFakeData(); err != nil {
|
||||
logger.Fatal("Error generating fake data", zap.Error(err))
|
||||
}
|
||||
|
||||
apiHandlers := server.NewAPIHandlers(trainingRepository, trainingDateRepository)
|
||||
server := server.NewServer(apiHandlers, port, logger)
|
||||
server.Run(shutdownCtx)
|
||||
|
|
|
|||
254
internal/faker/faker.go
Normal file
254
internal/faker/faker.go
Normal file
|
|
@ -0,0 +1,254 @@
|
|||
package faker
|
||||
|
||||
import (
|
||||
"math/rand/v2"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gitlab.mareshq.com/hq/yggdrasil/internal/currency"
|
||||
"gitlab.mareshq.com/hq/yggdrasil/pkg/training"
|
||||
)
|
||||
|
||||
type Faker struct {
|
||||
trainingRepository training.TrainingRepository
|
||||
trainingDateRepository training.TrainingDateRepository
|
||||
}
|
||||
|
||||
func NewFaker(trainingRepository training.TrainingRepository, trainingDateRepository training.TrainingDateRepository) *Faker {
|
||||
return &Faker{
|
||||
trainingRepository: trainingRepository,
|
||||
trainingDateRepository: trainingDateRepository,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Faker) GenerateFakeData() error {
|
||||
var trainings = []training.Training{
|
||||
{
|
||||
Name: "Kubernetes",
|
||||
Days: 2,
|
||||
Description: "Kubernetes",
|
||||
Pricing: []training.TrainingPrice{
|
||||
{
|
||||
Amount: decimal.NewFromInt(450),
|
||||
Currency: currency.USD,
|
||||
Type: training.OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(2000),
|
||||
Currency: currency.USD,
|
||||
Type: training.CorporateTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(9900),
|
||||
Currency: currency.CZK,
|
||||
Type: training.OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(49000),
|
||||
Currency: currency.CZK,
|
||||
Type: training.CorporateTrainingPrice,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "Terraform",
|
||||
Days: 1,
|
||||
Description: "Terraform",
|
||||
Pricing: []training.TrainingPrice{
|
||||
{
|
||||
Amount: decimal.NewFromInt(200),
|
||||
Currency: currency.USD,
|
||||
Type: training.OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(1000),
|
||||
Currency: currency.USD,
|
||||
Type: training.CorporateTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(4900),
|
||||
Currency: currency.CZK,
|
||||
Type: training.OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(24000),
|
||||
Currency: currency.CZK,
|
||||
Type: training.CorporateTrainingPrice,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "RKE2",
|
||||
Days: 1,
|
||||
Description: "RKE2",
|
||||
Pricing: []training.TrainingPrice{
|
||||
{
|
||||
Amount: decimal.NewFromInt(200),
|
||||
Currency: currency.USD,
|
||||
Type: training.OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(1000),
|
||||
Currency: currency.USD,
|
||||
Type: training.CorporateTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(4900),
|
||||
Currency: currency.CZK,
|
||||
Type: training.OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(24000),
|
||||
Currency: currency.CZK,
|
||||
Type: training.CorporateTrainingPrice,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "GitHub Actions",
|
||||
Days: 1,
|
||||
Description: "GitHub Actions",
|
||||
Pricing: []training.TrainingPrice{
|
||||
{
|
||||
Amount: decimal.NewFromInt(200),
|
||||
Currency: currency.USD,
|
||||
Type: training.OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(1000),
|
||||
Currency: currency.USD,
|
||||
Type: training.CorporateTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(4900),
|
||||
Currency: currency.CZK,
|
||||
Type: training.OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(24000),
|
||||
Currency: currency.CZK,
|
||||
Type: training.CorporateTrainingPrice,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "GitLab CI",
|
||||
Days: 1,
|
||||
Description: "GitLab CI",
|
||||
Pricing: []training.TrainingPrice{
|
||||
{
|
||||
Amount: decimal.NewFromInt(200),
|
||||
Currency: currency.USD,
|
||||
Type: training.OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(1000),
|
||||
Currency: currency.USD,
|
||||
Type: training.CorporateTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(4900),
|
||||
Currency: currency.CZK,
|
||||
Type: training.OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(24000),
|
||||
Currency: currency.CZK,
|
||||
Type: training.CorporateTrainingPrice,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "Prometheus",
|
||||
Days: 2,
|
||||
Description: "Prometheus",
|
||||
Pricing: []training.TrainingPrice{
|
||||
{
|
||||
Amount: decimal.NewFromInt(450),
|
||||
Currency: currency.USD,
|
||||
Type: training.OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(2000),
|
||||
Currency: currency.USD,
|
||||
Type: training.CorporateTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(9900),
|
||||
Currency: currency.CZK,
|
||||
Type: training.OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(49000),
|
||||
Currency: currency.CZK,
|
||||
Type: training.CorporateTrainingPrice,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, t := range trainings {
|
||||
err := f.trainingRepository.Create(&t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r := rand.IntN(5)
|
||||
for i := 0; i < r; i++ {
|
||||
online := rand.IntN(4) == 0
|
||||
|
||||
location := "Praha"
|
||||
if online {
|
||||
location = "Online"
|
||||
} else {
|
||||
if rand.IntN(2) == 0 {
|
||||
location = "Brno"
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
min := time.Date(now.Year(), now.Month(), now.Day(), 8, 0, 0, 0, time.UTC)
|
||||
max := time.Date(now.Year()+1, now.Month(), now.Day(), 8, 0, 0, 0, time.UTC)
|
||||
delta := max.Sub(min)
|
||||
|
||||
sec := rand.Int64N(int64(delta.Seconds())) + min.UnixNano()/1000000000
|
||||
date := time.Unix(sec, 0)
|
||||
|
||||
amount := decimal.NewFromInt(4900)
|
||||
if t.Days == 2 {
|
||||
amount = decimal.NewFromInt(9900)
|
||||
}
|
||||
|
||||
cur := currency.CZK
|
||||
if online {
|
||||
cur = currency.EUR
|
||||
|
||||
if t.Days == 2 {
|
||||
amount = decimal.NewFromInt(450)
|
||||
} else {
|
||||
amount = decimal.NewFromInt(200)
|
||||
}
|
||||
}
|
||||
|
||||
td := training.TrainingDate{
|
||||
Date: date,
|
||||
StartTime: date.Truncate(24 * time.Hour),
|
||||
Days: t.Days,
|
||||
IsOnline: online,
|
||||
Location: location,
|
||||
Address: "TBD",
|
||||
Capacity: 12,
|
||||
Price: training.TrainingDatePrice{
|
||||
Amount: amount,
|
||||
Currency: cur,
|
||||
},
|
||||
}
|
||||
err := f.trainingDateRepository.Create(t.ID, &td)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -3,9 +3,6 @@ package training
|
|||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gitlab.mareshq.com/hq/yggdrasil/internal/currency"
|
||||
)
|
||||
|
||||
type TrainingRepository interface {
|
||||
|
|
@ -22,174 +19,9 @@ type InMemoryTrainingRepository struct {
|
|||
}
|
||||
|
||||
func NewInMemoryTrainingRepository() *InMemoryTrainingRepository {
|
||||
repo := &InMemoryTrainingRepository{
|
||||
return &InMemoryTrainingRepository{
|
||||
trainings: make(map[TrainingID]Training),
|
||||
}
|
||||
|
||||
_ = repo.Create(&Training{
|
||||
Name: "Kubernetes",
|
||||
Days: 2,
|
||||
Description: "Kubernetes",
|
||||
Pricing: []TrainingPrice{
|
||||
{
|
||||
Amount: decimal.NewFromInt(450),
|
||||
Currency: currency.USD,
|
||||
Type: OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(2000),
|
||||
Currency: currency.USD,
|
||||
Type: CorporateTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(9900),
|
||||
Currency: currency.CZK,
|
||||
Type: OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(49000),
|
||||
Currency: currency.CZK,
|
||||
Type: CorporateTrainingPrice,
|
||||
},
|
||||
},
|
||||
})
|
||||
_ = repo.Create(&Training{
|
||||
Name: "Terraform",
|
||||
Days: 1,
|
||||
Description: "Terraform",
|
||||
Pricing: []TrainingPrice{
|
||||
{
|
||||
Amount: decimal.NewFromInt(200),
|
||||
Currency: currency.USD,
|
||||
Type: OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(1000),
|
||||
Currency: currency.USD,
|
||||
Type: CorporateTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(4900),
|
||||
Currency: currency.CZK,
|
||||
Type: OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(24000),
|
||||
Currency: currency.CZK,
|
||||
Type: CorporateTrainingPrice,
|
||||
},
|
||||
},
|
||||
})
|
||||
_ = repo.Create(&Training{
|
||||
Name: "RKE2",
|
||||
Days: 1,
|
||||
Description: "RKE2",
|
||||
Pricing: []TrainingPrice{
|
||||
{
|
||||
Amount: decimal.NewFromInt(200),
|
||||
Currency: currency.USD,
|
||||
Type: OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(1000),
|
||||
Currency: currency.USD,
|
||||
Type: CorporateTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(4900),
|
||||
Currency: currency.CZK,
|
||||
Type: OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(24000),
|
||||
Currency: currency.CZK,
|
||||
Type: CorporateTrainingPrice,
|
||||
},
|
||||
},
|
||||
})
|
||||
_ = repo.Create(&Training{
|
||||
Name: "GitHub Actions",
|
||||
Days: 1,
|
||||
Description: "GitHub Actions",
|
||||
Pricing: []TrainingPrice{
|
||||
{
|
||||
Amount: decimal.NewFromInt(200),
|
||||
Currency: currency.USD,
|
||||
Type: OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(1000),
|
||||
Currency: currency.USD,
|
||||
Type: CorporateTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(4900),
|
||||
Currency: currency.CZK,
|
||||
Type: OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(24000),
|
||||
Currency: currency.CZK,
|
||||
Type: CorporateTrainingPrice,
|
||||
},
|
||||
},
|
||||
})
|
||||
_ = repo.Create(&Training{
|
||||
Name: "GitLab CI",
|
||||
Days: 1,
|
||||
Description: "GitLab CI",
|
||||
Pricing: []TrainingPrice{
|
||||
{
|
||||
Amount: decimal.NewFromInt(200),
|
||||
Currency: currency.USD,
|
||||
Type: OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(1000),
|
||||
Currency: currency.USD,
|
||||
Type: CorporateTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(4900),
|
||||
Currency: currency.CZK,
|
||||
Type: OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(24000),
|
||||
Currency: currency.CZK,
|
||||
Type: CorporateTrainingPrice,
|
||||
},
|
||||
},
|
||||
})
|
||||
_ = repo.Create(&Training{
|
||||
Name: "Prometheus",
|
||||
Days: 2,
|
||||
Description: "Prometheus",
|
||||
Pricing: []TrainingPrice{
|
||||
{
|
||||
Amount: decimal.NewFromInt(450),
|
||||
Currency: currency.USD,
|
||||
Type: OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(2000),
|
||||
Currency: currency.USD,
|
||||
Type: CorporateTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(9900),
|
||||
Currency: currency.CZK,
|
||||
Type: OpenTrainingPrice,
|
||||
},
|
||||
{
|
||||
Amount: decimal.NewFromInt(49000),
|
||||
Currency: currency.CZK,
|
||||
Type: CorporateTrainingPrice,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return repo
|
||||
}
|
||||
|
||||
func (r *InMemoryTrainingRepository) Create(training *Training) error {
|
||||
|
|
|
|||
Reference in a new issue