@@ -2,24 +2,27 @@ package main
22
33import (
44 "context"
5+ "encoding/json"
56 "flag"
67 "fmt"
78 "log"
89 "math/rand"
910 "os"
11+ "path"
1012 "strconv"
1113 "sync"
1214 "time"
1315
16+ "github.com/danicc097/openapi-go-gin-postgres-sqlc/cmd/initial-data/e2e"
1417 "github.com/danicc097/openapi-go-gin-postgres-sqlc/internal"
1518 "github.com/danicc097/openapi-go-gin-postgres-sqlc/internal/envvar"
1619 "github.com/danicc097/openapi-go-gin-postgres-sqlc/internal/models"
1720 "github.com/danicc097/openapi-go-gin-postgres-sqlc/internal/repos"
1821 "github.com/danicc097/openapi-go-gin-postgres-sqlc/internal/repos/postgresql"
1922 "github.com/danicc097/openapi-go-gin-postgres-sqlc/internal/repos/postgresql/gen/db"
2023 "github.com/danicc097/openapi-go-gin-postgres-sqlc/internal/services"
21- "github.com/danicc097/openapi-go-gin-postgres-sqlc/internal/testutil"
2224 "github.com/danicc097/openapi-go-gin-postgres-sqlc/internal/utils/pointers"
25+ "github.com/gzuidhof/tygo/tygo"
2326 "github.com/jackc/pgx/v5/pgxpool"
2427 "go.uber.org/zap"
2528)
@@ -35,6 +38,15 @@ const (
3538 month = 30 * day
3639)
3740
41+ const e2eTestDataDir = "e2e/__tests__/data"
42+
43+ /**
44+ * TODO: for env = E2E marshal objects at the end to e2e/testdata json file
45+ * which includes useful e2e static info that doesn't change between this cli's runs (ie no ids or random data)
46+ * We can generate typescript types from these go structs as well if its somehow needed (since they won't be the same as openapi types).
47+ * We have library mode with https://github.com/gzuidhof/tygo so this is trivial and more than enough for E2E
48+ *.
49+ */
3850func main () {
3951 var err error
4052 var env , scopePolicyPath , rolePolicyPath string
@@ -56,7 +68,6 @@ func main() {
5668
5769 repositories := services .CreateRepos ()
5870
59- // TODO: services.Create(logger, repositories, pool)
6071 svc := services .New (logger , repositories , pool )
6172
6273 ctx := context .Background ()
@@ -108,8 +119,8 @@ func main() {
108119 for i := 0 ; i < 50 ; i ++ {
109120 u , err := svc .User .Register (ctx , pool , services.UserRegisterParams {
110121 Username : "user_" + strconv .Itoa (i ),
111- FirstName : pointers .New (testutil . RandomFirstName ( )),
112- LastName : pointers .New (testutil . RandomLastName ( )),
122+ FirstName : pointers .New ("First name " + fmt . Sprintf ( "%v" , i )),
123+ LastName : pointers .New ("Last name " + fmt . Sprintf ( "%v" , i )),
113124 Email : "user_" + strconv .Itoa (i ) + "@mail.com" ,
114125 ExternalID : "external_id_user_" + strconv .Itoa (i ),
115126 // Scopes: []models.Scope{models.}, // TODO:
@@ -141,25 +152,31 @@ func main() {
141152 *
142153 **/
143154 logger .Info ("Creating teams..." )
155+ var teams []* db.Team
144156
145157 teamDemo , err := svc .Team .Create (ctx , pool , & db.TeamCreateParams {
146158 ProjectID : internal .ProjectIDByName [models .ProjectDemo ],
147159 Name : "Team 1" ,
148160 Description : "Team 1 description" ,
149161 })
150162 handleError (err , teamDemo )
163+ teams = append (teams , teamDemo )
164+
151165 teamDemo2 , err := svc .Team .Create (ctx , pool , & db.TeamCreateParams {
152166 ProjectID : internal .ProjectIDByName [models .ProjectDemoTwo ],
153167 Name : "Team 2-1" ,
154168 Description : "Team 2-1 description" ,
155169 })
156170 handleError (err , teamDemo2 )
171+ teams = append (teams , teamDemo2 )
172+
157173 team2Demo2 , err := svc .Team .Create (ctx , pool , & db.TeamCreateParams {
158174 ProjectID : internal .ProjectIDByName [models .ProjectDemoTwo ],
159175 Name : "Team 2-2" ,
160176 Description : "Team 2-2 description" ,
161177 })
162178 handleError (err , team2Demo2 )
179+ teams = append (teams , team2Demo2 )
163180
164181 for i , u := range users {
165182 users [i ], err = svc .User .AssignTeam (ctx , pool , u .UserID , teamDemo .TeamID )
@@ -429,6 +446,54 @@ func main() {
429446 // handleError(err)
430447 // fmt.Printf("wis len: %v - First workitem found:\n", len(wis))
431448 // format.PrintJSONByTag(wis[0], "db")
449+
450+ if cfg .AppEnv == internal .AppEnvE2E {
451+ println ("Generating E2E fixtures" )
452+ config := & tygo.Config {
453+ Packages : []* tygo.PackageConfig {
454+ {
455+ Path : "github.com/danicc097/openapi-go-gin-postgres-sqlc/cmd/initial-data/e2e" ,
456+ TypeMappings : map [string ]string {
457+ "time.Time" : "string /* RFC3339Nano */" ,
458+ "uuid.UUID" : "string /* uuid */" ,
459+ "uuid.NullUUID" : "null | string /* uuid */" ,
460+ },
461+ // to import actual values from models package, do it explicitly
462+ Frontmatter : `import type * as models from "client/gen/model";` ,
463+ OutputPath : path .Join (e2eTestDataDir , "initial-data.ts" ),
464+ },
465+ },
466+ }
467+ gen := tygo .New (config )
468+ handleError (gen .Generate ())
469+ }
470+ uu := make (map [string ]e2e.User )
471+ for _ , u := range users {
472+ role , _ := svc .Authorization .RoleByRank (u .RoleRank )
473+ uu [u .Email ] = e2e.User {
474+ Username : u .Username ,
475+ Email : u .Email ,
476+ FirstName : u .FirstName ,
477+ LastName : u .LastName ,
478+ Role : role .Name ,
479+ Scopes : u .Scopes ,
480+ }
481+ }
482+
483+ uuj , err := json .MarshalIndent (uu , "" , " " )
484+ handleError (err )
485+ handleError (os .WriteFile (path .Join (e2eTestDataDir , "users.json" ), uuj , 0o644 ))
486+
487+ tt := make ([]e2e.Team , len (teams ))
488+ for i , t := range teams {
489+ tt [i ] = e2e.Team {
490+ Name : t .Name ,
491+ ProjectName : t .ProjectJoin .Name ,
492+ }
493+ }
494+ ttj , err := json .MarshalIndent (tt , "" , " " )
495+ handleError (err )
496+ handleError (os .WriteFile (path .Join (e2eTestDataDir , "teams.json" ), ttj , 0o644 ))
432497}
433498
434499func errAndExit (out []byte , err error ) {
0 commit comments