Skip to content

Commit

Permalink
chore: Improve unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
almas1992 committed Dec 26, 2024
1 parent 6a360b5 commit bd7c75c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 67 deletions.
2 changes: 1 addition & 1 deletion contracts/console/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const (
)

type Extend struct {
Category string
ArgsUsage string
Category string
Flags []Flag
}

Expand Down
2 changes: 1 addition & 1 deletion database/console/table_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (r *TableCommand) display(ctx console.Context, table schema.Table) {
}
key = fmt.Sprintf("%s <fg=gray>%s</>", key, strings.Join(attributes, ", "))
if columns[i].Default != "" {
value = fmt.Sprintf("<fg=gray>%s</> %s ", columns[i].Default, value)
value = fmt.Sprintf("<fg=gray>%s</> %s", columns[i].Default, value)
}
ctx.TwoColumnDetail(key, value)
}
Expand Down
165 changes: 100 additions & 65 deletions database/console/table_command_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package console

import (
"fmt"
"io"
"testing"

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

"github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/database/schema"
mocksconfig "github.com/goravel/framework/mocks/config"
mocksconsole "github.com/goravel/framework/mocks/console"
Expand All @@ -25,6 +26,17 @@ func TestTableCommand(t *testing.T) {
mockConfig = mocksconfig.NewConfig(t)
mockSchema = mocksschema.NewSchema(t)
}
successCaseExpected := [][2]string{
{"<fg=green;op=bold>test</>", ""},
{"Columns", "1"},
{"Size", "0.000MiB"},
{"<fg=green;op=bold>Column</>", "Type"},
{"foo <fg=gray>autoincrement, int, nullable</>", "<fg=gray>bar</> int"},
{"<fg=green;op=bold>Index</>", ""},
{"index_foo <fg=gray>foo, bar</>", "compound, unique, primary"},
{"<fg=green;op=bold>Foreign Key</>", "On Update / On Delete"},
{"fk_foo <fg=gray>foo references baz on bar</>", "restrict / cascade"},
}
tests := []struct {
name string
setup func()
Expand All @@ -33,117 +45,140 @@ func TestTableCommand(t *testing.T) {
{
name: "get tables failed",
setup: func() {
mockContext.EXPECT().NewLine()
mockContext.EXPECT().Option("database").Return("")
mockSchema.EXPECT().Connection(mock.Anything).Return(mockSchema)
mockContext.EXPECT().Argument(0).Return("")
mockSchema.EXPECT().GetTables().Return(nil, assert.AnError)
mockContext.EXPECT().Error(mock.Anything).Run(func(message string) {
mockContext.EXPECT().NewLine().Once()
mockContext.EXPECT().Option("database").Return("").Once()
mockSchema.EXPECT().Connection("").Return(mockSchema).Once()
mockContext.EXPECT().Argument(0).Return("").Once()
mockSchema.EXPECT().GetTables().Return(nil, assert.AnError).Once()
mockContext.EXPECT().Error(fmt.Sprintf("Failed to get tables: %s", assert.AnError.Error())).Run(func(message string) {
color.Errorln(message)
})
}).Once()
},
expected: assert.AnError.Error(),
},
{
name: "table not found",
setup: func() {
mockContext.EXPECT().NewLine()
mockContext.EXPECT().Option("database").Return("")
mockSchema.EXPECT().Connection(mock.Anything).Return(mockSchema)
mockContext.EXPECT().Argument(0).Return("test")
mockSchema.EXPECT().GetTables().Return(nil, nil)
mockContext.EXPECT().Warning(mock.Anything).Run(func(message string) {
mockContext.EXPECT().NewLine().Times(2)
mockContext.EXPECT().Option("database").Return("test").Once()
mockSchema.EXPECT().Connection("test").Return(mockSchema).Once()
mockContext.EXPECT().Argument(0).Return("test").Once()
mockSchema.EXPECT().GetTables().Return(nil, nil).Once()
mockContext.EXPECT().Warning("Table 'test' doesn't exist.").Run(func(message string) {
color.Warningln(message)
})
}).Once()
},
expected: "Table 'test' doesn't exist",
},
{
name: "choice table canceled",
setup: func() {
mockContext.EXPECT().NewLine()
mockContext.EXPECT().Option("database").Return("")
mockSchema.EXPECT().Connection(mock.Anything).Return(mockSchema)
mockContext.EXPECT().Argument(0).Return("")
mockSchema.EXPECT().GetTables().Return(nil, nil)
mockContext.EXPECT().Choice(mock.Anything, mock.Anything).Return("", assert.AnError)
mockContext.EXPECT().Line(mock.Anything).Run(func(message string) {
mockContext.EXPECT().NewLine().Times(1)
mockContext.EXPECT().Option("database").Return("test").Once()
mockSchema.EXPECT().Connection("test").Return(mockSchema).Once()
mockContext.EXPECT().Argument(0).Return("").Once()
mockSchema.EXPECT().GetTables().Return(nil, nil).Once()
mockContext.EXPECT().Choice("Which table would you like to inspect?",
[]console.Choice(nil)).Return("", assert.AnError).Once()
mockContext.EXPECT().Line(assert.AnError.Error()).Run(func(message string) {
color.Default().Println(message)
})
}).Once()
},
expected: assert.AnError.Error(),
},
{
name: "get columns failed",
setup: func() {
mockContext.EXPECT().NewLine()
mockContext.EXPECT().Option("database").Return("")
mockSchema.EXPECT().Connection(mock.Anything).Return(mockSchema)
mockContext.EXPECT().Argument(0).Return("")
mockSchema.EXPECT().GetTables().Return([]schema.Table{{Name: "test"}}, nil)
mockContext.EXPECT().Choice(mock.Anything, mock.Anything).Return("test", nil)
mockSchema.EXPECT().GetColumns("test").Return(nil, assert.AnError)
mockContext.EXPECT().Error(mock.Anything).Run(func(message string) {
mockContext.EXPECT().NewLine().Once()
mockContext.EXPECT().Option("database").Return("test").Once()
mockSchema.EXPECT().Connection("test").Return(mockSchema).Once()
mockContext.EXPECT().Argument(0).Return("").Once()
mockSchema.EXPECT().GetTables().Return([]schema.Table{{Name: "test"}}, nil).Once()
mockContext.EXPECT().Choice("Which table would you like to inspect?",
[]console.Choice{{Key: "test", Value: "test"}}).Return("test", nil).Once()
mockSchema.EXPECT().GetColumns("test").Return(nil, assert.AnError).Once()
mockContext.EXPECT().Error(fmt.Sprintf("Failed to get columns: %s", assert.AnError.Error())).Run(func(message string) {
color.Errorln(message)
})
}).Once()
},
expected: assert.AnError.Error(),
},
{
name: "get indexes failed",
setup: func() {
mockContext.EXPECT().NewLine()
mockContext.EXPECT().Option("database").Return("")
mockSchema.EXPECT().Connection(mock.Anything).Return(mockSchema)
mockContext.EXPECT().Argument(0).Return("")
mockSchema.EXPECT().GetTables().Return([]schema.Table{{Name: "test"}}, nil)
mockContext.EXPECT().Choice(mock.Anything, mock.Anything).Return("test", nil)
mockSchema.EXPECT().GetColumns("test").Return(nil, nil)
mockSchema.EXPECT().GetIndexes("test").Return(nil, assert.AnError)
mockContext.EXPECT().Error(mock.Anything).Run(func(message string) {
mockContext.EXPECT().NewLine().Once()
mockContext.EXPECT().Option("database").Return("test").Once()
mockSchema.EXPECT().Connection("test").Return(mockSchema).Once()
mockContext.EXPECT().Argument(0).Return("").Once()
mockSchema.EXPECT().GetTables().Return([]schema.Table{{Name: "test"}}, nil).Once()
mockContext.EXPECT().Choice("Which table would you like to inspect?",
[]console.Choice{{Key: "test", Value: "test"}}).Return("test", nil).Once()
mockSchema.EXPECT().GetColumns("test").Return(nil, nil).Once()
mockSchema.EXPECT().GetIndexes("test").Return(nil, assert.AnError).Once()
mockContext.EXPECT().Error(fmt.Sprintf("Failed to get indexes: %s", assert.AnError.Error())).Run(func(message string) {
color.Errorln(message)
})
}).Once()
},
expected: assert.AnError.Error(),
},
{
name: "get foreign keys failed",
setup: func() {
mockContext.EXPECT().NewLine()
mockContext.EXPECT().Option("database").Return("")
mockSchema.EXPECT().Connection(mock.Anything).Return(mockSchema)
mockContext.EXPECT().Argument(0).Return("")
mockSchema.EXPECT().GetTables().Return([]schema.Table{{Name: "test"}}, nil)
mockContext.EXPECT().Choice(mock.Anything, mock.Anything).Return("test", nil)
mockSchema.EXPECT().GetColumns("test").Return(nil, nil)
mockSchema.EXPECT().GetIndexes("test").Return(nil, nil)
mockSchema.EXPECT().GetForeignKeys("test").Return(nil, assert.AnError)
mockContext.EXPECT().Error(mock.Anything).Run(func(message string) {
mockContext.EXPECT().NewLine().Once()
mockContext.EXPECT().Option("database").Return("test").Once()
mockSchema.EXPECT().Connection("test").Return(mockSchema).Once()
mockContext.EXPECT().Argument(0).Return("").Once()
mockSchema.EXPECT().GetTables().Return([]schema.Table{{Name: "test"}}, nil).Once()
mockContext.EXPECT().Choice("Which table would you like to inspect?",
[]console.Choice{{Key: "test", Value: "test"}}).Return("test", nil).Once()
mockSchema.EXPECT().GetColumns("test").Return(nil, nil).Once()
mockSchema.EXPECT().GetIndexes("test").Return(nil, nil).Once()
mockSchema.EXPECT().GetForeignKeys("test").Return(nil, assert.AnError).Once()
mockContext.EXPECT().Error(fmt.Sprintf("Failed to get foreign keys: %s", assert.AnError.Error())).Run(func(message string) {
color.Errorln(message)
})
}).Once()
},
expected: assert.AnError.Error(),
},
{
name: "success",
setup: func() {
mockContext.EXPECT().NewLine()
mockContext.EXPECT().Option("database").Return("")
mockSchema.EXPECT().Connection(mock.Anything).Return(mockSchema)
mockContext.EXPECT().Argument(0).Return("")
mockSchema.EXPECT().GetTables().Return([]schema.Table{{Name: "test"}}, nil)
mockContext.EXPECT().Choice(mock.Anything, mock.Anything).Return("test", nil)
mockContext.EXPECT().NewLine().Times(5)
mockContext.EXPECT().Option("database").Return("test").Once()
mockSchema.EXPECT().Connection("test").Return(mockSchema).Once()
mockContext.EXPECT().Argument(0).Return("").Once()
mockSchema.EXPECT().GetTables().Return([]schema.Table{{Name: "test"}}, nil).Once()
mockContext.EXPECT().Choice("Which table would you like to inspect?",
[]console.Choice{{Key: "test", Value: "test"}}).Return("test", nil).Once()
mockSchema.EXPECT().GetColumns("test").Return([]schema.Column{
{Name: "foo", Type: "int", Autoincrement: true, Nullable: true, Default: "bar"},
}, nil)
{Name: "foo", Type: "int", TypeName: "int", Autoincrement: true, Nullable: true, Default: "bar"},
}, nil).Once()
mockSchema.EXPECT().GetIndexes("test").Return([]schema.Index{
{Name: "index_foo", Columns: []string{"foo", "bar"}, Unique: true, Primary: true},
}, nil)
}, nil).Once()
mockSchema.EXPECT().GetForeignKeys("test").Return([]schema.ForeignKey{
{Name: "fk_foo", Columns: []string{"foo"}, ForeignTable: "bar", ForeignColumns: []string{"baz"}},
}, nil)
mockContext.EXPECT().TwoColumnDetail(mock.Anything, mock.Anything)
{
Name: "fk_foo",
Columns: []string{"foo"},
ForeignTable: "bar",
ForeignColumns: []string{"baz"},
OnDelete: "cascade",
OnUpdate: "restrict",
},
}, nil).Once()
for i := range successCaseExpected {
mockContext.EXPECT().TwoColumnDetail(successCaseExpected[i][0], successCaseExpected[i][1]).Run(func(first string, second string, filler ...rune) {
color.Default().Printf("%s %s\n", first, second)
}).Once()
}
},
expected: func() string {
var result string
for i := range successCaseExpected {
result += color.Default().Sprintf("%s %s\n", successCaseExpected[i][0], successCaseExpected[i][1])
}
return result
}(),
},
}

Expand Down

0 comments on commit bd7c75c

Please sign in to comment.