Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Sep 17, 2021
1 parent a056759 commit 31052e7
Show file tree
Hide file tree
Showing 25 changed files with 112 additions and 59 deletions.
4 changes: 3 additions & 1 deletion core/handlers/session_triggered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ func TestSessionTriggered(t *testing.T) {
}

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

defer testsuite.Reset()

oa, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
assert.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions core/models/contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ func TestGetOrCreateContactIDsFromURNs(t *testing.T) {
func TestGetOrCreateContactIDsFromURNsRace(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer testsuite.ResetData(db)

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

Expand Down
2 changes: 2 additions & 0 deletions core/models/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func TestLoadGroups(t *testing.T) {
func TestDynamicGroups(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer testsuite.Reset()

// insert an event on our campaign
var eventID models.CampaignEventID
testsuite.DB().Get(&eventID,
Expand Down
5 changes: 1 addition & 4 deletions core/models/ticket_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import (
func TestTicketEvents(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer func() {
db.MustExec(`DELETE FROM tickets_ticketevent`)
db.MustExec(`DELETE FROM tickets_ticket`)
}()
defer testsuite.ResetData(db)

ticket := testdata.InsertOpenTicket(db, testdata.Org1, testdata.Cathy, testdata.Mailgun, testdata.DefaultTopic, "Have you seen my cookies?", "17", nil)
modelTicket := ticket.Load(db)
Expand Down
2 changes: 0 additions & 2 deletions core/models/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,6 @@ func TicketsChangeTopic(ctx context.Context, db Queryer, oa *OrgAssets, userID U
eventsByTicket := make(map[*Ticket]*TicketEvent, len(tickets))
now := dates.Now()

fmt.Printf("TicketsChangeTopic topic=%d\n", topicID)

for _, ticket := range tickets {
fmt.Printf("ticket #%d topic=%d\n", ticket.ID(), ticket.TopicID())
if ticket.TopicID() != topicID {
Expand Down
4 changes: 3 additions & 1 deletion core/models/tickets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
func TestTicketers(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer testsuite.Reset()

// can load directly by UUID
ticketer, err := models.LookupTicketerByUUID(ctx, db, testdata.Zendesk.UUID)
assert.NoError(t, err)
Expand Down Expand Up @@ -324,8 +326,8 @@ func TestCloseTickets(t *testing.T) {
func TestReopenTickets(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer httpx.SetRequestor(httpx.DefaultRequestor)
defer testsuite.ResetData(db)
defer httpx.SetRequestor(httpx.DefaultRequestor)

httpx.SetRequestor(httpx.NewMockRequestor(map[string][]httpx.MockResponse{
"https://api.mailgun.net/v3/tickets.rapidpro.io/messages": {
Expand Down
4 changes: 3 additions & 1 deletion core/msgio/courier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
)

func TestQueueCourierMessages(t *testing.T) {
ctx, _, db, rp := testsuite.Reset()
ctx, _, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

// create an Andoid channel
androidChannel := testdata.InsertChannel(db, testdata.Org1, "A", "Android 1", []string{"tel"}, "SR", map[string]interface{}{"FCM_ID": "FCMID"})

Expand Down
11 changes: 8 additions & 3 deletions core/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
)

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

defer testsuite.Reset()

campaign := triggers.NewCampaignReference(triggers.CampaignUUID(testdata.RemindersCampaign.UUID), "Doctor Reminders")

Expand Down Expand Up @@ -96,7 +98,9 @@ func TestCampaignStarts(t *testing.T) {
}

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

defer testsuite.Reset()

// create a start object
testdata.InsertFlowStart(db, testdata.Org1, testdata.SingleMessage, nil)
Expand Down Expand Up @@ -163,8 +167,9 @@ func TestBatchStart(t *testing.T) {
}

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

defer testsuite.Reset()
defer testsuite.ResetStorage()

// write sessions to s3 storage
Expand Down
8 changes: 6 additions & 2 deletions core/tasks/broadcasts/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
)

func TestBroadcastEvents(t *testing.T) {
ctx, _, db, rp := testsuite.Reset()
ctx, _, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

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

Expand Down Expand Up @@ -118,10 +120,12 @@ func TestBroadcastEvents(t *testing.T) {
}

func TestBroadcastTask(t *testing.T) {
ctx, _, db, rp := testsuite.Reset()
ctx, _, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

oa, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
assert.NoError(t, err)
eng := envs.Language("eng")
Expand Down
9 changes: 6 additions & 3 deletions core/tasks/campaigns/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import (
)

func TestCampaigns(t *testing.T) {
ctx, rt, db, rp := testsuite.Reset()

ctx, rt, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

// let's create a campaign event fire for one of our contacts (for now this is totally hacked, they aren't in the group and
// their relative to date isn't relative, but this still tests execution)
rt.DB.MustExec(`INSERT INTO campaigns_eventfire(scheduled, contact_id, event_id) VALUES (NOW(), $1, $3), (NOW(), $2, $3);`, testdata.Cathy.ID, testdata.George.ID, testdata.RemindersEvent1.ID)
Expand Down Expand Up @@ -48,10 +49,12 @@ func TestCampaigns(t *testing.T) {
}

func TestIVRCampaigns(t *testing.T) {
ctx, rt, db, rp := testsuite.Reset()
ctx, rt, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

// let's create a campaign event fire for one of our contacts (for now this is totally hacked, they aren't in the group and
// their relative to date isn't relative, but this still tests execution)
rt.DB.MustExec(`UPDATE campaigns_campaignevent SET flow_id = $1 WHERE id = $2`, testdata.IVRFlow.ID, testdata.RemindersEvent1.ID)
Expand Down
4 changes: 3 additions & 1 deletion core/tasks/campaigns/schedule_campaign_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
)

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

defer testsuite.Reset()

// add bob, george and alexandria to doctors group which campaign is based on
testdata.DoctorsGroup.Add(db, testdata.Bob, testdata.George, testdata.Alexandria)
Expand Down
4 changes: 3 additions & 1 deletion core/tasks/contacts/populate_dynamic_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
)

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

defer testsuite.Reset()

mes := testsuite.NewMockElasticServer()
defer mes.Close()
Expand Down
8 changes: 2 additions & 6 deletions core/tasks/expirations/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package expirations

import (
"encoding/json"
"os"
"testing"
"time"

Expand All @@ -17,16 +16,13 @@ import (
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
testsuite.Reset()
os.Exit(m.Run())
}

func TestExpirations(t *testing.T) {
ctx, _, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

err := marker.ClearTasks(rc, expirationLock)
assert.NoError(t, err)

Expand Down
4 changes: 3 additions & 1 deletion core/tasks/handler/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (
)

func TestRetryMsgs(t *testing.T) {
ctx, rt, db, rp := testsuite.Reset()
ctx, rt, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

// noop does nothing
err := handler.RetryPendingMsgs(ctx, db, rp, "test", "test")
assert.NoError(t, err)
Expand Down
22 changes: 15 additions & 7 deletions core/tasks/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (
)

func TestMsgEvents(t *testing.T) {
ctx, rt, db, rp := testsuite.Reset()
ctx, rt, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

testdata.InsertKeywordTrigger(db, testdata.Org1, testdata.Favorites, "start", models.MatchOnly, nil, nil)
testdata.InsertKeywordTrigger(db, testdata.Org1, testdata.IVRFlow, "ivr", models.MatchOnly, nil, nil)

Expand Down Expand Up @@ -233,10 +235,12 @@ func TestMsgEvents(t *testing.T) {
}

func TestChannelEvents(t *testing.T) {
ctx, rt, db, rp := testsuite.Reset()
ctx, rt, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

// add some channel event triggers
testdata.InsertNewConversationTrigger(db, testdata.Org1, testdata.Favorites, testdata.TwitterChannel)
testdata.InsertReferralTrigger(db, testdata.Org1, testdata.PickANumber, "", testdata.VonageChannel)
Expand Down Expand Up @@ -302,10 +306,12 @@ func TestChannelEvents(t *testing.T) {
}

func TestTicketEvents(t *testing.T) {
ctx, rt, db, _ := testsuite.Reset()
ctx, rt, db, _ := testsuite.Get()
rc := rt.RP.Get()
defer rc.Close()

defer testsuite.Reset()

// add a ticket closed trigger
testdata.InsertTicketClosedTrigger(rt.DB, testdata.Org1, testdata.Favorites)

Expand All @@ -327,10 +333,12 @@ func TestTicketEvents(t *testing.T) {
}

func TestStopEvent(t *testing.T) {
ctx, rt, db, rp := testsuite.Reset()
ctx, rt, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

// schedule an event for cathy and george
db.MustExec(`INSERT INTO campaigns_eventfire(scheduled, contact_id, event_id) VALUES (NOW(), $1, $3), (NOW(), $2, $3);`, testdata.Cathy.ID, testdata.George.ID, testdata.RemindersEvent1.ID)

Expand Down Expand Up @@ -368,10 +376,12 @@ func TestStopEvent(t *testing.T) {
}

func TestTimedEvents(t *testing.T) {
ctx, rt, db, rp := testsuite.Reset()
ctx, rt, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

// start to start our favorites flow
testdata.InsertKeywordTrigger(db, testdata.Org1, testdata.Favorites, "start", models.MatchOnly, nil, nil)

Expand Down Expand Up @@ -527,6 +537,4 @@ func TestTimedEvents(t *testing.T) {

testsuite.AssertQuery(t, db, `SELECT count(*) from flows_flowrun WHERE is_active = FALSE AND status = 'F' AND id = $1`, runID).Returns(1)
testsuite.AssertQuery(t, db, `SELECT count(*) from flows_flowsession WHERE status = 'F' AND id = $1`, sessionID).Returns(1)

testsuite.ResetDB()
}
4 changes: 3 additions & 1 deletion core/tasks/interrupts/interrupt_sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
)

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

defer testsuite.Reset()

insertConnection := func(orgID models.OrgID, channelID models.ChannelID, contactID models.ContactID, urnID models.URNID) models.ConnectionID {
var connectionID models.ConnectionID
Expand Down
4 changes: 3 additions & 1 deletion core/tasks/ivr/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (
)

func TestRetries(t *testing.T) {
ctx, _, db, rp := testsuite.Reset()
ctx, _, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

// register our mock client
ivr.RegisterClientType(models.ChannelType("ZZ"), newMockClient)

Expand Down
4 changes: 3 additions & 1 deletion core/tasks/ivr/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
)

func TestIVR(t *testing.T) {
ctx, _, db, rp := testsuite.Reset()
ctx, _, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

// register our mock client
ivr.RegisterClientType(models.ChannelType("ZZ"), newMockClient)

Expand Down
4 changes: 3 additions & 1 deletion core/tasks/schedules/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
)

func TestCheckSchedules(t *testing.T) {
ctx, _, db, rp := testsuite.Reset()
ctx, _, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

// add a schedule and tie a broadcast to it
var s1 models.ScheduleID
err := db.Get(
Expand Down
4 changes: 3 additions & 1 deletion core/tasks/starts/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import (
)

func TestStarts(t *testing.T) {
ctx, rt, db, rp := testsuite.Reset()
ctx, rt, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

mes := testsuite.NewMockElasticServer()
defer mes.Close()

Expand Down
4 changes: 3 additions & 1 deletion core/tasks/timeouts/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (
)

func TestTimeouts(t *testing.T) {
ctx, _, db, rp := testsuite.Reset()
ctx, _, db, rp := testsuite.Get()
rc := rp.Get()
defer rc.Close()

defer testsuite.Reset()

err := marker.ClearTasks(rc, timeoutLock)
assert.NoError(t, err)

Expand Down
Loading

0 comments on commit 31052e7

Please sign in to comment.