-
Notifications
You must be signed in to change notification settings - Fork 50
/
mysql.go
49 lines (38 loc) · 1.22 KB
/
mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package assert
import (
"database/sql"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type rowQuerying interface {
QueryRow(query string, args ...interface{}) *sql.Row
}
func SqlTableHasOneRowOnly(t *testing.T, db rowQuerying, tableName string) {
rows := db.QueryRow(fmt.Sprintf("select count(*) from %s", tableName))
count := "count(*)"
err := rows.Scan(&count)
if err != nil && err != sql.ErrNoRows {
assert.Fail(t, "error retrieving count from database", err.Error())
return
}
if err == sql.ErrNoRows {
assert.Fail(t, "table has 0 rows", err.Error())
return
}
assert.Equal(t, "1", count, "there is more than 1 row in the table")
}
func SqlColumnHasSpecificValue(t *testing.T, db rowQuerying, tableName string, column string, expectedValue interface{}) {
query := fmt.Sprintf("select %s from %s where %s = '%v'", column, tableName, column, expectedValue)
row := db.QueryRow(query)
err := row.Scan(&column)
if err != nil && err != sql.ErrNoRows {
assert.Fail(t, "error querying database", err.Error())
return
}
if err == sql.ErrNoRows {
assert.Fail(t, fmt.Sprintf("no rows existing with expected value: %v in column: %s", expectedValue, column))
return
}
assert.Equal(t, expectedValue, column)
}