Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Nov 20, 2024
1 parent 2b1d31c commit 9aad45a
Show file tree
Hide file tree
Showing 12 changed files with 405 additions and 62 deletions.
2 changes: 1 addition & 1 deletion contracts/database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Column struct {
Autoincrement bool
Collation string
Comment string
Default any
Default string
Name string
Nullable bool
Type string
Expand Down
35 changes: 35 additions & 0 deletions database/schema/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,41 @@ func (s *BlueprintTestSuite) TestCreateIndexName() {
s.Equal("public_goravel_users_id_name_1_name_2_index", name)
}

func (s *BlueprintTestSuite) TestDecimal() {
name := "name"
s.blueprint.Decimal(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
name: &name,
ttype: convert.Pointer("decimal"),
})
}

func (s *BlueprintTestSuite) TestDouble() {
name := "name"
s.blueprint.Double(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
name: &name,
ttype: convert.Pointer("double"),
})
}

func (s *BlueprintTestSuite) TestFloat() {
name := "name"
s.blueprint.Float(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
name: &name,
precision: convert.Pointer(53),
ttype: convert.Pointer("float"),
})

s.blueprint.Float(name, 10)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
name: &name,
precision: convert.Pointer(10),
ttype: convert.Pointer("float"),
})
}

func (s *BlueprintTestSuite) TestGetAddedColumns() {
name := "name"
addedColumn := &ColumnDefinition{
Expand Down
4 changes: 2 additions & 2 deletions database/schema/grammars/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ func (r *Mysql) GetAttributeCommands() []string {
}

func (r *Mysql) ModifyComment(blueprint schema.Blueprint, column schema.ColumnDefinition) string {
if column.GetComment() != "" {
return fmt.Sprintf(" comment '%s'", column.GetComment())
if comment := column.GetComment(); comment != "" {
return fmt.Sprintf(" comment '%s'", comment)
}

return ""
Expand Down
11 changes: 8 additions & 3 deletions database/schema/grammars/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ func (s *MysqlSuite) TestCompileAdd() {
mockColumn.EXPECT().GetDefault().Return("goravel").Twice()
mockColumn.EXPECT().GetNullable().Return(false).Once()
mockColumn.EXPECT().GetLength().Return(1).Once()
mockColumn.EXPECT().GetComment().Return("comment").Once()

sql := s.grammar.CompileAdd(mockBlueprint, &contractsschema.Command{
Column: mockColumn,
})

s.Equal("alter table `goravel_users` add `name` varchar(1) default 'goravel' not null", sql)
s.Equal("alter table `goravel_users` add `name` varchar(1) comment 'comment' default 'goravel' not null", sql)
}

func (s *MysqlSuite) TestCompileCreate() {
Expand Down Expand Up @@ -72,6 +73,7 @@ func (s *MysqlSuite) TestCompileCreate() {
mockColumn1.EXPECT().GetType().Return("integer").Once()
// postgres.go::ModifyNullable
mockColumn1.EXPECT().GetNullable().Return(false).Once()
mockColumn1.EXPECT().GetComment().Return("id").Once()

// utils.go::getColumns
mockColumn2.EXPECT().GetName().Return("name").Once()
Expand All @@ -85,8 +87,9 @@ func (s *MysqlSuite) TestCompileCreate() {
mockColumn2.EXPECT().GetType().Return("string").Once()
// postgres.go::ModifyNullable
mockColumn2.EXPECT().GetNullable().Return(true).Once()
mockColumn2.EXPECT().GetComment().Return("name").Once()

s.Equal("create table `goravel_users` (`id` int auto_increment primary key not null, `name` varchar(100) null, primary key using btree(`role_id`, `user_id`))",
s.Equal("create table `goravel_users` (`id` int comment 'id' auto_increment primary key not null, `name` varchar(100) comment 'name' null, primary key using btree(`role_id`, `user_id`))",
s.grammar.CompileCreate(mockBlueprint))
s.True(primaryCommand.ShouldBeSkipped)
}
Expand Down Expand Up @@ -219,14 +222,16 @@ func (s *MysqlSuite) TestGetColumns() {
mockColumn1.EXPECT().GetDefault().Return(nil).Once()
mockColumn1.EXPECT().GetNullable().Return(false).Once()
mockColumn1.EXPECT().GetAutoIncrement().Return(true).Once()
mockColumn1.EXPECT().GetComment().Return("id").Once()

mockColumn2.EXPECT().GetName().Return("name").Once()
mockColumn2.EXPECT().GetType().Return("string").Twice()
mockColumn2.EXPECT().GetDefault().Return("goravel").Twice()
mockColumn2.EXPECT().GetNullable().Return(true).Once()
mockColumn2.EXPECT().GetLength().Return(10).Once()
mockColumn2.EXPECT().GetComment().Return("name").Once()

s.Equal([]string{"`id` int auto_increment primary key not null", "`name` varchar(10) default 'goravel' null"}, s.grammar.getColumns(mockBlueprint))
s.Equal([]string{"`id` int comment 'id' auto_increment primary key not null", "`name` varchar(10) comment 'name' default 'goravel' null"}, s.grammar.getColumns(mockBlueprint))
}

func (s *MysqlSuite) TestModifyDefault() {
Expand Down
3 changes: 2 additions & 1 deletion database/schema/grammars/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ func (s *PostgresSuite) TestCompileComment() {
mockColumnDefinition := mocksschema.NewColumnDefinition(s.T())
mockBlueprint.On("GetTableName").Return("users").Once()
mockColumnDefinition.On("GetName").Return("id").Once()
mockColumnDefinition.On("IsSetComment").Return(true).Once()
mockColumnDefinition.On("GetComment").Return("comment").Once()

sql := s.grammar.CompileComment(mockBlueprint, &contractsschema.Command{
Column: mockColumnDefinition,
})

s.Equal("comment on column users.id is 'comment'", sql)
s.Equal(`comment on column "goravel_users"."id" is 'comment'`, sql)
}

func (s *PostgresSuite) TestCompileCreate() {
Expand Down
59 changes: 59 additions & 0 deletions database/schema/processors/mysql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package processors

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/goravel/framework/contracts/database/schema"
)

func TestMysqlProcessColumns(t *testing.T) {
tests := []struct {
name string
dbColumns []DBColumn
expected []schema.Column
}{
{
name: "ValidInput",
dbColumns: []DBColumn{
{Name: "id", Type: "int", TypeName: "INT", Nullable: "NO", Extra: "auto_increment", Collation: "utf8_general_ci", Comment: "primary key", Default: "0"},
{Name: "name", Type: "varchar", TypeName: "VARCHAR", Nullable: "YES", Extra: "", Collation: "utf8_general_ci", Comment: "user name", Default: ""},
},
expected: []schema.Column{
{Autoincrement: true, Collation: "utf8_general_ci", Comment: "primary key", Default: "0", Name: "id", Nullable: false, Type: "int", TypeName: "INT"},
{Autoincrement: false, Collation: "utf8_general_ci", Comment: "user name", Default: "", Name: "name", Nullable: true, Type: "varchar", TypeName: "VARCHAR"},
},
},
{
name: "EmptyInput",
dbColumns: []DBColumn{},
},
{
name: "NullableColumn",
dbColumns: []DBColumn{
{Name: "description", Type: "text", TypeName: "TEXT", Nullable: "YES", Extra: "", Collation: "utf8_general_ci", Comment: "description", Default: ""},
},
expected: []schema.Column{
{Autoincrement: false, Collation: "utf8_general_ci", Comment: "description", Default: "", Name: "description", Nullable: true, Type: "text", TypeName: "TEXT"},
},
},
{
name: "NonNullableColumn",
dbColumns: []DBColumn{
{Name: "created_at", Type: "timestamp", TypeName: "TIMESTAMP", Nullable: "NO", Extra: "", Collation: "", Comment: "creation time", Default: "CURRENT_TIMESTAMP"},
},
expected: []schema.Column{
{Autoincrement: false, Collation: "", Comment: "creation time", Default: "CURRENT_TIMESTAMP", Name: "created_at", Nullable: false, Type: "timestamp", TypeName: "TIMESTAMP"},
},
},
}

mysql := NewMysql()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := mysql.ProcessColumns(tt.dbColumns)
assert.Equal(t, tt.expected, result)
})
}
}
2 changes: 1 addition & 1 deletion database/schema/processors/postgres.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package processors

import (
"github.com/goravel/framework/support/str"
"github.com/spf13/cast"

"github.com/goravel/framework/contracts/database/schema"
"github.com/goravel/framework/support/str"
)

type Postgres struct {
Expand Down
50 changes: 50 additions & 0 deletions database/schema/processors/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,56 @@ import (
"github.com/goravel/framework/contracts/database/schema"
)

func TestPostgresProcessColumns(t *testing.T) {
tests := []struct {
name string
dbColumns []DBColumn
expected []schema.Column
}{
{
name: "ValidInput",
dbColumns: []DBColumn{
{Name: "id", Type: "int", TypeName: "INT", Nullable: "NO", Extra: "auto_increment", Collation: "utf8_general_ci", Comment: "primary key", Default: "nextval('id_seq'::regclass)"},
{Name: "name", Type: "varchar", TypeName: "VARCHAR", Nullable: "true", Extra: "", Collation: "utf8_general_ci", Comment: "user name", Default: ""},
},
expected: []schema.Column{
{Autoincrement: true, Collation: "utf8_general_ci", Comment: "primary key", Default: "nextval('id_seq'::regclass)", Name: "id", Nullable: false, Type: "int", TypeName: "INT"},
{Autoincrement: false, Collation: "utf8_general_ci", Comment: "user name", Default: "", Name: "name", Nullable: true, Type: "varchar", TypeName: "VARCHAR"},
},
},
{
name: "EmptyInput",
dbColumns: []DBColumn{},
},
{
name: "NullableColumn",
dbColumns: []DBColumn{
{Name: "description", Type: "text", TypeName: "TEXT", Nullable: "true", Extra: "", Collation: "utf8_general_ci", Comment: "description", Default: ""},
},
expected: []schema.Column{
{Autoincrement: false, Collation: "utf8_general_ci", Comment: "description", Default: "", Name: "description", Nullable: true, Type: "text", TypeName: "TEXT"},
},
},
{
name: "NonNullableColumn",
dbColumns: []DBColumn{
{Name: "created_at", Type: "timestamp", TypeName: "TIMESTAMP", Nullable: "false", Extra: "", Collation: "", Comment: "creation time", Default: "CURRENT_TIMESTAMP"},
},
expected: []schema.Column{
{Autoincrement: false, Collation: "", Comment: "creation time", Default: "CURRENT_TIMESTAMP", Name: "created_at", Nullable: false, Type: "timestamp", TypeName: "TIMESTAMP"},
},
},
}

postgres := NewPostgres()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := postgres.ProcessColumns(tt.dbColumns)
assert.Equal(t, tt.expected, result)
})
}
}

func TestPostgresProcessTypes(t *testing.T) {
// ValidTypes_ReturnsProcessedTypes
input := []schema.Type{
Expand Down
50 changes: 50 additions & 0 deletions database/schema/processors/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,56 @@ import (
"github.com/goravel/framework/contracts/database/schema"
)

func TestProcessColumns(t *testing.T) {
tests := []struct {
name string
dbColumns []DBColumn
expected []schema.Column
}{
{
name: "ValidInput",
dbColumns: []DBColumn{
{Name: "id", Type: "integer", Nullable: "false", Primary: true, Default: "1"},
{Name: "name", Type: "varchar", Nullable: "true", Default: "default_name"},
},
expected: []schema.Column{
{Autoincrement: true, Default: "1", Name: "id", Nullable: false, Type: "integer"},
{Autoincrement: false, Default: "default_name", Name: "name", Nullable: true, Type: "varchar"},
},
},
{
name: "EmptyInput",
dbColumns: []DBColumn{},
},
{
name: "NullableColumn",
dbColumns: []DBColumn{
{Name: "description", Type: "text", Nullable: "true", Default: "default_description"},
},
expected: []schema.Column{
{Autoincrement: false, Default: "default_description", Name: "description", Nullable: true, Type: "text"},
},
},
{
name: "NonNullableColumn",
dbColumns: []DBColumn{
{Name: "created_at", Type: "timestamp", Nullable: "false", Default: "CURRENT_TIMESTAMP"},
},
expected: []schema.Column{
{Autoincrement: false, Default: "CURRENT_TIMESTAMP", Name: "created_at", Nullable: false, Type: "timestamp"},
},
},
}

sqlite := NewSqlite()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := sqlite.ProcessColumns(tt.dbColumns)
assert.Equal(t, tt.expected, result)
})
}
}

func TestSqliteProcessIndexes(t *testing.T) {
// Test with valid indexes
input := []DBIndex{
Expand Down
1 change: 1 addition & 0 deletions database/schema/processors/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package processors

import (
"fmt"

"github.com/spf13/cast"

"github.com/goravel/framework/contracts/database/schema"
Expand Down
Loading

0 comments on commit 9aad45a

Please sign in to comment.