Skip to content

Commit

Permalink
Convert sqlutilmocks.DataSource -> sqlutil.TestDataSource using ramsq…
Browse files Browse the repository at this point in the history
…l driver
  • Loading branch information
reductionista committed Nov 13, 2024
1 parent 157ba64 commit dd73f4f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 86 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/linkedin/goavro/v2 v2.12.0
github.com/pelletier/go-toml/v2 v2.2.0
github.com/prometheus/client_golang v1.17.0
github.com/proullon/ramsql v0.1.4
github.com/riferrei/srclient v0.5.4
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0
github.com/shopspring/decimal v1.4.0
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/fxamacker/cbor/v2 v2.5.0 h1:oHsG0V/Q6E/wqTS2O1Cozzsy69nqCiguo5Q1a1ADivE=
github.com/fxamacker/cbor/v2 v2.5.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/go-gorp/gorp v2.2.0+incompatible h1:xAUh4QgEeqPPhK3vxZN+bzrim1z5Av6q837gtjUlshc=
github.com/go-gorp/gorp v2.2.0+incompatible/go.mod h1:7IfkAQnO7jfT/9IQ3R9wL1dFhukN6aQxzKTHnkxzA/E=
github.com/go-json-experiment/json v0.0.0-20231102232822-2e55bd4e08b0 h1:ymLjT4f35nQbASLnvxEde4XOBL+Sn7rFuV+FOJqkljg=
github.com/go-json-experiment/json v0.0.0-20231102232822-2e55bd4e08b0/go.mod h1:6daplAwHHGbUGib4990V3Il26O0OC4aRyvewaaAihaA=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -133,8 +135,18 @@ github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSAS
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.3.1 h1:Fcr8QJ1ZeLi5zsPZqQeUZhNhxfkkKBOgJuYkJHoBOtU=
github.com/jackc/pgx/v5 v5.3.1/go.mod h1:t3JDKnCBlYIc0ewLF0Q7B8MXmoIaBOZj/ic7iHozM/8=
github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c=
github.com/jhump/protoreflect v1.15.1/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gavG9e0Q693nKo=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
Expand Down Expand Up @@ -208,6 +220,8 @@ github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdO
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI=
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
github.com/proullon/ramsql v0.1.4 h1:yTFRTn46gFH/kPbzCx+mGjuFlyTBUeDr3h2ldwxddl0=
github.com/proullon/ramsql v0.1.4/go.mod h1:CFGqeQHQpdRfWqYmWD3yXqPTEaHkF4zgXy1C6qDWc9E=
github.com/riferrei/srclient v0.5.4 h1:dfwyR5u23QF7beuVl2WemUY2KXh5+Sc4DHKyPXBNYuc=
github.com/riferrei/srclient v0.5.4/go.mod h1:vbkLmWcgYa7JgfPvuy/+K8fTS0p1bApqadxrxi/S1MI=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
Expand Down Expand Up @@ -413,6 +427,10 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.5.2 h1:ytTDxxEv+MplXOfFe3Lzm7SjG09fcdb3Z/c056DTBx0=
gorm.io/driver/postgres v1.5.2/go.mod h1:fmpX0m2I1PKuR7mKZiEluwrP3hbs+ps7JIGMUBpCgl8=
gorm.io/gorm v1.25.2 h1:gs1o6Vsa+oVKG/a9ElL3XgyGfghFfkKA2SInQaCyMho=
gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.1.3/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las=
Expand Down
86 changes: 0 additions & 86 deletions pkg/sqlutil/mocks/data_source.go

This file was deleted.

85 changes: 85 additions & 0 deletions pkg/sqlutil/test_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package sqlutil

import (
"context"
"database/sql"
"testing"

"github.com/jmoiron/sqlx"
_ "github.com/proullon/ramsql/driver"
"github.com/stretchr/testify/require"
)

var _ DataSource = &TestDataSource{}

// TestDataSource is a simple in-memory DataSource type which can be used for some types of unit testing.
type TestDataSource struct {
db *sqlx.DB
}

// BindNamed provides a mock function with given fields: _a0, _a1
func (ds *TestDataSource) BindNamed(_a0 string, _a1 interface{}) (string, []interface{}, error) {
return ds.db.BindNamed(_a0, _a1)
}

// DriverName provides a mock function with given fields:
func (ds *TestDataSource) DriverName() string {
return ds.db.DriverName()
}

// ExecContext provides a mock function with given fields: ctx, query, args
func (ds *TestDataSource) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
return ds.db.ExecContext(ctx, query, args...)
}

// GetContext provides a mock function with given fields: ctx, dest, query, args
func (ds *TestDataSource) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return ds.db.GetContext(ctx, dest, query, args...)
}

// NamedExecContext provides a mock function with given fields: ctx, query, arg
func (ds *TestDataSource) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error) {
return ds.db.NamedExecContext(ctx, query, arg)
}

// PrepareContext provides a mock function with given fields: ctx, query
func (ds *TestDataSource) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
return nil, nil
}

func (ds *TestDataSource) PrepareNamedContext(ctx context.Context, query string) (*sqlx.NamedStmt, error) {
return ds.db.PrepareNamedContext(ctx, query)
}

// QueryContext provides a mock function with given fields: ctx, query, args
func (ds *TestDataSource) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
return ds.db.QueryContext(ctx, query, args...)
}

// QueryRowxContext provides a mock function with given fields: ctx, query, args
func (ds *TestDataSource) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row {
return ds.db.QueryRowxContext(ctx, query, args...)
}

// QueryxContext provides a mock function with given fields: ctx, query, args
func (ds *TestDataSource) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) {
return ds.db.QueryxContext(ctx, query, args...)
}

// Rebind provides a mock function with given fields: _a0
func (ds *TestDataSource) Rebind(_a0 string) string {
return ds.db.Rebind(_a0)
}

// SelectContext provides a mock function with given fields: ctx, dest, query, args
func (ds *TestDataSource) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return ds.SelectContext(ctx, dest, query, args...)
}

// NewTestDataSource creates a new instance of DataSource.
func NewTestDataSource(t *testing.T) *TestDataSource {
db, err := sqlx.Open("ramsql", "testdb")
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, db.Close()) })
return &TestDataSource{db: db}
}

0 comments on commit dd73f4f

Please sign in to comment.