-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor temporal scheduler tests (#234)
* Merge branch 'fix/payment_cron' into 'main' refactor saas cron workflow See merge request product/starhub/starhub-server!805 * Merge branch 'fix/payment_cron' into 'main' refactor API workflows See merge request product/starhub/starhub-server!806 --------- Co-authored-by: yiling.ji <yl.ji@opencsg.com>
- Loading branch information
Showing
6 changed files
with
86 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//go:build !saas | ||
|
||
package workflow_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"opencsg.com/csghub-server/builder/multisync" | ||
"opencsg.com/csghub-server/builder/store/database" | ||
) | ||
|
||
func TestSchedule_CalcRecomScoreWorkflow(t *testing.T) { | ||
tester, err := newWorkflowTester(t) | ||
require.NoError(t, err) | ||
|
||
tester.mocks.recom.EXPECT().CalculateRecomScore(mock.Anything).Return() | ||
tester.scheduler.Execute("calc-recom-score-schedule", tester.cronEnv) | ||
require.True(t, tester.cronEnv.IsWorkflowCompleted()) | ||
require.NoError(t, tester.cronEnv.GetWorkflowError()) | ||
} | ||
|
||
func TestSchedule_SyncAsClient(t *testing.T) { | ||
tester, err := newWorkflowTester(t) | ||
require.NoError(t, err) | ||
|
||
tester.mocks.stores.SyncClientSettingMock().EXPECT().First(mock.Anything).Return( | ||
&database.SyncClientSetting{Token: "tk"}, nil, | ||
) | ||
tester.mocks.multisync.EXPECT().SyncAsClient( | ||
mock.Anything, multisync.FromOpenCSG("", "tk"), | ||
).Return(nil) | ||
|
||
tester.scheduler.Execute("sync-as-client-schedule", tester.cronEnv) | ||
require.True(t, tester.cronEnv.IsWorkflowCompleted()) | ||
require.NoError(t, tester.cronEnv.GetWorkflowError()) | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package temporal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.temporal.io/sdk/client" | ||
"go.temporal.io/sdk/testsuite" | ||
) | ||
|
||
type TestScheduler struct { | ||
workflowOptions map[string]client.ScheduleOptions | ||
} | ||
|
||
func NewTestScheduler() *TestScheduler { | ||
return &TestScheduler{ | ||
workflowOptions: map[string]client.ScheduleOptions{}, | ||
} | ||
} | ||
|
||
func (ts *TestScheduler) Create(ctx context.Context, options client.ScheduleOptions) (client.ScheduleHandle, error) { | ||
ts.workflowOptions[options.ID] = options | ||
return nil, nil | ||
} | ||
|
||
func (ts *TestScheduler) Execute(id string, env *testsuite.TestWorkflowEnvironment) { | ||
ops, ok := ts.workflowOptions[id] | ||
if !ok { | ||
panic(fmt.Sprintf("%s not found", id)) | ||
} | ||
act := ops.Action.(*client.ScheduleWorkflowAction) | ||
env.ExecuteWorkflow(act.Workflow, act.Args...) | ||
} |