Skip to content

Commit

Permalink
Merge pull request #80 from grafana/75-create-driver-packages
Browse files Browse the repository at this point in the history
create driver packages
  • Loading branch information
szkiba authored Oct 17, 2024
2 parents 821bfa9 + e7561da commit 2e5f992
Show file tree
Hide file tree
Showing 27 changed files with 555 additions and 127 deletions.
12 changes: 12 additions & 0 deletions drivers/azuresql/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Package azuresql contains azuresql SQL driver registration for xk6-sql.
package azuresql

import (
"github.com/grafana/xk6-sql/sql"

"github.com/microsoft/go-mssqldb/azuread"
)

func init() {
sql.RegisterModule(azuread.DriverName)
}
13 changes: 13 additions & 0 deletions drivers/clickhouse/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Package clickhouse contains ClickHouse SQL driver registration for xk6-sql.
package clickhouse

import (
"github.com/grafana/xk6-sql/sql"

// Blank imports required for initialization of driver.
_ "github.com/ClickHouse/clickhouse-go/v2"
)

func init() {
sql.RegisterModule("clickhouse")
}
23 changes: 0 additions & 23 deletions drivers/drivers.go

This file was deleted.

13 changes: 13 additions & 0 deletions drivers/mysql/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Package mysql contains MySQL driver registration for xk6-sql.
package mysql

import (
"github.com/grafana/xk6-sql/sql"

// Blank import required for initialization of driver.
_ "github.com/go-sql-driver/mysql"
)

func init() {
sql.RegisterModule("mysql")
}
39 changes: 39 additions & 0 deletions drivers/mysql/register_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package mysql

import (
"context"
_ "embed"
"runtime"
"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
if testing.Short() {
t.Skip()
}

if runtime.GOOS != "linux" {
t.Skip("Works only on Linux (Testcontainers)")
}

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();
13 changes: 13 additions & 0 deletions drivers/postgres/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Package postgres contains PostgreSQL SQL driver registration for xk6-sql.
package postgres

import (
"github.com/grafana/xk6-sql/sql"

// Blank import required for initialization of driver.
_ "github.com/lib/pq"
)

func init() {
sql.RegisterModule("postgres")
}
45 changes: 45 additions & 0 deletions drivers/postgres/register_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package postgres

import (
"context"
_ "embed"
"runtime"
"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
if testing.Short() {
t.Skip()
}

if runtime.GOOS != "linux" {
t.Skip("Works only on Linux (Testcontainers)")
}

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();
13 changes: 13 additions & 0 deletions drivers/sqlite3/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Package sqlite3 contains SQLite3 driver registration for xk6-sql.
package sqlite3

import (
"github.com/grafana/xk6-sql/sql"

// Blank import required for initialization of driver.
_ "github.com/mattn/go-sqlite3"
)

func init() {
sql.RegisterModule("sqlite3")
}
15 changes: 15 additions & 0 deletions drivers/sqlite3/register_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sqlite3

import (
_ "embed"
"testing"

"github.com/grafana/xk6-sql/sqltest"
)

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

func TestIntegration(t *testing.T) { //nolint:paralleltest
sqltest.RunScript(t, "sqlite3", "integration-test.db", script)
}
26 changes: 26 additions & 0 deletions drivers/sqlite3/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 IF NOT EXISTS test_table (id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, value VARCHAR(50));DELETE FROM test_table;"
);

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();
13 changes: 13 additions & 0 deletions drivers/sqlserver/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Package sqlserver contains Miscrosoft SQL server driver registration for xk6-sql.
package sqlserver

import (
"github.com/grafana/xk6-sql/sql"

// Blank import required for initialization of driver.
_ "github.com/microsoft/go-mssqldb"
)

func init() {
sql.RegisterModule("sqlserver")
}
3 changes: 2 additions & 1 deletion examples/azuresql_test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sql from 'k6/x/sql';
import driver from "k6/x/sql/driver/azuresql";

// The second argument is a MS SQL connection string, e.g.
// Server=127.0.0.1;Database=myDB;fedauth=ActiveDirectoryDefault;
const db = sql.open('azuresql', '');
const db = sql.open(driver, '');

export function setup() {
db.exec(`IF object_id('keyvalues') is null
Expand Down
3 changes: 2 additions & 1 deletion examples/clickhouse_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sql from 'k6/x/sql';
import driver from "k6/x/sql/driver/clickhouse";

const db = sql.open("clickhouse", "clickhouse://127.0.0.1:19000");
const db = sql.open(driver, "clickhouse://127.0.0.1:19000");

export function setup() {
db.exec(`CREATE TABLE IF NOT EXISTS hits_by_user_url
Expand Down
3 changes: 2 additions & 1 deletion examples/mssql_test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sql from 'k6/x/sql';
import driver from "k6/x/sql/driver/sqlserver";

// The second argument is a MS SQL connection string, e.g.
// Server=127.0.0.1;Database=myDB;User Id=myUser;Password=myPassword;
const db = sql.open('sqlserver', '');
const db = sql.open(driver, '');

export function setup() {
db.exec(`IF object_id('keyvalues') is null
Expand Down
3 changes: 2 additions & 1 deletion examples/mysql_test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sql from 'k6/x/sql';
import driver from "k6/x/sql/driver/mysql";

// The second argument is a MySQL connection string, e.g.
// myuser:mypass@tcp(127.0.0.1:3306)/mydb
const db = sql.open('mysql', '');
const db = sql.open(driver, '');

export function setup() {
db.exec(`
Expand Down
3 changes: 2 additions & 1 deletion examples/postgres_test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sql from 'k6/x/sql';
import driver from "k6/x/sql/driver/postgres";

// The second argument is a PostgreSQL connection string, e.g.
// postgres://myuser:[email protected]:5432/postgres?sslmode=disable
const db = sql.open('postgres', '');
const db = sql.open(driver, '');

export function setup() {
db.exec(`CREATE TABLE IF NOT EXISTS keyvalues (
Expand Down
5 changes: 3 additions & 2 deletions examples/sqlite3_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sql from 'k6/x/sql';
import sql from "k6/x/sql";
import driver from "k6/x/sql/driver/sqlite3";

const db = sql.open("sqlite3", "./test.db");
const db = sql.open(driver, "./test.db");

export function setup() {
db.exec(`CREATE TABLE IF NOT EXISTS keyvalues (
Expand Down
Loading

0 comments on commit 2e5f992

Please sign in to comment.