Skip to content

Commit

Permalink
Add faker for templates
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Feb 11, 2023
1 parent 15766ef commit 8e1e35d
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 17 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module github.com/ohkrab/krab

go 1.17
go 1.19

require (
github.com/emirpasic/gods v1.18.1
github.com/gin-gonic/gin v1.8.0
github.com/hashicorp/hcl/v2 v2.12.0
github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80
github.com/jackc/pgx/v4 v4.16.1
github.com/jaswdr/faker v1.16.0
github.com/jmoiron/sqlx v1.3.5
github.com/mitchellh/cli v1.1.4
github.com/pkg/errors v0.9.1
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
Expand All @@ -106,7 +105,6 @@ github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-playground/validator/v10 v10.11.0 h1:0W+xRM511GY47Yy3bZUbJVitCNg2BOGlCyvTqsp/xIw=
github.com/go-playground/validator/v10 v10.11.0/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
Expand Down Expand Up @@ -157,7 +155,6 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down Expand Up @@ -205,7 +202,6 @@ github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8=
Expand All @@ -226,7 +222,6 @@ github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65 h1:DadwsjnMwFjfWc9y5W
github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak=
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/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A=
github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78=
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA=
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg=
Expand Down Expand Up @@ -254,6 +249,8 @@ github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0f
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.2.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jaswdr/faker v1.16.0 h1:5ZjusQbqIZwJnUymPirNKJI1yFCuozdSR9oeYPgD5Uk=
github.com/jaswdr/faker v1.16.0/go.mod h1:x7ZlyB1AZqwqKZgyQlnqEG8FDptmHlncA5u2zY/yi6w=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
Expand Down Expand Up @@ -337,7 +334,6 @@ github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
Expand All @@ -362,7 +358,6 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
Expand Down Expand Up @@ -564,7 +559,6 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbuf
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
2 changes: 1 addition & 1 deletion krab/cmd_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c *CmdAction) Do(ctx context.Context, o CmdOpts) (interface{}, error) {
func (c *CmdAction) run(ctx context.Context, db krabdb.DB, inputs NamedInputs) (ResponseAction, error) {
result := ResponseAction{}

tpl := tpls.New(inputs, krabtpl.Functions)
tpl := tpls.New(inputs, krabtpl.Functions())

sb := strings.Builder{}
c.Action.ToSQL(&sb)
Expand Down
2 changes: 1 addition & 1 deletion krab/cmd_migrate_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *CmdMigrateDown) Do(ctx context.Context, o CmdOpts) (interface{}, error)
func (c *CmdMigrateDown) run(ctx context.Context, db krabdb.DB, inputs NamedInputs) ([]ResponseMigrateDown, error) {
result := []ResponseMigrateDown{}

tpl := tpls.New(inputs, krabtpl.Functions)
tpl := tpls.New(inputs, krabtpl.Functions())
versions := NewSchemaMigrationTable(tpl.Render(c.Set.Schema))

migration := c.Set.FindMigrationByVersion(inputs["version"].(string))
Expand Down
2 changes: 1 addition & 1 deletion krab/cmd_migrate_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c *CmdMigrateStatus) Do(ctx context.Context, o CmdOpts) (interface{}, erro
func (c *CmdMigrateStatus) run(ctx context.Context, db krabdb.DB, inputs NamedInputs) ([]ResponseMigrateStatus, error) {
result := []ResponseMigrateStatus{}

tpl := tpls.New(inputs, krabtpl.Functions)
tpl := tpls.New(inputs, krabtpl.Functions())
versions := NewSchemaMigrationTable(tpl.Render(c.Set.Schema))

hooksRunner := HookRunner{}
Expand Down
2 changes: 1 addition & 1 deletion krab/cmd_migrate_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *CmdMigrateUp) Do(ctx context.Context, o CmdOpts) (interface{}, error) {
func (c *CmdMigrateUp) run(ctx context.Context, db krabdb.DB, inputs NamedInputs) ([]ResponseMigrateUp, error) {
result := []ResponseMigrateUp{}

tpl := tpls.New(inputs, krabtpl.Functions)
tpl := tpls.New(inputs, krabtpl.Functions())
versions := NewSchemaMigrationTable(tpl.Render(c.Set.Schema))

// locking
Expand Down
93 changes: 93 additions & 0 deletions krabtpl/fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package krabtpl

import (
"fmt"
"time"

"github.com/jaswdr/faker"
"github.com/wzshiming/ctc"
)

// Fake generates fake data
func Fake(fake faker.Faker) any {
return func(id string) string {
switch id {
case "Address.Country":
return fake.Address().Country()
case "Address.CountryCode":
return fake.Address().CountryCode()

case "Color.Name":
return fake.Color().ColorName()
case "Color.Hex":
return fake.Color().Hex()
case "Color.RGB":
return fake.Color().RGB()

case "Company.Name":
return fake.Company().Name()

case "Emoji.Emoji":
return fake.Emoji().Emoji()
case "Emoji.EmojiCode":
return fake.Emoji().EmojiCode()

case "File.Path":
return fake.File().AbsoluteFilePathForUnix(4)
case "File.Extension":
return fake.File().Extension()
case "File.FilenameWithExtension":
return fake.File().FilenameWithExtension()
case "File.MimeType":
return fake.MimeType().MimeType()

case "Food.Fruit":
return fake.Food().Fruit()
case "Food.Vegetable":
return fake.Food().Vegetable()

case "Hash.MD5":
return fake.Hash().MD5()
case "Hash.SHA256":
return fake.Hash().SHA256()

case "Internet.Domain":
return fake.Internet().Domain()
case "Internet.Email":
return fake.Internet().Email()
case "Internet.Ipv4":
return fake.Internet().Ipv4()
case "Internet.Ipv6":
return fake.Internet().Ipv6()
case "Internet.MacAddress":
return fake.Internet().MacAddress()
case "Internet.SafeEmail":
return fake.Internet().SafeEmail()
case "Internet.Slug":
return fake.Internet().Slug()
case "Internet.URL":
return fake.Internet().URL()
case "Internet.UserAgent":
return fake.UserAgent().UserAgent()

case "Lorem.Paragraph":
return fake.Lorem().Paragraph(5)
case "Lorem.Word":
return fake.Lorem().Word()

case "Person.FirstName":
return fake.Person().FirstName()
case "Person.LastName":
return fake.Person().LastName()
case "Person.Name":
return fake.Person().Name()

case "Time.ISO8601":
return fake.Time().ISO8601(time.Now())
case "Time.Timezone":
return fake.Time().Timezone()
}

panic(fmt.Sprintf("Invalid generator name: %s%s%s", ctc.ForegroundRed, id, ctc.Reset))
}
}
12 changes: 9 additions & 3 deletions krabtpl/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ package krabtpl
import (
"text/template"

"github.com/jaswdr/faker"
"github.com/ohkrab/krab/krabdb"
)

var Functions template.FuncMap = template.FuncMap{
"quote_ident": krabdb.QuoteIdent,
"quote": krabdb.Quote,
func Functions() template.FuncMap {
fake := faker.New()

return template.FuncMap{
"quote_ident": krabdb.QuoteIdent,
"quote": krabdb.Quote,
"fake": Fake(fake),
}
}
51 changes: 51 additions & 0 deletions spec/action_insert_fixtures_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package spec

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestActionInsertFixtures(t *testing.T) {
c := mockCli(mockConfig(`
migration "create_fakes" {
version = "v1"
up { sql = "CREATE TABLE fakes(name VARCHAR)" }
down { sql = "DROP TABLE fakes" }
}
migration_set "public" {
migrations = [migration.create_fakes]
}
action "seed" "fakes" {
sql = <<-SQL
INSERT INTO fakes(name) VALUES
({{ fake "Food.Fruit" | quote }}),
({{ fake "Internet.SafeEmail" | quote }}),
({{ fake "Internet.Domain" | quote }}),
({{ fake "Internet.Ipv4" | quote }}),
({{ fake "Person.FirstName" | quote }}),
({{ fake "Person.LastName" | quote }}),
({{ fake "Person.Name" | quote }}),
({{ fake "Address.CountryCode" | quote }}),
({{ fake "Color.Hex" | quote }})
SQL
}
`))
defer c.Teardown()

c.AssertSuccessfulRun(t, []string{"migrate", "up", "public"})
c.AssertSchemaMigrationTable(t, "public", "v1")

c.AssertSuccessfulRun(t, []string{"action", "seed", "fakes"})

cols, rows := c.Query(t, "SELECT * FROM fakes")
assert.ElementsMatch(t, []string{"name"}, cols, "Columns must match")
if assert.Equal(t, 9, len(rows)) {
for i := range rows {
assert.NotEmpty(t, rows[i]["name"])
}
}
}
2 changes: 1 addition & 1 deletion test/fixtures/tests/versions_test.krab.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test "versions" "version_inc()" {
describe "increases `minor` component and resets `patch` leaving `major` untouched" {
do { sql = "SELECT version_inc(row(1,1,1)::sem_version, 'minor') AS ver" }

# v1 - set scope
# v1 - set scope
row "0" {
it "ver" { expect = "ver = row(2,0,0)::sem_version" }
its { expect = "ver = row(2,0,0)::sem_version" }
Expand Down

0 comments on commit 8e1e35d

Please sign in to comment.