Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Nov 8, 2024
1 parent b95cc48 commit 4610865
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 16 deletions.
5 changes: 2 additions & 3 deletions database/schema/grammars/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func (s *PostgresSuite) TestCompileForeign() {

beforeEach := func() {
mockBlueprint = mocksschema.NewBlueprint(s.T())
mockBlueprint.EXPECT().GetPrefix().Return("goravel_").Once()
mockBlueprint.EXPECT().GetTableName().Return("users").Once()
}

Expand All @@ -116,7 +115,7 @@ func (s *PostgresSuite) TestCompileForeign() {
OnDelete: "cascade",
OnUpdate: "restrict",
},
expectSql: "alter table users add constraint fk_users_role_id foreign key (role_id) references goravel_roles (id) on delete cascade on update restrict",
expectSql: "alter table users add constraint fk_users_role_id foreign key (role_id) references roles (id) on delete cascade on update restrict",
},
{
name: "without on delete and on update",
Expand All @@ -126,7 +125,7 @@ func (s *PostgresSuite) TestCompileForeign() {
On: "roles",
References: []string{"id"},
},
expectSql: "alter table users add constraint fk_users_role_id foreign key (role_id) references goravel_roles (id)",
expectSql: "alter table users add constraint fk_users_role_id foreign key (role_id) references roles (id)",
},
}

Expand Down
2 changes: 1 addition & 1 deletion database/schema/grammars/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (r *Sqlite) getForeignKey(blueprint schema.Blueprint, command *schema.Comma
sql += " on update " + command.OnUpdate
}

return ""
return sql
}

func (r *Sqlite) pragma(name, value string) string {
Expand Down
187 changes: 187 additions & 0 deletions database/schema/grammars/sqlite_test.go
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))
}
17 changes: 17 additions & 0 deletions database/schema/grammars/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ func TestGetColumns(t *testing.T) {
assert.Equal(t, []string{"id varchar(100)", "name varchar"}, getColumns(mockGrammar, mockBlueprint))
}

func TestGetCommandByName(t *testing.T) {
commands := []*schema.Command{
{Name: "create"},
{Name: "update"},
{Name: "delete"},
}

// Test case: Command exists
result := getCommandByName(commands, "update")
assert.NotNil(t, result)
assert.Equal(t, "update", result.Name)

// Test case: Command does not exist
result = getCommandByName(commands, "drop")
assert.Nil(t, result)
}

func TestGetDefaultValue(t *testing.T) {
def := true
assert.Equal(t, "'1'", getDefaultValue(def))
Expand Down
21 changes: 10 additions & 11 deletions database/schema/processors/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package processors
import (
"testing"

"github.com/gookit/goutil/testutil/assert"
"github.com/stretchr/testify/assert"

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

func TestProcessIndexes(t *testing.T) {
func TestPostgresProcessIndexes(t *testing.T) {
// Test with valid indexes
input := []schema.Index{
{Name: "INDEX_A", Type: "BTREE"},
{Name: "INDEX_B", Type: "HASH"},
input := []DBIndex{
{Name: "INDEX_A", Type: "BTREE", Columns: "a,b"},
{Name: "INDEX_B", Type: "HASH", Columns: "c,d"},
}
expected := []schema.Index{
{Name: "index_a", Type: "btree"},
{Name: "index_b", Type: "hash"},
{Name: "index_a", Type: "btree", Columns: []string{"a", "b"}},
{Name: "index_b", Type: "hash", Columns: []string{"c", "d"}},
}

postgres := NewPostgres()
Expand All @@ -25,15 +25,14 @@ func TestProcessIndexes(t *testing.T) {
assert.Equal(t, expected, result)

// Test with empty input
input = []schema.Index{}
expected = []schema.Index{}
input = []DBIndex{}

result = postgres.ProcessIndexes(input)

assert.Equal(t, expected, result)
assert.Nil(t, result)
}

func TestProcessTypes(t *testing.T) {
func TestPostgresProcessTypes(t *testing.T) {
// ValidTypes_ReturnsProcessedTypes
input := []schema.Type{
{Type: "b", Category: "a"},
Expand Down
51 changes: 51 additions & 0 deletions database/schema/processors/sqlite_test.go
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)
}
2 changes: 1 addition & 1 deletion support/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
TestModelNormal

// Switch this value to control the test model.
TestModel = TestModelMinimum
TestModel = TestModelNormal
)

type ContainerType string
Expand Down

0 comments on commit 4610865

Please sign in to comment.