-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
333 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,289 @@ | ||
package grammars | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
contractsschema "github.com/goravel/framework/contracts/database/schema" | ||
mocksschema "github.com/goravel/framework/mocks/database/schema" | ||
) | ||
|
||
type SqlserverSuite struct { | ||
suite.Suite | ||
grammar *Sqlserver | ||
} | ||
|
||
func TestSqlserverSuite(t *testing.T) { | ||
suite.Run(t, &SqlserverSuite{}) | ||
} | ||
|
||
func (s *SqlserverSuite) SetupTest() { | ||
s.grammar = NewSqlserver("goravel_") | ||
} | ||
|
||
func (s *SqlserverSuite) TestCompileAdd() { | ||
mockBlueprint := mocksschema.NewBlueprint(s.T()) | ||
mockColumn := mocksschema.NewColumnDefinition(s.T()) | ||
|
||
mockBlueprint.EXPECT().GetTableName().Return("users").Once() | ||
mockColumn.EXPECT().GetName().Return("name").Once() | ||
mockColumn.EXPECT().GetType().Return("string").Twice() | ||
mockColumn.EXPECT().GetDefault().Return("goravel").Twice() | ||
mockColumn.EXPECT().GetNullable().Return(false).Once() | ||
mockColumn.EXPECT().GetLength().Return(1).Once() | ||
|
||
sql := s.grammar.CompileAdd(mockBlueprint, &contractsschema.Command{ | ||
Column: mockColumn, | ||
}) | ||
|
||
s.Equal(`alter table "goravel_users" add "name" nvarchar(1) default 'goravel' not null`, sql) | ||
} | ||
|
||
func (s *SqlserverSuite) TestCompileCreate() { | ||
mockColumn1 := mocksschema.NewColumnDefinition(s.T()) | ||
mockColumn2 := mocksschema.NewColumnDefinition(s.T()) | ||
mockBlueprint := mocksschema.NewBlueprint(s.T()) | ||
|
||
// postgres.go::CompileCreate | ||
mockBlueprint.EXPECT().GetTableName().Return("users").Once() | ||
// utils.go::getColumns | ||
mockBlueprint.EXPECT().GetAddedColumns().Return([]contractsschema.ColumnDefinition{ | ||
mockColumn1, mockColumn2, | ||
}).Once() | ||
// utils.go::getColumns | ||
mockColumn1.EXPECT().GetName().Return("id").Once() | ||
// utils.go::getType | ||
mockColumn1.EXPECT().GetType().Return("integer").Once() | ||
// postgres.go::TypeInteger | ||
mockColumn1.EXPECT().GetAutoIncrement().Return(true).Once() | ||
// postgres.go::ModifyDefault | ||
mockColumn1.EXPECT().GetDefault().Return(nil).Once() | ||
// postgres.go::ModifyIncrement | ||
mockBlueprint.EXPECT().HasCommand("primary").Return(false).Once() | ||
mockColumn1.EXPECT().GetType().Return("integer").Once() | ||
// postgres.go::ModifyNullable | ||
mockColumn1.EXPECT().GetNullable().Return(false).Once() | ||
|
||
// utils.go::getColumns | ||
mockColumn2.EXPECT().GetName().Return("name").Once() | ||
// utils.go::getType | ||
mockColumn2.EXPECT().GetType().Return("string").Once() | ||
// postgres.go::TypeString | ||
mockColumn2.EXPECT().GetLength().Return(100).Once() | ||
// postgres.go::ModifyDefault | ||
mockColumn2.EXPECT().GetDefault().Return(nil).Once() | ||
// postgres.go::ModifyIncrement | ||
mockColumn2.EXPECT().GetType().Return("string").Once() | ||
// postgres.go::ModifyNullable | ||
mockColumn2.EXPECT().GetNullable().Return(true).Once() | ||
|
||
s.Equal(`create table "goravel_users" ("id" int identity primary key not null, "name" nvarchar(100) null)`, | ||
s.grammar.CompileCreate(mockBlueprint)) | ||
} | ||
|
||
func (s *SqlserverSuite) TestCompileDropIfExists() { | ||
mockBlueprint := mocksschema.NewBlueprint(s.T()) | ||
mockBlueprint.EXPECT().GetTableName().Return("users").Once() | ||
|
||
s.Equal(`if object_id('"goravel_users"', 'U') is not null drop table "goravel_users"`, s.grammar.CompileDropIfExists(mockBlueprint)) | ||
} | ||
|
||
func (s *SqlserverSuite) TestCompileForeign() { | ||
var mockBlueprint *mocksschema.Blueprint | ||
|
||
beforeEach := func() { | ||
mockBlueprint = mocksschema.NewBlueprint(s.T()) | ||
mockBlueprint.EXPECT().GetTableName().Return("users").Once() | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
command *contractsschema.Command | ||
expectSql string | ||
}{ | ||
{ | ||
name: "with on delete and on update", | ||
command: &contractsschema.Command{ | ||
Index: "fk_users_role_id", | ||
Columns: []string{"role_id", "user_id"}, | ||
On: "roles", | ||
References: []string{"id", "user_id"}, | ||
OnDelete: "cascade", | ||
OnUpdate: "restrict", | ||
}, | ||
expectSql: `alter table "goravel_users" add constraint "fk_users_role_id" foreign key ("role_id", "user_id") references "goravel_roles" ("id", "user_id") on delete cascade on update restrict`, | ||
}, | ||
{ | ||
name: "without on delete and on update", | ||
command: &contractsschema.Command{ | ||
Index: "fk_users_role_id", | ||
Columns: []string{"role_id", "user_id"}, | ||
On: "roles", | ||
References: []string{"id", "user_id"}, | ||
}, | ||
expectSql: `alter table "goravel_users" add constraint "fk_users_role_id" foreign key ("role_id", "user_id") references "goravel_roles" ("id", "user_id")`, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
s.Run(test.name, func() { | ||
beforeEach() | ||
|
||
sql := s.grammar.CompileForeign(mockBlueprint, test.command) | ||
s.Equal(test.expectSql, sql) | ||
}) | ||
} | ||
} | ||
|
||
func (s *SqlserverSuite) TestCompileIndex() { | ||
var mockBlueprint *mocksschema.Blueprint | ||
|
||
beforeEach := func() { | ||
mockBlueprint = mocksschema.NewBlueprint(s.T()) | ||
mockBlueprint.EXPECT().GetTableName().Return("users").Once() | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
command *contractsschema.Command | ||
expectSql string | ||
}{ | ||
{ | ||
name: "with Algorithm", | ||
command: &contractsschema.Command{ | ||
Index: "fk_users_role_id", | ||
Columns: []string{"role_id", "user_id"}, | ||
Algorithm: "btree", | ||
}, | ||
expectSql: `create index "fk_users_role_id" on "goravel_users" ("role_id", "user_id")`, | ||
}, | ||
{ | ||
name: "without Algorithm", | ||
command: &contractsschema.Command{ | ||
Index: "fk_users_role_id", | ||
Columns: []string{"role_id", "user_id"}, | ||
}, | ||
expectSql: `create index "fk_users_role_id" on "goravel_users" ("role_id", "user_id")`, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
s.Run(test.name, func() { | ||
beforeEach() | ||
|
||
sql := s.grammar.CompileIndex(mockBlueprint, test.command) | ||
s.Equal(test.expectSql, sql) | ||
}) | ||
} | ||
} | ||
|
||
func (s *SqlserverSuite) TestCompilePrimary() { | ||
mockBlueprint := mocksschema.NewBlueprint(s.T()) | ||
mockBlueprint.EXPECT().GetTableName().Return("users").Once() | ||
|
||
s.Equal(`alter table "goravel_users" add constraint "role" primary key ("role_id", "user_id")`, s.grammar.CompilePrimary(mockBlueprint, &contractsschema.Command{ | ||
Columns: []string{"role_id", "user_id"}, | ||
Index: "role", | ||
})) | ||
} | ||
|
||
func (s *SqlserverSuite) TestGetColumns() { | ||
mockColumn1 := mocksschema.NewColumnDefinition(s.T()) | ||
mockColumn2 := mocksschema.NewColumnDefinition(s.T()) | ||
mockBlueprint := mocksschema.NewBlueprint(s.T()) | ||
|
||
mockBlueprint.EXPECT().GetAddedColumns().Return([]contractsschema.ColumnDefinition{ | ||
mockColumn1, mockColumn2, | ||
}).Once() | ||
mockBlueprint.EXPECT().HasCommand("primary").Return(false).Once() | ||
|
||
mockColumn1.EXPECT().GetName().Return("id").Once() | ||
mockColumn1.EXPECT().GetType().Return("integer").Twice() | ||
mockColumn1.EXPECT().GetDefault().Return(nil).Once() | ||
mockColumn1.EXPECT().GetNullable().Return(false).Once() | ||
mockColumn1.EXPECT().GetAutoIncrement().Return(true).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() | ||
|
||
s.Equal([]string{`"id" int identity primary key not null`, `"name" nvarchar(10) default 'goravel' null`}, s.grammar.getColumns(mockBlueprint)) | ||
} | ||
|
||
func (s *SqlserverSuite) TestModifyDefault() { | ||
var ( | ||
mockBlueprint *mocksschema.Blueprint | ||
mockColumn *mocksschema.ColumnDefinition | ||
) | ||
|
||
tests := []struct { | ||
name string | ||
setup func() | ||
expectSql string | ||
}{ | ||
{ | ||
name: "without change and default is nil", | ||
setup: func() { | ||
mockColumn.EXPECT().GetDefault().Return(nil).Once() | ||
}, | ||
}, | ||
{ | ||
name: "without change and default is not nil", | ||
setup: func() { | ||
mockColumn.EXPECT().GetDefault().Return("goravel").Twice() | ||
}, | ||
expectSql: " default 'goravel'", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
s.Run(test.name, func() { | ||
mockBlueprint = mocksschema.NewBlueprint(s.T()) | ||
mockColumn = mocksschema.NewColumnDefinition(s.T()) | ||
|
||
test.setup() | ||
|
||
sql := s.grammar.ModifyDefault(mockBlueprint, mockColumn) | ||
|
||
s.Equal(test.expectSql, sql) | ||
}) | ||
} | ||
} | ||
|
||
func (s *SqlserverSuite) TestModifyNullable() { | ||
mockBlueprint := mocksschema.NewBlueprint(s.T()) | ||
mockColumn := mocksschema.NewColumnDefinition(s.T()) | ||
mockColumn.EXPECT().GetNullable().Return(true).Once() | ||
|
||
s.Equal(" null", s.grammar.ModifyNullable(mockBlueprint, mockColumn)) | ||
|
||
mockColumn.EXPECT().GetNullable().Return(false).Once() | ||
|
||
s.Equal(" not null", s.grammar.ModifyNullable(mockBlueprint, mockColumn)) | ||
} | ||
|
||
func (s *SqlserverSuite) TestModifyIncrement() { | ||
mockBlueprint := mocksschema.NewBlueprint(s.T()) | ||
|
||
mockColumn := mocksschema.NewColumnDefinition(s.T()) | ||
mockBlueprint.EXPECT().HasCommand("primary").Return(false).Once() | ||
mockColumn.EXPECT().GetType().Return("bigInteger").Once() | ||
mockColumn.EXPECT().GetAutoIncrement().Return(true).Once() | ||
|
||
s.Equal(" identity primary key", s.grammar.ModifyIncrement(mockBlueprint, mockColumn)) | ||
} | ||
|
||
func (s *SqlserverSuite) TestTypeString() { | ||
mockColumn1 := mocksschema.NewColumnDefinition(s.T()) | ||
mockColumn1.EXPECT().GetLength().Return(100).Once() | ||
|
||
s.Equal("nvarchar(100)", s.grammar.TypeString(mockColumn1)) | ||
|
||
mockColumn2 := mocksschema.NewColumnDefinition(s.T()) | ||
mockColumn2.EXPECT().GetLength().Return(0).Once() | ||
|
||
s.Equal("nvarchar(255)", s.grammar.TypeString(mockColumn2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package schema | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/goravel/framework/database/schema/grammars" | ||
"github.com/goravel/framework/support/env" | ||
) | ||
|
||
type SqlserverSchemaSuite struct { | ||
suite.Suite | ||
sqlserverSchema *SqlserverSchema | ||
} | ||
|
||
func TestSqlserverSchemaSuite(t *testing.T) { | ||
if env.IsWindows() { | ||
t.Skip("Skip test that using Docker") | ||
} | ||
|
||
suite.Run(t, &SqlserverSchemaSuite{}) | ||
} | ||
|
||
func (s *SqlserverSchemaSuite) SetupTest() { | ||
s.sqlserverSchema = NewSqlserverSchema(grammars.NewSqlserver("goravel_"), nil, "goravel_") | ||
} | ||
|
||
func (s *SqlserverSchemaSuite) TestParseSchemaAndTable() { | ||
tests := []struct { | ||
reference string | ||
expectedSchema string | ||
expectedTable string | ||
}{ | ||
{"public.users", "public", "users"}, | ||
{"users", "", "users"}, | ||
} | ||
|
||
for _, test := range tests { | ||
schema, table := s.sqlserverSchema.parseSchemaAndTable(test.reference) | ||
s.Equal(test.expectedSchema, schema) | ||
s.Equal(test.expectedTable, table) | ||
} | ||
} |