Skip to content

Commit

Permalink
Simplify use of test contacts in handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed May 31, 2021
1 parent f229184 commit c42e9dd
Show file tree
Hide file tree
Showing 19 changed files with 71 additions and 67 deletions.
4 changes: 2 additions & 2 deletions core/handlers/airtime_transferred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func TestAirtimeTransferred(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewTransferAirtime(handlers.NewActionUUID(), map[string]decimal.Decimal{"USD": decimal.RequireFromString(`3.50`)}, "Transfer"),
},
},
Expand All @@ -385,7 +385,7 @@ func TestAirtimeTransferred(t *testing.T) {
},
{
Actions: handlers.ContactActionMap{
testdata.George.ID: []flows.Action{
testdata.George: []flows.Action{
actions.NewTransferAirtime(handlers.NewActionUUID(), map[string]decimal.Decimal{"USD": decimal.RequireFromString(`3.50`)}, "Transfer"),
},
},
Expand Down
26 changes: 15 additions & 11 deletions core/handlers/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
"github.com/stretchr/testify/require"
)

type ContactActionMap map[models.ContactID][]flows.Action
type ContactMsgMap map[models.ContactID]*flows.MsgIn
type ContactModifierMap map[models.ContactID][]flows.Modifier
type ContactActionMap map[*testdata.Contact][]flows.Action
type ContactMsgMap map[*testdata.Contact]*flows.MsgIn
type ContactModifierMap map[*testdata.Contact][]flows.Modifier

type modifyResult struct {
Contact *flows.Contact `json:"contact"`
Expand Down Expand Up @@ -83,11 +83,11 @@ func createTestFlow(t *testing.T, uuid assets.FlowUUID, tc TestCase) flows.Flow
exits := make([]flows.Exit, len(tc.Actions))
exitNodes := make([]flows.Node, len(tc.Actions))
i = 0
for cid, actions := range tc.Actions {
for contact, actions := range tc.Actions {
cases[i] = routers.NewCase(
uuids.New(),
"has_any_word",
[]string{fmt.Sprintf("%d", cid)},
[]string{fmt.Sprintf("%d", contact.ID)},
categoryUUIDs[i],
)

Expand All @@ -100,7 +100,7 @@ func createTestFlow(t *testing.T, uuid assets.FlowUUID, tc TestCase) flows.Flow

categories[i] = routers.NewCategory(
categoryUUIDs[i],
fmt.Sprintf("Contact %d", cid),
fmt.Sprintf("Contact %d", contact.ID),
exitUUIDs[i],
)

Expand Down Expand Up @@ -171,6 +171,11 @@ func RunTestCases(t *testing.T, tcs []TestCase) {
flowUUID := testdata.Favorites.UUID

for i, tc := range tcs {
msgsByContactID := make(map[models.ContactID]*flows.MsgIn)
for contact, msg := range tc.Msgs {
msgsByContactID[contact.ID] = msg
}

// build our flow for this test case
testFlow := createTestFlow(t, flowUUID, tc)
flowDef, err := json.Marshal(testFlow)
Expand All @@ -185,15 +190,15 @@ func RunTestCases(t *testing.T, tcs []TestCase) {
options := runner.NewStartOptions()
options.CommitHook = func(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, session []*models.Session) error {
for _, s := range session {
msg := tc.Msgs[s.ContactID()]
msg := msgsByContactID[s.ContactID()]
if msg != nil {
s.SetIncomingMsg(msg.ID(), "")
}
}
return nil
}
options.TriggerBuilder = func(contact *flows.Contact) flows.Trigger {
msg := tc.Msgs[models.ContactID(contact.ID())]
msg := msgsByContactID[models.ContactID(contact.ID())]
if msg == nil {
return triggers.NewBuilder(oa.Env(), testFlow.Reference(), contact).Manual().Build()
}
Expand All @@ -207,9 +212,8 @@ func RunTestCases(t *testing.T, tcs []TestCase) {

// create scenes for our contacts
scenes := make([]*models.Scene, 0, len(tc.Modifiers))
for contactID, mods := range tc.Modifiers {

contacts, err := models.LoadContacts(ctx, db, oa, []models.ContactID{contactID})
for contact, mods := range tc.Modifiers {
contacts, err := models.LoadContacts(ctx, db, oa, []models.ContactID{contact.ID})
assert.NoError(t, err)

contact := contacts[0]
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 @@ -26,7 +26,7 @@ func TestBroadcastCreated(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewSendBroadcast(handlers.NewActionUUID(), "hello world", nil, nil, []urns.URN{urns.URN("tel:+12065551212")}, nil, nil, nil),
},
},
Expand Down
8 changes: 4 additions & 4 deletions core/handlers/campaigns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ func TestCampaigns(t *testing.T) {
tcs := []handlers.TestCase{
{
Msgs: handlers.ContactMsgMap{
testdata.Cathy.ID: flows.NewMsgIn(flows.MsgUUID(uuids.New()), testdata.Cathy.URN, nil, "Hi there", nil),
testdata.Cathy: flows.NewMsgIn(flows.MsgUUID(uuids.New()), testdata.Cathy.URN, nil, "Hi there", nil),
},
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewRemoveContactGroups(handlers.NewActionUUID(), []*assets.GroupReference{doctors}, false),
actions.NewAddContactGroups(handlers.NewActionUUID(), []*assets.GroupReference{doctors}),
actions.NewSetContactField(handlers.NewActionUUID(), joined, "2029-09-15T12:00:00+00:00"),
actions.NewSetContactField(handlers.NewActionUUID(), joined, ""),
},
testdata.Bob.ID: []flows.Action{
testdata.Bob: []flows.Action{
actions.NewAddContactGroups(handlers.NewActionUUID(), []*assets.GroupReference{doctors}),
actions.NewSetContactField(handlers.NewActionUUID(), joined, "2029-09-15T12:00:00+00:00"),
actions.NewSetContactField(handlers.NewActionUUID(), joined, "2029-09-15T12:00:00+00:00"),
},
testdata.George.ID: []flows.Action{
testdata.George: []flows.Action{
actions.NewAddContactGroups(handlers.NewActionUUID(), []*assets.GroupReference{doctors}),
actions.NewSetContactField(handlers.NewActionUUID(), joined, "2029-09-15T12:00:00+00:00"),
actions.NewRemoveContactGroups(handlers.NewActionUUID(), []*assets.GroupReference{doctors}, false),
Expand Down
8 changes: 4 additions & 4 deletions core/handlers/contact_field_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ func TestContactFieldChanged(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewSetContactField(handlers.NewActionUUID(), gender, "Male"),
actions.NewSetContactField(handlers.NewActionUUID(), gender, "Female"),
actions.NewSetContactField(handlers.NewActionUUID(), age, ""),
},
testdata.George.ID: []flows.Action{
testdata.George: []flows.Action{
actions.NewSetContactField(handlers.NewActionUUID(), gender, "Male"),
actions.NewSetContactField(handlers.NewActionUUID(), gender, ""),
actions.NewSetContactField(handlers.NewActionUUID(), age, "40"),
},
testdata.Bob.ID: []flows.Action{
testdata.Bob: []flows.Action{
actions.NewSetContactField(handlers.NewActionUUID(), gender, ""),
actions.NewSetContactField(handlers.NewActionUUID(), gender, "Male"),
actions.NewSetContactField(handlers.NewActionUUID(), age, "Old"),
},
testdata.Alexandria.ID: []flows.Action{
testdata.Alexandria: []flows.Action{
actions.NewSetContactField(handlers.NewActionUUID(), age, ""),
actions.NewSetContactField(handlers.NewActionUUID(), gender, ""),
},
Expand Down
4 changes: 2 additions & 2 deletions core/handlers/contact_groups_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ func TestContactGroupsChanged(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewAddContactGroups(handlers.NewActionUUID(), []*assets.GroupReference{doctors}),
actions.NewAddContactGroups(handlers.NewActionUUID(), []*assets.GroupReference{doctors}),
actions.NewRemoveContactGroups(handlers.NewActionUUID(), []*assets.GroupReference{doctors}, false),
actions.NewAddContactGroups(handlers.NewActionUUID(), []*assets.GroupReference{testers}),
},
testdata.George.ID: []flows.Action{
testdata.George: []flows.Action{
actions.NewRemoveContactGroups(handlers.NewActionUUID(), []*assets.GroupReference{doctors}, false),
actions.NewAddContactGroups(handlers.NewActionUUID(), []*assets.GroupReference{testers}),
},
Expand Down
6 changes: 3 additions & 3 deletions core/handlers/contact_language_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ func TestContactLanguageChanged(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewSetContactLanguage(handlers.NewActionUUID(), "fra"),
actions.NewSetContactLanguage(handlers.NewActionUUID(), "eng"),
},
testdata.George.ID: []flows.Action{
testdata.George: []flows.Action{
actions.NewSetContactLanguage(handlers.NewActionUUID(), "spa"),
},
testdata.Alexandria.ID: []flows.Action{
testdata.Alexandria: []flows.Action{
actions.NewSetContactLanguage(handlers.NewActionUUID(), ""),
},
},
Expand Down
8 changes: 4 additions & 4 deletions core/handlers/contact_name_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ func TestContactNameChanged(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewSetContactName(handlers.NewActionUUID(), "Fred"),
actions.NewSetContactName(handlers.NewActionUUID(), "Tarzan"),
},
testdata.George.ID: []flows.Action{
testdata.George: []flows.Action{
actions.NewSetContactName(handlers.NewActionUUID(), "Geoff Newman"),
},
testdata.Bob.ID: []flows.Action{
testdata.Bob: []flows.Action{
actions.NewSetContactName(handlers.NewActionUUID(), ""),
},
testdata.Alexandria.ID: []flows.Action{
testdata.Alexandria: []flows.Action{
actions.NewSetContactName(handlers.NewActionUUID(), "😃234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"),
},
},
Expand Down
6 changes: 3 additions & 3 deletions core/handlers/contact_status_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestContactStatusChanged(t *testing.T) {
tcs := []handlers.TestCase{
{
Modifiers: handlers.ContactModifierMap{
testdata.Cathy.ID: []flows.Modifier{modifiers.NewStatus(flows.ContactStatusBlocked)},
testdata.Cathy: []flows.Modifier{modifiers.NewStatus(flows.ContactStatusBlocked)},
},
SQLAssertions: []handlers.SQLAssertion{
{
Expand All @@ -32,7 +32,7 @@ func TestContactStatusChanged(t *testing.T) {
},
{
Modifiers: handlers.ContactModifierMap{
testdata.Cathy.ID: []flows.Modifier{modifiers.NewStatus(flows.ContactStatusStopped)},
testdata.Cathy: []flows.Modifier{modifiers.NewStatus(flows.ContactStatusStopped)},
},
SQLAssertions: []handlers.SQLAssertion{
{
Expand All @@ -44,7 +44,7 @@ func TestContactStatusChanged(t *testing.T) {
},
{
Modifiers: handlers.ContactModifierMap{
testdata.Cathy.ID: []flows.Modifier{modifiers.NewStatus(flows.ContactStatusActive)},
testdata.Cathy: []flows.Modifier{modifiers.NewStatus(flows.ContactStatusActive)},
},
SQLAssertions: []handlers.SQLAssertion{
{
Expand Down
4 changes: 2 additions & 2 deletions core/handlers/contact_urns_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func TestContactURNsChanged(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewAddContactURN(handlers.NewActionUUID(), "tel", "12065551212"),
actions.NewAddContactURN(handlers.NewActionUUID(), "tel", "12065551212"),
actions.NewAddContactURN(handlers.NewActionUUID(), "telegram", "11551"),
actions.NewAddContactURN(handlers.NewActionUUID(), "tel", "+16055741111"),
},
testdata.George.ID: []flows.Action{},
testdata.George: []flows.Action{},
},
SQLAssertions: []handlers.SQLAssertion{
{
Expand Down
10 changes: 5 additions & 5 deletions core/handlers/input_labels_added_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ func TestInputLabelsAdded(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewAddInputLabels(handlers.NewActionUUID(), []*assets.LabelReference{reporting}),
actions.NewAddInputLabels(handlers.NewActionUUID(), []*assets.LabelReference{testing}),
actions.NewAddInputLabels(handlers.NewActionUUID(), []*assets.LabelReference{reporting}),
},
testdata.Bob.ID: []flows.Action{},
testdata.George.ID: []flows.Action{
testdata.Bob: []flows.Action{},
testdata.George: []flows.Action{
actions.NewAddInputLabels(handlers.NewActionUUID(), []*assets.LabelReference{testing}),
actions.NewAddInputLabels(handlers.NewActionUUID(), []*assets.LabelReference{reporting}),
},
},
Msgs: handlers.ContactMsgMap{
testdata.Cathy.ID: msg1,
testdata.Bob.ID: msg2,
testdata.Cathy: msg1,
testdata.Bob: msg2,
},
SQLAssertions: []handlers.SQLAssertion{
{
Expand Down
16 changes: 8 additions & 8 deletions core/handlers/msg_created_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ func TestMsgCreated(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewSendMsg(handlers.NewActionUUID(), "Hello World", nil, []string{"yes", "no"}, true),
},
testdata.George.ID: []flows.Action{
testdata.George: []flows.Action{
actions.NewSendMsg(handlers.NewActionUUID(), "Hello Attachments", []string{"image/png:/images/image1.png"}, nil, true),
},
testdata.Bob.ID: []flows.Action{
testdata.Bob: []flows.Action{
actions.NewSendMsg(handlers.NewActionUUID(), "No URNs", nil, nil, false),
},
testdata.Alexandria.ID: []flows.Action{
testdata.Alexandria: []flows.Action{
templateAction,
},
},
Msgs: handlers.ContactMsgMap{
testdata.Cathy.ID: msg1,
testdata.Cathy: msg1,
},
SQLAssertions: []handlers.SQLAssertion{
{
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestNoTopup(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewSendMsg(handlers.NewActionUUID(), "No Topup", nil, nil, false),
},
},
Expand Down Expand Up @@ -156,14 +156,14 @@ func TestNewURN(t *testing.T) {
{
Actions: handlers.ContactActionMap{
// brand new URN on Cathy
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewAddContactURN(handlers.NewActionUUID(), "telegram", "12345"),
actions.NewSetContactChannel(handlers.NewActionUUID(), assets.NewChannelReference(telegramUUID, "telegram")),
actions.NewSendMsg(handlers.NewActionUUID(), "Cathy Message", nil, nil, false),
},

// Bob is stealing a URN previously assigned to George
testdata.Bob.ID: []flows.Action{
testdata.Bob: []flows.Action{
actions.NewAddContactURN(handlers.NewActionUUID(), "telegram", "67890"),
actions.NewSetContactChannel(handlers.NewActionUUID(), assets.NewChannelReference(telegramUUID, "telegram")),
actions.NewSendMsg(handlers.NewActionUUID(), "Bob Message", nil, nil, false),
Expand Down
10 changes: 5 additions & 5 deletions core/handlers/msg_received_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func TestMsgReceived(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewSendMsg(handlers.NewActionUUID(), "Hello World", nil, nil, false),
},
testdata.George.ID: []flows.Action{
testdata.George: []flows.Action{
actions.NewSendMsg(handlers.NewActionUUID(), "Hello world", nil, nil, false),
},
},
Msgs: handlers.ContactMsgMap{
testdata.Cathy.ID: testdata.InsertIncomingMsg(t, db, testdata.Org1.ID, testdata.Cathy.ID, testdata.Cathy.URN, testdata.Cathy.URNID, "start"),
testdata.Cathy: testdata.InsertIncomingMsg(t, db, testdata.Org1.ID, testdata.Cathy.ID, testdata.Cathy.URN, testdata.Cathy.URNID, "start"),
},
SQLAssertions: []handlers.SQLAssertion{
{
Expand All @@ -48,12 +48,12 @@ func TestMsgReceived(t *testing.T) {
{
FlowType: flows.FlowTypeMessagingOffline,
Actions: handlers.ContactActionMap{
testdata.Bob.ID: []flows.Action{
testdata.Bob: []flows.Action{
actions.NewSendMsg(handlers.NewActionUUID(), "Hello World", nil, nil, false),
},
},
Msgs: handlers.ContactMsgMap{
testdata.Bob.ID: flows.NewMsgIn(flows.MsgUUID(uuids.New()), urns.NilURN, nil, "Hi offline", nil),
testdata.Bob: flows.NewMsgIn(flows.MsgUUID(uuids.New()), urns.NilURN, nil, "Hi offline", nil),
},
SQLAssertions: []handlers.SQLAssertion{
{
Expand Down
2 changes: 1 addition & 1 deletion core/handlers/service_called_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestServiceCalled(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewCallClassifier(handlers.NewActionUUID(), wit, "book me a flight", "flight"),
},
},
Expand Down
4 changes: 2 additions & 2 deletions core/handlers/session_triggered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestSessionTriggered(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{
testdata.Cathy: []flows.Action{
actions.NewStartSession(handlers.NewActionUUID(), simpleFlow.FlowReference(), nil, []*flows.ContactReference{contactRef}, []*assets.GroupReference{groupRef}, nil, true),
},
},
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestQuerySessionTriggered(t *testing.T) {
tcs := []handlers.TestCase{
{
Actions: handlers.ContactActionMap{
testdata.Cathy.ID: []flows.Action{sessionAction},
testdata.Cathy: []flows.Action{sessionAction},
},
SQLAssertions: []handlers.SQLAssertion{
{
Expand Down
Loading

0 comments on commit c42e9dd

Please sign in to comment.