Skip to content

Commit

Permalink
Add sql dump for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
helderfarias committed Nov 5, 2021
1 parent 5976276 commit 9064384
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
12 changes: 4 additions & 8 deletions dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func (f *FlatYmlDataSet) Load(fixtureName string) ([]Record, error) {

content, err := ioutil.ReadFile(file)
if err != nil {
return []Record{Record{}}, err
return []Record{}, err
}

var raw interface{}
if err := yaml.Unmarshal(content, &raw); err != nil {
return []Record{Record{}}, err
return []Record{}, err
}

records := []Record{}
Expand All @@ -32,7 +32,7 @@ func (f *FlatYmlDataSet) Load(fixtureName string) ([]Record, error) {
for _, r := range rawRecords {
recordMap, ok := r.(map[interface{}]interface{})
if !ok {
return []Record{}, errors.New("Wrong cast []interface{}")
return []Record{}, errors.New("wrong cast []interface{}")
}

records = append(records, f.mapper(file, recordMap))
Expand All @@ -42,7 +42,7 @@ func (f *FlatYmlDataSet) Load(fixtureName string) ([]Record, error) {
for _, record := range rawRecords {
recordMap, ok := record.(map[interface{}]interface{})
if !ok {
return []Record{}, errors.New("Wrong cast map[interface{}]interface{}")
return []Record{}, errors.New("wrong cast map[interface{}]interface{}")
}

records = append(records, f.mapper(file, recordMap))
Expand Down Expand Up @@ -77,7 +77,3 @@ func (f *FlatYmlDataSet) mapper(file string, raw map[interface{}]interface{}) Re
values: values,
}
}

func (f *FlatYmlDataSet) eval(source interface{}) interface{} {
return source
}
12 changes: 7 additions & 5 deletions dbunit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ type DatabaseFactory interface {
DB() *sql.DB
}

type DatabaseConfig interface {
}

type DataSet interface {
Load(fixtureName string) ([]Record, error)
}

func NewPostgresDatabaseFactory(driver, ds string) DatabaseFactory {
func NewPostgresDatabaseFactory(driver, ds string, opts ...Options) DatabaseFactory {
var conn *sqlx.DB

if driver != "" && ds != "" {
Expand All @@ -35,7 +32,12 @@ func NewPostgresDatabaseFactory(driver, ds string) DatabaseFactory {
}
}

return &PostgresDatabaseFactory{db: conn}
cfg := options{}
for _, apply := range opts {
apply(&cfg)
}

return &PostgresDatabaseFactory{db: conn, opts: cfg}
}

func NewFlatYmlDataSet(dir string) DataSet {
Expand Down
20 changes: 10 additions & 10 deletions dbunit_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func TestPostgresFixtureYmlToDeleteOperation(t *testing.T) {
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource)
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource, DebugSQL())
defer dbFactory.Close()

dataSet := NewFlatYmlDataSet("testdata/fixtures")
Expand All @@ -20,7 +20,7 @@ func TestPostgresFixtureYmlToDeleteOperation(t *testing.T) {
}

func TestPostgresFixtureYmlToDeleteAllOperationByPeople(t *testing.T) {
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource)
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource, DebugSQL())
defer dbFactory.Close()

dataSet := NewFlatYmlDataSet("testdata/fixtures")
Expand All @@ -36,7 +36,7 @@ func TestPostgresFixtureYmlToDeleteAllOperationByPeople(t *testing.T) {
}

func TestPostgresFixtureYmlToDeleteOperationByTags(t *testing.T) {
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource)
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource, DebugSQL())
defer dbFactory.Close()

dataSet := NewFlatYmlDataSet("testdata/fixtures")
Expand All @@ -51,7 +51,7 @@ func TestPostgresFixtureYmlToDeleteOperationByTags(t *testing.T) {
}

func TestPostgresFixtureYmlToDeleteOperationByUsers(t *testing.T) {
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource)
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource, DebugSQL())
defer dbFactory.Close()

dataSet := NewFlatYmlDataSet("testdata/fixtures")
Expand All @@ -62,7 +62,7 @@ func TestPostgresFixtureYmlToDeleteOperationByUsers(t *testing.T) {
}

func TestPostgresFixtureYmlToInsertOperationByUsers(t *testing.T) {
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource)
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource, DebugSQL())
defer dbFactory.Close()

dataSet := NewFlatYmlDataSet("testdata/fixtures")
Expand All @@ -73,7 +73,7 @@ func TestPostgresFixtureYmlToInsertOperationByUsers(t *testing.T) {
}

func TestPostgresFixtureYmlToComposeOperationByUsers(t *testing.T) {
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource)
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource, DebugSQL())
defer dbFactory.Close()

dataSet := NewFlatYmlDataSet("testdata/fixtures")
Expand All @@ -86,7 +86,7 @@ func TestPostgresFixtureYmlToComposeOperationByUsers(t *testing.T) {
}

func TestPostgresFixtureYmlToSuiteOperations(t *testing.T) {
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource)
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource, DebugSQL())
defer dbFactory.Close()

dataSet := NewFlatYmlDataSet("testdata/fixtures")
Expand All @@ -105,7 +105,7 @@ func TestPostgresFixtureYmlToSuiteOperations(t *testing.T) {
}

func TestPostgresFixtureYmlWithFilter(t *testing.T) {
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource)
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource, DebugSQL())
defer dbFactory.Close()

dataSet := NewFlatYmlDataSet("testdata/fixtures")
Expand All @@ -118,7 +118,7 @@ func TestPostgresFixtureYmlWithFilter(t *testing.T) {
}

func TestPostgresFixtureYmlWithFunc(t *testing.T) {
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource)
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource, DebugSQL())
defer dbFactory.Close()

dataSet := NewFlatYmlDataSet("testdata/fixtures")
Expand All @@ -133,7 +133,7 @@ func TestPostgresFixtureYmlWithFunc(t *testing.T) {
}

func TestPostgresFixtureYmlIncrementSequence(t *testing.T) {
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource)
dbFactory := NewPostgresDatabaseFactory(globalDriver, globalDataSource, DebugSQL())
defer dbFactory.Close()

dataSet := NewFlatYmlDataSet("testdata/fixtures")
Expand Down
13 changes: 13 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dbunit

type Options func(o *options)

type options struct {
sql bool
}

func DebugSQL() Options {
return func(o *options) {
o.sql = true
}
}
23 changes: 22 additions & 1 deletion postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

type PostgresDatabaseFactory struct {
db *sqlx.DB
db *sqlx.DB
opts options
}

type dbPredicate func(tableName string, column string, db *PostgresDatabaseFactory) int64
Expand Down Expand Up @@ -42,6 +43,26 @@ func (p *PostgresDatabaseFactory) Exec(cmds []Command) {
}
}

if p.opts.sql {
fmt.Println("[DBUnit] Debug SQL")
fmt.Println("---")
fmt.Println(c.sql)

if len(values) != 0 {
args := []string{}

for k, v := range values {
value := strings.ReplaceAll(fmt.Sprintf("%v", v), "RAW=", "")
args = append(args, fmt.Sprintf("%v=%v", k, value))
}

fmt.Println(strings.Join(args, ", "))
}

fmt.Println("---")
fmt.Println()
}

if _, err := p.db.NamedExec(c.sql, values); err != nil {
log.Println("file:", c.record.fileName, " error:", err, " sql:", c.sql, " values:", c.record.values)
}
Expand Down

0 comments on commit 9064384

Please sign in to comment.