Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): associate project asset #1410

Merged
merged 3 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +35 to +37
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider removing the global variable and using dependency injection.

The global variable fr could lead to state management issues in concurrent tests. Consider passing the gateway.File directly to functions that need it.

-var fr *gateway.File
+type TestContext struct {
+    file gateway.File
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var fr *gateway.File
type Seeder func(ctx context.Context, r *repo.Container, f gateway.File) error
type TestContext struct {
file 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