Skip to content

Commit 8dac8af

Browse files
authored
Dev (#304)
* update comments * fix msw again * should fix scrolling in table tests * properly test table scrolling with mocked scroll * fix test fetching more * update postgres with index extensions * clean mrt * clean mrt and gen * data testids * default rtl timeout * filters * update tests * fixmes * refactor * tygo e2e typescript model gen setup * attempt at golangcilint generated file fix * e2e users example * e2e teams
1 parent f515dcd commit 8dac8af

File tree

147 files changed

+2450
-349
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+2450
-349
lines changed

.env.e2e

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
APP_ENV=e2e
22
# using container name resolution when dockerized
3-
DB_PORT=5432
3+
DB_PORT=5656
44
EXPOSED_POSTGRES_PORT=5656
55
POSTGRES_USER=postgres
66
POSTGRES_PASSWORD=postgres
7-
POSTGRES_SERVER=postgres_db_openapi-go-gin
7+
POSTGRES_SERVER=localhost
88
POSTGRES_PORT=5432
99
POSTGRES_DB=postgres_e2e
1010
PROJECT_PREFIX=openapi-go-gin
@@ -24,6 +24,7 @@ FRONTEND_PORT=80
2424
OIDC_CLIENT_ID=web
2525
OIDC_CLIENT_SECRET=secret
2626
AUTH_SERVER_UI_PROFILE="https://authserver.local.localhost/oidc/profile"
27+
MOCK_OIDC_SERVER_PATH_PREFIX="/oidc"
2728
OIDC_DOMAIN="authserver.local.localhost"
2829
OIDC_ISSUER="https://authserver.local.localhost/oidc"
2930
OIDC_SCOPES="openid profile email auth"

.golangci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# https://github.com/MarioCarrion/todo-api-microservice-example/blob/main/.golangci.yml
2-
2+
run:
3+
skip-files: # ignore yaml error...
4+
- ".*\\.xo\\.go$"
5+
- ".*\\.gen\\.go$"
6+
- ".*\\.pb\\.go$"
37
linters:
48
enable-all: true
59
disable:

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"--config=${workspaceFolder}/.golangci.yml",
66
"--issues-exit-code=0",
77
// golangci possibly broke generated code skipping in recent releases
8-
// "--fix",
8+
"--fix",
99
"--fast" // breaks some linters like errcheck, if its too slow then enable back
1010
],
1111
"go.lintOnSave": "package",

cmd/initial-data/e2e/models.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package e2e
2+
3+
import "github.com/danicc097/openapi-go-gin-postgres-sqlc/internal/models"
4+
5+
/*
6+
NOTE: tygo just looks the ast from the current package.
7+
All spec models available in tstype as `models` namespace
8+
*/
9+
10+
type User struct {
11+
Username string `json:"username"`
12+
Email string `json:"email"`
13+
FirstName *string `json:"firstName"`
14+
LastName *string `json:"lastName"`
15+
Scopes models.Scopes `json:"scopes" tstype:"models.Scopes"`
16+
Role models.Role `json:"role" tstype:"models.Role"`
17+
}
18+
19+
type Team struct {
20+
Name string `json:"name"`
21+
ProjectName models.ProjectName `json:"projectName" tstype:"models.Project"`
22+
}

cmd/initial-data/main.go

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,27 @@ package main
22

33
import (
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+
*/
3850
func 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

434499
func errAndExit(out []byte, err error) {

db/migrations/0000002_init.up.sql

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ create schema if not exists "cache";
55

66
create extension if not exists pg_stat_statements schema extensions;
77

8+
create extension if not exists hypopg schema extensions;
9+
10+
create extension if not exists index_advisor schema extensions;
11+
812
create extension if not exists pg_trgm schema extensions;
913

1014
create extension if not exists btree_gin schema extensions;
@@ -316,21 +320,13 @@ alter table user_api_keys
316320

317321
comment on column user_api_keys.user_api_key_id is '"properties":private';
318322

319-
-- -- pg13 alt for CONSTRAINT uq_external_id UNIQUE NULLS NOT DISTINCT (external_id)
320-
-- create unique index on users (user_id , external_id)
321-
-- where
322-
-- external_id is not null;
323-
-- create unique index on users (user_id)
324-
-- where
325-
-- external_id is null;
326-
-- composite on id, deleted_at, email, deleted_at, etc. will not improve speed
327323
-- create unique index on users (user_id) where deleted_at is null; -- helps if you have much more deleted rows only
328324
-- create index on users (deleted_at); - not worth the extra overhead.
329-
-- for finding all deleted users exclusively
325+
-- does get used when filtering deleted users exclusively and there's few of them
330326
create index on users (deleted_at)
331327
where (deleted_at is not null);
332328

333-
create index on users (updated_at);
329+
create index on users using gin (role_rank , age , username gin_trgm_ops , email gin_trgm_ops , full_name gin_trgm_ops);
334330

335331
-- notification_types are append-only
336332
create type notification_type as ENUM (

docker/postgres/Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
FROM postgres:16.2
22

3-
RUN apt-get update && apt-get install -y curl postgresql-$PG_MAJOR-plpgsql-check postgresql-$PG_MAJOR-rum
3+
RUN apt-get update && apt-get install -y curl postgresql-$PG_MAJOR-plpgsql-check postgresql-$PG_MAJOR-rum postgresql-$PG_MAJOR-hypopg
44
# 0.3.1
55
ENV SUPA_AUDIT_COMMIT="0b87d13840e26fc68d0e01e9917c67c894121f6f"
66

77
RUN curl -SL "https://github.com/danicc097/supa_audit/archive/${SUPA_AUDIT_COMMIT}.tar.gz" -o /supa_audit.tar.gz \
88
&& tar -xzvf /supa_audit.tar.gz -C /usr/share/postgresql/$PG_MAJOR/extension/ --strip-components=1 \
99
&& rm /supa_audit.tar.gz \
1010
&& chown -R postgres:postgres /usr/share/postgresql/$PG_MAJOR/extension/supa_audit*
11+
12+
RUN curl -SL "https://github.com/supabase/index_advisor/archive/refs/tags/v0.2.0.tar.gz" -o /index_advisor.tar.gz \
13+
&& tar -xzvf /index_advisor.tar.gz -C /usr/share/postgresql/$PG_MAJOR/extension/ --strip-components=1 \
14+
&& rm /index_advisor.tar.gz \
15+
&& chown -R postgres:postgres /usr/share/postgresql/$PG_MAJOR/extension/index_advisor*

e2e/__tests__/data/initial-data.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Code generated by tygo. DO NOT EDIT.
2+
import type * as models from "client/gen/model";
3+
//////////
4+
// source: models.go
5+
6+
export interface User {
7+
username: string;
8+
email: string;
9+
firstName?: string;
10+
lastName?: string;
11+
scopes: models.Scopes;
12+
role: models.Role;
13+
}
14+
export interface Team {
15+
name: string;
16+
projectName: models.Project;
17+
}

e2e/__tests__/data/teams.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"name": "Team 1",
4+
"projectName": "demo"
5+
},
6+
{
7+
"name": "Team 2-1",
8+
"projectName": "demo_two"
9+
},
10+
{
11+
"name": "Team 2-2",
12+
"projectName": "demo_two"
13+
}
14+
]

0 commit comments

Comments
 (0)