-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
8 changed files
with
160 additions
and
29 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
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,20 @@ | ||
module github.com/gdt-dev/examples/fixtures/db | ||
|
||
go 1.21.4 | ||
|
||
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 | ||
) |
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,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= |
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,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) | ||
} |
Binary file not shown.
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,12 @@ | ||
fixtures: | ||
- db | ||
tests: | ||
- exec: sqlite3 test.db "SELECT COUNT(*) FROM users" | ||
assert: | ||
out: | ||
is: 3 | ||
- exec: sqlite3 test.db "INSERT INTO users (id, name, created_on) VALUES (4, 'David', DATETIME())" | ||
- exec: sqlite3 test.db "SELECT COUNT(*) FROM users" | ||
assert: | ||
out: | ||
is: 4 |
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
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