-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dad003
commit 331f884
Showing
9 changed files
with
237 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package engine | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/nyaruka/goflow/excellent" | ||
"github.com/nyaruka/goflow/flows/events" | ||
"github.com/nyaruka/goflow/flows/triggers" | ||
"github.com/nyaruka/goflow/utils" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type testRequest struct { | ||
Trigger *utils.TypedEnvelope `json:"trigger"` | ||
Events []*utils.TypedEnvelope `json:"events"` | ||
} | ||
|
||
func TestEvaluateTemplate(t *testing.T) { | ||
tests := []struct { | ||
template string | ||
expected string | ||
hasError bool | ||
}{ | ||
{"@contact.uuid", "ba96bf7f-bc2a-4873-a7c7-254d1927c4e3", false}, | ||
{"@contact.name", "Ben Haggerty", false}, | ||
{"@contact.first_name", "Ben", false}, | ||
{"@contact.language", "eng", false}, | ||
{"@contact.timezone", "America/Guayaquil", false}, | ||
{"@contact.urns", "tel:+12065551212", false}, // TODO should be list | ||
{"@contact.urns.tel", "tel:+12065551212", false}, | ||
{"@contact.urns.0", "tel:+12065551212", false}, | ||
{"@contact.urns.1", "facebook:1122334455667788", false}, | ||
{"@(format_urn(contact.urns.0))", "(206) 555-1212", false}, | ||
{"@contact.groups", "Survey Audience", false}, // TODO should be list | ||
{"@contact.fields.state", "Azuay", false}, | ||
{"@contact.fields.favorite_icecream", "", false}, // TODO should be error | ||
{"@(has_error(contact.fields.favorite_icecream))", "false", false}, // TODO should be true | ||
|
||
{"@run.input", "Hi there\nhttp://s3.amazon.com/bucket/test_en.jpg?a=Azuay", false}, | ||
{"@run.input.text", "Hi there", false}, | ||
{"@run.input.attachments", "", false}, // TODO should be list | ||
{"@run.input.attachments.0", "http://s3.amazon.com/bucket/test_en.jpg?a=Azuay", false}, | ||
{"@run.input.created_on", "2000-01-01T00:00:00.000000Z", false}, | ||
{"@run.input.channel.name", "Nexmo", false}, | ||
{"@run.results", "", false}, // TODO should be empty dict? | ||
{"@run.results.favorite_icecream", "", false}, // TODO should be error | ||
{"@(has_error(run.results.favorite_icecream))", "false", false}, // TODO should be true | ||
{"@run.exited_on", "", false}, | ||
|
||
{"@trigger.params", "{\n \"coupons\": [\n {\n \"code\": \"AAA-BBB-CCC\",\n \"expiration\": \"2000-01-01T00:00:00.000000000-00:00\"\n }\n ]\n }", false}, | ||
{"@trigger.params.coupons.0.code", "AAA-BBB-CCC", false}, | ||
} | ||
|
||
assetsJSON, err := ioutil.ReadFile("testdata/assets.json") | ||
require.NoError(t, err) | ||
|
||
// build our session | ||
assetCache := NewAssetCache(100, 5, "testing/1.0") | ||
err = assetCache.Include(assetsJSON) | ||
require.NoError(t, err) | ||
|
||
session := NewSession(assetCache, NewMockAssetServer()) | ||
require.NoError(t, err) | ||
|
||
// read trigger from file | ||
requestJSON, err := ioutil.ReadFile("testdata/trigger.json") | ||
require.NoError(t, err) | ||
|
||
testRequest := testRequest{} | ||
err = json.Unmarshal(requestJSON, &testRequest) | ||
require.NoError(t, err) | ||
|
||
trigger, err := triggers.ReadTrigger(session, testRequest.Trigger) | ||
require.NoError(t, err) | ||
|
||
initialEvents, err := events.ReadEvents(testRequest.Events) | ||
require.NoError(t, err) | ||
|
||
session.Start(trigger, initialEvents) | ||
run := session.Runs()[0] | ||
|
||
for _, test := range tests { | ||
eval, err := excellent.EvaluateTemplateAsString(session.Environment(), run.Context(), test.template, false) | ||
if err != nil { | ||
assert.True(t, test.hasError, "Received error evaluating '%s': %s", test.template, err) | ||
} else { | ||
assert.False(t, test.hasError, "Did not receive error evaluating '%s'", test.template) | ||
} | ||
|
||
assert.Equal(t, eval, test.expected, "Actual '%s' does not match expected '%s' evaluating template: '%s'", eval, test.expected, test.template) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{ | ||
"events": [ | ||
{ | ||
"created_on": "2000-01-01T00:00:00.000000000-00:00", | ||
"msg": { | ||
"uuid": "84f8a3cf-0f2c-4881-9502-2d7b114bf01f", | ||
"channel": { | ||
"name": "Nexmo", | ||
"uuid": "57f1078f-88aa-46f4-a59a-948a5739c03d" | ||
}, | ||
"created_on": "2000-01-01T00:00:00.000000000-00:00", | ||
"text": "Hi there", | ||
"attachments": [ | ||
"image/jpeg:http://s3.amazon.com/bucket/test_en.jpg?a=Azuay" | ||
], | ||
"urn": "tel:+12065551212" | ||
}, | ||
"type": "msg_received" | ||
} | ||
], | ||
"trigger": { | ||
"type": "manual", | ||
"triggered_on": "2000-01-01T00:00:00.000000000-00:00", | ||
"contact": { | ||
"uuid": "ba96bf7f-bc2a-4873-a7c7-254d1927c4e3", | ||
"name": "Ben Haggerty", | ||
"language": "eng", | ||
"timezone": "America/Guayaquil", | ||
"fields": { | ||
"first_name": { | ||
"created_on": "2000-01-01T00:00:00.000000000-00:00", | ||
"value": "Ben" | ||
}, | ||
"state": { | ||
"created_on": "2000-01-01T00:00:00.000000000-00:00", | ||
"value": "Azuay" | ||
} | ||
}, | ||
"group_uuids": ["d7ff4872-9238-452f-9d38-2f558fea89e0"], | ||
"urns": [ | ||
"tel:+12065551212", | ||
"facebook:1122334455667788", | ||
"mailto:ben@macklemore" | ||
] | ||
}, | ||
"environment": { | ||
"date_format": "yyyy-MM-dd", | ||
"languages": [], | ||
"time_format": "hh:mm", | ||
"timezone": "America/Los_Angeles" | ||
}, | ||
"flow": { | ||
"name": "Test Flow", | ||
"uuid": "76f0a02f-3b75-4b86-9064-e9195e1b3a02" | ||
}, | ||
"params": { | ||
"coupons": [ | ||
{ | ||
"code": "AAA-BBB-CCC", | ||
"expiration": "2000-01-01T00:00:00.000000000-00:00" | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters