Skip to content

Commit

Permalink
test: added Testcontainers based integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
szkiba committed Oct 16, 2024
1 parent a0f3d74 commit 16d12dc
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 8 deletions.
30 changes: 30 additions & 0 deletions drivers/mysql/register_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mysql

import (
"context"
_ "embed"
"testing"

"github.com/grafana/xk6-sql/sqltest"
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go/modules/mysql"
)

//go:embed testdata/script.js
var script string

func TestIntegration(t *testing.T) { //nolint:paralleltest
ctx := context.Background()

ctr, err := mysql.Run(ctx, "mysql:8.0.36")

require.NoError(t, err)
defer func() { require.NoError(t, ctr.Terminate(ctx)) }()

conn, err := ctr.ConnectionString(ctx)

require.NoError(t, err)

sqltest.RunScript(t, "mysql", conn, script)
}
26 changes: 26 additions & 0 deletions drivers/mysql/testdata/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const db = sql.open(driver, connection);

db.exec(
"CREATE TABLE test_table (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, value VARCHAR(50));"
);

for (let i = 0; i < 5; i++) {
db.exec("INSERT INTO test_table (name, value) VALUES ('name-" + i + "', 'value-" + i + "');");
}

let all_rows = sql.query(db, "SELECT * FROM test_table;");
if (all_rows.length != 5) {
throw new Error("Expected all five rows to be returned; got " + all_rows.length);
}

let one_row = sql.query(db, "SELECT * FROM test_table WHERE name = ?;", "name-2");
if (one_row.length != 1) {
throw new Error("Expected single row to be returned; got " + one_row.length);
}

let no_rows = sql.query(db, "SELECT * FROM test_table WHERE name = ?;", "bogus-name");
if (no_rows.length != 0) {
throw new Error("Expected no rows to be returned; got " + no_rows.length);
}

db.close();
36 changes: 36 additions & 0 deletions drivers/postgres/register_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package postgres

import (
"context"
_ "embed"
"testing"

"github.com/grafana/xk6-sql/sqltest"
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
)

//go:embed testdata/script.js
var script string

func TestIntegration(t *testing.T) { //nolint:paralleltest
ctx := context.Background()

ctr, err := postgres.Run(ctx, "docker.io/postgres:16-alpine",
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2)),
)

require.NoError(t, err)
defer func() { require.NoError(t, ctr.Terminate(ctx)) }()

conn, err := ctr.ConnectionString(ctx, "sslmode=disable")

require.NoError(t, err)

sqltest.RunScript(t, "postgres", conn, script)
}
24 changes: 24 additions & 0 deletions drivers/postgres/testdata/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const db = sql.open(driver, connection);

db.exec("CREATE TABLE test_table (id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, value VARCHAR(50));");

for (let i = 0; i < 5; i++) {
db.exec("INSERT INTO test_table (name, value) VALUES ('name-" + i + "', 'value-" + i + "');");
}

let all_rows = sql.query(db, "SELECT * FROM test_table;");
if (all_rows.length != 5) {
throw new Error("Expected all five rows to be returned; got " + all_rows.length);
}

let one_row = sql.query(db, "SELECT * FROM test_table WHERE name = $1;", "name-2");
if (one_row.length != 1) {
throw new Error("Expected single row to be returned; got " + one_row.length);
}

let no_rows = sql.query(db, "SELECT * FROM test_table WHERE name = $1;", "bogus-name");
if (no_rows.length != 0) {
throw new Error("Expected no rows to be returned; got " + no_rows.length);
}

db.close();
42 changes: 38 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,55 @@ module github.com/grafana/xk6-sql
go 1.22

require (
github.com/go-sql-driver/mysql v1.7.1
github.com/lib/pq v1.10.9
github.com/mattn/go-sqlite3 v1.14.18
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
go.k6.io/k6 v0.54.0
)

require (
github.com/ClickHouse/clickhouse-go/v2 v2.22.0
github.com/go-sql-driver/mysql v1.7.1
github.com/grafana/sobek v0.0.0-20240927094302-19dd311f018f
github.com/lib/pq v1.10.9
github.com/mattn/go-sqlite3 v1.14.18
github.com/microsoft/go-mssqldb v1.6.0
github.com/proullon/ramsql v0.1.4
github.com/testcontainers/testcontainers-go v0.33.0
github.com/testcontainers/testcontainers-go/modules/mysql v0.33.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.33.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.1.0 // indirect
github.com/ClickHouse/ch-go v0.61.5 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/containerd/containerd v1.7.18 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/docker/docker v27.1.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dop251/goja v0.0.0-20240516125602-ccbae20bcec2 // indirect
github.com/evanw/esbuild v0.21.2 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-faster/city v1.0.1 // indirect
github.com/go-faster/errors v0.7.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
Expand All @@ -45,22 +61,40 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd // indirect
github.com/mstoykov/k6-taskqueue-lib v0.1.0 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.27.6 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/paulmach/orb v0.11.1 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.29.0 // indirect
Expand Down
Loading

0 comments on commit 16d12dc

Please sign in to comment.