/
database_functions.go
127 lines (112 loc) · 4.03 KB
/
database_functions.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package test
import (
"database/sql"
"fmt"
"testing"
// Microsoft SQL Database driver
_ "github.com/denisenkom/go-mssqldb"
)
// DBConfig using server name, user name, password and database name
type DBConfig struct {
server string
user string
password string
database string
}
// DBConnection connects to the database using database configuration and database type, i.e. mssql, and then return the database. If there's any error, fail the test.
func DBConnection(t *testing.T, dbType string, dbConfig DBConfig) *sql.DB {
db, err := DBConnectionE(t, dbType, dbConfig)
if err != nil {
t.Fatal(err)
}
return db
}
// DBConnectionE connects to the database using database configuration and database type, i.e. mssql. Return the database or an error.
func DBConnectionE(t *testing.T, dbType string, dbConfig DBConfig) (*sql.DB, error) {
config := fmt.Sprintf("server = %s; user id = %s; password = %s; database = %s", dbConfig.server, dbConfig.user, dbConfig.password, dbConfig.database)
db, err := sql.Open(dbType, config)
if err != nil {
return nil, err
}
return db, nil
}
// DBExecution executes specific SQL commands, i.e. insertion. If there's any error, fail the test.
func DBExecution(t *testing.T, db *sql.DB, command string) {
_, err := DBExecutionE(t, db, command)
if err != nil {
t.Fatal(err)
}
}
// DBExecutionE executes specific SQL commands, i.e. insertion. Return the result or an error.
func DBExecutionE(t *testing.T, db *sql.DB, command string) (sql.Result, error) {
result, err := db.Exec(command)
if err != nil {
return nil, err
}
return result, nil
}
// DBQuery queries from database, i.e. selection, and then return the result. If there's any error, fail the test.
func DBQuery(t *testing.T, db *sql.DB, command string) *sql.Rows {
rows, err := DBQueryE(t, db, command)
if err != nil {
t.Fatal(err)
}
return rows
}
// DBQueryE queries from database, i.e. selection. Return the result or an error.
func DBQueryE(t *testing.T, db *sql.DB, command string) (*sql.Rows, error) {
rows, err := db.Query(command)
if err != nil {
return nil, err
}
return rows, nil
}
// DBQueryWithValidation queries from database and validate whether the result is the same as expected text. If there's any error, fail the test.
func DBQueryWithValidation(t *testing.T, db *sql.DB, command string, expected string) {
err := DBQueryWithValidationE(t, db, command, expected)
if err != nil {
t.Fatal(err)
}
}
// DBQueryWithValidationE queries from database and validate whether the result is the same as expected text. If not, return an error.
func DBQueryWithValidationE(t *testing.T, db *sql.DB, command string, expected string) error {
return DBQueryWithCustomValidationE(t, db, command, func(rows *sql.Rows) bool {
var name string
for rows.Next() {
err := rows.Scan(&name)
if err != nil {
t.Fatal(err)
}
if name != expected {
return false
}
}
return true
})
}
// DBQueryWithCustomValidation queries from database and validate whether the result meets the requirement. If there's any error, fail the test.
func DBQueryWithCustomValidation(t *testing.T, db *sql.DB, command string, validateResponse func(*sql.Rows) bool) {
err := DBQueryWithCustomValidationE(t, db, command, validateResponse)
if err != nil {
t.Fatal(err)
}
}
// DBQueryWithCustomValidationE queries from database and validate whether the result meets the requirement. If not, return an error.
func DBQueryWithCustomValidationE(t *testing.T, db *sql.DB, command string, validateResponse func(*sql.Rows) bool) error {
rows, err := DBQueryE(t, db, command)
defer rows.Close()
if err != nil {
return err
}
if !validateResponse(rows) {
return ValidationFunctionFailed{command}
}
return nil
}
// ValidationFunctionFailed is an error that occurs if the validation fails.
type ValidationFunctionFailed struct {
command string
}
func (err ValidationFunctionFailed) Error() string {
return fmt.Sprintf("Validation failed for command: %s.", err.command)
}