Implement lib to generate fake data for testing

This commit is contained in:
William Bouzourène 2025-01-23 15:53:58 +01:00
parent e96fff646f
commit 02d2cc6629
4 changed files with 111 additions and 0 deletions

103
controllers/debug.go Normal file
View file

@ -0,0 +1,103 @@
package controllers
import (
"fmt"
"math/rand/v2"
"git.readonly.ch/bouzoure/pop-camarades/helpers"
"git.readonly.ch/bouzoure/pop-camarades/models"
"github.com/brianvoe/gofakeit/v7"
"github.com/gofiber/fiber/v2"
)
func DebugFakeMembers(c *fiber.Ctx) error {
number, _ := c.ParamsInt("number")
db, err := helpers.GetDatabase()
if err != nil {
return err
}
var sections []models.Section
result := db.Find(&sections, "contains_members = ?", true)
if result.Error != nil {
return result.Error
}
if result.RowsAffected < 1 {
return fiber.NewError(400, "cannot create members because no sections")
}
for i := 0; i < number; i++ {
section := sections[rand.IntN(len(sections))]
address := gofakeit.Address()
address2 := fmt.Sprintf("C/O %s", gofakeit.Name())
person := models.Person{
IsMember: true,
FirstName: gofakeit.FirstName(),
LastName: gofakeit.LastName(),
Email: gofakeit.Email(),
Phone: gofakeit.Phone(),
Mobile: gofakeit.Phone(),
Address1: address.Street,
Address2: address2,
PostalCode: address.Zip[:4],
City: address.City,
SectionID: section.ID,
}
db.Create(&person)
}
return c.SendString(fmt.Sprintf(
"Created %d member(s)", number,
))
}
func DebugFakeContacts(c *fiber.Ctx) error {
number, _ := c.ParamsInt("number")
db, err := helpers.GetDatabase()
if err != nil {
return err
}
var sections []models.Section
result := db.Find(&sections, "contains_contacts = ?", true)
if result.Error != nil {
return result.Error
}
if result.RowsAffected < 1 {
return fiber.NewError(400, "cannot create contacts because no sections")
}
for i := 0; i < number; i++ {
section := sections[rand.IntN(len(sections))]
address := gofakeit.Address()
address2 := fmt.Sprintf("C/O %s", gofakeit.Name())
person := models.Person{
IsContact: true,
FirstName: gofakeit.FirstName(),
LastName: gofakeit.LastName(),
Email: gofakeit.Email(),
Phone: gofakeit.Phone(),
Mobile: gofakeit.Phone(),
Address1: address.Street,
Address2: address2,
PostalCode: address.Zip[:4],
City: address.City,
SectionID: section.ID,
}
db.Create(&person)
}
return c.SendString(fmt.Sprintf(
"Created %d contact(s)", number,
))
}