-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added Testcontainers based integration tests
- Loading branch information
Showing
6 changed files
with
252 additions
and
8 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
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) | ||
} |
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,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(); |
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,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) | ||
} |
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,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(); |
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
Oops, something went wrong.