Skip to content
This repository was archived by the owner on Oct 29, 2019. It is now read-only.

Commit d77bd75

Browse files
committed
add skeletal test
1 parent c46a855 commit d77bd75

File tree

4 files changed

+57
-29
lines changed

4 files changed

+57
-29
lines changed

data_file.go data_commands.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,43 @@ import (
55
"github.com/gchaincl/dotsql"
66
)
77

8-
// LoadDataFile takes a filepath to a sql file with create & drop table commands
9-
// and returns a DataFile
10-
func LoadDataFile(sqlFilePath string) (*DataFile, error) {
8+
// LoadDataCommands takes a filepath to a sql file with create & drop table commands
9+
// and returns a DataCommands
10+
func LoadDataCommands(sqlFilePath string) (*DataCommands, error) {
1111
f, err := dotsql.LoadFromFile(sqlFilePath)
1212
if err != nil {
1313
return nil, err
1414
}
1515

16-
return &DataFile{
16+
return &DataCommands{
1717
file: f,
1818
}, nil
1919
}
2020

21-
func LoadDataString(sql string) (*DataFile, error) {
21+
func LoadDataString(sql string) (*DataCommands, error) {
2222
f, err := dotsql.LoadFromString(sql)
2323
if err != nil {
2424
return nil, err
2525
}
2626

27-
return &DataFile{
27+
return &DataCommands{
2828
file: f,
2929
}, nil
3030
}
3131

3232
// SchemaFile is an sql file that defines a database schema
33-
type DataFile struct {
33+
type DataCommands struct {
3434
file *dotsql.DotSql
3535
}
3636

37-
func (d *DataFile) Commands() []string {
37+
func (d *DataCommands) Commands() []string {
3838
return commandsWithPrefix(d.file, "")
3939
}
4040

4141
// DropAll executes the command named "drop-all" from the sql file
4242
// this should be a command in the form:
4343
// DROP TABLE IF EXISTS foo, bar, baz ...
44-
func (d *DataFile) DeleteAll(db Execable) error {
44+
func (d *DataCommands) DeleteAll(db Execable) error {
4545
for _, cmd := range commandsWithPrefix(d.file, "delete") {
4646
if _, err := d.file.Exec(db, cmd); err != nil {
4747
return fmt.Errorf("error executing '%s': %s", cmd, err)
@@ -50,7 +50,7 @@ func (d *DataFile) DeleteAll(db Execable) error {
5050
return nil
5151
}
5252

53-
func (d *DataFile) Reset(db Execable, tables ...string) error {
53+
func (d *DataCommands) Reset(db Execable, tables ...string) error {
5454
for _, t := range tables {
5555
if _, err := d.file.Exec(db, fmt.Sprintf("delete-%s", t)); err != nil {
5656
return fmt.Errorf("error executing 'delete-%s': %s", t, err)
@@ -64,7 +64,7 @@ func (d *DataFile) Reset(db Execable, tables ...string) error {
6464
}
6565

6666
// CreateAll executes all commands that have the prefix "create"
67-
// func (d *DataFile) InsertAll(db Execable) error {
67+
// func (d *DataCommands) InsertAll(db Execable) error {
6868
// for _, cmd := range commandsWithPrefix(d.file, "insert") {
6969
// if _, err := d.file.Exec(db, cmd); err != nil {
7070
// return err
@@ -73,7 +73,7 @@ func (d *DataFile) Reset(db Execable, tables ...string) error {
7373
// return nil
7474
// }
7575

76-
// func (d *DataFile) ResetAll(db Execable) error {
76+
// func (d *DataCommands) ResetAll(db Execable) error {
7777
// if err := d.DeleteAll(db); err != nil {
7878
// return err
7979
// }

schema_file.go schema_commands.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,47 @@ import (
55
"github.com/gchaincl/dotsql"
66
)
77

8-
// LoadSchemaFile takes a filepath to a sql file with create & drop table commands
9-
// and returns a SchemaFile
10-
func LoadSchemaFile(sqlFilePath string) (*SchemaFile, error) {
8+
// LoadSchemaCommands takes a filepath to a sql file with create & drop table commands
9+
// and returns a SchemaCommands
10+
func LoadSchemaCommands(sqlFilePath string) (*SchemaCommands, error) {
1111
f, err := dotsql.LoadFromFile(sqlFilePath)
1212
if err != nil {
1313
return nil, err
1414
}
1515

16-
return &SchemaFile{
16+
return &SchemaCommands{
1717
file: f,
1818
}, nil
1919
}
2020

21-
func LoadSchemaString(sql string) (*SchemaFile, error) {
21+
func LoadSchemaString(sql string) (*SchemaCommands, error) {
2222
f, err := dotsql.LoadFromString(sql)
2323
if err != nil {
2424
return nil, err
2525
}
2626

27-
return &SchemaFile{
27+
return &SchemaCommands{
2828
file: f,
2929
}, nil
3030
}
3131

32-
// SchemaFile is an sql file that defines a database schema
33-
type SchemaFile struct {
32+
// SchemaCommands is an sql file that defines a database schema
33+
type SchemaCommands struct {
3434
file *dotsql.DotSql
3535
}
3636

3737
// DropAll executes the command named "drop-all" from the sql file
3838
// this should be a command in the form:
3939
// DROP TABLE IF EXISTS foo, bar, baz ...
40-
func (s *SchemaFile) DropAll(db Execable) error {
40+
func (s *SchemaCommands) DropAll(db Execable) error {
4141
_, err := s.file.Exec(db, "drop-all")
42-
return fmt.Errorf("error executing 'drop-all': %s", err.Error())
42+
if err != nil {
43+
fmt.Errorf("error executing 'drop-all': %s", err.Error())
44+
}
45+
return nil
4346
}
4447

45-
func (s *SchemaFile) Create(db Execable, tables ...string) error {
48+
func (s *SchemaCommands) Create(db Execable, tables ...string) error {
4649
for _, t := range tables {
4750
cmd := fmt.Sprintf("create-%s", t)
4851
if _, err := s.file.Exec(db, cmd); err != nil {
@@ -52,7 +55,7 @@ func (s *SchemaFile) Create(db Execable, tables ...string) error {
5255
return nil
5356
}
5457

55-
func (s *SchemaFile) DropAllCreate(db Execable, tables ...string) error {
58+
func (s *SchemaCommands) DropAllCreate(db Execable, tables ...string) error {
5659
if err := s.DropAll(db); err != nil {
5760
return err
5861
}
@@ -65,7 +68,7 @@ func (s *SchemaFile) DropAllCreate(db Execable, tables ...string) error {
6568
// InitializeDatabase drops everything and calls create on all tables
6669
// WARNING - THIS ZAPS WHATEVER DB IT'S GIVEN. DO NOT CALL THIS SHIT.
6770
// used for testing only, returns a teardown func
68-
// func (s *SchemaFile) InitializeDatabase(db Execable) error {
71+
// func (s *SchemaCommands) InitializeDatabase(db Execable) error {
6972
// // TODO - infer table names from de-prefixed create commands,
7073
// // use this to check for data existence
7174
// // // test query to check for database schema existence

test_suite.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66

77
type TestSuite struct {
88
DB *sql.DB
9-
Schema *SchemaFile
10-
Data *DataFile
9+
Schema *SchemaCommands
10+
Data *DataCommands
1111
Cascade []string
1212
}
1313

@@ -33,14 +33,14 @@ func InitTestSuite(o *TestSuiteOpts) (*TestSuite, error) {
3333
ts.DB = db
3434

3535
if o.SchemaPath != "" && o.DataPath != "" {
36-
sf, err := LoadSchemaFile(o.SchemaPath)
36+
sf, err := LoadSchemaCommands(o.SchemaPath)
3737
if err != nil {
3838
return nil, err
3939
}
4040

4141
ts.Schema = sf
4242

43-
df, err := LoadDataFile(o.DataPath)
43+
df, err := LoadDataCommands(o.DataPath)
4444
if err != nil {
4545
return nil, err
4646
}

test_suite_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package sqlutil
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestInitTestSuite(t *testing.T) {
8+
cases := []struct {
9+
o *TestSuiteOpts
10+
ts *TestSuite
11+
err error
12+
}{}
13+
14+
for i, c := range cases {
15+
ts, err := InitTestSuite(c.o)
16+
if err != c.err {
17+
t.Errorf("case %d error mismatch: %s != %s", i, c.err, err)
18+
return
19+
}
20+
if ts == nil && c.ts != nil || ts != nil && c.ts == nil {
21+
t.Errorf("case %d TestSuite nil mismatch: %s != %s", i, c.ts, ts)
22+
return
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)