-
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
7 changed files
with
269 additions
and
16 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
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
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,187 @@ | ||
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 SqliteSuite struct { | ||
suite.Suite | ||
grammar *Sqlite | ||
} | ||
|
||
func TestSqliteSuite(t *testing.T) { | ||
suite.Run(t, &SqliteSuite{}) | ||
} | ||
|
||
func (s *SqliteSuite) SetupTest() { | ||
s.grammar = NewSqlite() | ||
} | ||
|
||
func (s *SqliteSuite) 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() | ||
|
||
sql := s.grammar.CompileAdd(mockBlueprint, &contractsschema.Command{ | ||
Column: mockColumn, | ||
}) | ||
|
||
s.Equal("alter table users add column name varchar default 'goravel' not null", sql) | ||
} | ||
|
||
func (s *SqliteSuite) TestCompileCreate() { | ||
mockColumn1 := mocksschema.NewColumnDefinition(s.T()) | ||
mockColumn2 := mocksschema.NewColumnDefinition(s.T()) | ||
mockBlueprint := mocksschema.NewBlueprint(s.T()) | ||
|
||
// sqlite.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() | ||
// sqlite.go::TypeInteger | ||
mockColumn1.EXPECT().GetAutoIncrement().Return(true).Once() | ||
// sqlite.go::ModifyDefault | ||
mockColumn1.EXPECT().GetDefault().Return(nil).Once() | ||
// sqlite.go::ModifyIncrement | ||
mockColumn1.EXPECT().GetType().Return("integer").Once() | ||
mockColumn1.EXPECT().GetAutoIncrement().Return(true).Once() | ||
// sqlite.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() | ||
// sqlite.go::ModifyDefault | ||
mockColumn2.EXPECT().GetDefault().Return(nil).Once() | ||
// sqlite.go::ModifyIncrement | ||
mockColumn2.EXPECT().GetType().Return("string").Once() | ||
// sqlite.go::ModifyNullable | ||
mockColumn2.EXPECT().GetNullable().Return(true).Once() | ||
|
||
// sqlite.go::CompileCreate | ||
mockBlueprint.EXPECT().GetCommands().Return([]*contractsschema.Command{ | ||
{ | ||
Name: "primary", | ||
Columns: []string{"id"}, | ||
}, | ||
{ | ||
Name: "foreign", | ||
Columns: []string{"role_id"}, | ||
On: "roles", | ||
References: []string{"id"}, | ||
OnDelete: "cascade", | ||
OnUpdate: "restrict", | ||
}, | ||
{ | ||
Name: "foreign", | ||
Columns: []string{"permission_id"}, | ||
On: "permissions", | ||
References: []string{"id"}, | ||
OnDelete: "cascade", | ||
OnUpdate: "restrict", | ||
}, | ||
}).Twice() | ||
|
||
s.Equal("create table users (id serial primary key autoincrement not null,name varchar null, foreign key(role_id) references roles(id) on delete cascade on update restrict, foreign key(permission_id) references permissions(id) on delete cascade on update restrict, primary key (id))", | ||
s.grammar.CompileCreate(mockBlueprint)) | ||
} | ||
|
||
func (s *SqliteSuite) TestCompileDropIfExists() { | ||
mockBlueprint := mocksschema.NewBlueprint(s.T()) | ||
mockBlueprint.EXPECT().GetTableName().Return("users").Once() | ||
|
||
s.Equal("drop table if exists users", s.grammar.CompileDropIfExists(mockBlueprint)) | ||
} | ||
|
||
func (s *SqliteSuite) 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 *SqliteSuite) 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 *SqliteSuite) TestModifyIncrement() { | ||
mockBlueprint := mocksschema.NewBlueprint(s.T()) | ||
|
||
mockColumn := mocksschema.NewColumnDefinition(s.T()) | ||
mockColumn.EXPECT().GetType().Return("bigInteger").Once() | ||
mockColumn.EXPECT().GetAutoIncrement().Return(true).Once() | ||
|
||
s.Equal(" primary key autoincrement", s.grammar.ModifyIncrement(mockBlueprint, mockColumn)) | ||
} | ||
|
||
func (s *SqliteSuite) TestTypeInteger() { | ||
mockColumn1 := mocksschema.NewColumnDefinition(s.T()) | ||
mockColumn1.EXPECT().GetAutoIncrement().Return(true).Once() | ||
|
||
s.Equal("serial", s.grammar.TypeInteger(mockColumn1)) | ||
|
||
mockColumn2 := mocksschema.NewColumnDefinition(s.T()) | ||
mockColumn2.EXPECT().GetAutoIncrement().Return(false).Once() | ||
|
||
s.Equal("integer", s.grammar.TypeInteger(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
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
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,51 @@ | ||
package processors | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/goravel/framework/contracts/database/schema" | ||
) | ||
|
||
func TestSqliteProcessIndexes(t *testing.T) { | ||
// Test with valid indexes | ||
input := []DBIndex{ | ||
{Name: "INDEX_A", Type: "BTREE", Columns: "a,b"}, | ||
{Name: "INDEX_B", Type: "HASH", Columns: "c,d"}, | ||
{Name: "INDEX_C", Type: "HASH", Columns: "e,f", Primary: true}, | ||
} | ||
expected := []schema.Index{ | ||
{Name: "index_a", Columns: []string{"a", "b"}}, | ||
{Name: "index_b", Columns: []string{"c", "d"}}, | ||
{Name: "index_c", Columns: []string{"e", "f"}, Primary: true}, | ||
} | ||
|
||
sqlite := NewSqlite() | ||
result := sqlite.ProcessIndexes(input) | ||
|
||
assert.Equal(t, expected, result) | ||
|
||
// Test with valid indexes with multiple primary keys | ||
input = []DBIndex{ | ||
{Name: "INDEX_A", Type: "BTREE", Columns: "a,b"}, | ||
{Name: "INDEX_B", Type: "HASH", Columns: "c,d"}, | ||
{Name: "INDEX_C", Type: "HASH", Columns: "e,f", Primary: true}, | ||
{Name: "INDEX_D", Type: "HASH", Columns: "g,h", Primary: true}, | ||
} | ||
expected = []schema.Index{ | ||
{Name: "index_a", Columns: []string{"a", "b"}}, | ||
{Name: "index_b", Columns: []string{"c", "d"}}, | ||
} | ||
|
||
result = sqlite.ProcessIndexes(input) | ||
|
||
assert.Equal(t, expected, result) | ||
|
||
// Test with empty input | ||
input = []DBIndex{} | ||
|
||
result = sqlite.ProcessIndexes(input) | ||
|
||
assert.Nil(t, result) | ||
} |
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