Skip to content

Commit

Permalink
Rework tests to lceanup after themselves and reduce need to reset ent…
Browse files Browse the repository at this point in the history
…ire db
  • Loading branch information
rowanseymour committed Jun 26, 2021
1 parent 7c42f79 commit 6727a2f
Show file tree
Hide file tree
Showing 30 changed files with 128 additions and 94 deletions.
9 changes: 4 additions & 5 deletions core/goflow/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func TestEngineWebhook(t *testing.T) {
rt := testsuite.RT()
_, rt, _, _ := testsuite.Get()

svc, err := goflow.Engine(rt.Config).Services().Webhook(nil)
assert.NoError(t, err)
Expand All @@ -40,7 +40,7 @@ func TestEngineWebhook(t *testing.T) {
}

func TestSimulatorAirtime(t *testing.T) {
rt := testsuite.RT()
_, rt, _, _ := testsuite.Get()

svc, err := goflow.Simulator(rt.Config).Services().Airtime(nil)
assert.NoError(t, err)
Expand All @@ -60,8 +60,7 @@ func TestSimulatorAirtime(t *testing.T) {
}

func TestSimulatorTicket(t *testing.T) {
ctx, db, _ := testsuite.Reset()
rt := testsuite.RT()
ctx, rt, db, _ := testsuite.Get()

ticketer, err := models.LookupTicketerByUUID(ctx, db, testdata.Mailgun.UUID)
require.NoError(t, err)
Expand All @@ -77,7 +76,7 @@ func TestSimulatorTicket(t *testing.T) {
}

func TestSimulatorWebhook(t *testing.T) {
rt := testsuite.RT()
_, rt, _, _ := testsuite.Get()

svc, err := goflow.Simulator(rt.Config).Services().Webhook(nil)
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions core/goflow/flows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestSpecVersion(t *testing.T) {
}

func TestReadFlow(t *testing.T) {
rt := testsuite.RT()
_, rt, _, _ := testsuite.Get()

// try to read empty definition
flow, err := goflow.ReadFlow(rt.Config, []byte(`{}`))
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestCloneDefinition(t *testing.T) {
}

func TestMigrateDefinition(t *testing.T) {
rt := testsuite.RT()
_, rt, _, _ := testsuite.Get()

// 13.0 > 13.1
migrated, err := goflow.MigrateDefinition(rt.Config, []byte(`{"uuid": "502c3ee4-3249-4dee-8e71-c62070667d52", "name": "New", "spec_version": "13.0.0", "type": "messaging", "language": "eng", "nodes": []}`), semver.MustParse("13.1.0"))
Expand Down
3 changes: 1 addition & 2 deletions core/goflow/modifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
)

func TestReadModifiers(t *testing.T) {
ctx := testsuite.CTX()
db := testsuite.DB()
ctx, _, db, _ := testsuite.Get()

oa, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
assert.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions core/handlers/airtime_transferred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,9 @@ var transactionRejectedResponse = `{
}`

func TestAirtimeTransferred(t *testing.T) {
testsuite.Reset()
_, _, db, _ := testsuite.Get()

defer testsuite.Reset()
defer httpx.SetRequestor(httpx.DefaultRequestor)

httpx.SetRequestor(httpx.NewMockRequestor(map[string][]httpx.MockResponse{
Expand All @@ -288,7 +289,7 @@ func TestAirtimeTransferred(t *testing.T) {
},
}))

testsuite.DB().MustExec(`UPDATE orgs_org SET config = '{"dtone_key": "key123", "dtone_secret": "sesame"}'::jsonb WHERE id = $1`, testdata.Org1.ID)
db.MustExec(`UPDATE orgs_org SET config = '{"dtone_key": "key123", "dtone_secret": "sesame"}'::jsonb WHERE id = $1`, testdata.Org1.ID)

tcs := []handlers.TestCase{
{
Expand Down
2 changes: 1 addition & 1 deletion core/handlers/broadcast_created_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func TestBroadcastCreated(t *testing.T) {
testsuite.Reset()
defer testsuite.Reset()

// TODO: test contacts, groups

Expand Down
8 changes: 5 additions & 3 deletions core/handlers/contact_field_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (
)

func TestContactFieldChanged(t *testing.T) {
_, _, db, _ := testsuite.Get()

defer testsuite.Reset()

gender := assets.NewFieldReference("gender", "Gender")
age := assets.NewFieldReference("age", "Age")

db := testsuite.DB()

// populate some field values on alexandria
db.Exec(`UPDATE contacts_contact SET fields = '{"903f51da-2717-47c7-a0d3-f2f32877013d": {"text":"34"}, "3a5891e4-756e-4dc9-8e12-b7a766168824": {"text":"female"}}' WHERE id = $1`, testdata.Alexandria.ID)
db.MustExec(`UPDATE contacts_contact SET fields = '{"903f51da-2717-47c7-a0d3-f2f32877013d": {"text":"34"}, "3a5891e4-756e-4dc9-8e12-b7a766168824": {"text":"female"}}' WHERE id = $1`, testdata.Alexandria.ID)

tcs := []handlers.TestCase{
{
Expand Down
3 changes: 3 additions & 0 deletions core/handlers/contact_groups_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/actions"
"github.com/nyaruka/mailroom/core/handlers"
"github.com/nyaruka/mailroom/testsuite"
"github.com/nyaruka/mailroom/testsuite/testdata"
)

func TestContactGroupsChanged(t *testing.T) {
defer testsuite.Reset()

doctors := assets.NewGroupReference(testdata.DoctorsGroup.UUID, "Doctors")
testers := assets.NewGroupReference(testdata.TestersGroup.UUID, "Testers")

Expand Down
3 changes: 3 additions & 0 deletions core/handlers/contact_language_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/actions"
"github.com/nyaruka/mailroom/core/handlers"
"github.com/nyaruka/mailroom/testsuite"
"github.com/nyaruka/mailroom/testsuite/testdata"
)

func TestContactLanguageChanged(t *testing.T) {
defer testsuite.Reset()

tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
Expand Down
3 changes: 3 additions & 0 deletions core/handlers/contact_name_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/actions"
"github.com/nyaruka/mailroom/core/handlers"
"github.com/nyaruka/mailroom/testsuite"
"github.com/nyaruka/mailroom/testsuite/testdata"
)

func TestContactNameChanged(t *testing.T) {
defer testsuite.Reset()

tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
Expand Down
3 changes: 2 additions & 1 deletion core/handlers/contact_status_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
)

func TestContactStatusChanged(t *testing.T) {
_, _, db, _ := testsuite.Get()

db := testsuite.DB()
defer testsuite.Reset()

// make sure cathyID contact is active
db.Exec(`UPDATE contacts_contact SET status = 'A' WHERE id = $1`, testdata.Cathy.ID)
Expand Down
4 changes: 3 additions & 1 deletion core/handlers/contact_urns_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
)

func TestContactURNsChanged(t *testing.T) {
db := testsuite.DB()
_, _, db, _ := testsuite.Get()

defer testsuite.Reset()

// add a URN to george that cathy will steal
testdata.InsertContactURN(db, testdata.Org1, testdata.George, urns.URN("tel:+12065551212"), 100)
Expand Down
4 changes: 3 additions & 1 deletion core/handlers/input_labels_added_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
)

func TestInputLabelsAdded(t *testing.T) {
db := testsuite.DB()
_, _, db, _ := testsuite.Get()

defer testsuite.Reset()

reporting := assets.NewLabelReference(assets.LabelUUID("ebc4dedc-91c4-4ed4-9dd6-daa05ea82698"), "Reporting")
testing := assets.NewLabelReference(assets.LabelUUID("a6338cdc-7938-4437-8b05-2d5d785e3a08"), "Testing")
Expand Down
5 changes: 3 additions & 2 deletions core/handlers/msg_created_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
)

func TestMsgCreated(t *testing.T) {
testsuite.Reset()
db := testsuite.DB()
_, _, db, _ := testsuite.Get()

defer testsuite.Reset()

config.Mailroom.AttachmentDomain = "foo.bar.com"
defer func() { config.Mailroom.AttachmentDomain = "" }()
Expand Down
5 changes: 3 additions & 2 deletions core/handlers/msg_received_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
)

func TestMsgReceived(t *testing.T) {
testsuite.Reset()
db := testsuite.DB()
_, _, db, _ := testsuite.Get()

defer testsuite.Reset()

now := time.Now()

Expand Down
2 changes: 2 additions & 0 deletions core/handlers/service_called_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/actions"
"github.com/nyaruka/mailroom/core/handlers"
"github.com/nyaruka/mailroom/testsuite"
"github.com/nyaruka/mailroom/testsuite/testdata"
)

func TestServiceCalled(t *testing.T) {
defer testsuite.Reset()
defer httpx.SetRequestor(httpx.DefaultRequestor)

httpx.SetRequestor(httpx.NewMockRequestor(map[string][]httpx.MockResponse{
Expand Down
9 changes: 4 additions & 5 deletions core/handlers/session_triggered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import (
)

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

oa, err := models.GetOrgAssets(ctx, rt.DB, testdata.Org1.ID)
defer testsuite.Reset()

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

simpleFlow, err := oa.FlowByID(testdata.SingleMessage.ID)
Expand Down
5 changes: 2 additions & 3 deletions core/handlers/ticket_opened_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import (
)

func TestTicketOpened(t *testing.T) {
testsuite.Reset()
db := testsuite.DB()
ctx := testsuite.CTX()
ctx, _, db, _ := testsuite.Get()

defer testsuite.Reset()
defer httpx.SetRequestor(httpx.DefaultRequestor)

httpx.SetRequestor(httpx.NewMockRequestor(map[string][]httpx.MockResponse{
Expand Down
13 changes: 7 additions & 6 deletions core/handlers/webhook_called_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
)

func TestWebhookCalled(t *testing.T) {
testsuite.Reset()
_, _, db, _ := testsuite.Get()

defer testsuite.Reset()
defer httpx.SetRequestor(httpx.DefaultRequestor)

httpx.SetRequestor(httpx.NewMockRequestor(map[string][]httpx.MockResponse{
Expand All @@ -30,13 +31,13 @@ func TestWebhookCalled(t *testing.T) {
}))

// add a few resthooks
testsuite.DB().MustExec(`INSERT INTO api_resthook(is_active, slug, org_id, created_on, modified_on, created_by_id, modified_by_id) VALUES(TRUE, 'foo', 1, NOW(), NOW(), 1, 1);`)
testsuite.DB().MustExec(`INSERT INTO api_resthook(is_active, slug, org_id, created_on, modified_on, created_by_id, modified_by_id) VALUES(TRUE, 'bar', 1, NOW(), NOW(), 1, 1);`)
db.MustExec(`INSERT INTO api_resthook(is_active, slug, org_id, created_on, modified_on, created_by_id, modified_by_id) VALUES(TRUE, 'foo', 1, NOW(), NOW(), 1, 1);`)
db.MustExec(`INSERT INTO api_resthook(is_active, slug, org_id, created_on, modified_on, created_by_id, modified_by_id) VALUES(TRUE, 'bar', 1, NOW(), NOW(), 1, 1);`)

// and a few targets
testsuite.DB().MustExec(`INSERT INTO api_resthooksubscriber(is_active, created_on, modified_on, target_url, created_by_id, modified_by_id, resthook_id) VALUES(TRUE, NOW(), NOW(), 'http://rapidpro.io/', 1, 1, 1);`)
testsuite.DB().MustExec(`INSERT INTO api_resthooksubscriber(is_active, created_on, modified_on, target_url, created_by_id, modified_by_id, resthook_id) VALUES(TRUE, NOW(), NOW(), 'http://rapidpro.io/?unsub=1', 1, 1, 2);`)
testsuite.DB().MustExec(`INSERT INTO api_resthooksubscriber(is_active, created_on, modified_on, target_url, created_by_id, modified_by_id, resthook_id) VALUES(TRUE, NOW(), NOW(), 'http://rapidpro.io/?unsub=1', 1, 1, 1);`)
db.MustExec(`INSERT INTO api_resthooksubscriber(is_active, created_on, modified_on, target_url, created_by_id, modified_by_id, resthook_id) VALUES(TRUE, NOW(), NOW(), 'http://rapidpro.io/', 1, 1, 1);`)
db.MustExec(`INSERT INTO api_resthooksubscriber(is_active, created_on, modified_on, target_url, created_by_id, modified_by_id, resthook_id) VALUES(TRUE, NOW(), NOW(), 'http://rapidpro.io/?unsub=1', 1, 1, 2);`)
db.MustExec(`INSERT INTO api_resthooksubscriber(is_active, created_on, modified_on, target_url, created_by_id, modified_by_id, resthook_id) VALUES(TRUE, NOW(), NOW(), 'http://rapidpro.io/?unsub=1', 1, 1, 1);`)

tcs := []handlers.TestCase{
{
Expand Down
6 changes: 4 additions & 2 deletions core/ivr/vonage/vonage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (
)

func TestResponseForSprint(t *testing.T) {
ctx, db, rp := testsuite.Reset()
ctx, _, db, rp := testsuite.Get()

defer testsuite.Reset()

rc := rp.Get()
defer rc.Close()
models.FlushCache()

urn := urns.URN("tel:+12067799294")
channelRef := assets.NewChannelReference(testdata.VonageChannel.UUID, "Vonage Channel")
Expand Down
7 changes: 4 additions & 3 deletions core/models/airtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
)

func TestAirtimeTransfers(t *testing.T) {
ctx := testsuite.CTX()
db := testsuite.DB()
ctx, _, db, _ := testsuite.Get()

defer db.MustExec(`DELETE FROM airtime_airtimetransfer`)

// insert a transfer
transfer := models.NewAirtimeTransfer(
Expand All @@ -32,7 +33,7 @@ func TestAirtimeTransfers(t *testing.T) {
err := models.InsertAirtimeTransfers(ctx, db, []*models.AirtimeTransfer{transfer})
assert.Nil(t, err)

testsuite.AssertQuery(t, db, `SELECT count(*) from airtime_airtimetransfer WHERE org_id = $1 AND status = $2`, testdata.Org1.ID, models.AirtimeTransferStatusSuccess).Returns(1)
testsuite.AssertQuery(t, db, `SELECT org_id, status from airtime_airtimetransfer`).Columns(map[string]interface{}{"org_id": int64(1), "status": "S"})

// insert a failed transfer with nil sender, empty currency
transfer = models.NewAirtimeTransfer(
Expand Down
4 changes: 2 additions & 2 deletions core/models/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
)

func TestCloneForSimulation(t *testing.T) {
ctx := testsuite.CTX()
db := testsuite.DB()
ctx, _, db, _ := testsuite.Get()

models.FlushCache()

oa, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
Expand Down
6 changes: 3 additions & 3 deletions core/models/campaigns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ func TestCampaignSchedule(t *testing.T) {
}

func TestAddEventFires(t *testing.T) {
ctx := testsuite.CTX()
db := testsuite.DB()
testsuite.Reset()
ctx, _, db, _ := testsuite.Get()

defer db.MustExec(`DELETE FROM campaigns_eventfire`)

scheduled1 := time.Date(2020, 9, 8, 14, 38, 30, 123456789, time.UTC)

Expand Down
5 changes: 3 additions & 2 deletions core/models/channel_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
)

func TestChannelConnections(t *testing.T) {
ctx := testsuite.CTX()
db := testsuite.DB()
ctx, _, db, _ := testsuite.Get()

defer db.MustExec(`DELETE FROM channels_channelconnection`)

conn, err := models.InsertIVRConnection(ctx, db, testdata.Org1.ID, testdata.TwilioChannel.ID, models.NilStartID, testdata.Cathy.ID, testdata.Cathy.URNID, models.ConnectionDirectionOut, models.ConnectionStatusPending, "")
assert.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions core/models/channel_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
)

func TestChannelEvents(t *testing.T) {
ctx := testsuite.CTX()
db := testsuite.DB()
ctx, _, db, _ := testsuite.Get()

defer db.MustExec(`DELETE FROM channels_channelevent`)

start := time.Now()

Expand Down
6 changes: 3 additions & 3 deletions core/models/channel_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import (
)

func TestChannelLogs(t *testing.T) {
ctx := testsuite.CTX()
db := testsuite.DB()
ctx, _, db, _ := testsuite.Get()

db.MustExec(`DELETE FROM channels_channellog;`)
defer db.MustExec(`DELETE FROM channels_channellog`)

defer httpx.SetRequestor(httpx.DefaultRequestor)
httpx.SetRequestor(httpx.NewMockRequestor(map[string][]httpx.MockResponse{
Expand All @@ -29,6 +28,7 @@ func TestChannelLogs(t *testing.T) {
require.NoError(t, err)

channel := oa.ChannelByID(testdata.TwilioChannel.ID)
require.NotNil(t, channel)

req1, _ := httpx.NewRequest("GET", "http://rapidpro.io", nil, nil)
trace1, err := httpx.DoTrace(http.DefaultClient, req1, nil, nil, -1)
Expand Down
Loading

0 comments on commit 6727a2f

Please sign in to comment.