Skip to content

Commit

Permalink
feat: remove some useless error check and improve unit test for `Init…
Browse files Browse the repository at this point in the history
…Data`
  • Loading branch information
hedon954 committed Aug 22, 2024
1 parent 572fb4c commit d5d3398
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 22 deletions.
17 changes: 0 additions & 17 deletions gmm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ func Builder(db ...string) *GMMBuilder {
// Port sets the port for the MySQL server,
// if not set, gmm would generate a port start from 19527
func (b *GMMBuilder) Port(port int) *GMMBuilder {
if b.err != nil {
return b
}
b.port = port
return b
}
Expand Down Expand Up @@ -129,19 +126,12 @@ func (b *GMMBuilder) initServer() *GMMBuilder {

// CreateTable adds a table to be created upon initialization
func (b *GMMBuilder) CreateTable(table schema.Tabler) *GMMBuilder {
if b.err != nil {
return b
}
b.tables = append(b.tables, table)
return b
}

// InitData adds initialization data to the mock database
func (b *GMMBuilder) InitData(data interface{}) *GMMBuilder {
if b.err != nil {
return b
}

if reflect.TypeOf(data).Kind() == reflect.Slice {
slice := reflect.ValueOf(data)
for i := 0; i < slice.Len(); i++ {
Expand All @@ -167,19 +157,12 @@ func (b *GMMBuilder) InitData(data interface{}) *GMMBuilder {

// SQLStmts adds SQL statements to be executed upon initialization
func (b *GMMBuilder) SQLStmts(stmts ...string) *GMMBuilder {
if b.err != nil {
return b
}
b.sqlStmts = append(b.sqlStmts, stmts...)
return b
}

// SQLFiles adds SQL files whose contents are to be executed upon initialization
func (b *GMMBuilder) SQLFiles(files ...string) *GMMBuilder {
if b.err != nil {
return b
}

for _, file := range files {
if _, err := os.Stat(file); os.IsNotExist(err) {
b.err = fmt.Errorf("sql file %s not exist", file)
Expand Down
6 changes: 6 additions & 0 deletions gmm/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ func Test_GMMBuilder_InitData(t *testing.T) {
_, _, _, err := Builder().InitData([]interface{}{&CertificationInfo{}, &UserState{}}).Build()
assert.Nil(t, err)
})

t.Run("slice of different types but some do not implement schema.Tabler should return err", func(t *testing.T) {
_, _, _, err := Builder().InitData([]interface{}{&CertificationInfo{}, 1}).Build()
assert.NotNil(t, err)
assert.Equal(t, "every single data should implement gorm schema.Tabler", err.Error())
})
}

func TestGMMBuilder_CreateTable(t *testing.T) {
Expand Down
5 changes: 0 additions & 5 deletions main.go

This file was deleted.

0 comments on commit d5d3398

Please sign in to comment.