-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more tests, updated temba.dump, add testsuite package
- Loading branch information
1 parent
6ba5c07
commit ec9925d
Showing
14 changed files
with
251 additions
and
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
*.so | ||
*.dylib | ||
mailroom | ||
dist | ||
|
||
# Test binary, build with `go test -c` | ||
*.test | ||
|
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 |
---|---|---|
@@ -1,38 +1,59 @@ | ||
package campaigns | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
"time" | ||
|
||
"github.com/jmoiron/sqlx" | ||
"github.com/nyaruka/mailroom/marker" | ||
"github.com/nyaruka/mailroom/queue" | ||
"github.com/nyaruka/mailroom/testsuite" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
resetDB() | ||
testsuite.Reset() | ||
os.Exit(m.Run()) | ||
} | ||
|
||
func mustExec(command string, args ...string) { | ||
cmd := exec.Command(command, args...) | ||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
panic(fmt.Sprintf("error restoring database: %s: %s", err, string(output))) | ||
} | ||
} | ||
func TestCampaigns(t *testing.T) { | ||
ctx := testsuite.CTX() | ||
rp := testsuite.RP() | ||
rc := testsuite.RC() | ||
defer rc.Close() | ||
|
||
func resetDB() { | ||
db := sqlx.MustOpen("postgres", "postgres://temba@localhost/temba?sslmode=disable") | ||
db.MustExec("drop owned by temba cascade") | ||
mustExec("pg_restore", "-d", "temba", "../temba.dump") | ||
} | ||
err := marker.ClearTasks(rc, campaignsLock) | ||
assert.NoError(t, err) | ||
|
||
// 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) | ||
db := testsuite.DB() | ||
db.MustExec(`UPDATE flows_flow SET flow_server_enabled=TRUE WHERE id = 31;`) | ||
db.MustExec(`INSERT INTO campaigns_eventfire(scheduled, contact_id, event_id) VALUES (NOW(), 42, 2), (NOW(), 43, 2);`) | ||
time.Sleep(10 * time.Millisecond) | ||
|
||
func getDB() *sqlx.DB { | ||
db := sqlx.MustOpen("postgres", "postgres://temba@localhost/temba?sslmode=disable") | ||
return db | ||
// schedule our campaign to be started | ||
err = fireCampaignEvents(ctx, db, rp, campaignsLock, "lock") | ||
assert.NoError(t, err) | ||
|
||
// then actually work on the event | ||
task, err := queue.PopNextTask(rc, eventQueue) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, task) | ||
|
||
// work on that task | ||
err = fireEventFires(ctx, db, rp, task) | ||
assert.NoError(t, err) | ||
|
||
// should now have a flow run for that contact and flow | ||
assertCount(t, db, `SELECT COUNT(*) from flows_flowrun WHERE contact_id = 42 AND flow_id = 31;`, 1) | ||
assertCount(t, db, `SELECT COUNT(*) from flows_flowrun WHERE contact_id = 43 AND flow_id = 31;`, 1) | ||
} | ||
func TestCampaigns(t *testing.T) { | ||
// create a campaign and event | ||
|
||
func assertCount(t *testing.T, db *sqlx.DB, query string, count int) { | ||
var c int | ||
err := db.Get(&c, query) | ||
assert.NoError(t, err) | ||
assert.Equal(t, count, c) | ||
} |
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
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
Oops, something went wrong.