-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fad47b8
commit 7f976aa
Showing
12 changed files
with
535 additions
and
16 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
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// +build !noserver | ||
|
||
package gnomockd_test | ||
|
||
import ( | ||
"bytes" | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/orlangure/gnomock" | ||
"github.com/orlangure/gnomock/gnomockd" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
//nolint:bodyclose | ||
func TestMariaDB(t *testing.T) { | ||
t.Parallel() | ||
|
||
h := gnomockd.Handler() | ||
bs, err := ioutil.ReadFile("./testdata/mariadb.json") | ||
require.NoError(t, err) | ||
|
||
buf := bytes.NewBuffer(bs) | ||
w, r := httptest.NewRecorder(), httptest.NewRequest(http.MethodPost, "/start/mariadb", buf) | ||
h.ServeHTTP(w, r) | ||
|
||
res := w.Result() | ||
|
||
defer func() { require.NoError(t, res.Body.Close()) }() | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
require.NoError(t, err) | ||
|
||
require.Equalf(t, http.StatusOK, res.StatusCode, string(body)) | ||
|
||
var c *gnomock.Container | ||
|
||
err = json.Unmarshal(body, &c) | ||
require.NoError(t, err) | ||
|
||
addr := c.DefaultAddress() | ||
connStr := fmt.Sprintf( | ||
"%s:%s@tcp(%s)/%s", | ||
"gnomock", "foobar", addr, "gnomockd_db", | ||
) | ||
|
||
db, err := sql.Open("mysql", connStr) | ||
require.NoError(t, err) | ||
|
||
row := db.QueryRow(`select count(distinct ip_address) from customers`) | ||
count := 0 | ||
require.NoError(t, row.Scan(&count)) | ||
require.Equal(t, 1000, count) | ||
|
||
row = db.QueryRow(`select a from tbl`) | ||
value := 0 | ||
require.NoError(t, row.Scan(&value)) | ||
require.Equal(t, 42, value) | ||
|
||
bs, err = json.Marshal(c) | ||
require.NoError(t, err) | ||
|
||
buf = bytes.NewBuffer(bs) | ||
w, r = httptest.NewRecorder(), httptest.NewRequest(http.MethodPost, "/stop", buf) | ||
h.ServeHTTP(w, r) | ||
|
||
res = w.Result() | ||
require.Equal(t, http.StatusOK, res.StatusCode) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"preset": { | ||
"queries_files": [ | ||
"./testdata/mysql/customers.sql" | ||
], | ||
"queries": [ | ||
"create table tbl(a int)", | ||
"insert into tbl(a) values (42)" | ||
], | ||
"user": "gnomock", | ||
"password": "foobar", | ||
"db": "gnomockd_db", | ||
"version": "10" | ||
}, | ||
"options": { | ||
"debug": true | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Gnomock MariaDB | ||
|
||
Gnomock MariaDB is a [Gnomock](https://github.com/orlangure/gnomock) preset for | ||
running tests against a real MariaDB database, without mocks. | ||
|
||
```go | ||
package mariadb_test | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/orlangure/gnomock" | ||
"github.com/orlangure/gnomock/preset/mariadb" | ||
) | ||
|
||
func ExamplePreset() { | ||
queries := ` | ||
create table t(a int); | ||
insert into t (a) values (1); | ||
insert into t (a) values (2); | ||
` | ||
query := `insert into t (a) values (3);` | ||
p := mariadb.Preset( | ||
mariadb.WithUser("Sherlock", "Holmes"), | ||
mariadb.WithDatabase("books"), | ||
mariadb.WithQueries(queries, query), | ||
) | ||
|
||
container, err := gnomock.Start(p) | ||
|
||
defer func() { _ = gnomock.Stop(container) }() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
addr := container.DefaultAddress() | ||
connStr := fmt.Sprintf( | ||
"%s:%s@tcp(%s)/%s", | ||
"Sherlock", "Holmes", addr, "books", | ||
) | ||
|
||
db, err := sql.Open("mysql", connStr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var max, avg, min, count float64 | ||
|
||
rows := db.QueryRow("select max(a), avg(a), min(a), count(a) from t") | ||
|
||
err = rows.Scan(&max, &avg, &min, &count) | ||
if err != nil { | ||
panic("can't query the database: " + err.Error()) | ||
} | ||
|
||
fmt.Println("max", 3) | ||
fmt.Println("avg", 2) | ||
fmt.Println("min", 1) | ||
fmt.Println("count", 3) | ||
|
||
// Output: | ||
// max 3 | ||
// avg 2 | ||
// min 1 | ||
// count 3 | ||
} | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package mariadb | ||
|
||
// Option is an optional configuration of this Gnomock preset. Use available | ||
// Options to configure the container | ||
type Option func(*P) | ||
|
||
// WithUser creates a new superuser with the provided credentials in the | ||
// container. If not used, the default credentials are gnomock:gnoria | ||
func WithUser(user, password string) Option { | ||
return func(p *P) { | ||
p.User = user | ||
p.Password = password | ||
} | ||
} | ||
|
||
// WithDatabase creates a database with the provided name in the container. If | ||
// not provided, "mydb" is used by default. WithQueries, if provided, runs | ||
// against the new database | ||
func WithDatabase(db string) Option { | ||
return func(p *P) { | ||
p.DB = db | ||
} | ||
} | ||
|
||
// WithQueries executes the provided queries against the database created with | ||
// WithDatabase, or against default "mydb" database | ||
func WithQueries(queries ...string) Option { | ||
return func(p *P) { | ||
p.Queries = append(p.Queries, queries...) | ||
} | ||
} | ||
|
||
// WithQueriesFile sets a file name to read initial queries from. Queries from | ||
// this file are executed before any other queries provided in WithQueries | ||
func WithQueriesFile(file string) Option { | ||
return func(p *P) { | ||
p.QueriesFiles = append(p.QueriesFiles, file) | ||
} | ||
} | ||
|
||
// WithVersion sets image version. | ||
func WithVersion(version string) Option { | ||
return func(o *P) { | ||
o.Version = version | ||
} | ||
} |
Oops, something went wrong.