Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

integration: add grafana queries to go integration #477

Merged
merged 3 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion SUPPORTED_CLIENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ These are the clients we actively test against to check are compatible with go-m
- [mariadb-java-client](#mariadb-java-client)
- Go
- [go-mysql-driver/mysql](#go-mysql-driver-mysql)
- Grafana

## Example client usage

Expand Down Expand Up @@ -129,7 +130,7 @@ namespace something
public async Task DoQuery()
{
var connectionString = "server=127.0.0.1;user id=user;password=pass;port=3306;database=dbname;";

using (var conn = new MySqlConnection(connectionString))
{
await conn.OpenAsync();
Expand Down
94 changes: 93 additions & 1 deletion _integration/go/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package testmysql

import (
"database/sql"
"reflect"
"testing"

_ "github.com/go-sql-driver/mysql"
)

const connectionString = "user:pass@tcp(127.0.0.1:3306)/test"

func TestMySQL(t *testing.T) {
db, err := sql.Open("mysql", "user:pass@tcp(127.0.0.1:3306)/test")
db, err := sql.Open("mysql", connectionString)
if err != nil {
t.Fatalf("can't connect to mysql: %s", err)
}
Expand Down Expand Up @@ -54,3 +57,92 @@ func TestMySQL(t *testing.T) {
}
}
}

func TestGrafana(t *testing.T) {
db, err := sql.Open("mysql", connectionString)
if err != nil {
t.Fatalf("can't connect to mysql: %s", err)
}

tests := []struct {
query string
expected [][]string
}{
{
`SELECT 1`,
[][]string{{"1"}},
},
{
`select @@version_comment limit 1`,
[][]string{{"NULL"}},
},
{
`describe table mytable`,
[][]string{
{"name", "TEXT"},
{"email", "TEXT"},
{"phone_numbers", "JSON"},
{"created_at", "TIMESTAMP"},
},
},
{
`select count(*) from mytable where created_at ` +
`between '2000-01-01T00:00:00Z' and '2999-01-01T00:00:00Z'`,
[][]string{{"4"}},
},
}

for _, c := range tests {
rs, err := db.Query(c.query)
if err != nil {
t.Fatalf("unable to execute query: %s", err)
}

result := getResult(t, rs)

if !reflect.DeepEqual(result, c.expected) {
t.Fatalf("rows do not match, expected: %v, got: %v", c.expected, result)
}
}
}

func getResult(t *testing.T, rs *sql.Rows) [][]string {
t.Helper()

columns, err := rs.Columns()
if err != nil {
t.Fatalf("unable to get columns: %s", err)
}

var result [][]string
p := make([]interface{}, len(columns))

for rs.Next() {
row := make([]interface{}, len(columns))
for i := range row {
p[i] = &row[i]
}

err = rs.Scan(p...)
if err != nil {
t.Fatalf("could not retrieve row: %s", err)
}

result = append(result, getStringSlice(row))
}

return result
}

func getStringSlice(row []interface{}) []string {
rowStrings := make([]string, len(row))
for i, r := range row {
if r == nil {
rowStrings[i] = "NULL"
} else {
rowStrings[i] = string(r.([]uint8))
}
}

return rowStrings
}