Skip to content

Commit

Permalink
Rename test files
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Mar 8, 2016
1 parent 9d57c6b commit d08894c
Show file tree
Hide file tree
Showing 15 changed files with 1,004 additions and 1,025 deletions.
7 changes: 3 additions & 4 deletions callback_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ func queryCallback(scope *Scope) {
defer scope.trace(NowFunc())

var (
isSlice bool
isPtr bool
results = scope.IndirectValue()
resultType reflect.Type
isSlice, isPtr bool
resultType reflect.Type
results = scope.IndirectValue()
)

if orderBy, ok := scope.Get("gorm:order_by_primary_key"); ok {
Expand Down
11 changes: 6 additions & 5 deletions callback_save.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ func saveBeforeAssociationsCallback(scope *Scope) {
for _, field := range scope.Fields() {
if scope.changeableField(field) && !field.IsBlank && !field.IsIgnored {
if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
value := field.Field
scope.Err(scope.NewDB().Save(value.Addr().Interface()).Error)
fieldValue := field.Field.Addr().Interface()
scope.Err(scope.NewDB().Save(fieldValue).Error)
if len(relationship.ForeignFieldNames) != 0 {
// set value's foreign key
for idx, fieldName := range relationship.ForeignFieldNames {
associationForeignName := relationship.AssociationForeignDBNames[idx]
if f, ok := scope.New(value.Addr().Interface()).FieldByName(associationForeignName); ok {
scope.Err(scope.SetColumn(fieldName, f.Field.Interface()))
if foreignField, ok := scope.New(fieldValue).FieldByName(associationForeignName); ok {
scope.Err(scope.SetColumn(fieldName, foreignField.Field.Interface()))
}
}
}
Expand Down Expand Up @@ -65,7 +66,7 @@ func saveAfterAssociationsCallback(scope *Scope) {
scope.Err(newDB.Save(elem).Error)

if joinTableHandler := relationship.JoinTableHandler; joinTableHandler != nil {
scope.Err(joinTableHandler.Add(joinTableHandler, scope.NewDB(), scope.Value, newScope.Value))
scope.Err(joinTableHandler.Add(joinTableHandler, newDB, scope.Value, newScope.Value))
}
}
default:
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestCreateWithNoStdPrimaryKeyAndDefaultValues(t *testing.T) {

// We must fetch the value again, to have the default fields updated
// (We can't do this in the update statements, since sql default can be expressions
// And be different from the fields' type (eg. a time.Time fiels has a default value of "now()"
// And be different from the fields' type (eg. a time.Time fields has a default value of "now()"
DB.Model(Animal{}).Where(&Animal{Counter: an.Counter}).First(&an)

if an.Name != "galeone" {
Expand Down
23 changes: 0 additions & 23 deletions ddl_errors_test.go

This file was deleted.

42 changes: 40 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"reflect"
"strings"
"time"
)

// DB contains information for current db connection
Expand Down Expand Up @@ -221,7 +222,7 @@ func (s *DB) Unscoped() *DB {
return s.clone().search.unscoped().db
}

// Attrs initalize struct with argument if record not found with `FirstOrInit` https://jinzhu.github.io/gorm/curd.html#firstorinit or `FirstOrCreate` https://jinzhu.github.io/gorm/curd.html#firstorcreate
// Attrs initialize struct with argument if record not found with `FirstOrInit` https://jinzhu.github.io/gorm/curd.html#firstorinit or `FirstOrCreate` https://jinzhu.github.io/gorm/curd.html#firstorcreate
func (s *DB) Attrs(attrs ...interface{}) *DB {
return s.clone().search.Attrs(attrs...).db
}
Expand Down Expand Up @@ -299,7 +300,7 @@ func (s *DB) Related(value interface{}, foreignKeys ...string) *DB {
return s.clone().NewScope(s.Value).related(value, foreignKeys...).db
}

// FirstOrInit find first matched record or initalize a new one with given conditions (only works with struct, map conditions)
// FirstOrInit find first matched record or initialize a new one with given conditions (only works with struct, map conditions)
// https://jinzhu.github.io/gorm/curd.html#firstorinit
func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB {
c := s.clone()
Expand Down Expand Up @@ -660,3 +661,40 @@ func (s *DB) GetErrors() (errors []error) {
}
return
}

////////////////////////////////////////////////////////////////////////////////
// Private Methods For *gorm.DB
////////////////////////////////////////////////////////////////////////////////

func (s *DB) clone() *DB {
db := DB{db: s.db, parent: s.parent, logger: s.logger, logMode: s.logMode, values: map[string]interface{}{}, Value: s.Value, Error: s.Error}

for key, value := range s.values {
db.values[key] = value
}

if s.search == nil {
db.search = &search{limit: -1, offset: -1}
} else {
db.search = s.search.clone()
}

db.search.db = &db
return &db
}

func (s *DB) print(v ...interface{}) {
s.logger.(logger).Print(v...)
}

func (s *DB) log(v ...interface{}) {
if s != nil && s.logMode == 2 {
s.print(append([]interface{}{"log", fileWithLineNum()}, v...)...)
}
}

func (s *DB) slog(sql string, t time.Time, vars ...interface{}) {
if s.logMode == 2 {
s.print("sql", fileWithLineNum(), NowFunc().Sub(t), sql, vars)
}
}
36 changes: 0 additions & 36 deletions main_private.go

This file was deleted.

18 changes: 18 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,24 @@ func TestOpenExistingDB(t *testing.T) {
}
}

func TestDdlErrors(t *testing.T) {
var err error

if err = DB.Close(); err != nil {
t.Errorf("Closing DDL test db connection err=%s", err)
}
defer func() {
// Reopen DB connection.
if DB, err = OpenTestConnection(); err != nil {
t.Fatalf("Failed re-opening db connection: %s", err)
}
}()

if err := DB.Find(&User{}).Error; err == nil {
t.Errorf("Expected operation on closed db to produce an error, but err was nil")
}
}

func BenchmarkGorm(b *testing.B) {
b.N = 2000
for x := 0; x < b.N; x++ {
Expand Down
Loading

0 comments on commit d08894c

Please sign in to comment.