From 04de68316b61c170a275ad5613e6f81c9d69a7d7 Mon Sep 17 00:00:00 2001 From: Bowen Date: Sat, 30 Nov 2024 16:01:33 +0800 Subject: [PATCH] fix test --- database/schema/grammars/mysql.go | 18 +++++++------ database/schema/grammars/mysql_test.go | 32 +++++++++++++++++++++++ database/schema/grammars/postgres_test.go | 8 ++++++ database/schema/grammars/utils.go | 4 +-- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/database/schema/grammars/mysql.go b/database/schema/grammars/mysql.go index 8d02b8d97..172962873 100644 --- a/database/schema/grammars/mysql.go +++ b/database/schema/grammars/mysql.go @@ -237,8 +237,9 @@ func (r *Mysql) TypeDate(column schema.ColumnDefinition) string { func (r *Mysql) TypeDateTime(column schema.ColumnDefinition) string { current := "CURRENT_TIMESTAMP" - if column.GetPrecision() > 0 { - current = fmt.Sprintf("CURRENT_TIMESTAMP(%d)", column.GetPrecision()) + precision := column.GetPrecision() + if precision > 0 { + current = fmt.Sprintf("CURRENT_TIMESTAMP(%d)", precision) } if column.GetUseCurrent() { column.Default(Expression(current)) @@ -247,8 +248,8 @@ func (r *Mysql) TypeDateTime(column schema.ColumnDefinition) string { column.OnUpdate(Expression(current)) } - if column.GetPrecision() > 0 { - return fmt.Sprintf("datetime(%d)", column.GetPrecision()) + if precision > 0 { + return fmt.Sprintf("datetime(%d)", precision) } else { return "datetime" } @@ -334,8 +335,9 @@ func (r *Mysql) TypeTimeTz(column schema.ColumnDefinition) string { func (r *Mysql) TypeTimestamp(column schema.ColumnDefinition) string { current := "CURRENT_TIMESTAMP" - if column.GetPrecision() > 0 { - current = fmt.Sprintf("CURRENT_TIMESTAMP(%d)", column.GetPrecision()) + precision := column.GetPrecision() + if precision > 0 { + current = fmt.Sprintf("CURRENT_TIMESTAMP(%d)", precision) } if column.GetUseCurrent() { column.Default(Expression(current)) @@ -344,8 +346,8 @@ func (r *Mysql) TypeTimestamp(column schema.ColumnDefinition) string { column.OnUpdate(Expression(current)) } - if column.GetPrecision() > 0 { - return fmt.Sprintf("timestamp(%d)", column.GetPrecision()) + if precision > 0 { + return fmt.Sprintf("timestamp(%d)", precision) } else { return "timestamp" } diff --git a/database/schema/grammars/mysql_test.go b/database/schema/grammars/mysql_test.go index defd4dd4d..3bb624f71 100644 --- a/database/schema/grammars/mysql_test.go +++ b/database/schema/grammars/mysql_test.go @@ -319,6 +319,22 @@ func (s *MysqlSuite) TestModifyOnUpdate() { s.Empty(s.grammar.ModifyOnUpdate(mockBlueprint, mockColumn)) } +func (s *MysqlSuite) TestTypeDateTime() { + mockColumn := mocksschema.NewColumnDefinition(s.T()) + mockColumn.EXPECT().GetPrecision().Return(3).Once() + mockColumn.EXPECT().GetUseCurrent().Return(true).Once() + mockColumn.EXPECT().Default(Expression("CURRENT_TIMESTAMP(3)")).Return(mockColumn).Once() + mockColumn.EXPECT().GetUseCurrentOnUpdate().Return(true).Once() + mockColumn.EXPECT().OnUpdate(Expression("CURRENT_TIMESTAMP(3)")).Return(mockColumn).Once() + s.Equal("datetime(3)", s.grammar.TypeDateTime(mockColumn)) + + mockColumn = mocksschema.NewColumnDefinition(s.T()) + mockColumn.EXPECT().GetPrecision().Return(0).Once() + mockColumn.EXPECT().GetUseCurrent().Return(false).Once() + mockColumn.EXPECT().GetUseCurrentOnUpdate().Return(false).Once() + s.Equal("datetime", s.grammar.TypeDateTime(mockColumn)) +} + func (s *MysqlSuite) TestTypeDecimal() { mockColumn := mocksschema.NewColumnDefinition(s.T()) mockColumn.EXPECT().GetTotal().Return(4).Once() @@ -356,3 +372,19 @@ func (s *MysqlSuite) TestTypeString() { s.Equal("varchar(255)", s.grammar.TypeString(mockColumn2)) } + +func (s *MysqlSuite) TestTypeTimestamp() { + mockColumn := mocksschema.NewColumnDefinition(s.T()) + mockColumn.EXPECT().GetPrecision().Return(3).Once() + mockColumn.EXPECT().GetUseCurrent().Return(true).Once() + mockColumn.EXPECT().Default(Expression("CURRENT_TIMESTAMP(3)")).Return(mockColumn).Once() + mockColumn.EXPECT().GetUseCurrentOnUpdate().Return(true).Once() + mockColumn.EXPECT().OnUpdate(Expression("CURRENT_TIMESTAMP(3)")).Return(mockColumn).Once() + s.Equal("timestamp(3)", s.grammar.TypeTimestamp(mockColumn)) + + mockColumn = mocksschema.NewColumnDefinition(s.T()) + mockColumn.EXPECT().GetPrecision().Return(0).Once() + mockColumn.EXPECT().GetUseCurrent().Return(false).Once() + mockColumn.EXPECT().GetUseCurrentOnUpdate().Return(false).Once() + s.Equal("timestamp", s.grammar.TypeTimestamp(mockColumn)) +} diff --git a/database/schema/grammars/postgres_test.go b/database/schema/grammars/postgres_test.go index d08b9e482..06617fd8c 100644 --- a/database/schema/grammars/postgres_test.go +++ b/database/schema/grammars/postgres_test.go @@ -390,3 +390,11 @@ func (s *PostgresSuite) TestTypeString() { s.Equal("varchar", s.grammar.TypeString(mockColumn2)) } + +func (s *PostgresSuite) TestTypeTimestamp() { + mockColumn := mocksschema.NewColumnDefinition(s.T()) + mockColumn.EXPECT().GetUseCurrent().Return(true).Once() + mockColumn.EXPECT().Default(Expression("CURRENT_TIMESTAMP")).Return(mockColumn).Once() + mockColumn.EXPECT().GetPrecision().Return(3).Once() + s.Equal("timestamp(3) without time zone", s.grammar.TypeTimestamp(mockColumn)) +} diff --git a/database/schema/grammars/utils.go b/database/schema/grammars/utils.go index 370e94b15..eb74a2252 100644 --- a/database/schema/grammars/utils.go +++ b/database/schema/grammars/utils.go @@ -38,9 +38,9 @@ func getDefaultValue(def any) string { return "'" + cast.ToString(cast.ToInt(value)) + "'" case Expression: return string(value) + default: + return "'" + cast.ToString(def) + "'" } - - return "'" + cast.ToString(def) + "'" } func getType(grammar schema.Grammar, column schema.ColumnDefinition) string {