From 6ea638b57b1de23d12d33d1d40d4047072505c6b Mon Sep 17 00:00:00 2001 From: Jay Pipes Date: Mon, 4 Mar 2024 22:45:14 -0500 Subject: [PATCH] add example for gdt.WithStarter() Adds a `fixtures/db` directory with an example of using `gdt.WithStarter()` and `gdt.NewFixture()` to reset a SQLite database that is being tested in a gdt test scenario. Signed-off-by: Jay Pipes --- .github/workflows/gate-tests.yml | 7 ++- Makefile | 5 +- fixtures/db/go.mod | 20 ++++++++ fixtures/db/go.sum | 25 ++++++++++ fixtures/db/suite_test.go | 64 ++++++++++++++++++++++++++ fixtures/db/test.db | Bin 0 -> 12288 bytes fixtures/db/testdata/data-access.yaml | 12 +++++ go.mod => http/go.mod | 18 ++++---- go.sum => http/go.sum | 41 ++++++++++------- 9 files changed, 163 insertions(+), 29 deletions(-) create mode 100644 fixtures/db/go.mod create mode 100644 fixtures/db/go.sum create mode 100644 fixtures/db/suite_test.go create mode 100644 fixtures/db/test.db create mode 100644 fixtures/db/testdata/data-access.yaml rename go.mod => http/go.mod (74%) rename go.sum => http/go.sum (83%) diff --git a/.github/workflows/gate-tests.yml b/.github/workflows/gate-tests.yml index 6553a6c..73b3e09 100644 --- a/.github/workflows/gate-tests.yml +++ b/.github/workflows/gate-tests.yml @@ -13,7 +13,7 @@ jobs: test: strategy: matrix: - go-version: [1.19.x, 1.20.x] + go-version: [1.19.x, 1.20.x, 1.21.x] os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: @@ -31,4 +31,7 @@ jobs: uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1 with: go-version: ${{ matrix.go }} - - run: go test -v ./... + - name: install SQLite3 on Windows + if: matrix.os == 'windows-latest' + run: choco install sqlite -y + - run: make test diff --git a/Makefile b/Makefile index 266aaf1..27eac5d 100644 --- a/Makefile +++ b/Makefile @@ -3,4 +3,7 @@ VERSION ?= $(shell git describe --tags --always --dirty) .PHONY: test test: - go test -v ./... + cd http && \ + go test -count=1 -v ./... && \ + cd ../fixtures/db && \ + go test -count=1 -v ./... diff --git a/fixtures/db/go.mod b/fixtures/db/go.mod new file mode 100644 index 0000000..79be4e9 --- /dev/null +++ b/fixtures/db/go.mod @@ -0,0 +1,20 @@ +module github.com/gdt-dev/examples/fixtures/db + +go 1.21 + +require ( + github.com/gdt-dev/gdt v1.3.0 + github.com/mattn/go-sqlite3 v1.14.22 + github.com/stretchr/testify v1.9.0 +) + +require ( + github.com/PaesslerAG/gval v1.0.0 // indirect + github.com/PaesslerAG/jsonpath v0.1.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/samber/lo v1.38.1 // indirect + golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/fixtures/db/go.sum b/fixtures/db/go.sum new file mode 100644 index 0000000..7e4ee59 --- /dev/null +++ b/fixtures/db/go.sum @@ -0,0 +1,25 @@ +github.com/PaesslerAG/gval v1.0.0 h1:GEKnRwkWDdf9dOmKcNrar9EA1bz1z9DqPIO1+iLzhd8= +github.com/PaesslerAG/gval v1.0.0/go.mod h1:y/nm5yEyTeX6av0OfKJNp9rBNj2XrGhAf5+v24IBN1I= +github.com/PaesslerAG/jsonpath v0.1.0/go.mod h1:4BzmtoM/PI8fPO4aQGIusjGxGir2BzcV0grWtFzq1Y8= +github.com/PaesslerAG/jsonpath v0.1.1 h1:c1/AToHQMVsduPAa4Vh6xp2U0evy4t8SWp8imEsylIk= +github.com/PaesslerAG/jsonpath v0.1.1/go.mod h1:lVboNxFGal/VwW6d9JzIy56bUsYAP6tH/x80vjnCseY= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gdt-dev/gdt v1.3.0 h1:uUKOdNcXsc399lBQ3ELUarTWqqZBRWhwgXq2J35ocR4= +github.com/gdt-dev/gdt v1.3.0/go.mod h1:StnyGjC/67u59La2u6fh3HwW9MmodVhKdXcLlkgvNSY= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= +github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM= +golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/fixtures/db/suite_test.go b/fixtures/db/suite_test.go new file mode 100644 index 0000000..d939f5e --- /dev/null +++ b/fixtures/db/suite_test.go @@ -0,0 +1,64 @@ +package db_test + +import ( + "database/sql" + "os" + "path/filepath" + "testing" + + "github.com/gdt-dev/gdt" + gdtfix "github.com/gdt-dev/gdt/fixture" + _ "github.com/mattn/go-sqlite3" + "github.com/stretchr/testify/require" +) + +const ( + testDBPath = "./test.db" + initDB = ` +PRAGMA synchronous=FULL; +PRAGMA foreign_keys=1; + +CREATE TABLE users ( + id INTEGER PRIMARY KEY NOT NULL +, name TEXT NOT NULL +, created_on TEXT NOT NULL +); +CREATE INDEX ix_users_created ON users(created_on); + +INSERT INTO users (id, name, created_on) VALUES (1, 'Alice', DATETIME('2009-01-03')); +INSERT INTO users (id, name, created_on) VALUES (2, 'Bob', DATETIME('2015-11-16')); +INSERT INTO users (id, name, created_on) VALUES (3, 'Chuck', DATETIME('2019-08-07')); +` +) + +// resetDB clears any data in our database and populates the database with our +// base starting data set +func resetDB() { + os.Remove(testDBPath) + db, err := sql.Open("sqlite3", testDBPath) + if err != nil { + panic(err) + } + defer db.Close() + _, err = db.Exec(initDB) + if err != nil { + panic(err) + } +} + +func TestDataAccessLayer(t *testing.T) { + require := require.New(t) + + fp := filepath.Join("testdata", "data-access.yaml") + + s, err := gdt.From(fp) + require.Nil(err) + + dbFix := gdtfix.New(gdtfix.WithStarter(resetDB)) + + ctx := gdt.NewContext() + ctx = gdt.RegisterFixture(ctx, "db", dbFix) + + err = s.Run(ctx, t) + require.Nil(err) +} diff --git a/fixtures/db/test.db b/fixtures/db/test.db new file mode 100644 index 0000000000000000000000000000000000000000..07970711b19c669189d737b849ccecb0a8f40d9a GIT binary patch literal 12288 zcmeI$O-sWt7zgn5g^2@+AXtzcp5rD~lGfLqoLiCUoH{G&K}Ovw7Uza@iZ}7#6YPC;;4!LZUX&PH1VvHqJHB@~o zx(dvR}S=!A( zCpc_`aXN1F{#Jf&x#abhQ94N!)z$94x8hZ3vr=5oRrYDk+o$q|PvxCbDLF;W;4~!P z+fxG{gCAcUNM*WbbCcgP{>&fwy$Vnu009U<00Izz00bZa0SG_<0ucB`fs|=7%Pj<# zQQZ;Fj4fPS%#w7n(#=YlGP>FP@kQ;#5pvNM3$~aiA+qr^p_^>XU2Yw!+^j97E$7~I zDXqIQ*NAGNBZOKh)z