Skip to content

Commit

Permalink
Move test code into test file
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jul 27, 2023
1 parent 35add0e commit 40b54ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
24 changes: 24 additions & 0 deletions backends/rapidpro/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rapidpro

import (
"context"
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -1526,3 +1527,26 @@ func readMsgFromDB(b *backend, id courier.MsgID) *DBMsg {
m.channel = ch
return m
}

const selectMsgIDForID = `
SELECT m."id" FROM "msgs_msg" m INNER JOIN "channels_channel" c ON (m."channel_id" = c."id") WHERE (m."id" = $1 AND c."uuid" = $2 AND m."direction" = 'O')`

const selectMsgIDForExternalID = `
SELECT m."id" FROM "msgs_msg" m INNER JOIN "channels_channel" c ON (m."channel_id" = c."id") WHERE (m."external_id" = $1 AND c."uuid" = $2 AND m."direction" = 'O')`

func checkMsgExists(b *backend, status courier.MsgStatus) (err error) {
var id int64

if status.ID() != courier.NilMsgID {
err = b.db.QueryRow(selectMsgIDForID, status.ID(), status.ChannelUUID()).Scan(&id)
} else if status.ExternalID() != "" {
err = b.db.QueryRow(selectMsgIDForExternalID, status.ExternalID(), status.ChannelUUID()).Scan(&id)
} else {
return fmt.Errorf("no id or external id for status update")
}

if err == sql.ErrNoRows {
return courier.ErrMsgNotFound
}
return err
}
24 changes: 0 additions & 24 deletions backends/rapidpro/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rapidpro

import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -56,29 +55,6 @@ func writeMsgStatus(ctx context.Context, b *backend, status courier.MsgStatus) e
return err
}

const selectMsgIDForID = `
SELECT m."id" FROM "msgs_msg" m INNER JOIN "channels_channel" c ON (m."channel_id" = c."id") WHERE (m."id" = $1 AND c."uuid" = $2 AND m."direction" = 'O')`

const selectMsgIDForExternalID = `
SELECT m."id" FROM "msgs_msg" m INNER JOIN "channels_channel" c ON (m."channel_id" = c."id") WHERE (m."external_id" = $1 AND c."uuid" = $2 AND m."direction" = 'O')`

func checkMsgExists(b *backend, status courier.MsgStatus) (err error) {
var id int64

if status.ID() != courier.NilMsgID {
err = b.db.QueryRow(selectMsgIDForID, status.ID(), status.ChannelUUID()).Scan(&id)
} else if status.ExternalID() != "" {
err = b.db.QueryRow(selectMsgIDForExternalID, status.ExternalID(), status.ChannelUUID()).Scan(&id)
} else {
return fmt.Errorf("no id or external id for status update")
}

if err == sql.ErrNoRows {
return courier.ErrMsgNotFound
}
return err
}

// the craziness below lets us update our status to 'F' and schedule retries without knowing anything about the message
const sqlUpdateMsgByID = `
UPDATE msgs_msg SET
Expand Down

0 comments on commit 40b54ee

Please sign in to comment.