fix: stabilize backend tests
This commit is contained in:
@@ -2,13 +2,16 @@ package testx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
jobs_args "quyun/v2/app/jobs/args"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/providers/job"
|
||||
"quyun/v2/providers/jwt"
|
||||
"quyun/v2/providers/postgres"
|
||||
"quyun/v2/providers/storage"
|
||||
|
||||
"github.com/riverqueue/river"
|
||||
"go.ipao.vip/atom"
|
||||
@@ -26,6 +29,7 @@ func Default(providers ...container.ProviderContainer) container.Providers {
|
||||
postgres.DefaultProvider(),
|
||||
jwt.DefaultProvider(),
|
||||
job.DefaultProvider(),
|
||||
storage.DefaultProvider(),
|
||||
testJobWorkersProvider(),
|
||||
database.DefaultProvider(),
|
||||
}, providers...)
|
||||
@@ -47,6 +51,22 @@ func (w *mediaAssetProcessTestWorker) Work(ctx context.Context, job *river.Job[j
|
||||
return nil
|
||||
}
|
||||
|
||||
type notificationTestWorker struct {
|
||||
river.WorkerDefaults[jobs_args.NotificationArgs]
|
||||
}
|
||||
|
||||
func (w *notificationTestWorker) Work(ctx context.Context, job *river.Job[jobs_args.NotificationArgs]) error {
|
||||
arg := job.Args
|
||||
n := &models.Notification{
|
||||
UserID: arg.UserID,
|
||||
Type: arg.Type,
|
||||
Title: arg.Title,
|
||||
Content: arg.Content,
|
||||
IsRead: false,
|
||||
}
|
||||
return models.NotificationQuery.WithContext(ctx).Create(n)
|
||||
}
|
||||
|
||||
func testJobWorkersProvider() container.ProviderContainer {
|
||||
return container.ProviderContainer{
|
||||
Provider: func(opts ...opt.Option) error {
|
||||
@@ -60,6 +80,10 @@ func testJobWorkersProvider() container.ProviderContainer {
|
||||
if err := river.AddWorkerSafely(__job.Workers, obj2); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
obj3 := ¬ificationTestWorker{}
|
||||
if err := river.AddWorkerSafely(__job.Workers, obj3); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return obj, nil
|
||||
}, atom.GroupInitial)
|
||||
},
|
||||
@@ -76,13 +100,30 @@ func Serve(providers container.Providers, t *testing.T, invoke any) {
|
||||
So(container.Container.Provide(func() context.Context { return context.Background() }), ShouldBeNil)
|
||||
|
||||
file := fabfile.MustFind("config.toml")
|
||||
|
||||
// localEnv := os.Getenv("ENV_LOCAL")
|
||||
// if localEnv != "" {
|
||||
// file = fabfile.MustFind("config." + localEnv + ".toml")
|
||||
// }
|
||||
// 支持通过 ENV_LOCAL 指定测试环境配置:config.<env>.toml
|
||||
localEnv := os.Getenv("ENV_LOCAL")
|
||||
if localEnv != "" {
|
||||
file = fabfile.MustFind("config." + localEnv + ".toml")
|
||||
}
|
||||
|
||||
So(atom.LoadProviders(file, providers), ShouldBeNil)
|
||||
So(os.Setenv("JOB_INLINE", "1"), ShouldBeNil)
|
||||
t.Cleanup(func() {
|
||||
_ = os.Unsetenv("JOB_INLINE")
|
||||
})
|
||||
So(container.Container.Invoke(func(p struct {
|
||||
dig.In
|
||||
Initials []contracts.Initial `group:"initials"`
|
||||
Job *job.Job
|
||||
}) error {
|
||||
_ = p.Initials
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
go func() {
|
||||
_ = p.Job.Start(ctx)
|
||||
}()
|
||||
return nil
|
||||
}), ShouldBeNil)
|
||||
So(container.Container.Invoke(invoke), ShouldBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user