Skip to content

Commit

Permalink
Test tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jun 9, 2021
1 parent b032561 commit f7e8d35
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
14 changes: 6 additions & 8 deletions core/handlers/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/nyaruka/goflow/flows/triggers"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/core/runner"
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/mailroom/testsuite"
"github.com/nyaruka/mailroom/testsuite/testdata"

Expand Down Expand Up @@ -44,7 +45,7 @@ type TestCase struct {
SQLAssertions []SQLAssertion
}

type Assertion func(t *testing.T, db *sqlx.DB, rc redis.Conn) error
type Assertion func(t *testing.T, rt *runtime.Runtime) error

type SQLAssertion struct {
SQL string
Expand Down Expand Up @@ -162,7 +163,6 @@ func RunTestCases(t *testing.T, tcs []TestCase) {
ctx := testsuite.CTX()
rt := testsuite.RT()
db := rt.DB
rp := rt.RP

oa, err := models.GetOrgAssets(ctx, db, models.OrgID(1))
assert.NoError(t, err)
Expand Down Expand Up @@ -241,11 +241,11 @@ func RunTestCases(t *testing.T, tcs []TestCase) {
assert.NoError(t, err)

for _, scene := range scenes {
err := models.HandleEvents(ctx, tx, rp, oa, scene, results[scene.ContactID()].Events)
err := models.HandleEvents(ctx, tx, rt.RP, oa, scene, results[scene.ContactID()].Events)
assert.NoError(t, err)
}

err = models.ApplyEventPreCommitHooks(ctx, tx, rp, oa, scenes)
err = models.ApplyEventPreCommitHooks(ctx, tx, rt.RP, oa, scenes)
assert.NoError(t, err)

err = tx.Commit()
Expand All @@ -254,7 +254,7 @@ func RunTestCases(t *testing.T, tcs []TestCase) {
tx, err = db.BeginTxx(ctx, nil)
assert.NoError(t, err)

err = models.ApplyEventPostCommitHooks(ctx, tx, rp, oa, scenes)
err = models.ApplyEventPostCommitHooks(ctx, tx, rt.RP, oa, scenes)
assert.NoError(t, err)

err = tx.Commit()
Expand All @@ -266,11 +266,9 @@ func RunTestCases(t *testing.T, tcs []TestCase) {
testsuite.AssertQueryCount(t, db, a.SQL, a.Args, a.Count, "%d:%d: mismatch in expected count for query: %s", i, ii, a.SQL)
}

rc := rp.Get()
for ii, a := range tc.Assertions {
err := a(t, db, rc)
err := a(t, rt)
assert.NoError(t, err, "%d: %d error checking assertion", i, ii)
}
rc.Close()
}
}
8 changes: 5 additions & 3 deletions core/handlers/broadcast_created_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"github.com/nyaruka/mailroom/core/handlers"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/core/queue"
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/mailroom/testsuite"
"github.com/nyaruka/mailroom/testsuite/testdata"

"github.com/gomodule/redigo/redis"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
)

Expand All @@ -38,7 +37,10 @@ func TestBroadcastCreated(t *testing.T) {
},
},
Assertions: []handlers.Assertion{
func(t *testing.T, db *sqlx.DB, rc redis.Conn) error {
func(t *testing.T, rt *runtime.Runtime) error {
rc := rt.RP.Get()
defer rc.Close()

task, err := queue.PopNextTask(rc, queue.HandlerQueue)
assert.NoError(t, err)
assert.NotNil(t, task)
Expand Down
19 changes: 11 additions & 8 deletions core/handlers/session_triggered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ import (
"github.com/nyaruka/mailroom/core/handlers"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/core/queue"
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/mailroom/testsuite"
"github.com/nyaruka/mailroom/testsuite/testdata"

"github.com/gomodule/redigo/redis"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
)

func TestSessionTriggered(t *testing.T) {
testsuite.Reset()
testsuite.ResetRP()
models.FlushCache()
db := testsuite.DB()
rt := testsuite.RT()
ctx := testsuite.CTX()

oa, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
oa, err := models.GetOrgAssets(ctx, rt.DB, testdata.Org1.ID)
assert.NoError(t, err)

simpleFlow, err := oa.FlowByID(testdata.SingleMessage.ID)
Expand Down Expand Up @@ -73,7 +71,10 @@ func TestSessionTriggered(t *testing.T) {
},
},
Assertions: []handlers.Assertion{
func(t *testing.T, db *sqlx.DB, rc redis.Conn) error {
func(t *testing.T, rt *runtime.Runtime) error {
rc := rt.RP.Get()
defer rc.Close()

task, err := queue.PopNextTask(rc, queue.BatchQueue)
assert.NoError(t, err)
assert.NotNil(t, task)
Expand All @@ -96,7 +97,6 @@ func TestSessionTriggered(t *testing.T) {

func TestQuerySessionTriggered(t *testing.T) {
testsuite.Reset()
testsuite.ResetRP()
models.FlushCache()
db := testsuite.DB()
ctx := testsuite.CTX()
Expand All @@ -123,7 +123,10 @@ func TestQuerySessionTriggered(t *testing.T) {
},
},
Assertions: []handlers.Assertion{
func(t *testing.T, db *sqlx.DB, rc redis.Conn) error {
func(t *testing.T, rt *runtime.Runtime) error {
rc := rt.RP.Get()
defer rc.Close()

task, err := queue.PopNextTask(rc, queue.BatchQueue)
assert.NoError(t, err)
assert.NotNil(t, task)
Expand Down
7 changes: 4 additions & 3 deletions core/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,15 @@ func TestBatchStart(t *testing.T) {
}
}

func TestContactRuns(t *testing.T) {
func TestResume(t *testing.T) {
testsuite.Reset()
ctx := testsuite.CTX()
rt := testsuite.RT()
db := rt.DB
defer testsuite.ResetStorage()

db.MustExec(`UPDATE orgs_org set config = '{"session_storage_mode": "s3"}' WHERE id=1;`)
// write sessions to storage as well
db.MustExec(`UPDATE orgs_org set config = '{"session_storage_mode": "s3_write"}' WHERE id = 1`)

oa, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
require.NoError(t, err)
Expand All @@ -193,7 +194,7 @@ func TestContactRuns(t *testing.T) {
require.NoError(t, err)

contact, err := contacts[0].FlowContact(oa)
assert.NoError(t, err)
require.NoError(t, err)

trigger := triggers.NewBuilder(oa.Env(), flow.FlowReference(), contact).Manual().Build()
sessions, err := runner.StartFlowForContacts(ctx, rt, oa, flow, []flows.Trigger{trigger}, nil, true)
Expand Down

0 comments on commit f7e8d35

Please sign in to comment.