Skip to content

Commit

Permalink
Merge pull request rapidpro#495 from nyaruka/testdata
Browse files Browse the repository at this point in the history
Less resetting of test database for faster tests
  • Loading branch information
rowanseymour authored Sep 9, 2021
2 parents 7d5220e + a2f2fca commit 2baf4a0
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 73 deletions.
76 changes: 37 additions & 39 deletions core/models/contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestContacts(t *testing.T) {
assert.NotNil(t, bob.Fields()["joined"].QueryValue())
assert.Equal(t, 2, len(bob.URNs()))
assert.Equal(t, "tel:+16055742222?id=10001&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "whatsapp:250788373373?id=20121&priority=999", bob.URNs()[1].String())
assert.Equal(t, "whatsapp:250788373373?id=30000&priority=999", bob.URNs()[1].String())
assert.Equal(t, 0, bob.Groups().Count())
assert.Equal(t, 0, bob.Tickets().Count()) // because ticketer no longer exists

Expand All @@ -97,7 +97,7 @@ func TestContacts(t *testing.T) {
bob, err = modelContacts[1].FlowContact(org)
assert.NoError(t, err)
assert.Equal(t, "tel:+16055742222?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=10001&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "whatsapp:250788373373?id=20121&priority=999", bob.URNs()[1].String())
assert.Equal(t, "whatsapp:250788373373?id=30000&priority=999", bob.URNs()[1].String())

// add another tel urn to bob
testdata.InsertContactURN(db, testdata.Org1, testdata.Bob, urns.URN("tel:+250788373373"), 10)
Expand All @@ -107,40 +107,40 @@ func TestContacts(t *testing.T) {
assert.NoError(t, err)

// set our preferred channel again
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, models.URNID(20122), channel)
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, models.URNID(30001), channel)
assert.NoError(t, err)

bob, err = modelContacts[0].FlowContact(org)
assert.NoError(t, err)
assert.Equal(t, "tel:+250788373373?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20122&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+250788373373?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30001&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+16055742222?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=10001&priority=999", bob.URNs()[1].String())
assert.Equal(t, "whatsapp:250788373373?id=20121&priority=998", bob.URNs()[2].String())
assert.Equal(t, "whatsapp:250788373373?id=30000&priority=998", bob.URNs()[2].String())

// no op this time
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, models.URNID(20122), channel)
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, models.URNID(30001), channel)
assert.NoError(t, err)

bob, err = modelContacts[0].FlowContact(org)
assert.NoError(t, err)
assert.Equal(t, "tel:+250788373373?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20122&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+250788373373?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30001&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+16055742222?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=10001&priority=999", bob.URNs()[1].String())
assert.Equal(t, "whatsapp:250788373373?id=20121&priority=998", bob.URNs()[2].String())
assert.Equal(t, "whatsapp:250788373373?id=30000&priority=998", bob.URNs()[2].String())

// calling with no channel is a noop on the channel
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, models.URNID(20122), nil)
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, models.URNID(30001), nil)
assert.NoError(t, err)

bob, err = modelContacts[0].FlowContact(org)
assert.NoError(t, err)
assert.Equal(t, "tel:+250788373373?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20122&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+250788373373?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30001&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+16055742222?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=10001&priority=999", bob.URNs()[1].String())
assert.Equal(t, "whatsapp:250788373373?id=20121&priority=998", bob.URNs()[2].String())
assert.Equal(t, "whatsapp:250788373373?id=30000&priority=998", bob.URNs()[2].String())
}

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

defer testsuite.Reset()
defer testdata.ResetContactData(db)

testdata.InsertContactGroup(db, testdata.Org1, "d636c966-79c1-4417-9f1c-82ad629773a2", "Kinyarwanda", "language = kin")

Expand All @@ -155,11 +155,11 @@ func TestCreateContact(t *testing.T) {

assert.Equal(t, "Rich", contact.Name())
assert.Equal(t, envs.Language(`kin`), contact.Language())
assert.Equal(t, []urns.URN{"telegram:200001?id=20122&priority=1000", "telegram:200002?id=20121&priority=999"}, contact.URNs())
assert.Equal(t, []urns.URN{"telegram:200001?id=30001&priority=1000", "telegram:200002?id=30000&priority=999"}, contact.URNs())

assert.Equal(t, "Rich", flowContact.Name())
assert.Equal(t, envs.Language(`kin`), flowContact.Language())
assert.Equal(t, []urns.URN{"telegram:200001?id=20122&priority=1000", "telegram:200002?id=20121&priority=999"}, flowContact.URNs().RawURNs())
assert.Equal(t, []urns.URN{"telegram:200001?id=30001&priority=1000", "telegram:200002?id=30000&priority=999"}, flowContact.URNs().RawURNs())
assert.Len(t, flowContact.Groups().All(), 1)
assert.Equal(t, assets.GroupUUID("d636c966-79c1-4417-9f1c-82ad629773a2"), flowContact.Groups().All()[0].UUID())

Expand All @@ -170,7 +170,7 @@ func TestCreateContact(t *testing.T) {
func TestCreateContactRace(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer testsuite.Reset()
defer testdata.ResetContactData(db)

oa, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
assert.NoError(t, err)
Expand Down Expand Up @@ -199,18 +199,17 @@ func TestCreateContactRace(t *testing.T) {
func TestGetOrCreateContact(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer testsuite.Reset()
defer testdata.ResetContactData(db)

testdata.InsertContactGroup(db, testdata.Org1, "d636c966-79c1-4417-9f1c-82ad629773a2", "Telegrammer", `telegram = 100001`)
testdata.InsertContactGroup(db, testdata.Org1, "dcc16d85-8274-4d19-a3c2-152d4ee99380", "Telegrammer", `telegram = 100001`)

// add some orphaned URNs
testdata.InsertContactURN(db, testdata.Org1, nil, urns.URN("telegram:200001"), 100)
testdata.InsertContactURN(db, testdata.Org1, nil, urns.URN("telegram:200002"), 100)

var maxContactID models.ContactID
db.Get(&maxContactID, `SELECT max(id) FROM contacts_contact`)
newContact := func() models.ContactID { maxContactID++; return maxContactID }
prevContact := func() models.ContactID { return maxContactID }
contactIDSeq := models.ContactID(30000)
newContact := func() models.ContactID { id := contactIDSeq; contactIDSeq++; return id }
prevContact := func() models.ContactID { return contactIDSeq - 1 }

oa, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
require.NoError(t, err)
Expand Down Expand Up @@ -247,52 +246,52 @@ func TestGetOrCreateContact(t *testing.T) {
[]urns.URN{urns.URN("telegram:100001")},
newContact(), // creates new contact
true,
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20123&priority=1000"},
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30002&priority=1000"},
testdata.TwilioChannel.ID,
[]assets.GroupUUID{"d636c966-79c1-4417-9f1c-82ad629773a2"},
[]assets.GroupUUID{"dcc16d85-8274-4d19-a3c2-152d4ee99380"},
},
{
testdata.Org1.ID,
[]urns.URN{urns.URN("telegram:100001")},
prevContact(), // returns the same created contact
false,
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20123&priority=1000"},
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30002&priority=1000"},
models.NilChannelID,
[]assets.GroupUUID{"d636c966-79c1-4417-9f1c-82ad629773a2"},
[]assets.GroupUUID{"dcc16d85-8274-4d19-a3c2-152d4ee99380"},
},
{
testdata.Org1.ID,
[]urns.URN{urns.URN("telegram:100001"), urns.URN("telegram:100002")},
prevContact(), // same again as other URNs don't exist
false,
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20123&priority=1000"},
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30002&priority=1000"},
models.NilChannelID,
[]assets.GroupUUID{"d636c966-79c1-4417-9f1c-82ad629773a2"},
[]assets.GroupUUID{"dcc16d85-8274-4d19-a3c2-152d4ee99380"},
},
{
testdata.Org1.ID,
[]urns.URN{urns.URN("telegram:100002"), urns.URN("telegram:100001")},
prevContact(), // same again as other URNs don't exist
false,
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20123&priority=1000"},
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30002&priority=1000"},
models.NilChannelID,
[]assets.GroupUUID{"d636c966-79c1-4417-9f1c-82ad629773a2"},
[]assets.GroupUUID{"dcc16d85-8274-4d19-a3c2-152d4ee99380"},
},
{
testdata.Org1.ID,
[]urns.URN{urns.URN("telegram:200001"), urns.URN("telegram:100001")},
prevContact(), // same again as other URNs are orphaned
false,
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20123&priority=1000"},
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30002&priority=1000"},
models.NilChannelID,
[]assets.GroupUUID{"d636c966-79c1-4417-9f1c-82ad629773a2"},
[]assets.GroupUUID{"dcc16d85-8274-4d19-a3c2-152d4ee99380"},
},
{
testdata.Org1.ID,
[]urns.URN{urns.URN("telegram:100003"), urns.URN("telegram:100004")}, // 2 new URNs
newContact(),
true,
[]urns.URN{"telegram:100003?id=20124&priority=1000", "telegram:100004?id=20125&priority=999"},
[]urns.URN{"telegram:100003?id=30003&priority=1000", "telegram:100004?id=30004&priority=999"},
models.NilChannelID,
[]assets.GroupUUID{},
},
Expand All @@ -301,7 +300,7 @@ func TestGetOrCreateContact(t *testing.T) {
[]urns.URN{urns.URN("telegram:100005"), urns.URN("telegram:200002")}, // 1 new, 1 orphaned
newContact(),
true,
[]urns.URN{"telegram:100005?id=20126&priority=1000", "telegram:200002?id=20122&priority=999"},
[]urns.URN{"telegram:100005?id=30005&priority=1000", "telegram:200002?id=30001&priority=999"},
models.NilChannelID,
[]assets.GroupUUID{},
},
Expand All @@ -327,7 +326,7 @@ func TestGetOrCreateContact(t *testing.T) {
func TestGetOrCreateContactRace(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer testsuite.Reset()
defer testdata.ResetContactData(db)

oa, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
assert.NoError(t, err)
Expand Down Expand Up @@ -356,15 +355,14 @@ func TestGetOrCreateContactRace(t *testing.T) {
func TestGetOrCreateContactIDsFromURNs(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer testsuite.Reset()
defer testdata.ResetContactData(db)

// add an orphaned URN
testdata.InsertContactURN(db, testdata.Org1, nil, urns.URN("telegram:200001"), 100)

var maxContactID models.ContactID
db.Get(&maxContactID, `SELECT max(id) FROM contacts_contact`)
newContact := func() models.ContactID { maxContactID++; return maxContactID }
prevContact := func() models.ContactID { return maxContactID }
contactIDSeq := models.ContactID(30000)
newContact := func() models.ContactID { id := contactIDSeq; contactIDSeq++; return id }
prevContact := func() models.ContactID { return contactIDSeq - 1 }

org, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
assert.NoError(t, err)
Expand Down
11 changes: 8 additions & 3 deletions core/models/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ func TestContactImports(t *testing.T) {

defer testsuite.Reset()

testdata.DeleteContactsAndURNs(db)
// start with no contacts or URNs
db.MustExec(`DELETE FROM contacts_contacturn`)
db.MustExec(`DELETE FROM contacts_contactgroup_contacts`)
db.MustExec(`DELETE FROM contacts_contact`)
db.MustExec(`ALTER SEQUENCE contacts_contact_id_seq RESTART WITH 10000`)
db.MustExec(`ALTER SEQUENCE contacts_contacturn_id_seq RESTART WITH 10000`)

// add contact in other org to make sure we can't update it
testdata.InsertContact(db, testdata.Org2, "f7a8016d-69a6-434b-aae7-5142ce4a98ba", "Xavier", "spa")
Expand Down Expand Up @@ -63,7 +68,7 @@ func TestContactImports(t *testing.T) {
defer uuids.SetGenerator(uuids.DefaultGenerator)

for i, tc := range tcs {
importID := testdata.InsertContactImport(db, testdata.Org1)
importID := testdata.InsertContactImport(db, testdata.Org1, testdata.Admin)
batchID := testdata.InsertContactImportBatch(db, importID, tc.Specs)

batch, err := models.LoadContactImportBatch(ctx, db, batchID)
Expand Down Expand Up @@ -144,7 +149,7 @@ func TestContactImports(t *testing.T) {
func TestContactImportBatch(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

importID := testdata.InsertContactImport(db, testdata.Org1)
importID := testdata.InsertContactImport(db, testdata.Org1, testdata.Admin)
batchID := testdata.InsertContactImportBatch(db, importID, []byte(`[
{"name": "Norbert", "language": "eng", "urns": ["tel:+16055740001"]},
{"name": "Leah", "urns": ["tel:+16055740002"]}
Expand Down
2 changes: 1 addition & 1 deletion core/tasks/contacts/import_contact_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestImportContactBatch(t *testing.T) {
ctx, rt, db, _ := testsuite.Get()

importID := testdata.InsertContactImport(db, testdata.Org1)
importID := testdata.InsertContactImport(db, testdata.Org1, testdata.Admin)
batchID := testdata.InsertContactImportBatch(db, importID, []byte(`[
{"name": "Norbert", "language": "eng", "urns": ["tel:+16055740001"]},
{"name": "Leah", "urns": ["tel:+16055740002"]}
Expand Down
Binary file modified mailroom_test.dump
Binary file not shown.
30 changes: 21 additions & 9 deletions testsuite/testdata/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,26 @@ func InsertContactURN(db *sqlx.DB, org *Org, contact *Contact, urn urns.URN, pri
return id
}

// DeleteContactsAndURNs deletes all contacts and URNs
func DeleteContactsAndURNs(db *sqlx.DB) {
// ResetContactData removes contact data not in the test database dump. Note that this function can't
// undo changes made to the contact data in the test database dump.
func ResetContactData(db *sqlx.DB) {
db.MustExec(`DELETE FROM tickets_ticketevent`)
db.MustExec(`DELETE FROM tickets_ticket`)
db.MustExec(`DELETE FROM channels_channelcount`)
db.MustExec(`DELETE FROM msgs_msg`)
db.MustExec(`DELETE FROM contacts_contacturn`)
db.MustExec(`DELETE FROM contacts_contactgroup_contacts`)
db.MustExec(`DELETE FROM contacts_contact`)

// reset id sequences back to a known number
db.MustExec(`ALTER SEQUENCE contacts_contact_id_seq RESTART WITH 10000`)
db.MustExec(`ALTER SEQUENCE contacts_contacturn_id_seq RESTART WITH 10000`)
db.MustExec(`DELETE FROM contacts_contacturn WHERE id >= 30000`)
db.MustExec(`DELETE FROM contacts_contactgroup_contacts WHERE contact_id >= 30000`)
db.MustExec(`DELETE FROM contacts_contact WHERE id >= 30000`)
db.MustExec(`DELETE FROM contacts_contactgroupcount WHERE group_id >= 30000`)
db.MustExec(`DELETE FROM contacts_contactgroup WHERE id >= 30000`)

// reset id sequences back
db.MustExec(`ALTER SEQUENCE tickets_ticket_id_seq RESTART WITH 1`)
db.MustExec(`ALTER SEQUENCE msgs_msg_id_seq RESTART WITH 1`)
db.MustExec(`ALTER SEQUENCE contacts_contact_id_seq RESTART WITH 30000`)
db.MustExec(`ALTER SEQUENCE contacts_contacturn_id_seq RESTART WITH 30000`)
db.MustExec(`ALTER SEQUENCE contacts_contactgroup_id_seq RESTART WITH 30000`)

// because groups have changed
models.FlushCache()
}
6 changes: 3 additions & 3 deletions testsuite/testdata/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
)

// InsertContactImport inserts a contact import
func InsertContactImport(db *sqlx.DB, org *Org) models.ContactImportID {
func InsertContactImport(db *sqlx.DB, org *Org, createdBy *User) models.ContactImportID {
var importID models.ContactImportID
must(db.Get(&importID, `INSERT INTO contacts_contactimport(org_id, file, original_filename, headers, mappings, num_records, group_id, started_on, created_on, created_by_id, modified_on, modified_by_id, is_active)
VALUES($1, 'contact_imports/1234.xlsx', 'contacts.xlsx', '{"Name", "URN:Tel"}', '{}', 30, NULL, $2, $2, 1, $2, 1, TRUE) RETURNING id`, org.ID, dates.Now(),
must(db.Get(&importID, `INSERT INTO contacts_contactimport(org_id, file, original_filename, mappings, num_records, group_id, started_on, status, created_on, created_by_id, modified_on, modified_by_id, is_active)
VALUES($1, 'contact_imports/1234.xlsx', 'contacts.xlsx', '{}', 30, NULL, $2, 'O', $2, $3, $2, $3, TRUE) RETURNING id`, org.ID, dates.Now(), createdBy.ID,
))
return importID
}
Expand Down
2 changes: 1 addition & 1 deletion web/contact/testdata/create.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"timezone": "America/Los_Angeles",
"created_on": "2018-07-06T12:30:00.123457Z",
"urns": [
"tel:+16055700001?id=20121&priority=1000"
"tel:+16055700001?id=30000&priority=1000"
],
"groups": [
{
Expand Down
14 changes: 7 additions & 7 deletions web/contact/testdata/modify.json
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@
"timezone": "America/Los_Angeles",
"created_on": "2018-07-06T12:30:00.123457Z",
"urns": [
"tel:+255788555111?id=20121&priority=1000"
"tel:+255788555111?id=30000&priority=1000"
]
},
"events": [
Expand Down Expand Up @@ -1060,7 +1060,7 @@
"timezone": "America/Los_Angeles",
"created_on": "2018-07-06T12:30:00.123457Z",
"urns": [
"tel:+255788555111?id=20121&priority=1000",
"tel:+255788555111?id=30000&priority=1000",
"tel:+255788555222"
]
},
Expand All @@ -1069,7 +1069,7 @@
"type": "contact_urns_changed",
"created_on": "2018-07-06T12:30:00.123456789Z",
"urns": [
"tel:+255788555111?id=20121&priority=1000",
"tel:+255788555111?id=30000&priority=1000",
"tel:+255788555222"
]
}
Expand Down Expand Up @@ -1113,8 +1113,8 @@
"timezone": "America/Los_Angeles",
"created_on": "2018-07-06T12:30:00.123457Z",
"urns": [
"tel:+255788555111?id=20121&priority=1000",
"tel:+255788555222?id=20122&priority=999"
"tel:+255788555111?id=30000&priority=1000",
"tel:+255788555222?id=30001&priority=999"
]
},
"events": []
Expand Down Expand Up @@ -1157,15 +1157,15 @@
"timezone": "America/Los_Angeles",
"created_on": "2018-07-06T12:30:00.123457Z",
"urns": [
"tel:+255788555111?id=20121&priority=1000"
"tel:+255788555111?id=30000&priority=1000"
]
},
"events": [
{
"type": "contact_urns_changed",
"created_on": "2018-07-06T12:30:00.123456789Z",
"urns": [
"tel:+255788555111?id=20121&priority=1000"
"tel:+255788555111?id=30000&priority=1000"
]
}
]
Expand Down
4 changes: 2 additions & 2 deletions web/contact/testdata/resolve.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@
"timezone": "America/Los_Angeles",
"created_on": "2018-07-06T12:30:00.123457Z",
"urns": [
"tel:+16055747777?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20121&priority=1000"
"tel:+16055747777?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30000&priority=1000"
]
},
"created": true,
"urn": {
"id": 20121,
"id": 30000,
"identity": "tel:+16055747777"
}
},
Expand Down
2 changes: 2 additions & 0 deletions web/msg/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
func TestServer(t *testing.T) {
_, _, db, _ := testsuite.Get()

defer testdata.ResetContactData(db)

cathyIn := testdata.InsertIncomingMsg(db, testdata.Org1, testdata.TwilioChannel, testdata.Cathy, "hello", models.MsgStatusHandled)
cathyOut := testdata.InsertOutgoingMsg(db, testdata.Org1, testdata.TwilioChannel, testdata.Cathy, "how can we help", nil, models.MsgStatusSent)
bobOut := testdata.InsertOutgoingMsg(db, testdata.Org1, testdata.VonageChannel, testdata.Bob, "this failed", nil, models.MsgStatusFailed)
Expand Down
Loading

0 comments on commit 2baf4a0

Please sign in to comment.