Skip to content

Commit

Permalink
associate project asset
Browse files Browse the repository at this point in the history
  • Loading branch information
hexaforce committed Feb 20, 2025
1 parent 292a094 commit 47a97b9
Show file tree
Hide file tree
Showing 51 changed files with 1,816 additions and 748 deletions.
51 changes: 41 additions & 10 deletions server/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net"
"net/http"
"reflect"
"regexp"
"strings"
"testing"
Expand All @@ -31,13 +32,15 @@ import (
"golang.org/x/text/language"
)

type Seeder func(ctx context.Context, r *repo.Container) error
var fr *gateway.File

type Seeder func(ctx context.Context, r *repo.Container, f gateway.File) error

func init() {
mongotest.Env = "REEARTH_DB"
}

func initRepos(t *testing.T, useMongo bool, seeder Seeder) (repos *repo.Container) {
func initRepos(t *testing.T, useMongo bool, seeder Seeder) (repos *repo.Container, file gateway.File) {
ctx := context.Background()

if useMongo {
Expand All @@ -48,18 +51,25 @@ func initRepos(t *testing.T, useMongo bool, seeder Seeder) (repos *repo.Containe
repos = memory.New()
}

file = lo.Must(fs.NewFile(afero.NewMemMapFs(), "https://example.com/"))
fr = &file
if seeder != nil {
if err := seeder(ctx, repos); err != nil {
if err := seeder(ctx, repos, file); err != nil {
t.Fatalf("failed to seed the db: %s", err)
}
}

return repos
return repos, file
}

func initGateway() *gateway.Container {
if fr == nil {
return &gateway.Container{
File: lo.Must(fs.NewFile(afero.NewMemMapFs(), "https://example.com/")),
}
}
return &gateway.Container{
File: lo.Must(fs.NewFile(afero.NewMemMapFs(), "https://example.com")),
File: *fr,
}
}

Expand Down Expand Up @@ -123,7 +133,7 @@ func StartGQLServerAndRepos(t *testing.T, seeder Seeder) (*httpexpect.Expect, *a
Disabled: true,
},
}
repos := initRepos(t, true, seeder)
repos, _ := initRepos(t, true, seeder)
e, _, _ := StartGQLServerWithRepos(t, cfg, repos)
return e, repos.AccountRepos()
}
Expand Down Expand Up @@ -175,7 +185,7 @@ func StartServerWithRepos(t *testing.T, cfg *config.Config, repos *repo.Containe
}

func StartServerAndRepos(t *testing.T, cfg *config.Config, useMongo bool, seeder Seeder) (*httpexpect.Expect, *repo.Container, *gateway.Container) {
repos := initRepos(t, useMongo, seeder)
repos, _ := initRepos(t, useMongo, seeder)
e, gateways := StartServerWithRepos(t, cfg, repos)
return e, repos, gateways
}
Expand Down Expand Up @@ -203,8 +213,8 @@ func ServerLanguage(t *testing.T, lang language.Tag) *httpexpect.Expect {
},
}
return StartServer(t, c, true,
func(ctx context.Context, r *repo.Container) error {
return baseSeederWithLang(ctx, r, lang)
func(ctx context.Context, r *repo.Container, f gateway.File) error {
return baseSeederWithLang(ctx, r, f, lang)
},
)
}
Expand Down Expand Up @@ -275,6 +285,12 @@ func RegexpJSONEReadCloser(t *testing.T, actual io.ReadCloser, expected string)
actualBuf := new(bytes.Buffer)
_, err := actualBuf.ReadFrom(actual)
assert.NoError(t, err)
// var data map[string]interface{}
// err = json.Unmarshal(actualBuf.Bytes(), &data)
// assert.NoError(t, err)
// if text, err := json.MarshalIndent(data, "", " "); err == nil {
// fmt.Println(string(text))
// }
return JSONEqRegexp(t, actualBuf.String(), expected)
}

Expand All @@ -284,6 +300,13 @@ func JSONEqRegexpInterface(t *testing.T, actual interface{}, expected string) bo
return JSONEqRegexp(t, string(actualBytes), expected)
}

func JSONEqRegexpValue(t *testing.T, actual *httpexpect.Value, expected string) bool {
if actualData, ok := actual.Raw().(map[string]interface{}); ok {
return JSONEqRegexpInterface(t, actualData, expected)
}
return false
}

func aligningJSON(t *testing.T, str string) string {
// Unmarshal and Marshal to make the JSON format consistent
var obj interface{}
Expand All @@ -295,9 +318,17 @@ func aligningJSON(t *testing.T, str string) string {
}

func ValueDump(val *httpexpect.Value) {
if data, ok := val.Raw().(map[string]interface{}); ok {
raw := val.Raw()
switch data := raw.(type) {
case map[string]interface{}:
if text, err := json.MarshalIndent(data, "", " "); err == nil {
fmt.Println(string(text))
}
case []interface{}:
if text, err := json.MarshalIndent(data, "", " "); err == nil {
fmt.Println(string(text))
}
default:
fmt.Println("Unsupported type:", reflect.TypeOf(raw))
}
}
Loading

0 comments on commit 47a97b9

Please sign in to comment.