Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Sep 1, 2020
1 parent 4af0517 commit a1361f4
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 9 deletions.
251 changes: 245 additions & 6 deletions models/contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/test"
"github.com/nyaruka/mailroom/testsuite"
Expand All @@ -16,6 +17,242 @@ import (
"github.com/stretchr/testify/require"
)

func TestContactIDsForQueryPage(t *testing.T) {
testsuite.Reset()
ctx := testsuite.CTX()
db := testsuite.DB()

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

client, err := elastic.NewClient(
elastic.SetURL(es.URL()),
elastic.SetHealthcheck(false),
elastic.SetSniff(false),
)
require.NoError(t, err)

oa, err := GetOrgAssets(ctx, db, 1)
require.NoError(t, err)

tcs := []struct {
Group assets.GroupUUID
ExcludeIDs []ContactID
Query string
Sort string
ExpectedESRequest string
MockedESResponse string
ExpectedContacts []ContactID
ExpectedTotal int64
ExpectedError string
}{
{
Group: AllContactsGroupUUID,
Query: "george",
ExpectedESRequest: `{
"_source": false,
"from": 0,
"query": {
"bool": {
"must": [
{
"term": {
"org_id": 1
}
},
{
"term": {
"is_active": true
}
},
{
"term": {
"groups": "d1ee73f0-bdb5-47ce-99dd-0c95d4ebf008"
}
},
{
"match": {
"name": {
"query": "george"
}
}
}
]
}
},
"size": 50,
"sort": [
{
"id": {
"order": "desc"
}
}
]
}`,
MockedESResponse: fmt.Sprintf(`{
"_scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAbgc0WS1hqbHlfb01SM2lLTWJRMnVOSVZDdw==",
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "contacts",
"_type": "_doc",
"_id": "%d",
"_score": null,
"_routing": "1",
"sort": [
15124352
]
}
]
}
}`, GeorgeID),
ExpectedContacts: []ContactID{GeorgeID},
ExpectedTotal: 1,
},
{
Group: BlockedContactsGroupUUID,
ExcludeIDs: []ContactID{BobID, CathyID},
Query: "age > 32",
Sort: "-age",
ExpectedESRequest: `{
"_source": false,
"from": 0,
"query": {
"bool": {
"must": [
{
"term": {
"org_id": 1
}
},
{
"term": {
"is_active": true
}
},
{
"term": {
"groups": "9295ebab-5c2d-4eb1-86f9-7c15ed2f3219"
}
},
{
"nested": {
"path": "fields",
"query": {
"bool": {
"must": [
{
"term": {
"fields.field": "903f51da-2717-47c7-a0d3-f2f32877013d"
}
},
{
"range": {
"fields.number": {
"from": 32,
"include_lower": false,
"include_upper": true,
"to": null
}
}
}
]
}
}
}
}
],
"must_not": {
"ids": {
"type": "_doc",
"values": [
"10001",
"10000"
]
}
}
}
},
"size": 50,
"sort": [
{
"fields.number": {
"nested": {
"filter": {
"term": {
"fields.field": "903f51da-2717-47c7-a0d3-f2f32877013d"
}
},
"path": "fields"
},
"order": "desc"
}
}
]
}`,
MockedESResponse: fmt.Sprintf(`{
"_scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAbgc0WS1hqbHlfb01SM2lLTWJRMnVOSVZDdw==",
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "contacts",
"_type": "_doc",
"_id": "%d",
"_score": null,
"_routing": "1",
"sort": [
15124352
]
}
]
}
}`, GeorgeID),
ExpectedContacts: []ContactID{GeorgeID},
ExpectedTotal: 1,
},
{
Query: "goats > 2", // no such contact field
ExpectedError: "error parsing query: goats > 2: can't resolve 'goats' to attribute, scheme or field",
},
}

for i, tc := range tcs {
es.NextResponse = tc.MockedESResponse

_, ids, total, err := ContactIDsForQueryPage(ctx, client, oa, tc.Group, tc.ExcludeIDs, tc.Query, tc.Sort, 0, 50)

if tc.ExpectedError != "" {
assert.EqualError(t, err, tc.ExpectedError)
} else {
assert.NoError(t, err, "%d: error encountered performing query", i)
assert.Equal(t, tc.ExpectedContacts, ids, "%d: ids mismatch", i)
assert.Equal(t, tc.ExpectedTotal, total, "%d: total mismatch", i)

test.AssertEqualJSON(t, []byte(tc.ExpectedESRequest), []byte(es.LastBody), "%d: ES request mismatch", i)
}
}
}

func TestContactIDsForQuery(t *testing.T) {
testsuite.Reset()
ctx := testsuite.CTX()
Expand All @@ -29,10 +266,10 @@ func TestContactIDsForQuery(t *testing.T) {
elastic.SetHealthcheck(false),
elastic.SetSniff(false),
)
assert.NoError(t, err)
require.NoError(t, err)

org, err := GetOrgAssets(ctx, db, 1)
assert.NoError(t, err)
oa, err := GetOrgAssets(ctx, db, 1)
require.NoError(t, err)

tcs := []struct {
Query string
Expand Down Expand Up @@ -132,7 +369,8 @@ func TestContactIDsForQuery(t *testing.T) {
}
}`,
ExpectedContacts: []ContactID{},
}, {
},
{
Query: "goats > 2", // no such contact field
ExpectedError: "error parsing query: goats > 2: can't resolve 'goats' to attribute, scheme or field",
},
Expand All @@ -141,14 +379,15 @@ func TestContactIDsForQuery(t *testing.T) {
for i, tc := range tcs {
es.NextResponse = tc.MockedESResponse

ids, err := ContactIDsForQuery(ctx, client, org, tc.Query)
ids, err := ContactIDsForQuery(ctx, client, oa, tc.Query)

if tc.ExpectedError != "" {
assert.EqualError(t, err, tc.ExpectedError)
} else {
assert.NoError(t, err, "%d: error encountered performing query", i)
test.AssertEqualJSON(t, []byte(tc.ExpectedESRequest), []byte(es.LastBody), "%d: request mismatch, got: %s", i, es.LastBody)
assert.Equal(t, tc.ExpectedContacts, ids, "%d: ids mismatch", i)

test.AssertEqualJSON(t, []byte(tc.ExpectedESRequest), []byte(es.LastBody), "%d: request mismatch, got: %s", i, es.LastBody)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion models/test_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ var DoctorsGroupID = GroupID(10000)
var DoctorsGroupUUID = assets.GroupUUID("c153e265-f7c9-4539-9dbc-9b358714b638")

var AllContactsGroupID = GroupID(1)
var AllContactsGroupUUID = assets.GroupUUID("bc268217-9ffa-49e0-883e-e4e09c252a5a")
var AllContactsGroupUUID = assets.GroupUUID("d1ee73f0-bdb5-47ce-99dd-0c95d4ebf008")

var BlockedContactsGroupID = GroupID(2)
var BlockedContactsGroupUUID = assets.GroupUUID("9295ebab-5c2d-4eb1-86f9-7c15ed2f3219")

var TestersGroupID = GroupID(10001)
var TestersGroupUUID = assets.GroupUUID("5e9d8fab-5e7e-4f51-b533-261af5dea70d")
Expand Down
2 changes: 1 addition & 1 deletion web/contact/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ func init() {
type searchRequest struct {
OrgID models.OrgID `json:"org_id" validate:"required"`
GroupUUID assets.GroupUUID `json:"group_uuid" validate:"required"`
ExcludeIDs []models.ContactID `json:"exclude_ids"`
Query string `json:"query"`
PageSize int `json:"page_size"`
Offset int `json:"offset"`
Sort string `json:"sort"`
ExcludeIDs []models.ContactID `json:"exclude_ids"`
}

// Response for a contact search
Expand Down
2 changes: 1 addition & 1 deletion web/contact/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestSearch(t *testing.T) {
},
{
"term": {
"groups": "bc268217-9ffa-49e0-883e-e4e09c252a5a"
"groups": "d1ee73f0-bdb5-47ce-99dd-0c95d4ebf008"
}
},
{
Expand Down

0 comments on commit a1361f4

Please sign in to comment.