Last active
February 28, 2023 14:36
-
-
Save damif94/7d26e7e2f7ff7fce2813e07f9242f4d1 to your computer and use it in GitHub Desktop.
Factories for test demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"github.com/Drafteame/game-engine/internal/models" | |
"github.com/Drafteame/game-engine/internal/test/factories" | |
"github.com/Drafteame/game-engine/internal/test/randomdata" | |
"github.com/bluele/factory-go/factory" | |
"go.mongodb.org/mongo-driver/bson/primitive" | |
"testing" | |
"time" | |
) | |
// nolint | |
func main() { | |
// Paso 1: Factory básica con un atributo | |
lineupFactoryOneAttribute(true) | |
// Paso 2: Factory básica con varios atributos juntos | |
lineupFactoryManyAttributes(true) | |
// Paso 3: Factory anidada | |
lineupFactoryWithEmbeddedStructures(true) | |
// Paso 4: Factories con nuestro wrapper | |
lineupFactoryFromWrapper(true) | |
// Paso 5: Factories con nuestro wrapper para crear varias instancias juntas | |
lineupFactoryManyFromWrapper(true) | |
// Paso 6: Factories con nuestro wrapper con opciones custom | |
lineupFactoryCustomFromWrapper(false) | |
} | |
func lineupFactoryOneAttribute(skip bool) { | |
if skip { | |
return | |
} | |
lineupFactory := factories.NewFactory( | |
models.Lineup{}, | |
).Attr( | |
"ID", | |
func(args factory.Args) (any, error) { | |
id := primitive.NewObjectID() | |
return &id, nil | |
}, | |
) | |
lineup := lineupFactory.MustCreate().(models.Lineup) | |
bytes, _ := json.MarshalIndent(lineup, "", " ") | |
fmt.Println(string(bytes)) | |
} | |
func lineupFactoryManyAttributes(skip bool) { | |
if skip { | |
return | |
} | |
lineupFactory := factories.NewFactory( | |
models.Lineup{}, | |
).Attrs( | |
[]string{"ID", "UserID", "ContestID", "FixtureID"}, | |
factories.MongoObjectIDGenerator, | |
).Attr( | |
"Configuration", factories.ConstantGenerator("4-4-2"), | |
) | |
lineup := lineupFactory.MustCreate().(models.Lineup) | |
bytes, _ := json.MarshalIndent(lineup, "", " ") | |
fmt.Println(string(bytes)) | |
} | |
func lineupFactoryWithEmbeddedStructures(skip bool) { | |
if skip { | |
return | |
} | |
specialTeamFactory := factories.NewFactory( | |
&models.SpecialTeamFreezed{}, | |
).Attr( | |
"ID", factories.ConstantGenerator("123"), | |
) | |
playerPositionFreezedFactory := factories.NewFactory( | |
&models.PlayerPositionFreezed{}, | |
).Attr( | |
"ID", factories.MongoObjectIDGenerator, | |
) | |
lineupFactory := factories.NewFactory( | |
models.Lineup{}, | |
).Attrs( | |
[]string{"ID", "UserID", "ContestID", "FixtureID"}, | |
func(args factory.Args) (any, error) { | |
id := primitive.NewObjectID() | |
return &id, nil | |
}, | |
).Attr( | |
"Configuration", factories.ConstantGenerator("4-4-2"), | |
).SubSliceFactory( | |
"PlayerPositionsFreezed", playerPositionFreezedFactory, func() int { return 11 }, | |
).SubFactory( | |
"SpecialTeamFreezed", specialTeamFactory, | |
) | |
lineup := lineupFactory.MustCreate().(models.Lineup) | |
bytes, _ := json.MarshalIndent(lineup, "", " ") | |
fmt.Println(string(bytes)) | |
} | |
func lineupFactoryFromWrapper(skip bool) { | |
if skip { | |
return | |
} | |
lineup := factories.MustCreate[models.Lineup](&testing.T{}) | |
bytes, _ := json.MarshalIndent(lineup, "", " ") | |
fmt.Println(string(bytes)) | |
} | |
func lineupFactoryManyFromWrapper(skip bool) { | |
if skip { | |
return | |
} | |
lineup := factories.MustCreateMany[models.Lineup](&testing.T{}, 2) | |
bytes, _ := json.MarshalIndent(lineup, "", " ") | |
fmt.Println(string(bytes)) | |
} | |
func lineupFactoryCustomFromWrapper(skip bool) { | |
if skip { | |
return | |
} | |
date := randomdata.DateUntil(time.Now()) | |
mongoDate := primitive.NewDateTimeFromTime(date) | |
lineup := factories.MustCreate[models.Lineup](&testing.T{}, factories.WithFieldValue("CreatedAt", &mongoDate)) | |
bytes, _ := json.MarshalIndent(lineup, "", " ") | |
fmt.Println(string(bytes)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment