From dd73f4f1a01700f7e997e4c00f57dcb9102313d6 Mon Sep 17 00:00:00 2001 From: Domino Valdano <2644901+reductionista@users.noreply.github.com> Date: Thu, 7 Nov 2024 21:25:23 -0800 Subject: [PATCH] Convert sqlutilmocks.DataSource -> sqlutil.TestDataSource using ramsql driver --- go.mod | 1 + go.sum | 18 +++++++ pkg/sqlutil/mocks/data_source.go | 86 -------------------------------- pkg/sqlutil/test_data_source.go | 85 +++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 86 deletions(-) delete mode 100644 pkg/sqlutil/mocks/data_source.go create mode 100644 pkg/sqlutil/test_data_source.go diff --git a/go.mod b/go.mod index d27908094..c04f0ee64 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 3727477f1..541175f52 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/pkg/sqlutil/mocks/data_source.go b/pkg/sqlutil/mocks/data_source.go deleted file mode 100644 index 40cd9017a..000000000 --- a/pkg/sqlutil/mocks/data_source.go +++ /dev/null @@ -1,86 +0,0 @@ -package mocks - -import ( - "context" - "database/sql" - - "github.com/stretchr/testify/mock" - - "github.com/jmoiron/sqlx" - - "github.com/smartcontractkit/chainlink-common/pkg/sqlutil" -) - -var _ sqlutil.DataSource = &DataSource{} - -// DataSource is a mocked DataSource type. Note: this will only work to satisfy -// the interface, don't expect any of the usual Mock methods such as On() to -// work on it. This is to avoid depending on any particular version of mockery -// (which could be different in different repos making use of this) -type DataSource struct { - mock.Mock -} - -// BindNamed provides a mock function with given fields: _a0, _a1 -func (ds *DataSource) BindNamed(_a0 string, _a1 interface{}) (string, []interface{}, error) { - return "", nil, nil -} - -// DriverName provides a mock function with given fields: -func (ds *DataSource) DriverName() string { - return "" -} - -// ExecContext provides a mock function with given fields: ctx, query, args -func (ds *DataSource) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { - return nil, nil -} - -// GetContext provides a mock function with given fields: ctx, dest, query, args -func (ds *DataSource) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error { - return nil -} - -// NamedExecContext provides a mock function with given fields: ctx, query, arg -func (ds *DataSource) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error) { - return nil, nil -} - -// PrepareContext provides a mock function with given fields: ctx, query -func (ds *DataSource) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) { - return nil, nil -} - -func (_m *DataSource) PrepareNamedContext(ctx context.Context, query string) (*sqlx.NamedStmt, error) { - return nil, nil -} - -// QueryContext provides a mock function with given fields: ctx, query, args -func (ds *DataSource) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) { - return nil, nil -} - -// QueryRowxContext provides a mock function with given fields: ctx, query, args -func (ds *DataSource) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row { - return nil -} - -// QueryxContext provides a mock function with given fields: ctx, query, args -func (ds *DataSource) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) { - return nil, nil -} - -// Rebind provides a mock function with given fields: _a0 -func (ds *DataSource) Rebind(_a0 string) string { - return "" -} - -// SelectContext provides a mock function with given fields: ctx, dest, query, args -func (ds *DataSource) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error { - return nil -} - -// NewDataSource creates a new instance of DataSource. -func NewDataSource() *DataSource { - return &DataSource{} -} diff --git a/pkg/sqlutil/test_data_source.go b/pkg/sqlutil/test_data_source.go new file mode 100644 index 000000000..9c77f7027 --- /dev/null +++ b/pkg/sqlutil/test_data_source.go @@ -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} +}