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 4610865 commit bac8738
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 22 deletions.
12 changes: 4 additions & 8 deletions database/schema/grammars/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (r *Sqlite) CompileCreate(blueprint schema.Blueprint) string {
return fmt.Sprintf("create table %s (%s%s%s)",
blueprint.GetTableName(),
strings.Join(getColumns(r, blueprint), ","),
r.addForeignKeys(blueprint, getCommandsByName(blueprint.GetCommands(), "foreign")),
r.addForeignKeys(getCommandsByName(blueprint.GetCommands(), "foreign")),
r.addPrimaryKeys(getCommandByName(blueprint.GetCommands(), "primary")))
}

Expand Down Expand Up @@ -151,22 +151,18 @@ func (r *Sqlite) TypeBigInteger(column schema.ColumnDefinition) string {
}

func (r *Sqlite) TypeInteger(column schema.ColumnDefinition) string {
if column.GetAutoIncrement() {
return "serial"
}

return "integer"
}

func (r *Sqlite) TypeString(column schema.ColumnDefinition) string {
return "varchar"
}

func (r *Sqlite) addForeignKeys(blueprint schema.Blueprint, commands []*schema.Command) string {
func (r *Sqlite) addForeignKeys(commands []*schema.Command) string {
var sql string

for _, command := range commands {
sql += r.getForeignKey(blueprint, command)
sql += r.getForeignKey(command)
}

return sql
Expand All @@ -180,7 +176,7 @@ func (r *Sqlite) addPrimaryKeys(command *schema.Command) string {
return fmt.Sprintf(", primary key (%s)", strings.Join(command.Columns, ", "))
}

func (r *Sqlite) getForeignKey(blueprint schema.Blueprint, command *schema.Command) string {
func (r *Sqlite) getForeignKey(command *schema.Command) string {
sql := fmt.Sprintf(", foreign key(%s) references %s(%s)",
strings.Join(command.Columns, ","),
command.On,
Expand Down
15 changes: 1 addition & 14 deletions database/schema/grammars/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func (s *SqliteSuite) TestCompileCreate() {
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()

Expand Down Expand Up @@ -99,7 +98,7 @@ func (s *SqliteSuite) TestCompileCreate() {
},
}).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.Equal("create table users (id integer 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))
}

Expand Down Expand Up @@ -173,15 +172,3 @@ func (s *SqliteSuite) TestModifyIncrement() {

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))
}

0 comments on commit bac8738

Please sign in to comment.