Best Is code snippet using is.Fail
orm_test.go
Source:orm_test.go
...137 }138 return fmt.Sprintf("%s:%d: \n%s", fn, line, strings.Join(codes, "\n"))139}140141func throwFail(t *testing.T, err error, args ...interface{}) {142 if err != nil {143 con := fmt.Sprintf("\t\nError: %s\n%s\n", err.Error(), getCaller(2))144 if len(args) > 0 {145 parts := make([]string, 0, len(args))146 for _, arg := range args {147 parts = append(parts, fmt.Sprintf("%v", arg))148 }149 con += " " + strings.Join(parts, ", ")150 }151 t.Error(con)152 t.Fail()153 }154}155156func throwFailNow(t *testing.T, err error, args ...interface{}) {157 if err != nil {158 con := fmt.Sprintf("\t\nError: %s\n%s\n", err.Error(), getCaller(2))159 if len(args) > 0 {160 parts := make([]string, 0, len(args))161 for _, arg := range args {162 parts = append(parts, fmt.Sprintf("%v", arg))163 }164 con += " " + strings.Join(parts, ", ")165 }166 t.Error(con)167 t.FailNow()168 }169}170171func TestGetDB(t *testing.T) {172 if db, err := GetDB(); err != nil {173 throwFailNow(t, err)174 } else {175 err = db.Ping()176 throwFailNow(t, err)177 }178}179180func TestSyncDb(t *testing.T) {181 RegisterModel(new(Data), new(DataNull), new(DataCustom))182 RegisterModel(new(User))183 RegisterModel(new(Profile))184 RegisterModel(new(Post))185 RegisterModel(new(Tag))186 RegisterModel(new(Comment))187 RegisterModel(new(UserBig))188 RegisterModel(new(PostTags))189 RegisterModel(new(Group))190 RegisterModel(new(Permission))191 RegisterModel(new(GroupPermissions))192 RegisterModel(new(InLine))193 RegisterModel(new(InLineOneToOne))194 RegisterModel(new(IntegerPk))195 RegisterModel(new(UintPk))196 RegisterModel(new(PtrPk))197198 err := RunSyncdb("default", true, Debug)199 throwFail(t, err)200201 modelCache.clean()202}203204func TestRegisterModels(t *testing.T) {205 RegisterModel(new(Data), new(DataNull), new(DataCustom))206 RegisterModel(new(User))207 RegisterModel(new(Profile))208 RegisterModel(new(Post))209 RegisterModel(new(Tag))210 RegisterModel(new(Comment))211 RegisterModel(new(UserBig))212 RegisterModel(new(PostTags))213 RegisterModel(new(Group))214 RegisterModel(new(Permission))215 RegisterModel(new(GroupPermissions))216 RegisterModel(new(InLine))217 RegisterModel(new(InLineOneToOne))218 RegisterModel(new(IntegerPk))219 RegisterModel(new(UintPk))220 RegisterModel(new(PtrPk))221222 BootStrap()223224 dORM = NewOrm()225 dDbBaser = getDbAlias("default").DbBaser226}227228func TestModelSyntax(t *testing.T) {229 user := &User{}230 ind := reflect.ValueOf(user).Elem()231 fn := getFullName(ind.Type())232 mi, ok := modelCache.getByFullName(fn)233 throwFail(t, AssertIs(ok, true))234235 mi, ok = modelCache.get("user")236 throwFail(t, AssertIs(ok, true))237 if ok {238 throwFail(t, AssertIs(mi.fields.GetByName("ShouldSkip") == nil, true))239 }240}241242var DataValues = map[string]interface{}{243 "Boolean": true,244 "Char": "char",245 "Text": "text",246 "JSON": `{"name":"json"}`,247 "Jsonb": `{"name": "jsonb"}`,248 "Time": time.Now(),249 "Date": time.Now(),250 "DateTime": time.Now(),251 "Byte": byte(1<<8 - 1),252 "Rune": rune(1<<31 - 1),253 "Int": int(1<<31 - 1),254 "Int8": int8(1<<7 - 1),255 "Int16": int16(1<<15 - 1),256 "Int32": int32(1<<31 - 1),257 "Int64": int64(1<<63 - 1),258 "Uint": uint(1<<32 - 1),259 "Uint8": uint8(1<<8 - 1),260 "Uint16": uint16(1<<16 - 1),261 "Uint32": uint32(1<<32 - 1),262 "Uint64": uint64(1<<63 - 1), // uint64 values with high bit set are not supported263 "Float32": float32(100.1234),264 "Float64": float64(100.1234),265 "Decimal": float64(100.1234),266}267268func TestDataTypes(t *testing.T) {269 d := Data{}270 ind := reflect.Indirect(reflect.ValueOf(&d))271272 for name, value := range DataValues {273 if name == "JSON" {274 continue275 }276 e := ind.FieldByName(name)277 e.Set(reflect.ValueOf(value))278 }279 id, err := dORM.Insert(&d)280 throwFail(t, err)281 throwFail(t, AssertIs(id, 1))282283 d = Data{ID: 1}284 err = dORM.Read(&d)285 throwFail(t, err)286287 ind = reflect.Indirect(reflect.ValueOf(&d))288289 for name, value := range DataValues {290 e := ind.FieldByName(name)291 vu := e.Interface()292 switch name {293 case "Date":294 vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDate)295 value = value.(time.Time).In(DefaultTimeLoc).Format(testDate)296 case "DateTime":297 vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDateTime)298 value = value.(time.Time).In(DefaultTimeLoc).Format(testDateTime)299 case "Time":300 vu = vu.(time.Time).In(DefaultTimeLoc).Format(testTime)301 value = value.(time.Time).In(DefaultTimeLoc).Format(testTime)302 }303 throwFail(t, AssertIs(vu == value, true), value, vu)304 }305}306307func TestNullDataTypes(t *testing.T) {308 d := DataNull{}309310 if IsPostgres {311 // can removed when this fixed312 // https://github.com/lib/pq/pull/125313 d.DateTime = time.Now()314 }315316 id, err := dORM.Insert(&d)317 throwFail(t, err)318 throwFail(t, AssertIs(id, 1))319320 data := `{"ok":1,"data":{"arr":[1,2],"msg":"gopher"}}`321 d = DataNull{ID: 1, JSON: data}322 num, err := dORM.Update(&d)323 throwFail(t, err)324 throwFail(t, AssertIs(num, 1))325326 d = DataNull{ID: 1}327 err = dORM.Read(&d)328 throwFail(t, err)329330 throwFail(t, AssertIs(d.JSON, data))331332 throwFail(t, AssertIs(d.NullBool.Valid, false))333 throwFail(t, AssertIs(d.NullString.Valid, false))334 throwFail(t, AssertIs(d.NullInt64.Valid, false))335 throwFail(t, AssertIs(d.NullFloat64.Valid, false))336337 throwFail(t, AssertIs(d.BooleanPtr, nil))338 throwFail(t, AssertIs(d.CharPtr, nil))339 throwFail(t, AssertIs(d.TextPtr, nil))340 throwFail(t, AssertIs(d.BytePtr, nil))341 throwFail(t, AssertIs(d.RunePtr, nil))342 throwFail(t, AssertIs(d.IntPtr, nil))343 throwFail(t, AssertIs(d.Int8Ptr, nil))344 throwFail(t, AssertIs(d.Int16Ptr, nil))345 throwFail(t, AssertIs(d.Int32Ptr, nil))346 throwFail(t, AssertIs(d.Int64Ptr, nil))347 throwFail(t, AssertIs(d.UintPtr, nil))348 throwFail(t, AssertIs(d.Uint8Ptr, nil))349 throwFail(t, AssertIs(d.Uint16Ptr, nil))350 throwFail(t, AssertIs(d.Uint32Ptr, nil))351 throwFail(t, AssertIs(d.Uint64Ptr, nil))352 throwFail(t, AssertIs(d.Float32Ptr, nil))353 throwFail(t, AssertIs(d.Float64Ptr, nil))354 throwFail(t, AssertIs(d.DecimalPtr, nil))355 throwFail(t, AssertIs(d.TimePtr, nil))356 throwFail(t, AssertIs(d.DatePtr, nil))357 throwFail(t, AssertIs(d.DateTimePtr, nil))358359 _, err = dORM.Raw(`INSERT INTO data_null (boolean) VALUES (?)`, nil).Exec()360 throwFail(t, err)361362 d = DataNull{ID: 2}363 err = dORM.Read(&d)364 throwFail(t, err)365366 booleanPtr := true367 charPtr := string("test")368 textPtr := string("test")369 bytePtr := byte('t')370 runePtr := rune('t')371 intPtr := int(42)372 int8Ptr := int8(42)373 int16Ptr := int16(42)374 int32Ptr := int32(42)375 int64Ptr := int64(42)376 uintPtr := uint(42)377 uint8Ptr := uint8(42)378 uint16Ptr := uint16(42)379 uint32Ptr := uint32(42)380 uint64Ptr := uint64(42)381 float32Ptr := float32(42.0)382 float64Ptr := float64(42.0)383 decimalPtr := float64(42.0)384 timePtr := time.Now()385 datePtr := time.Now()386 dateTimePtr := time.Now()387388 d = DataNull{389 DateTime: time.Now(),390 NullString: sql.NullString{String: "test", Valid: true},391 NullBool: sql.NullBool{Bool: true, Valid: true},392 NullInt64: sql.NullInt64{Int64: 42, Valid: true},393 NullFloat64: sql.NullFloat64{Float64: 42.42, Valid: true},394 BooleanPtr: &booleanPtr,395 CharPtr: &charPtr,396 TextPtr: &textPtr,397 BytePtr: &bytePtr,398 RunePtr: &runePtr,399 IntPtr: &intPtr,400 Int8Ptr: &int8Ptr,401 Int16Ptr: &int16Ptr,402 Int32Ptr: &int32Ptr,403 Int64Ptr: &int64Ptr,404 UintPtr: &uintPtr,405 Uint8Ptr: &uint8Ptr,406 Uint16Ptr: &uint16Ptr,407 Uint32Ptr: &uint32Ptr,408 Uint64Ptr: &uint64Ptr,409 Float32Ptr: &float32Ptr,410 Float64Ptr: &float64Ptr,411 DecimalPtr: &decimalPtr,412 TimePtr: &timePtr,413 DatePtr: &datePtr,414 DateTimePtr: &dateTimePtr,415 }416417 id, err = dORM.Insert(&d)418 throwFail(t, err)419 throwFail(t, AssertIs(id, 3))420421 d = DataNull{ID: 3}422 err = dORM.Read(&d)423 throwFail(t, err)424425 throwFail(t, AssertIs(d.NullBool.Valid, true))426 throwFail(t, AssertIs(d.NullBool.Bool, true))427428 throwFail(t, AssertIs(d.NullString.Valid, true))429 throwFail(t, AssertIs(d.NullString.String, "test"))430431 throwFail(t, AssertIs(d.NullInt64.Valid, true))432 throwFail(t, AssertIs(d.NullInt64.Int64, 42))433434 throwFail(t, AssertIs(d.NullFloat64.Valid, true))435 throwFail(t, AssertIs(d.NullFloat64.Float64, 42.42))436437 throwFail(t, AssertIs(*d.BooleanPtr, booleanPtr))438 throwFail(t, AssertIs(*d.CharPtr, charPtr))439 throwFail(t, AssertIs(*d.TextPtr, textPtr))440 throwFail(t, AssertIs(*d.BytePtr, bytePtr))441 throwFail(t, AssertIs(*d.RunePtr, runePtr))442 throwFail(t, AssertIs(*d.IntPtr, intPtr))443 throwFail(t, AssertIs(*d.Int8Ptr, int8Ptr))444 throwFail(t, AssertIs(*d.Int16Ptr, int16Ptr))445 throwFail(t, AssertIs(*d.Int32Ptr, int32Ptr))446 throwFail(t, AssertIs(*d.Int64Ptr, int64Ptr))447 throwFail(t, AssertIs(*d.UintPtr, uintPtr))448 throwFail(t, AssertIs(*d.Uint8Ptr, uint8Ptr))449 throwFail(t, AssertIs(*d.Uint16Ptr, uint16Ptr))450 throwFail(t, AssertIs(*d.Uint32Ptr, uint32Ptr))451 throwFail(t, AssertIs(*d.Uint64Ptr, uint64Ptr))452 throwFail(t, AssertIs(*d.Float32Ptr, float32Ptr))453 throwFail(t, AssertIs(*d.Float64Ptr, float64Ptr))454 throwFail(t, AssertIs(*d.DecimalPtr, decimalPtr))455 throwFail(t, AssertIs((*d.TimePtr).Format(testTime), timePtr.Format(testTime)))456 throwFail(t, AssertIs((*d.DatePtr).Format(testDate), datePtr.Format(testDate)))457 throwFail(t, AssertIs((*d.DateTimePtr).Format(testDateTime), dateTimePtr.Format(testDateTime)))458}459460func TestDataCustomTypes(t *testing.T) {461 d := DataCustom{}462 ind := reflect.Indirect(reflect.ValueOf(&d))463464 for name, value := range DataValues {465 e := ind.FieldByName(name)466 if !e.IsValid() {467 continue468 }469 e.Set(reflect.ValueOf(value).Convert(e.Type()))470 }471472 id, err := dORM.Insert(&d)473 throwFail(t, err)474 throwFail(t, AssertIs(id, 1))475476 d = DataCustom{ID: 1}477 err = dORM.Read(&d)478 throwFail(t, err)479480 ind = reflect.Indirect(reflect.ValueOf(&d))481482 for name, value := range DataValues {483 e := ind.FieldByName(name)484 if !e.IsValid() {485 continue486 }487 vu := e.Interface()488 value = reflect.ValueOf(value).Convert(e.Type()).Interface()489 throwFail(t, AssertIs(vu == value, true), value, vu)490 }491}492493func TestCRUD(t *testing.T) {494 profile := NewProfile()495 profile.Age = 30496 profile.Money = 1234.12497 id, err := dORM.Insert(profile)498 throwFail(t, err)499 throwFail(t, AssertIs(id, 1))500501 user := NewUser()502 user.UserName = "slene"503 user.Email = "vslene@gmail.com"504 user.Password = "pass"505 user.Status = 3506 user.IsStaff = true507 user.IsActive = true508509 id, err = dORM.Insert(user)510 throwFail(t, err)511 throwFail(t, AssertIs(id, 1))512513 u := &User{ID: user.ID}514 err = dORM.Read(u)515 throwFail(t, err)516517 throwFail(t, AssertIs(u.UserName, "slene"))518 throwFail(t, AssertIs(u.Email, "vslene@gmail.com"))519 throwFail(t, AssertIs(u.Password, "pass"))520 throwFail(t, AssertIs(u.Status, 3))521 throwFail(t, AssertIs(u.IsStaff, true))522 throwFail(t, AssertIs(u.IsActive, true))523 throwFail(t, AssertIs(u.Created.In(DefaultTimeLoc), user.Created.In(DefaultTimeLoc), testDate))524 throwFail(t, AssertIs(u.Updated.In(DefaultTimeLoc), user.Updated.In(DefaultTimeLoc), testDateTime))525526 user.UserName = "astaxie"527 user.Profile = profile528 num, err := dORM.Update(user)529 throwFail(t, err)530 throwFail(t, AssertIs(num, 1))531532 u = &User{ID: user.ID}533 err = dORM.Read(u)534 throwFailNow(t, err)535 throwFail(t, AssertIs(u.UserName, "astaxie"))536 throwFail(t, AssertIs(u.Profile.ID, profile.ID))537538 u = &User{UserName: "astaxie", Password: "pass"}539 err = dORM.Read(u, "UserName")540 throwFailNow(t, err)541 throwFailNow(t, AssertIs(id, 1))542543 u.UserName = "QQ"544 u.Password = "111"545 num, err = dORM.Update(u, "UserName")546 throwFail(t, err)547 throwFail(t, AssertIs(num, 1))548549 u = &User{ID: user.ID}550 err = dORM.Read(u)551 throwFailNow(t, err)552 throwFail(t, AssertIs(u.UserName, "QQ"))553 throwFail(t, AssertIs(u.Password, "pass"))554555 num, err = dORM.Delete(profile)556 throwFail(t, err)557 throwFail(t, AssertIs(num, 1))558559 u = &User{ID: user.ID}560 err = dORM.Read(u)561 throwFail(t, err)562 throwFail(t, AssertIs(true, u.Profile == nil))563564 num, err = dORM.Delete(user)565 throwFail(t, err)566 throwFail(t, AssertIs(num, 1))567568 u = &User{ID: 100}569 err = dORM.Read(u)570 throwFail(t, AssertIs(err, ErrNoRows))571572 ub := UserBig{}573 ub.Name = "name"574 id, err = dORM.Insert(&ub)575 throwFail(t, err)576 throwFail(t, AssertIs(id, 1))577578 ub = UserBig{ID: 1}579 err = dORM.Read(&ub)580 throwFail(t, err)581 throwFail(t, AssertIs(ub.Name, "name"))582583 num, err = dORM.Delete(&ub, "name")584 throwFail(t, err)585 throwFail(t, AssertIs(num, 1))586}587588func TestInsertTestData(t *testing.T) {589 var users []*User590591 profile := NewProfile()592 profile.Age = 28593 profile.Money = 1234.12594595 id, err := dORM.Insert(profile)596 throwFail(t, err)597 throwFail(t, AssertIs(id, 2))598599 user := NewUser()600 user.UserName = "slene"601 user.Email = "vslene@gmail.com"602 user.Password = "pass"603 user.Status = 1604 user.IsStaff = false605 user.IsActive = true606 user.Profile = profile607608 users = append(users, user)609610 id, err = dORM.Insert(user)611 throwFail(t, err)612 throwFail(t, AssertIs(id, 2))613614 profile = NewProfile()615 profile.Age = 30616 profile.Money = 4321.09617618 id, err = dORM.Insert(profile)619 throwFail(t, err)620 throwFail(t, AssertIs(id, 3))621622 user = NewUser()623 user.UserName = "astaxie"624 user.Email = "astaxie@gmail.com"625 user.Password = "password"626 user.Status = 2627 user.IsStaff = true628 user.IsActive = false629 user.Profile = profile630631 users = append(users, user)632633 id, err = dORM.Insert(user)634 throwFail(t, err)635 throwFail(t, AssertIs(id, 3))636637 user = NewUser()638 user.UserName = "nobody"639 user.Email = "nobody@gmail.com"640 user.Password = "nobody"641 user.Status = 3642 user.IsStaff = false643 user.IsActive = false644645 users = append(users, user)646647 id, err = dORM.Insert(user)648 throwFail(t, err)649 throwFail(t, AssertIs(id, 4))650651 tags := []*Tag{652 {Name: "golang", BestPost: &Post{ID: 2}},653 {Name: "example"},654 {Name: "format"},655 {Name: "c++"},656 }657658 posts := []*Post{659 {User: users[0], Tags: []*Tag{tags[0]}, Title: "Introduction", Content: `Go is a new language. Although it borrows ideas from existing languages, it has unusual properties that make effective Go programs different in character from programs written in its relatives. A straightforward translation of a C++ or Java program into Go is unlikely to produce a satisfactory resultâJava programs are written in Java, not Go. On the other hand, thinking about the problem from a Go perspective could produce a successful but quite different program. In other words, to write Go well, it's important to understand its properties and idioms. It's also important to know the established conventions for programming in Go, such as naming, formatting, program construction, and so on, so that programs you write will be easy for other Go programmers to understand.660This document gives tips for writing clear, idiomatic Go code. It augments the language specification, the Tour of Go, and How to Write Go Code, all of which you should read first.`},661 {User: users[1], Tags: []*Tag{tags[0], tags[1]}, Title: "Examples", Content: `The Go package sources are intended to serve not only as the core library but also as examples of how to use the language. Moreover, many of the packages contain working, self-contained executable examples you can run directly from the golang.org web site, such as this one (click on the word "Example" to open it up). If you have a question about how to approach a problem or how something might be implemented, the documentation, code and examples in the library can provide answers, ideas and background.`},662 {User: users[1], Tags: []*Tag{tags[0], tags[2]}, Title: "Formatting", Content: `Formatting issues are the most contentious but the least consequential. People can adapt to different formatting styles but it's better if they don't have to, and less time is devoted to the topic if everyone adheres to the same style. The problem is how to approach this Utopia without a long prescriptive style guide.663With Go we take an unusual approach and let the machine take care of most formatting issues. The gofmt program (also available as go fmt, which operates at the package level rather than source file level) reads a Go program and emits the source in a standard style of indentation and vertical alignment, retaining and if necessary reformatting comments. If you want to know how to handle some new layout situation, run gofmt; if the answer doesn't seem right, rearrange your program (or file a bug about gofmt), don't work around it.`},664 {User: users[2], Tags: []*Tag{tags[3]}, Title: "Commentary", Content: `Go provides C-style /* */ block comments and C++-style // line comments. Line comments are the norm; block comments appear mostly as package comments, but are useful within an expression or to disable large swaths of code.665The programâand web serverâgodoc processes Go source files to extract documentation about the contents of the package. Comments that appear before top-level declarations, with no intervening newlines, are extracted along with the declaration to serve as explanatory text for the item. The nature and style of these comments determines the quality of the documentation godoc produces.`},666 }667668 comments := []*Comment{669 {Post: posts[0], Content: "a comment"},670 {Post: posts[1], Content: "yes"},671 {Post: posts[1]},672 {Post: posts[1]},673 {Post: posts[2]},674 {Post: posts[2]},675 }676677 for _, tag := range tags {678 id, err := dORM.Insert(tag)679 throwFail(t, err)680 throwFail(t, AssertIs(id > 0, true))681 }682683 for _, post := range posts {684 id, err := dORM.Insert(post)685 throwFail(t, err)686 throwFail(t, AssertIs(id > 0, true))687688 num := len(post.Tags)689 if num > 0 {690 nums, err := dORM.QueryM2M(post, "tags").Add(post.Tags)691 throwFailNow(t, err)692 throwFailNow(t, AssertIs(nums, num))693 }694 }695696 for _, comment := range comments {697 id, err := dORM.Insert(comment)698 throwFail(t, err)699 throwFail(t, AssertIs(id > 0, true))700 }701702 permissions := []*Permission{703 {Name: "writePosts"},704 {Name: "readComments"},705 {Name: "readPosts"},706 }707708 groups := []*Group{709 {710 Name: "admins",711 Permissions: []*Permission{permissions[0], permissions[1], permissions[2]},712 },713 {714 Name: "users",715 Permissions: []*Permission{permissions[1], permissions[2]},716 },717 }718719 for _, permission := range permissions {720 id, err := dORM.Insert(permission)721 throwFail(t, err)722 throwFail(t, AssertIs(id > 0, true))723 }724725 for _, group := range groups {726 _, err := dORM.Insert(group)727 throwFail(t, err)728 throwFail(t, AssertIs(id > 0, true))729730 num := len(group.Permissions)731 if num > 0 {732 nums, err := dORM.QueryM2M(group, "permissions").Add(group.Permissions)733 throwFailNow(t, err)734 throwFailNow(t, AssertIs(nums, num))735 }736 }737738}739740func TestCustomField(t *testing.T) {741 user := User{ID: 2}742 err := dORM.Read(&user)743 throwFailNow(t, err)744745 user.Langs = append(user.Langs, "zh-CN", "en-US")746 user.Extra.Name = "beego"747 user.Extra.Data = "orm"748 _, err = dORM.Update(&user, "Langs", "Extra")749 throwFailNow(t, err)750751 user = User{ID: 2}752 err = dORM.Read(&user)753 throwFailNow(t, err)754 throwFailNow(t, AssertIs(len(user.Langs), 2))755 throwFailNow(t, AssertIs(user.Langs[0], "zh-CN"))756 throwFailNow(t, AssertIs(user.Langs[1], "en-US"))757758 throwFailNow(t, AssertIs(user.Extra.Name, "beego"))759 throwFailNow(t, AssertIs(user.Extra.Data, "orm"))760}761762func TestExpr(t *testing.T) {763 user := &User{}764 qs := dORM.QueryTable(user)765 qs = dORM.QueryTable((*User)(nil))766 qs = dORM.QueryTable("User")767 qs = dORM.QueryTable("user")768 num, err := qs.Filter("UserName", "slene").Filter("user_name", "slene").Filter("profile__Age", 28).Count()769 throwFail(t, err)770 throwFail(t, AssertIs(num, 1))771772 num, err = qs.Filter("created", time.Now()).Count()773 throwFail(t, err)774 throwFail(t, AssertIs(num, 3))775776 // num, err = qs.Filter("created", time.Now().Format(format_Date)).Count()777 // throwFail(t, err)778 // throwFail(t, AssertIs(num, 3))779}780781func TestOperators(t *testing.T) {782 qs := dORM.QueryTable("user")783 num, err := qs.Filter("user_name", "slene").Count()784 throwFail(t, err)785 throwFail(t, AssertIs(num, 1))786787 num, err = qs.Filter("user_name__exact", String("slene")).Count()788 throwFail(t, err)789 throwFail(t, AssertIs(num, 1))790791 num, err = qs.Filter("user_name__exact", "slene").Count()792 throwFail(t, err)793 throwFail(t, AssertIs(num, 1))794795 num, err = qs.Filter("user_name__iexact", "Slene").Count()796 throwFail(t, err)797 throwFail(t, AssertIs(num, 1))798799 num, err = qs.Filter("user_name__contains", "e").Count()800 throwFail(t, err)801 throwFail(t, AssertIs(num, 2))802803 var shouldNum int804805 if IsSqlite || IsTidb {806 shouldNum = 2807 } else {808 shouldNum = 0809 }810811 num, err = qs.Filter("user_name__contains", "E").Count()812 throwFail(t, err)813 throwFail(t, AssertIs(num, shouldNum))814815 num, err = qs.Filter("user_name__icontains", "E").Count()816 throwFail(t, err)817 throwFail(t, AssertIs(num, 2))818819 num, err = qs.Filter("user_name__icontains", "E").Count()820 throwFail(t, err)821 throwFail(t, AssertIs(num, 2))822823 num, err = qs.Filter("status__gt", 1).Count()824 throwFail(t, err)825 throwFail(t, AssertIs(num, 2))826827 num, err = qs.Filter("status__gte", 1).Count()828 throwFail(t, err)829 throwFail(t, AssertIs(num, 3))830831 num, err = qs.Filter("status__lt", Uint(3)).Count()832 throwFail(t, err)833 throwFail(t, AssertIs(num, 2))834835 num, err = qs.Filter("status__lte", Int(3)).Count()836 throwFail(t, err)837 throwFail(t, AssertIs(num, 3))838839 num, err = qs.Filter("user_name__startswith", "s").Count()840 throwFail(t, err)841 throwFail(t, AssertIs(num, 1))842843 if IsSqlite || IsTidb {844 shouldNum = 1845 } else {846 shouldNum = 0847 }848849 num, err = qs.Filter("user_name__startswith", "S").Count()850 throwFail(t, err)851 throwFail(t, AssertIs(num, shouldNum))852853 num, err = qs.Filter("user_name__istartswith", "S").Count()854 throwFail(t, err)855 throwFail(t, AssertIs(num, 1))856857 num, err = qs.Filter("user_name__endswith", "e").Count()858 throwFail(t, err)859 throwFail(t, AssertIs(num, 2))860861 if IsSqlite || IsTidb {862 shouldNum = 2863 } else {864 shouldNum = 0865 }866867 num, err = qs.Filter("user_name__endswith", "E").Count()868 throwFail(t, err)869 throwFail(t, AssertIs(num, shouldNum))870871 num, err = qs.Filter("user_name__iendswith", "E").Count()872 throwFail(t, err)873 throwFail(t, AssertIs(num, 2))874875 num, err = qs.Filter("profile__isnull", true).Count()876 throwFail(t, err)877 throwFail(t, AssertIs(num, 1))878879 num, err = qs.Filter("status__in", 1, 2).Count()880 throwFail(t, err)881 throwFail(t, AssertIs(num, 2))882883 num, err = qs.Filter("status__in", []int{1, 2}).Count()884 throwFail(t, err)885 throwFail(t, AssertIs(num, 2))886887 n1, n2 := 1, 2888 num, err = qs.Filter("status__in", []*int{&n1}, &n2).Count()889 throwFail(t, err)890 throwFail(t, AssertIs(num, 2))891892 num, err = qs.Filter("id__between", 2, 3).Count()893 throwFail(t, err)894 throwFail(t, AssertIs(num, 2))895896 num, err = qs.Filter("id__between", []int{2, 3}).Count()897 throwFail(t, err)898 throwFail(t, AssertIs(num, 2))899}900901func TestSetCond(t *testing.T) {902 cond := NewCondition()903 cond1 := cond.And("profile__isnull", false).AndNot("status__in", 1).Or("profile__age__gt", 2000)904905 qs := dORM.QueryTable("user")906 num, err := qs.SetCond(cond1).Count()907 throwFail(t, err)908 throwFail(t, AssertIs(num, 1))909910 cond2 := cond.AndCond(cond1).OrCond(cond.And("user_name", "slene"))911 num, err = qs.SetCond(cond2).Count()912 throwFail(t, err)913 throwFail(t, AssertIs(num, 2))914915 cond3 := cond.AndNotCond(cond.And("status__in", 1))916 num, err = qs.SetCond(cond3).Count()917 throwFail(t, err)918 throwFail(t, AssertIs(num, 2))919920 cond4 := cond.And("user_name", "slene").OrNotCond(cond.And("user_name", "slene"))921 num, err = qs.SetCond(cond4).Count()922 throwFail(t, err)923 throwFail(t, AssertIs(num, 3))924}925926func TestLimit(t *testing.T) {927 var posts []*Post928 qs := dORM.QueryTable("post")929 num, err := qs.Limit(1).All(&posts)930 throwFail(t, err)931 throwFail(t, AssertIs(num, 1))932933 num, err = qs.Limit(-1).All(&posts)934 throwFail(t, err)935 throwFail(t, AssertIs(num, 4))936937 num, err = qs.Limit(-1, 2).All(&posts)938 throwFail(t, err)939 throwFail(t, AssertIs(num, 2))940941 num, err = qs.Limit(0, 2).All(&posts)942 throwFail(t, err)943 throwFail(t, AssertIs(num, 2))944}945946func TestOffset(t *testing.T) {947 var posts []*Post948 qs := dORM.QueryTable("post")949 num, err := qs.Limit(1).Offset(2).All(&posts)950 throwFail(t, err)951 throwFail(t, AssertIs(num, 1))952953 num, err = qs.Offset(2).All(&posts)954 throwFail(t, err)955 throwFail(t, AssertIs(num, 2))956}957958func TestOrderBy(t *testing.T) {959 qs := dORM.QueryTable("user")960 num, err := qs.OrderBy("-status").Filter("user_name", "nobody").Count()961 throwFail(t, err)962 throwFail(t, AssertIs(num, 1))963964 num, err = qs.OrderBy("status").Filter("user_name", "slene").Count()965 throwFail(t, err)966 throwFail(t, AssertIs(num, 1))967968 num, err = qs.OrderBy("-profile__age").Filter("user_name", "astaxie").Count()969 throwFail(t, err)970 throwFail(t, AssertIs(num, 1))971}972973func TestAll(t *testing.T) {974 var users []*User975 qs := dORM.QueryTable("user")976 num, err := qs.OrderBy("Id").All(&users)977 throwFail(t, err)978 throwFailNow(t, AssertIs(num, 3))979980 throwFail(t, AssertIs(users[0].UserName, "slene"))981 throwFail(t, AssertIs(users[1].UserName, "astaxie"))982 throwFail(t, AssertIs(users[2].UserName, "nobody"))983984 var users2 []User985 qs = dORM.QueryTable("user")986 num, err = qs.OrderBy("Id").All(&users2)987 throwFail(t, err)988 throwFailNow(t, AssertIs(num, 3))989990 throwFailNow(t, AssertIs(users2[0].UserName, "slene"))991 throwFailNow(t, AssertIs(users2[1].UserName, "astaxie"))992 throwFailNow(t, AssertIs(users2[2].UserName, "nobody"))993994 qs = dORM.QueryTable("user")995 num, err = qs.OrderBy("Id").RelatedSel().All(&users2, "UserName")996 throwFail(t, err)997 throwFailNow(t, AssertIs(num, 3))998 throwFailNow(t, AssertIs(len(users2), 3))999 throwFailNow(t, AssertIs(users2[0].UserName, "slene"))1000 throwFailNow(t, AssertIs(users2[1].UserName, "astaxie"))1001 throwFailNow(t, AssertIs(users2[2].UserName, "nobody"))1002 throwFailNow(t, AssertIs(users2[0].ID, 0))1003 throwFailNow(t, AssertIs(users2[1].ID, 0))1004 throwFailNow(t, AssertIs(users2[2].ID, 0))1005 throwFailNow(t, AssertIs(users2[0].Profile == nil, false))1006 throwFailNow(t, AssertIs(users2[1].Profile == nil, false))1007 throwFailNow(t, AssertIs(users2[2].Profile == nil, true))10081009 qs = dORM.QueryTable("user")1010 num, err = qs.Filter("user_name", "nothing").All(&users)1011 throwFailNow(t, err)1012 throwFailNow(t, AssertIs(num, 0))10131014 var users3 []*User1015 qs = dORM.QueryTable("user")1016 num, err = qs.Filter("user_name", "nothing").All(&users3)1017 throwFailNow(t, AssertIs(users3 == nil, false))1018}10191020func TestOne(t *testing.T) {1021 var user User1022 qs := dORM.QueryTable("user")1023 err := qs.One(&user)1024 throwFail(t, err)10251026 user = User{}1027 err = qs.OrderBy("Id").Limit(1).One(&user)1028 throwFailNow(t, err)1029 throwFail(t, AssertIs(user.UserName, "slene"))1030 throwFail(t, AssertNot(err, ErrMultiRows))10311032 user = User{}1033 err = qs.OrderBy("-Id").Limit(100).One(&user)1034 throwFailNow(t, err)1035 throwFail(t, AssertIs(user.UserName, "nobody"))1036 throwFail(t, AssertNot(err, ErrMultiRows))10371038 err = qs.Filter("user_name", "nothing").One(&user)1039 throwFail(t, AssertIs(err, ErrNoRows))10401041}10421043func TestValues(t *testing.T) {1044 var maps []Params1045 qs := dORM.QueryTable("user")10461047 num, err := qs.OrderBy("Id").Values(&maps)1048 throwFail(t, err)1049 throwFail(t, AssertIs(num, 3))1050 if num == 3 {1051 throwFail(t, AssertIs(maps[0]["UserName"], "slene"))1052 throwFail(t, AssertIs(maps[2]["Profile"], nil))1053 }10541055 num, err = qs.OrderBy("Id").Values(&maps, "UserName", "Profile__Age")1056 throwFail(t, err)1057 throwFail(t, AssertIs(num, 3))1058 if num == 3 {1059 throwFail(t, AssertIs(maps[0]["UserName"], "slene"))1060 throwFail(t, AssertIs(maps[0]["Profile__Age"], 28))1061 throwFail(t, AssertIs(maps[2]["Profile__Age"], nil))1062 }10631064 num, err = qs.Filter("UserName", "slene").Values(&maps)1065 throwFail(t, err)1066 throwFail(t, AssertIs(num, 1))1067}10681069func TestValuesList(t *testing.T) {1070 var list []ParamsList1071 qs := dORM.QueryTable("user")10721073 num, err := qs.OrderBy("Id").ValuesList(&list)1074 throwFail(t, err)1075 throwFail(t, AssertIs(num, 3))1076 if num == 3 {1077 throwFail(t, AssertIs(list[0][1], "slene"))1078 throwFail(t, AssertIs(list[2][9], nil))1079 }10801081 num, err = qs.OrderBy("Id").ValuesList(&list, "UserName", "Profile__Age")1082 throwFail(t, err)1083 throwFail(t, AssertIs(num, 3))1084 if num == 3 {1085 throwFail(t, AssertIs(list[0][0], "slene"))1086 throwFail(t, AssertIs(list[0][1], 28))1087 throwFail(t, AssertIs(list[2][1], nil))1088 }1089}10901091func TestValuesFlat(t *testing.T) {1092 var list ParamsList1093 qs := dORM.QueryTable("user")10941095 num, err := qs.OrderBy("id").ValuesFlat(&list, "UserName")1096 throwFail(t, err)1097 throwFail(t, AssertIs(num, 3))1098 if num == 3 {1099 throwFail(t, AssertIs(list[0], "slene"))1100 throwFail(t, AssertIs(list[1], "astaxie"))1101 throwFail(t, AssertIs(list[2], "nobody"))1102 }1103}11041105func TestRelatedSel(t *testing.T) {1106 if IsTidb {1107 // Skip it. TiDB does not support relation now.1108 return1109 }1110 qs := dORM.QueryTable("user")1111 num, err := qs.Filter("profile__age", 28).Count()1112 throwFail(t, err)1113 throwFail(t, AssertIs(num, 1))11141115 num, err = qs.Filter("profile__age__gt", 28).Count()1116 throwFail(t, err)1117 throwFail(t, AssertIs(num, 1))11181119 num, err = qs.Filter("profile__user__profile__age__gt", 28).Count()1120 throwFail(t, err)1121 throwFail(t, AssertIs(num, 1))11221123 var user User1124 err = qs.Filter("user_name", "slene").RelatedSel("profile").One(&user)1125 throwFail(t, err)1126 throwFail(t, AssertIs(num, 1))1127 throwFail(t, AssertNot(user.Profile, nil))1128 if user.Profile != nil {1129 throwFail(t, AssertIs(user.Profile.Age, 28))1130 }11311132 err = qs.Filter("user_name", "slene").RelatedSel().One(&user)1133 throwFail(t, err)1134 throwFail(t, AssertIs(num, 1))1135 throwFail(t, AssertNot(user.Profile, nil))1136 if user.Profile != nil {1137 throwFail(t, AssertIs(user.Profile.Age, 28))1138 }11391140 err = qs.Filter("user_name", "nobody").RelatedSel("profile").One(&user)1141 throwFail(t, AssertIs(num, 1))1142 throwFail(t, AssertIs(user.Profile, nil))11431144 qs = dORM.QueryTable("user_profile")1145 num, err = qs.Filter("user__username", "slene").Count()1146 throwFail(t, err)1147 throwFail(t, AssertIs(num, 1))11481149 var posts []*Post1150 qs = dORM.QueryTable("post")1151 num, err = qs.RelatedSel().All(&posts)1152 throwFail(t, err)1153 throwFailNow(t, AssertIs(num, 4))11541155 throwFailNow(t, AssertIs(posts[0].User.UserName, "slene"))1156 throwFailNow(t, AssertIs(posts[1].User.UserName, "astaxie"))1157 throwFailNow(t, AssertIs(posts[2].User.UserName, "astaxie"))1158 throwFailNow(t, AssertIs(posts[3].User.UserName, "nobody"))1159}11601161func TestReverseQuery(t *testing.T) {1162 var profile Profile1163 err := dORM.QueryTable("user_profile").Filter("User", 3).One(&profile)1164 throwFailNow(t, err)1165 throwFailNow(t, AssertIs(profile.Age, 30))11661167 profile = Profile{}1168 err = dORM.QueryTable("user_profile").Filter("User__UserName", "astaxie").One(&profile)1169 throwFailNow(t, err)1170 throwFailNow(t, AssertIs(profile.Age, 30))11711172 var user User1173 err = dORM.QueryTable("user").Filter("Posts__Title", "Examples").One(&user)1174 throwFailNow(t, err)1175 throwFailNow(t, AssertIs(user.UserName, "astaxie"))11761177 user = User{}1178 err = dORM.QueryTable("user").Filter("Posts__User__UserName", "astaxie").Limit(1).One(&user)1179 throwFailNow(t, err)1180 throwFailNow(t, AssertIs(user.UserName, "astaxie"))11811182 user = User{}1183 err = dORM.QueryTable("user").Filter("Posts__User__UserName", "astaxie").RelatedSel().Limit(1).One(&user)1184 throwFailNow(t, err)1185 throwFailNow(t, AssertIs(user.UserName, "astaxie"))1186 throwFailNow(t, AssertIs(user.Profile == nil, false))1187 throwFailNow(t, AssertIs(user.Profile.Age, 30))11881189 var posts []*Post1190 num, err := dORM.QueryTable("post").Filter("Tags__Tag__Name", "golang").All(&posts)1191 throwFailNow(t, err)1192 throwFailNow(t, AssertIs(num, 3))1193 throwFailNow(t, AssertIs(posts[0].Title, "Introduction"))11941195 posts = []*Post{}1196 num, err = dORM.QueryTable("post").Filter("Tags__Tag__Name", "golang").Filter("User__UserName", "slene").All(&posts)1197 throwFailNow(t, err)1198 throwFailNow(t, AssertIs(num, 1))1199 throwFailNow(t, AssertIs(posts[0].Title, "Introduction"))12001201 posts = []*Post{}1202 num, err = dORM.QueryTable("post").Filter("Tags__Tag__Name", "golang").1203 Filter("User__UserName", "slene").RelatedSel().All(&posts)1204 throwFailNow(t, err)1205 throwFailNow(t, AssertIs(num, 1))1206 throwFailNow(t, AssertIs(posts[0].User == nil, false))1207 throwFailNow(t, AssertIs(posts[0].User.UserName, "slene"))12081209 var tags []*Tag1210 num, err = dORM.QueryTable("tag").Filter("Posts__Post__Title", "Introduction").All(&tags)1211 throwFailNow(t, err)1212 throwFailNow(t, AssertIs(num, 1))1213 throwFailNow(t, AssertIs(tags[0].Name, "golang"))12141215 tags = []*Tag{}1216 num, err = dORM.QueryTable("tag").Filter("Posts__Post__Title", "Introduction").1217 Filter("BestPost__User__UserName", "astaxie").All(&tags)1218 throwFailNow(t, err)1219 throwFailNow(t, AssertIs(num, 1))1220 throwFailNow(t, AssertIs(tags[0].Name, "golang"))12211222 tags = []*Tag{}1223 num, err = dORM.QueryTable("tag").Filter("Posts__Post__Title", "Introduction").1224 Filter("BestPost__User__UserName", "astaxie").RelatedSel().All(&tags)1225 throwFailNow(t, err)1226 throwFailNow(t, AssertIs(num, 1))1227 throwFailNow(t, AssertIs(tags[0].Name, "golang"))1228 throwFailNow(t, AssertIs(tags[0].BestPost == nil, false))1229 throwFailNow(t, AssertIs(tags[0].BestPost.Title, "Examples"))1230 throwFailNow(t, AssertIs(tags[0].BestPost.User == nil, false))1231 throwFailNow(t, AssertIs(tags[0].BestPost.User.UserName, "astaxie"))1232}12331234func TestLoadRelated(t *testing.T) {1235 // load reverse foreign key1236 user := User{ID: 3}12371238 err := dORM.Read(&user)1239 throwFailNow(t, err)12401241 num, err := dORM.LoadRelated(&user, "Posts")1242 throwFailNow(t, err)1243 throwFailNow(t, AssertIs(num, 2))1244 throwFailNow(t, AssertIs(len(user.Posts), 2))1245 throwFailNow(t, AssertIs(user.Posts[0].User.ID, 3))12461247 num, err = dORM.LoadRelated(&user, "Posts", true)1248 throwFailNow(t, err)1249 throwFailNow(t, AssertIs(len(user.Posts), 2))1250 throwFailNow(t, AssertIs(user.Posts[0].User.UserName, "astaxie"))12511252 num, err = dORM.LoadRelated(&user, "Posts", true, 1)1253 throwFailNow(t, err)1254 throwFailNow(t, AssertIs(len(user.Posts), 1))12551256 num, err = dORM.LoadRelated(&user, "Posts", true, 0, 0, "-Id")1257 throwFailNow(t, err)1258 throwFailNow(t, AssertIs(len(user.Posts), 2))1259 throwFailNow(t, AssertIs(user.Posts[0].Title, "Formatting"))12601261 num, err = dORM.LoadRelated(&user, "Posts", true, 1, 1, "Id")1262 throwFailNow(t, err)1263 throwFailNow(t, AssertIs(len(user.Posts), 1))1264 throwFailNow(t, AssertIs(user.Posts[0].Title, "Formatting"))12651266 // load reverse one to one1267 profile := Profile{ID: 3}1268 profile.BestPost = &Post{ID: 2}1269 num, err = dORM.Update(&profile, "BestPost")1270 throwFailNow(t, err)1271 throwFailNow(t, AssertIs(num, 1))12721273 err = dORM.Read(&profile)1274 throwFailNow(t, err)12751276 num, err = dORM.LoadRelated(&profile, "User")1277 throwFailNow(t, err)1278 throwFailNow(t, AssertIs(num, 1))1279 throwFailNow(t, AssertIs(profile.User == nil, false))1280 throwFailNow(t, AssertIs(profile.User.UserName, "astaxie"))12811282 num, err = dORM.LoadRelated(&profile, "User", true)1283 throwFailNow(t, err)1284 throwFailNow(t, AssertIs(num, 1))1285 throwFailNow(t, AssertIs(profile.User == nil, false))1286 throwFailNow(t, AssertIs(profile.User.UserName, "astaxie"))1287 throwFailNow(t, AssertIs(profile.User.Profile.Age, profile.Age))12881289 // load rel one to one1290 err = dORM.Read(&user)1291 throwFailNow(t, err)12921293 num, err = dORM.LoadRelated(&user, "Profile")1294 throwFailNow(t, err)1295 throwFailNow(t, AssertIs(num, 1))1296 throwFailNow(t, AssertIs(user.Profile == nil, false))1297 throwFailNow(t, AssertIs(user.Profile.Age, 30))12981299 num, err = dORM.LoadRelated(&user, "Profile", true)1300 throwFailNow(t, err)1301 throwFailNow(t, AssertIs(num, 1))1302 throwFailNow(t, AssertIs(user.Profile == nil, false))1303 throwFailNow(t, AssertIs(user.Profile.Age, 30))1304 throwFailNow(t, AssertIs(user.Profile.BestPost == nil, false))1305 throwFailNow(t, AssertIs(user.Profile.BestPost.Title, "Examples"))13061307 post := Post{ID: 2}13081309 // load rel foreign key1310 err = dORM.Read(&post)1311 throwFailNow(t, err)13121313 num, err = dORM.LoadRelated(&post, "User")1314 throwFailNow(t, err)1315 throwFailNow(t, AssertIs(num, 1))1316 throwFailNow(t, AssertIs(post.User == nil, false))1317 throwFailNow(t, AssertIs(post.User.UserName, "astaxie"))13181319 num, err = dORM.LoadRelated(&post, "User", true)1320 throwFailNow(t, err)1321 throwFailNow(t, AssertIs(num, 1))1322 throwFailNow(t, AssertIs(post.User == nil, false))1323 throwFailNow(t, AssertIs(post.User.UserName, "astaxie"))1324 throwFailNow(t, AssertIs(post.User.Profile == nil, false))1325 throwFailNow(t, AssertIs(post.User.Profile.Age, 30))13261327 // load rel m2m1328 post = Post{ID: 2}13291330 err = dORM.Read(&post)1331 throwFailNow(t, err)13321333 num, err = dORM.LoadRelated(&post, "Tags")1334 throwFailNow(t, err)1335 throwFailNow(t, AssertIs(num, 2))1336 throwFailNow(t, AssertIs(len(post.Tags), 2))1337 throwFailNow(t, AssertIs(post.Tags[0].Name, "golang"))13381339 num, err = dORM.LoadRelated(&post, "Tags", true)1340 throwFailNow(t, err)1341 throwFailNow(t, AssertIs(num, 2))1342 throwFailNow(t, AssertIs(len(post.Tags), 2))1343 throwFailNow(t, AssertIs(post.Tags[0].Name, "golang"))1344 throwFailNow(t, AssertIs(post.Tags[0].BestPost == nil, false))1345 throwFailNow(t, AssertIs(post.Tags[0].BestPost.User.UserName, "astaxie"))13461347 // load reverse m2m1348 tag := Tag{ID: 1}13491350 err = dORM.Read(&tag)1351 throwFailNow(t, err)13521353 num, err = dORM.LoadRelated(&tag, "Posts")1354 throwFailNow(t, err)1355 throwFailNow(t, AssertIs(num, 3))1356 throwFailNow(t, AssertIs(tag.Posts[0].Title, "Introduction"))1357 throwFailNow(t, AssertIs(tag.Posts[0].User.ID, 2))1358 throwFailNow(t, AssertIs(tag.Posts[0].User.Profile == nil, true))13591360 num, err = dORM.LoadRelated(&tag, "Posts", true)1361 throwFailNow(t, err)1362 throwFailNow(t, AssertIs(num, 3))1363 throwFailNow(t, AssertIs(tag.Posts[0].Title, "Introduction"))1364 throwFailNow(t, AssertIs(tag.Posts[0].User.ID, 2))1365 throwFailNow(t, AssertIs(tag.Posts[0].User.UserName, "slene"))1366}13671368func TestQueryM2M(t *testing.T) {1369 post := Post{ID: 4}1370 m2m := dORM.QueryM2M(&post, "Tags")13711372 tag1 := []*Tag{{Name: "TestTag1"}, {Name: "TestTag2"}}1373 tag2 := &Tag{Name: "TestTag3"}1374 tag3 := []interface{}{&Tag{Name: "TestTag4"}}13751376 tags := []interface{}{tag1[0], tag1[1], tag2, tag3[0]}13771378 for _, tag := range tags {1379 _, err := dORM.Insert(tag)1380 throwFailNow(t, err)1381 }13821383 num, err := m2m.Add(tag1)1384 throwFailNow(t, err)1385 throwFailNow(t, AssertIs(num, 2))13861387 num, err = m2m.Add(tag2)1388 throwFailNow(t, err)1389 throwFailNow(t, AssertIs(num, 1))13901391 num, err = m2m.Add(tag3)1392 throwFailNow(t, err)1393 throwFailNow(t, AssertIs(num, 1))13941395 num, err = m2m.Count()1396 throwFailNow(t, err)1397 throwFailNow(t, AssertIs(num, 5))13981399 num, err = m2m.Remove(tag3)1400 throwFailNow(t, err)1401 throwFailNow(t, AssertIs(num, 1))14021403 num, err = m2m.Count()1404 throwFailNow(t, err)1405 throwFailNow(t, AssertIs(num, 4))14061407 exist := m2m.Exist(tag2)1408 throwFailNow(t, AssertIs(exist, true))14091410 num, err = m2m.Remove(tag2)1411 throwFailNow(t, err)1412 throwFailNow(t, AssertIs(num, 1))14131414 exist = m2m.Exist(tag2)1415 throwFailNow(t, AssertIs(exist, false))14161417 num, err = m2m.Count()1418 throwFailNow(t, err)1419 throwFailNow(t, AssertIs(num, 3))14201421 num, err = m2m.Clear()1422 throwFailNow(t, err)1423 throwFailNow(t, AssertIs(num, 3))14241425 num, err = m2m.Count()1426 throwFailNow(t, err)1427 throwFailNow(t, AssertIs(num, 0))14281429 tag := Tag{Name: "test"}1430 _, err = dORM.Insert(&tag)1431 throwFailNow(t, err)14321433 m2m = dORM.QueryM2M(&tag, "Posts")14341435 post1 := []*Post{{Title: "TestPost1"}, {Title: "TestPost2"}}1436 post2 := &Post{Title: "TestPost3"}1437 post3 := []interface{}{&Post{Title: "TestPost4"}}14381439 posts := []interface{}{post1[0], post1[1], post2, post3[0]}14401441 for _, post := range posts {1442 p := post.(*Post)1443 p.User = &User{ID: 1}1444 _, err := dORM.Insert(post)1445 throwFailNow(t, err)1446 }14471448 num, err = m2m.Add(post1)1449 throwFailNow(t, err)1450 throwFailNow(t, AssertIs(num, 2))14511452 num, err = m2m.Add(post2)1453 throwFailNow(t, err)1454 throwFailNow(t, AssertIs(num, 1))14551456 num, err = m2m.Add(post3)1457 throwFailNow(t, err)1458 throwFailNow(t, AssertIs(num, 1))14591460 num, err = m2m.Count()1461 throwFailNow(t, err)1462 throwFailNow(t, AssertIs(num, 4))14631464 num, err = m2m.Remove(post3)1465 throwFailNow(t, err)1466 throwFailNow(t, AssertIs(num, 1))14671468 num, err = m2m.Count()1469 throwFailNow(t, err)1470 throwFailNow(t, AssertIs(num, 3))14711472 exist = m2m.Exist(post2)1473 throwFailNow(t, AssertIs(exist, true))14741475 num, err = m2m.Remove(post2)1476 throwFailNow(t, err)1477 throwFailNow(t, AssertIs(num, 1))14781479 exist = m2m.Exist(post2)1480 throwFailNow(t, AssertIs(exist, false))14811482 num, err = m2m.Count()1483 throwFailNow(t, err)1484 throwFailNow(t, AssertIs(num, 2))14851486 num, err = m2m.Clear()1487 throwFailNow(t, err)1488 throwFailNow(t, AssertIs(num, 2))14891490 num, err = m2m.Count()1491 throwFailNow(t, err)1492 throwFailNow(t, AssertIs(num, 0))14931494 num, err = dORM.Delete(&tag)1495 throwFailNow(t, err)1496 throwFailNow(t, AssertIs(num, 1))1497}14981499func TestQueryRelate(t *testing.T) {1500 // post := &Post{Id: 2}15011502 // qs := dORM.QueryRelate(post, "Tags")1503 // num, err := qs.Count()1504 // throwFailNow(t, err)1505 // throwFailNow(t, AssertIs(num, 2))15061507 // var tags []*Tag1508 // num, err = qs.All(&tags)1509 // throwFailNow(t, err)1510 // throwFailNow(t, AssertIs(num, 2))1511 // throwFailNow(t, AssertIs(tags[0].Name, "golang"))15121513 // num, err = dORM.QueryTable("Tag").Filter("Posts__Post", 2).Count()1514 // throwFailNow(t, err)1515 // throwFailNow(t, AssertIs(num, 2))1516}15171518func TestPkManyRelated(t *testing.T) {1519 permission := &Permission{Name: "readPosts"}1520 err := dORM.Read(permission, "Name")1521 throwFailNow(t, err)15221523 var groups []*Group1524 qs := dORM.QueryTable("Group")1525 num, err := qs.Filter("Permissions__Permission", permission.ID).All(&groups)1526 throwFailNow(t, err)1527 throwFailNow(t, AssertIs(num, 2))1528}15291530func TestPrepareInsert(t *testing.T) {1531 qs := dORM.QueryTable("user")1532 i, err := qs.PrepareInsert()1533 throwFailNow(t, err)15341535 var user User1536 user.UserName = "testing1"1537 num, err := i.Insert(&user)1538 throwFail(t, err)1539 throwFail(t, AssertIs(num > 0, true))15401541 user.UserName = "testing2"1542 num, err = i.Insert(&user)1543 throwFail(t, err)1544 throwFail(t, AssertIs(num > 0, true))15451546 num, err = qs.Filter("user_name__in", "testing1", "testing2").Delete()1547 throwFail(t, err)1548 throwFail(t, AssertIs(num, 2))15491550 err = i.Close()1551 throwFail(t, err)1552 err = i.Close()1553 throwFail(t, AssertIs(err, ErrStmtClosed))1554}15551556func TestRawExec(t *testing.T) {1557 Q := dDbBaser.TableQuote()15581559 query := fmt.Sprintf("UPDATE %suser%s SET %suser_name%s = ? WHERE %suser_name%s = ?", Q, Q, Q, Q, Q, Q)1560 res, err := dORM.Raw(query, "testing", "slene").Exec()1561 throwFail(t, err)1562 num, err := res.RowsAffected()1563 throwFail(t, AssertIs(num, 1), err)15641565 res, err = dORM.Raw(query, "slene", "testing").Exec()1566 throwFail(t, err)1567 num, err = res.RowsAffected()1568 throwFail(t, AssertIs(num, 1), err)1569}15701571func TestRawQueryRow(t *testing.T) {1572 var (1573 Boolean bool1574 Char string1575 Text string1576 Time time.Time1577 Date time.Time1578 DateTime time.Time1579 Byte byte1580 Rune rune1581 Int int1582 Int8 int1583 Int16 int161584 Int32 int321585 Int64 int641586 Uint uint1587 Uint8 uint81588 Uint16 uint161589 Uint32 uint321590 Uint64 uint641591 Float32 float321592 Float64 float641593 Decimal float641594 )15951596 dataValues := make(map[string]interface{}, len(DataValues))15971598 for k, v := range DataValues {1599 dataValues[strings.ToLower(k)] = v1600 }16011602 Q := dDbBaser.TableQuote()16031604 cols := []string{1605 "id", "boolean", "char", "text", "time", "date", "datetime", "byte", "rune", "int", "int8", "int16", "int32",1606 "int64", "uint", "uint8", "uint16", "uint32", "uint64", "float32", "float64", "decimal",1607 }1608 sep := fmt.Sprintf("%s, %s", Q, Q)1609 query := fmt.Sprintf("SELECT %s%s%s FROM data WHERE id = ?", Q, strings.Join(cols, sep), Q)1610 var id int1611 values := []interface{}{1612 &id, &Boolean, &Char, &Text, &Time, &Date, &DateTime, &Byte, &Rune, &Int, &Int8, &Int16, &Int32,1613 &Int64, &Uint, &Uint8, &Uint16, &Uint32, &Uint64, &Float32, &Float64, &Decimal,1614 }1615 err := dORM.Raw(query, 1).QueryRow(values...)1616 throwFailNow(t, err)1617 for i, col := range cols {1618 vu := values[i]1619 v := reflect.ValueOf(vu).Elem().Interface()1620 switch col {1621 case "id":1622 throwFail(t, AssertIs(id, 1))1623 case "time":1624 v = v.(time.Time).In(DefaultTimeLoc)1625 value := dataValues[col].(time.Time).In(DefaultTimeLoc)1626 throwFail(t, AssertIs(v, value, testTime))1627 case "date":1628 v = v.(time.Time).In(DefaultTimeLoc)1629 value := dataValues[col].(time.Time).In(DefaultTimeLoc)1630 throwFail(t, AssertIs(v, value, testDate))1631 case "datetime":1632 v = v.(time.Time).In(DefaultTimeLoc)1633 value := dataValues[col].(time.Time).In(DefaultTimeLoc)1634 throwFail(t, AssertIs(v, value, testDateTime))1635 default:1636 throwFail(t, AssertIs(v, dataValues[col]))1637 }1638 }16391640 var (1641 uid int1642 status *int1643 pid *int1644 )16451646 cols = []string{1647 "id", "Status", "profile_id",1648 }1649 query = fmt.Sprintf("SELECT %s%s%s FROM %suser%s WHERE id = ?", Q, strings.Join(cols, sep), Q, Q, Q)1650 err = dORM.Raw(query, 4).QueryRow(&uid, &status, &pid)1651 throwFail(t, err)1652 throwFail(t, AssertIs(uid, 4))1653 throwFail(t, AssertIs(*status, 3))1654 throwFail(t, AssertIs(pid, nil))1655}16561657func TestQueryRows(t *testing.T) {1658 Q := dDbBaser.TableQuote()16591660 var datas []*Data16611662 query := fmt.Sprintf("SELECT * FROM %sdata%s", Q, Q)1663 num, err := dORM.Raw(query).QueryRows(&datas)1664 throwFailNow(t, err)1665 throwFailNow(t, AssertIs(num, 1))1666 throwFailNow(t, AssertIs(len(datas), 1))16671668 ind := reflect.Indirect(reflect.ValueOf(datas[0]))16691670 for name, value := range DataValues {1671 e := ind.FieldByName(name)1672 vu := e.Interface()1673 switch name {1674 case "Time":1675 vu = vu.(time.Time).In(DefaultTimeLoc).Format(testTime)1676 value = value.(time.Time).In(DefaultTimeLoc).Format(testTime)1677 case "Date":1678 vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDate)1679 value = value.(time.Time).In(DefaultTimeLoc).Format(testDate)1680 case "DateTime":1681 vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDateTime)1682 value = value.(time.Time).In(DefaultTimeLoc).Format(testDateTime)1683 }1684 throwFail(t, AssertIs(vu == value, true), value, vu)1685 }16861687 var datas2 []Data16881689 query = fmt.Sprintf("SELECT * FROM %sdata%s", Q, Q)1690 num, err = dORM.Raw(query).QueryRows(&datas2)1691 throwFailNow(t, err)1692 throwFailNow(t, AssertIs(num, 1))1693 throwFailNow(t, AssertIs(len(datas2), 1))16941695 ind = reflect.Indirect(reflect.ValueOf(datas2[0]))16961697 for name, value := range DataValues {1698 e := ind.FieldByName(name)1699 vu := e.Interface()1700 switch name {1701 case "Time":1702 vu = vu.(time.Time).In(DefaultTimeLoc).Format(testTime)1703 value = value.(time.Time).In(DefaultTimeLoc).Format(testTime)1704 case "Date":1705 vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDate)1706 value = value.(time.Time).In(DefaultTimeLoc).Format(testDate)1707 case "DateTime":1708 vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDateTime)1709 value = value.(time.Time).In(DefaultTimeLoc).Format(testDateTime)1710 }1711 throwFail(t, AssertIs(vu == value, true), value, vu)1712 }17131714 var ids []int1715 var usernames []string1716 query = fmt.Sprintf("SELECT %sid%s, %suser_name%s FROM %suser%s ORDER BY %sid%s ASC", Q, Q, Q, Q, Q, Q, Q, Q)1717 num, err = dORM.Raw(query).QueryRows(&ids, &usernames)1718 throwFailNow(t, err)1719 throwFailNow(t, AssertIs(num, 3))1720 throwFailNow(t, AssertIs(len(ids), 3))1721 throwFailNow(t, AssertIs(ids[0], 2))1722 throwFailNow(t, AssertIs(usernames[0], "slene"))1723 throwFailNow(t, AssertIs(ids[1], 3))1724 throwFailNow(t, AssertIs(usernames[1], "astaxie"))1725 throwFailNow(t, AssertIs(ids[2], 4))1726 throwFailNow(t, AssertIs(usernames[2], "nobody"))1727}17281729func TestRawValues(t *testing.T) {1730 Q := dDbBaser.TableQuote()17311732 var maps []Params1733 query := fmt.Sprintf("SELECT %suser_name%s FROM %suser%s WHERE %sStatus%s = ?", Q, Q, Q, Q, Q, Q)1734 num, err := dORM.Raw(query, 1).Values(&maps)1735 throwFail(t, err)1736 throwFail(t, AssertIs(num, 1))1737 if num == 1 {1738 throwFail(t, AssertIs(maps[0]["user_name"], "slene"))1739 }17401741 var lists []ParamsList1742 num, err = dORM.Raw(query, 1).ValuesList(&lists)1743 throwFail(t, err)1744 throwFail(t, AssertIs(num, 1))1745 if num == 1 {1746 throwFail(t, AssertIs(lists[0][0], "slene"))1747 }17481749 query = fmt.Sprintf("SELECT %sprofile_id%s FROM %suser%s ORDER BY %sid%s ASC", Q, Q, Q, Q, Q, Q)1750 var list ParamsList1751 num, err = dORM.Raw(query).ValuesFlat(&list)1752 throwFail(t, err)1753 throwFail(t, AssertIs(num, 3))1754 if num == 3 {1755 throwFail(t, AssertIs(list[0], "2"))1756 throwFail(t, AssertIs(list[1], "3"))1757 throwFail(t, AssertIs(list[2], nil))1758 }1759}17601761func TestRawPrepare(t *testing.T) {1762 switch {1763 case IsMysql || IsSqlite:17641765 pre, err := dORM.Raw("INSERT INTO tag (name) VALUES (?)").Prepare()1766 throwFail(t, err)1767 if pre != nil {1768 r, err := pre.Exec("name1")1769 throwFail(t, err)17701771 tid, err := r.LastInsertId()1772 throwFail(t, err)1773 throwFail(t, AssertIs(tid > 0, true))17741775 r, err = pre.Exec("name2")1776 throwFail(t, err)17771778 id, err := r.LastInsertId()1779 throwFail(t, err)1780 throwFail(t, AssertIs(id, tid+1))17811782 r, err = pre.Exec("name3")1783 throwFail(t, err)17841785 id, err = r.LastInsertId()1786 throwFail(t, err)1787 throwFail(t, AssertIs(id, tid+2))17881789 err = pre.Close()1790 throwFail(t, err)17911792 res, err := dORM.Raw("DELETE FROM tag WHERE name IN (?, ?, ?)", []string{"name1", "name2", "name3"}).Exec()1793 throwFail(t, err)17941795 num, err := res.RowsAffected()1796 throwFail(t, err)1797 throwFail(t, AssertIs(num, 3))1798 }17991800 case IsPostgres:18011802 pre, err := dORM.Raw(`INSERT INTO "tag" ("name") VALUES (?) RETURNING "id"`).Prepare()1803 throwFail(t, err)1804 if pre != nil {1805 _, err := pre.Exec("name1")1806 throwFail(t, err)18071808 _, err = pre.Exec("name2")1809 throwFail(t, err)18101811 _, err = pre.Exec("name3")1812 throwFail(t, err)18131814 err = pre.Close()1815 throwFail(t, err)18161817 res, err := dORM.Raw(`DELETE FROM "tag" WHERE "name" IN (?, ?, ?)`, []string{"name1", "name2", "name3"}).Exec()1818 throwFail(t, err)18191820 if err == nil {1821 num, err := res.RowsAffected()1822 throwFail(t, err)1823 throwFail(t, AssertIs(num, 3))1824 }1825 }1826 }1827}18281829func TestUpdate(t *testing.T) {1830 qs := dORM.QueryTable("user")1831 num, err := qs.Filter("user_name", "slene").Filter("is_staff", false).Update(Params{1832 "is_staff": true,1833 "is_active": true,1834 })1835 throwFail(t, err)1836 throwFail(t, AssertIs(num, 1))18371838 // with join1839 num, err = qs.Filter("user_name", "slene").Filter("profile__age", 28).Filter("is_staff", true).Update(Params{1840 "is_staff": false,1841 })1842 throwFail(t, err)1843 throwFail(t, AssertIs(num, 1))18441845 num, err = qs.Filter("user_name", "slene").Update(Params{1846 "Nums": ColValue(ColAdd, 100),1847 })1848 throwFail(t, err)1849 throwFail(t, AssertIs(num, 1))18501851 num, err = qs.Filter("user_name", "slene").Update(Params{1852 "Nums": ColValue(ColMinus, 50),1853 })1854 throwFail(t, err)1855 throwFail(t, AssertIs(num, 1))18561857 num, err = qs.Filter("user_name", "slene").Update(Params{1858 "Nums": ColValue(ColMultiply, 3),1859 })1860 throwFail(t, err)1861 throwFail(t, AssertIs(num, 1))18621863 num, err = qs.Filter("user_name", "slene").Update(Params{1864 "Nums": ColValue(ColExcept, 5),1865 })1866 throwFail(t, err)1867 throwFail(t, AssertIs(num, 1))18681869 user := User{UserName: "slene"}1870 err = dORM.Read(&user, "UserName")1871 throwFail(t, err)1872 throwFail(t, AssertIs(user.Nums, 30))1873}18741875func TestDelete(t *testing.T) {1876 qs := dORM.QueryTable("user_profile")1877 num, err := qs.Filter("user__user_name", "slene").Delete()1878 throwFail(t, err)1879 throwFail(t, AssertIs(num, 1))18801881 qs = dORM.QueryTable("user")1882 num, err = qs.Filter("user_name", "slene").Filter("profile__isnull", true).Count()1883 throwFail(t, err)1884 throwFail(t, AssertIs(num, 1))18851886 qs = dORM.QueryTable("comment")1887 num, err = qs.Count()1888 throwFail(t, err)1889 throwFail(t, AssertIs(num, 6))18901891 qs = dORM.QueryTable("post")1892 num, err = qs.Filter("Id", 3).Delete()1893 throwFail(t, err)1894 throwFail(t, AssertIs(num, 1))18951896 qs = dORM.QueryTable("comment")1897 num, err = qs.Count()1898 throwFail(t, err)1899 throwFail(t, AssertIs(num, 4))19001901 qs = dORM.QueryTable("comment")1902 num, err = qs.Filter("Post__User", 3).Delete()1903 throwFail(t, err)1904 throwFail(t, AssertIs(num, 3))19051906 qs = dORM.QueryTable("comment")1907 num, err = qs.Count()1908 throwFail(t, err)1909 throwFail(t, AssertIs(num, 1))1910}19111912func TestTransaction(t *testing.T) {1913 // this test worked when database support transaction19141915 o := NewOrm()1916 err := o.Begin()1917 throwFail(t, err)19181919 var names = []string{"1", "2", "3"}19201921 var tag Tag1922 tag.Name = names[0]1923 id, err := o.Insert(&tag)1924 throwFail(t, err)1925 throwFail(t, AssertIs(id > 0, true))19261927 num, err := o.QueryTable("tag").Filter("name", "golang").Update(Params{"name": names[1]})1928 throwFail(t, err)1929 throwFail(t, AssertIs(num, 1))19301931 switch {1932 case IsMysql || IsSqlite:1933 res, err := o.Raw("INSERT INTO tag (name) VALUES (?)", names[2]).Exec()1934 throwFail(t, err)1935 if err == nil {1936 id, err = res.LastInsertId()1937 throwFail(t, err)1938 throwFail(t, AssertIs(id > 0, true))1939 }1940 }19411942 err = o.Rollback()1943 throwFail(t, err)19441945 num, err = o.QueryTable("tag").Filter("name__in", names).Count()1946 throwFail(t, err)1947 throwFail(t, AssertIs(num, 0))19481949 err = o.Begin()1950 throwFail(t, err)19511952 tag.Name = "commit"1953 id, err = o.Insert(&tag)1954 throwFail(t, err)1955 throwFail(t, AssertIs(id > 0, true))19561957 o.Commit()1958 throwFail(t, err)19591960 num, err = o.QueryTable("tag").Filter("name", "commit").Delete()1961 throwFail(t, err)1962 throwFail(t, AssertIs(num, 1))19631964}19651966func TestReadOrCreate(t *testing.T) {1967 u := &User{1968 UserName: "Kyle",1969 Email: "kylemcc@gmail.com",1970 Password: "other_pass",1971 Status: 7,1972 IsStaff: false,1973 IsActive: true,1974 }19751976 created, pk, err := dORM.ReadOrCreate(u, "UserName")1977 throwFail(t, err)1978 throwFail(t, AssertIs(created, true))1979 throwFail(t, AssertIs(u.UserName, "Kyle"))1980 throwFail(t, AssertIs(u.Email, "kylemcc@gmail.com"))1981 throwFail(t, AssertIs(u.Password, "other_pass"))1982 throwFail(t, AssertIs(u.Status, 7))1983 throwFail(t, AssertIs(u.IsStaff, false))1984 throwFail(t, AssertIs(u.IsActive, true))1985 throwFail(t, AssertIs(u.Created.In(DefaultTimeLoc), u.Created.In(DefaultTimeLoc), testDate))1986 throwFail(t, AssertIs(u.Updated.In(DefaultTimeLoc), u.Updated.In(DefaultTimeLoc), testDateTime))19871988 nu := &User{UserName: u.UserName, Email: "someotheremail@gmail.com"}1989 created, pk, err = dORM.ReadOrCreate(nu, "UserName")1990 throwFail(t, err)1991 throwFail(t, AssertIs(created, false))1992 throwFail(t, AssertIs(nu.ID, u.ID))1993 throwFail(t, AssertIs(pk, u.ID))1994 throwFail(t, AssertIs(nu.UserName, u.UserName))1995 throwFail(t, AssertIs(nu.Email, u.Email)) // should contain the value in the table, not the one specified above1996 throwFail(t, AssertIs(nu.Password, u.Password))1997 throwFail(t, AssertIs(nu.Status, u.Status))1998 throwFail(t, AssertIs(nu.IsStaff, u.IsStaff))1999 throwFail(t, AssertIs(nu.IsActive, u.IsActive))20002001 dORM.Delete(u)2002}20032004func TestInLine(t *testing.T) {2005 name := "inline"2006 email := "hello@go.com"2007 inline := NewInLine()2008 inline.Name = name2009 inline.Email = email20102011 id, err := dORM.Insert(inline)2012 throwFail(t, err)2013 throwFail(t, AssertIs(id, 1))20142015 il := NewInLine()2016 il.ID = 12017 err = dORM.Read(il)2018 throwFail(t, err)20192020 throwFail(t, AssertIs(il.Name, name))2021 throwFail(t, AssertIs(il.Email, email))2022 throwFail(t, AssertIs(il.Created.In(DefaultTimeLoc), inline.Created.In(DefaultTimeLoc), testDate))2023 throwFail(t, AssertIs(il.Updated.In(DefaultTimeLoc), inline.Updated.In(DefaultTimeLoc), testDateTime))2024}20252026func TestInLineOneToOne(t *testing.T) {2027 name := "121"2028 email := "121@go.com"2029 inline := NewInLine()2030 inline.Name = name2031 inline.Email = email20322033 id, err := dORM.Insert(inline)2034 throwFail(t, err)2035 throwFail(t, AssertIs(id, 2))20362037 note := "one2one"2038 il121 := NewInLineOneToOne()2039 il121.Note = note2040 il121.InLine = inline2041 _, err = dORM.Insert(il121)2042 throwFail(t, err)2043 throwFail(t, AssertIs(il121.ID, 1))20442045 il := NewInLineOneToOne()2046 err = dORM.QueryTable(il).Filter("Id", 1).RelatedSel().One(il)20472048 throwFail(t, err)2049 throwFail(t, AssertIs(il.Note, note))2050 throwFail(t, AssertIs(il.InLine.ID, id))2051 throwFail(t, AssertIs(il.InLine.Name, name))2052 throwFail(t, AssertIs(il.InLine.Email, email))20532054 rinline := NewInLine()2055 err = dORM.QueryTable(rinline).Filter("InLineOneToOne__Id", 1).One(rinline)20562057 throwFail(t, err)2058 throwFail(t, AssertIs(rinline.ID, id))2059 throwFail(t, AssertIs(rinline.Name, name))2060 throwFail(t, AssertIs(rinline.Email, email))2061}20622063func TestIntegerPk(t *testing.T) {2064 its := []IntegerPk{2065 {ID: math.MinInt64, Value: "-"},2066 {ID: 0, Value: "0"},2067 {ID: math.MaxInt64, Value: "+"},2068 }20692070 num, err := dORM.InsertMulti(len(its), its)2071 throwFail(t, err)2072 throwFail(t, AssertIs(num, len(its)))20732074 for _, intPk := range its {2075 out := IntegerPk{ID: intPk.ID}2076 err = dORM.Read(&out)2077 throwFail(t, err)2078 throwFail(t, AssertIs(out.Value, intPk.Value))2079 }20802081 num, err = dORM.InsertMulti(1, []*IntegerPk{{2082 ID: 1, Value: "ok",2083 }})2084 throwFail(t, err)2085 throwFail(t, AssertIs(num, 1))2086}20872088func TestInsertAuto(t *testing.T) {2089 u := &User{2090 UserName: "autoPre",2091 Email: "autoPre@gmail.com",2092 }20932094 id, err := dORM.Insert(u)2095 throwFail(t, err)20962097 id += 1002098 su := &User{2099 ID: int(id),2100 UserName: "auto",2101 Email: "auto@gmail.com",2102 }21032104 nid, err := dORM.Insert(su)2105 throwFail(t, err)2106 throwFail(t, AssertIs(nid, id))21072108 users := []User{2109 {ID: int(id + 100), UserName: "auto_100"},2110 {ID: int(id + 110), UserName: "auto_110"},2111 {ID: int(id + 120), UserName: "auto_120"},2112 }2113 num, err := dORM.InsertMulti(100, users)2114 throwFail(t, err)2115 throwFail(t, AssertIs(num, 3))21162117 u = &User{2118 UserName: "auto_121",2119 }21202121 nid, err = dORM.Insert(u)2122 throwFail(t, err)2123 throwFail(t, AssertIs(nid, id+120+1))2124}21252126func TestUintPk(t *testing.T) {2127 name := "go"2128 u := &UintPk{2129 ID: 8,2130 Name: name,2131 }21322133 created, pk, err := dORM.ReadOrCreate(u, "ID")2134 throwFail(t, err)2135 throwFail(t, AssertIs(created, true))2136 throwFail(t, AssertIs(u.Name, name))21372138 nu := &UintPk{ID: 8}2139 created, pk, err = dORM.ReadOrCreate(nu, "ID")2140 throwFail(t, err)2141 throwFail(t, AssertIs(created, false))2142 throwFail(t, AssertIs(nu.ID, u.ID))2143 throwFail(t, AssertIs(pk, u.ID))2144 throwFail(t, AssertIs(nu.Name, name))21452146 dORM.Delete(u)2147}21482149func TestPtrPk(t *testing.T) {2150 parent := &IntegerPk{ID: 10, Value: "10"}21512152 id, _ := dORM.Insert(parent)2153 if !IsMysql {2154 // MySql does not support last_insert_id in this case: see #23822155 throwFail(t, AssertIs(id, 10))2156 }21572158 ptr := PtrPk{ID: parent, Positive: true}2159 num, err := dORM.InsertMulti(2, []PtrPk{ptr})2160 throwFail(t, err)2161 throwFail(t, AssertIs(num, 1))2162 throwFail(t, AssertIs(ptr.ID, parent))21632164 nptr := &PtrPk{ID: parent}2165 created, pk, err := dORM.ReadOrCreate(nptr, "ID")2166 throwFail(t, err)2167 throwFail(t, AssertIs(created, false))2168 throwFail(t, AssertIs(pk, 10))2169 throwFail(t, AssertIs(nptr.ID, parent))2170 throwFail(t, AssertIs(nptr.Positive, true))21712172 nptr = &PtrPk{Positive: true}2173 created, pk, err = dORM.ReadOrCreate(nptr, "Positive")2174 throwFail(t, err)2175 throwFail(t, AssertIs(created, false))2176 throwFail(t, AssertIs(pk, 10))2177 throwFail(t, AssertIs(nptr.ID, parent))21782179 nptr.Positive = false2180 num, err = dORM.Update(nptr)2181 throwFail(t, err)2182 throwFail(t, AssertIs(num, 1))2183 throwFail(t, AssertIs(nptr.ID, parent))2184 throwFail(t, AssertIs(nptr.Positive, false))21852186 num, err = dORM.Delete(nptr)2187 throwFail(t, err)2188 throwFail(t, AssertIs(num, 1))2189}21902191func TestSnake(t *testing.T) {2192 cases := map[string]string{2193 "i": "i",2194 "I": "i",2195 "iD": "i_d",2196 "ID": "i_d",2197 "NO": "n_o",2198 "NOO": "n_o_o",2199 "NOOooOOoo": "n_o_ooo_o_ooo",2200 "OrderNO": "order_n_o",2201 "tagName": "tag_name",2202 "tag_Name": "tag__name",2203 "tag_name": "tag_name",2204 "_tag_name": "_tag_name",2205 "tag_666name": "tag_666name",2206 "tag_666Name": "tag_666_name",2207 }2208 for name, want := range cases {2209 got := snakeString(name)2210 throwFail(t, AssertIs(got, want))2211 }2212}22132214func TestIgnoreCaseTag(t *testing.T) {2215 type testTagModel struct {2216 ID int `orm:"pk"`2217 NOO string `orm:"column(n)"`2218 Name01 string `orm:"NULL"`2219 Name02 string `orm:"COLUMN(Name)"`2220 Name03 string `orm:"Column(name)"`2221 }2222 modelCache.clean()2223 RegisterModel(&testTagModel{})2224 info, ok := modelCache.get("test_tag_model")2225 throwFail(t, AssertIs(ok, true))2226 throwFail(t, AssertNot(info, nil))2227 if t == nil {2228 return2229 }2230 throwFail(t, AssertIs(info.fields.GetByName("NOO").column, "n"))2231 throwFail(t, AssertIs(info.fields.GetByName("Name01").null, true))2232 throwFail(t, AssertIs(info.fields.GetByName("Name02").column, "Name"))2233 throwFail(t, AssertIs(info.fields.GetByName("Name03").column, "name"))2234}2235func TestInsertOrUpdate(t *testing.T) {2236 RegisterModel(new(User))2237 user := User{UserName: "unique_username133", Status: 1, Password: "o"}2238 user1 := User{UserName: "unique_username133", Status: 2, Password: "o"}2239 user2 := User{UserName: "unique_username133", Status: 3, Password: "oo"}2240 dORM.Insert(&user)2241 test := User{UserName: "unique_username133"}2242 fmt.Println(dORM.Driver().Name())2243 if dORM.Driver().Name() == "sqlite3" {2244 fmt.Println("sqlite3 is nonsupport")2245 return2246 }2247 //test12248 _, err := dORM.InsertOrUpdate(&user1, "user_name")2249 if err != nil {2250 fmt.Println(err)2251 if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {2252 } else {2253 throwFailNow(t, err)2254 }2255 } else {2256 dORM.Read(&test, "user_name")2257 throwFailNow(t, AssertIs(user1.Status, test.Status))2258 }2259 //test22260 _, err = dORM.InsertOrUpdate(&user2, "user_name")2261 if err != nil {2262 fmt.Println(err)2263 if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {2264 } else {2265 throwFailNow(t, err)2266 }2267 } else {2268 dORM.Read(&test, "user_name")2269 throwFailNow(t, AssertIs(user2.Status, test.Status))2270 throwFailNow(t, AssertIs(user2.Password, strings.TrimSpace(test.Password)))2271 }2272 //test3 +2273 _, err = dORM.InsertOrUpdate(&user2, "user_name", "status=status+1")2274 if err != nil {2275 fmt.Println(err)2276 if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {2277 } else {2278 throwFailNow(t, err)2279 }2280 } else {2281 dORM.Read(&test, "user_name")2282 throwFailNow(t, AssertIs(user2.Status+1, test.Status))2283 }2284 //test4 -2285 _, err = dORM.InsertOrUpdate(&user2, "user_name", "status=status-1")2286 if err != nil {2287 fmt.Println(err)2288 if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {2289 } else {2290 throwFailNow(t, err)2291 }2292 } else {2293 dORM.Read(&test, "user_name")2294 throwFailNow(t, AssertIs((user2.Status+1)-1, test.Status))2295 }2296 //test5 *2297 _, err = dORM.InsertOrUpdate(&user2, "user_name", "status=status*3")2298 if err != nil {2299 fmt.Println(err)2300 if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {2301 } else {2302 throwFailNow(t, err)2303 }2304 } else {2305 dORM.Read(&test, "user_name")2306 throwFailNow(t, AssertIs(((user2.Status+1)-1)*3, test.Status))2307 }2308 //test6 /2309 _, err = dORM.InsertOrUpdate(&user2, "user_name", "Status=Status/3")2310 if err != nil {2311 fmt.Println(err)2312 if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {2313 } else {2314 throwFailNow(t, err)2315 }2316 } else {2317 dORM.Read(&test, "user_name")2318 throwFailNow(t, AssertIs((((user2.Status+1)-1)*3)/3, test.Status))2319 }2320}
...
Fail
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4}5import (6func main() {7 fmt.Println("Hello, playground")8}9import (10func main() {11 fmt.Println("Hello, playground")12}13import (14func main() {15 fmt.Println("Hello, playground")16}17import (18func main() {19 fmt.Println("Hello, playground")20}21import (22func main() {23 fmt.Println("Hello, playground")24}25import (26func main() {27 fmt.Println("Hello, playground")28}29import (30func main() {31 fmt.Println("Hello, playground")32}33import (34func main() {35 fmt.Println("Hello, playground")36}37import (38func main() {39 fmt.Println("Hello, playground")40}41import (42func main() {43 fmt.Println("Hello, playground")44}45import (46func main() {47 fmt.Println("Hello, playground")48}49import (
Fail
Using AI Code Generation
1import "testing"2func TestFail(t *testing.T) {3 t.Fail()4}5import "testing"6func TestFailNow(t *testing.T) {7 t.FailNow()8}9import "testing"10func TestFatal(t *testing.T) {11 t.Fatal()12}13import "testing"14func TestError(t *testing.T) {15 t.Error()16}17import "testing"18func TestErrorf(t *testing.T) {19 t.Errorf("Errorf")20}21import "testing"22func TestLog(t *testing.T) {23 t.Log("Log")24}25import "testing"26func TestLogf(t *testing.T) {27 t.Logf("Logf")28}
Fail
Using AI Code Generation
1import (2func TestFail(t *testing.T) {3 t.Fail()4 t.Log("hello")5}6import (7func TestFailNow(t *testing.T) {8 t.FailNow()9 t.Log("hello")10}11import (12func TestFatal(t *testing.T) {13 t.Fatal("Fatal")14 t.Log("hello")15}16import (17func TestFatal(t *testing.T) {18 t.Fatalf("Fatal")19 t.Log("hello")20}21import (22func TestFatal(t *testing.T) {23 t.Fatalf("Fatal %v", "hello")24 t.Log("hello")25}26import (27func TestFatal(t *testing.T) {28 t.Fatalf("Fatal %v %v", "hello", "world")29 t.Log("hello")30}31import (32func TestFatal(t *testing.T) {33 t.Fatalf("Fatal %v %v %v", "hello", "world")34 t.Log("hello")35}36import (37func TestFatal(t *testing.T) {38 t.Fatalf("Fatal %v %v %v %v", "hello", "world")39 t.Log("hello")40}41import (42func TestFatal(t *testing.T) {43 t.Fatalf("Fatal %v %v %v %v %v", "hello", "world")44 t.Log("hello")45}
Fail
Using AI Code Generation
1import (2func TestFail(t *testing.T) {3 fmt.Println("Test Fail")4 t.Fail()5}6--- FAIL: TestFail (0.00s)71.2. FailNow() method8import (9func TestFailNow(t *testing.T) {10 fmt.Println("Test FailNow")11 t.FailNow()12}131.3. Parallel() method14import (15func TestParallel(t *testing.T) {16 fmt.Println("Test Parallel")17 t.Parallel()18}191.4. Run() method20import (21func TestRun(t *testing.T) {22 fmt.Println("Test Run")23 t.Run("subtest", func(t *testing.T) {24 fmt.Println("subtest")25 })26}271.5. Skip() method28import (29func TestSkip(t *testing.T) {30 fmt.Println("Test Skip")31 t.Skip("skipping")32}33--- SKIP: TestSkip (0.00s)341.6. SkipNow() method35import (
Fail
Using AI Code Generation
1import (2func TestFail(t *testing.T) {3 fmt.Println("Hi")4 t.Fail()5 fmt.Println("Bye")6}7import (8func TestFailNow(t *testing.T) {9 fmt.Println("Hi")10 t.FailNow()11 fmt.Println("Bye")12}13import (14func TestError(t *testing.T) {15 fmt.Println("Hi")16 t.Error("This is an error")17 fmt.Println("Bye")18}19import (20func TestErrorf(t *testing.T) {21 fmt.Println("Hi")22 t.Errorf("This is an error")23 fmt.Println("Bye")24}25import (26func TestFailNow(t *testing.T) {27 fmt.Println("Hi")28 t.Fatal("This is an error")29 fmt.Println("Bye")30}31import (32func TestFailNow(t *testing.T) {33 fmt.Println("Hi")34 t.Fatalf("This is an error")35 fmt.Println("Bye")36}
Fail
Using AI Code Generation
1import (2func TestFail(t *testing.T) {3 assert.Fail(t, "This is a failure")4}5func main() {6 fmt.Println("This is a test")7}8--- FAIL: TestFail (0.00s)9assert.FailNow(t, "This is a failure")10--- FAIL: TestFailNow (0.00s)11assert.FailNow(t, "This is a failure")12--- FAIL: TestFailNow (0.00s)13assert.Fail(t, "This is a failure")14--- FAIL: TestFail (0.00s)15assert.FailNow(t, "This is a failure")16--- FAIL: TestFailNow (0.00s)17assert.Fail(t, "This is a failure")18--- FAIL: TestFail (0.00s)19assert.FailNow(t, "This is a failure")20--- FAIL: TestFailNow (0.00s)
Fail
Using AI Code Generation
1import (2func main() {3 test := testing.T{}4 test.Fail()5 fmt.Println("Test failed")6}7FailNow()8import (9func main() {10 test := testing.T{}11 test.FailNow()12 fmt.Println("Test failed")13}14Fatal()15import (16func main() {17 test := testing.T{}18 test.Fatal()19 fmt.Println("Test failed")20}21FatalIf()22import (23func main() {24 test := testing.T{}25 test.FatalIf(true)26 fmt.Println("Test failed")27}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!