github.com/tufanbarisyildirim/pop@v4.13.1+incompatible/executors_test.go (about) 1 package pop 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/gobuffalo/nulls" 8 "github.com/gofrs/uuid" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func Test_IsZeroOfUnderlyingType(t *testing.T) { 13 if PDB == nil { 14 t.Skip("skipping integration tests") 15 } 16 r := require.New(t) 17 transaction(func(tx *Connection) { 18 car := &ValidatableCar{Name: "VW"} 19 r.True(IsZeroOfUnderlyingType(car.ID)) 20 err := tx.Save(car) 21 r.NoError(err) 22 r.NotZero(car.ID) 23 r.NotZero(car.CreatedAt) 24 25 r.False(IsZeroOfUnderlyingType(car.ID)) 26 27 var i int 28 r.True(IsZeroOfUnderlyingType(i)) 29 i = 32 30 r.False(IsZeroOfUnderlyingType(i)) 31 32 var s string 33 r.True(IsZeroOfUnderlyingType(s)) 34 s = "42" 35 r.False(IsZeroOfUnderlyingType(s)) 36 37 var u uuid.UUID 38 r.True(IsZeroOfUnderlyingType(u)) 39 u, err = uuid.NewV1() 40 r.NoError(err) 41 r.False(IsZeroOfUnderlyingType(u)) 42 }) 43 } 44 45 func Test_ValidateAndSave(t *testing.T) { 46 if PDB == nil { 47 t.Skip("skipping integration tests") 48 } 49 r := require.New(t) 50 validationLogs = []string{} 51 transaction(func(tx *Connection) { 52 car := &ValidatableCar{Name: "VW"} 53 verrs, err := tx.ValidateAndSave(car) 54 r.NoError(err) 55 r.False(verrs.HasAny()) 56 r.Len(validationLogs, 2) 57 r.Equal([]string{"Validate", "ValidateSave"}, validationLogs) 58 r.NotZero(car.ID) 59 r.NotZero(car.CreatedAt) 60 61 validationLogs = []string{} 62 car = &ValidatableCar{Name: ""} 63 verrs, err = tx.ValidateAndSave(car) 64 r.NoError(err) 65 r.True(verrs.HasAny()) 66 r.Len(validationLogs, 2) 67 errs := verrs.Get("name") 68 r.Len(errs, 1) 69 70 validationLogs = []string{} 71 ncar := &NotValidatableCar{Name: ""} 72 verrs, err = tx.ValidateAndSave(ncar) 73 r.NoError(err) 74 r.False(verrs.HasAny()) 75 r.Len(validationLogs, 0) 76 }) 77 } 78 79 func Test_ValidateAndSave_With_Slice(t *testing.T) { 80 if PDB == nil { 81 t.Skip("skipping integration tests") 82 } 83 r := require.New(t) 84 validationLogs = []string{} 85 transaction(func(tx *Connection) { 86 car := []ValidatableCar{ 87 {Name: "VW"}, 88 {Name: "AU"}, 89 } 90 verrs, err := tx.ValidateAndSave(&car) 91 r.NoError(err) 92 r.False(verrs.HasAny()) 93 r.Len(validationLogs, 4) 94 r.Equal([]string{"Validate", "ValidateSave", "Validate", "ValidateSave"}, validationLogs) 95 96 r.NotZero(car[0].ID) 97 r.NotZero(car[0].CreatedAt) 98 r.NotZero(car[1].ID) 99 r.NotZero(car[1].CreatedAt) 100 101 validationLogs = []string{} 102 car = []ValidatableCar{ 103 {Name: ""}, 104 {Name: "AU"}, 105 } 106 verrs, err = tx.ValidateAndSave(&car) 107 r.NoError(err) 108 r.True(verrs.HasAny()) 109 r.Len(validationLogs, 2) 110 errs := verrs.Get("name") 111 r.Len(errs, 1) 112 113 validationLogs = []string{} 114 ncar := []NotValidatableCar{ 115 {Name: ""}, 116 {Name: "AU"}, 117 } 118 verrs, err = tx.ValidateAndSave(&ncar) 119 r.NoError(err) 120 r.False(verrs.HasAny()) 121 r.Len(validationLogs, 0) 122 }) 123 } 124 125 func Test_ValidateAndCreate(t *testing.T) { 126 if PDB == nil { 127 t.Skip("skipping integration tests") 128 } 129 r := require.New(t) 130 validationLogs = []string{} 131 transaction(func(tx *Connection) { 132 car := &ValidatableCar{Name: "VW"} 133 verrs, err := tx.ValidateAndCreate(car) 134 r.NoError(err) 135 r.False(verrs.HasAny()) 136 r.Len(validationLogs, 2) 137 r.Equal([]string{"Validate", "ValidateCreate"}, validationLogs) 138 r.NotZero(car.ID) 139 r.NotZero(car.CreatedAt) 140 141 validationLogs = []string{} 142 car = &ValidatableCar{Name: ""} 143 verrs, err = tx.ValidateAndSave(car) 144 r.NoError(err) 145 r.True(verrs.HasAny()) 146 r.Len(validationLogs, 2) 147 errs := verrs.Get("name") 148 r.Len(errs, 1) 149 150 validationLogs = []string{} 151 ncar := &NotValidatableCar{Name: ""} 152 verrs, err = tx.ValidateAndCreate(ncar) 153 r.NoError(err) 154 r.False(verrs.HasAny()) 155 r.Len(validationLogs, 0) 156 }) 157 } 158 159 func Test_Create_Single_Incremental_ID(t *testing.T) { 160 if PDB == nil { 161 t.Skip("skipping integration tests") 162 } 163 r := require.New(t) 164 validationLogs = []string{} 165 transaction(func(tx *Connection) { 166 singleID := &SingleID{} 167 err := tx.Create(singleID) 168 r.NoError(err) 169 r.NotZero(singleID.ID) 170 }) 171 } 172 173 func Test_ValidateAndCreate_With_Slice(t *testing.T) { 174 if PDB == nil { 175 t.Skip("skipping integration tests") 176 } 177 r := require.New(t) 178 validationLogs = []string{} 179 transaction(func(tx *Connection) { 180 car := []ValidatableCar{ 181 {Name: "VW"}, 182 {Name: "AU"}, 183 } 184 verrs, err := tx.ValidateAndCreate(&car) 185 r.NoError(err) 186 r.False(verrs.HasAny()) 187 r.Len(validationLogs, 4) 188 r.Equal([]string{"Validate", "ValidateCreate", "Validate", "ValidateCreate"}, validationLogs) 189 r.NotZero(car[0].ID) 190 r.NotZero(car[0].CreatedAt) 191 r.NotZero(car[1].ID) 192 r.NotZero(car[1].CreatedAt) 193 194 validationLogs = []string{} 195 car = []ValidatableCar{ 196 {Name: ""}, 197 {Name: "AU"}, 198 } 199 verrs, err = tx.ValidateAndSave(&car) 200 r.NoError(err) 201 r.True(verrs.HasAny()) 202 r.Len(validationLogs, 2) 203 errs := verrs.Get("name") 204 r.Len(errs, 1) 205 206 validationLogs = []string{} 207 ncar := []NotValidatableCar{ 208 {Name: ""}, 209 {Name: "AU"}, 210 } 211 verrs, err = tx.ValidateAndCreate(ncar) 212 r.NoError(err) 213 r.False(verrs.HasAny()) 214 r.Len(validationLogs, 0) 215 }) 216 } 217 218 func Test_ValidateAndUpdate(t *testing.T) { 219 if PDB == nil { 220 t.Skip("skipping integration tests") 221 } 222 r := require.New(t) 223 validationLogs = []string{} 224 transaction(func(tx *Connection) { 225 car := &ValidatableCar{Name: "VW"} 226 verrs, err := tx.ValidateAndCreate(car) 227 r.NoError(err) 228 r.False(verrs.HasAny()) 229 r.Len(validationLogs, 2) 230 r.Equal([]string{"Validate", "ValidateCreate"}, validationLogs) 231 r.NotZero(car.ID) 232 r.NotZero(car.CreatedAt) 233 234 validationLogs = []string{} 235 car.Name = "" 236 verrs, err = tx.ValidateAndUpdate(car) 237 r.NoError(err) 238 r.True(verrs.HasAny()) 239 r.Len(validationLogs, 2) 240 errs := verrs.Get("name") 241 r.Len(errs, 1) 242 243 validationLogs = []string{} 244 ncar := &NotValidatableCar{Name: ""} 245 verrs, err = tx.ValidateAndCreate(ncar) 246 r.NoError(err) 247 r.False(verrs.HasAny()) 248 r.Len(validationLogs, 0) 249 250 validationLogs = []string{} 251 ncar.Name = "" 252 verrs, err = tx.ValidateAndUpdate(ncar) 253 r.NoError(err) 254 r.False(verrs.HasAny()) 255 r.Len(validationLogs, 0) 256 }) 257 } 258 259 func Test_ValidateAndUpdate_With_Slice(t *testing.T) { 260 if PDB == nil { 261 t.Skip("skipping integration tests") 262 } 263 r := require.New(t) 264 validationLogs = []string{} 265 transaction(func(tx *Connection) { 266 car := []ValidatableCar{ 267 {Name: "VW"}, 268 {Name: "AU"}, 269 } 270 verrs, err := tx.ValidateAndCreate(&car) 271 r.NoError(err) 272 r.False(verrs.HasAny()) 273 r.Len(validationLogs, 4) 274 r.Equal([]string{"Validate", "ValidateCreate", "Validate", "ValidateCreate"}, validationLogs) 275 r.NotZero(car[0].ID) 276 r.NotZero(car[0].CreatedAt) 277 r.NotZero(car[1].ID) 278 r.NotZero(car[1].CreatedAt) 279 280 validationLogs = []string{} 281 car[0].Name = "" 282 verrs, err = tx.ValidateAndUpdate(&car) 283 r.NoError(err) 284 r.True(verrs.HasAny()) 285 r.Len(validationLogs, 2) 286 errs := verrs.Get("name") 287 r.Len(errs, 1) 288 289 validationLogs = []string{} 290 ncar := []NotValidatableCar{ 291 {Name: ""}, 292 {Name: "AU"}, 293 } 294 verrs, err = tx.ValidateAndCreate(&ncar) 295 r.NoError(err) 296 r.False(verrs.HasAny()) 297 r.Len(validationLogs, 0) 298 299 validationLogs = []string{} 300 ncar[1].Name = "" 301 verrs, err = tx.ValidateAndUpdate(&ncar) 302 r.NoError(err) 303 r.False(verrs.HasAny()) 304 r.Len(validationLogs, 0) 305 }) 306 } 307 308 func Test_Exec(t *testing.T) { 309 if PDB == nil { 310 t.Skip("skipping integration tests") 311 } 312 transaction(func(tx *Connection) { 313 r := require.New(t) 314 315 user := User{Name: nulls.NewString("Mark 'Awesome' Bates")} 316 tx.Create(&user) 317 318 ctx, _ := tx.Count(user) 319 r.Equal(1, ctx) 320 321 q := tx.RawQuery("delete from users where id = ?", user.ID) 322 err := q.Exec() 323 r.NoError(err) 324 325 ctx, _ = tx.Count(user) 326 r.Equal(0, ctx) 327 }) 328 } 329 330 func Test_ExecCount(t *testing.T) { 331 if PDB == nil { 332 t.Skip("skipping integration tests") 333 } 334 transaction(func(tx *Connection) { 335 r := require.New(t) 336 337 user := User{Name: nulls.NewString("Mark 'Awesome' Bates")} 338 tx.Create(&user) 339 340 ctx, _ := tx.Count(user) 341 r.Equal(1, ctx) 342 343 q := tx.RawQuery("delete from users where id = ?", user.ID) 344 count, err := q.ExecWithCount() 345 r.NoError(err) 346 347 r.Equal(1, count) 348 349 ctx, _ = tx.Count(user) 350 r.Equal(0, ctx) 351 }) 352 } 353 354 func Test_Save(t *testing.T) { 355 if PDB == nil { 356 t.Skip("skipping integration tests") 357 } 358 r := require.New(t) 359 transaction(func(tx *Connection) { 360 u := &User{Name: nulls.NewString("Mark")} 361 r.Zero(u.ID) 362 r.NoError(tx.Save(u)) 363 r.NotZero(u.ID) 364 365 uat := u.UpdatedAt.UnixNano() 366 367 r.NoError(tx.Save(u)) 368 time.Sleep(1 * time.Second) 369 r.NotEqual(uat, u.UpdatedAt.UnixNano()) 370 }) 371 } 372 373 func Test_Save_With_Slice(t *testing.T) { 374 if PDB == nil { 375 t.Skip("skipping integration tests") 376 } 377 r := require.New(t) 378 transaction(func(tx *Connection) { 379 u := Users{ 380 {Name: nulls.NewString("Mark")}, 381 {Name: nulls.NewString("Larry")}, 382 } 383 r.Zero(u[0].ID) 384 r.Zero(u[1].ID) 385 386 r.NoError(tx.Save(&u)) 387 r.NotZero(u[0].ID) 388 r.NotZero(u[1].ID) 389 390 uat := u[0].UpdatedAt.UnixNano() 391 392 r.NoError(tx.Save(u)) 393 r.NotEqual(uat, u[0].UpdatedAt.UnixNano()) 394 }) 395 } 396 397 func Test_Create(t *testing.T) { 398 if PDB == nil { 399 t.Skip("skipping integration tests") 400 } 401 transaction(func(tx *Connection) { 402 r := require.New(t) 403 404 count, _ := tx.Count(&User{}) 405 user := User{Name: nulls.NewString("Mark 'Awesome' Bates")} 406 err := tx.Create(&user) 407 r.NoError(err) 408 r.NotEqual(0, user.ID) 409 410 ctx, _ := tx.Count(&User{}) 411 r.Equal(count+1, ctx) 412 413 u := User{} 414 q := tx.Where("name = ?", "Mark 'Awesome' Bates") 415 err = q.First(&u) 416 r.NoError(err) 417 r.Equal("Mark 'Awesome' Bates", user.Name.String) 418 }) 419 } 420 421 func Test_Create_stringID(t *testing.T) { 422 if PDB == nil { 423 t.Skip("skipping integration tests") 424 } 425 transaction(func(tx *Connection) { 426 r := require.New(t) 427 428 count, err := tx.Count(&Label{}) 429 r.NoError(err) 430 label := Label{ID: "red"} 431 err = tx.Create(&label) 432 r.NoError(err) 433 r.Equal("red", label.ID) 434 435 ctx, err := tx.Count(&Label{}) 436 r.NoError(err) 437 r.Equal(count+1, ctx) 438 439 l := Label{} 440 err = tx.Find(&l, "red") 441 r.NoError(err) 442 r.Equal("red", l.ID) 443 }) 444 } 445 446 func Test_Create_With_Slice(t *testing.T) { 447 if PDB == nil { 448 t.Skip("skipping integration tests") 449 } 450 transaction(func(tx *Connection) { 451 r := require.New(t) 452 453 count, _ := tx.Count(&User{}) 454 users := Users{ 455 {Name: nulls.NewString("Mark Bates")}, 456 {Name: nulls.NewString("Larry M. Jordan")}, 457 {Name: nulls.NewString("Pop")}, 458 } 459 err := tx.Create(&users) 460 r.NoError(err) 461 462 ctx, _ := tx.Count(&User{}) 463 r.Equal(count+3, ctx) 464 }) 465 } 466 467 func Test_Eager_Create_Has_Many(t *testing.T) { 468 if PDB == nil { 469 t.Skip("skipping integration tests") 470 } 471 transaction(func(tx *Connection) { 472 r := require.New(t) 473 count, _ := tx.Count(&User{}) 474 user := User{ 475 Name: nulls.NewString("Mark 'Awesome' Bates"), 476 Books: Books{{Title: "Pop Book", Description: "Pop Book", Isbn: "PB1"}}, 477 FavoriteSong: Song{Title: "Hook - Blues Traveler"}, 478 Houses: Addresses{ 479 Address{HouseNumber: 86, Street: "Modelo"}, 480 }, 481 } 482 483 err := tx.Eager().Create(&user) 484 r.NoError(err) 485 r.NotEqual(user.ID, 0) 486 487 ctx, _ := tx.Count(&User{}) 488 r.Equal(count+1, ctx) 489 490 ctx, _ = tx.Count(&Book{}) 491 r.Equal(count+1, ctx) 492 493 ctx, _ = tx.Count(&Song{}) 494 r.Equal(count+1, ctx) 495 496 ctx, _ = tx.Count(&Address{}) 497 r.Equal(count+1, ctx) 498 499 u := User{} 500 q := tx.Eager().Where("name = ?", "Mark 'Awesome' Bates") 501 err = q.First(&u) 502 r.NoError(err) 503 r.Equal(u.Name.String, "Mark 'Awesome' Bates") 504 r.Equal(1, len(u.Books)) 505 r.Equal(u.Books[0].Title, "Pop Book") 506 r.Equal(u.FavoriteSong.Title, "Hook - Blues Traveler") 507 r.Equal(1, len(u.Houses)) 508 r.Equal(u.Houses[0].Street, "Modelo") 509 }) 510 } 511 512 func Test_Eager_Create_Has_Many_With_Existing(t *testing.T) { 513 if PDB == nil { 514 t.Skip("skipping integration tests") 515 } 516 transaction(func(tx *Connection) { 517 r := require.New(t) 518 519 addr := Address{HouseNumber: 42, Street: "Life"} 520 addrVerrs, addrErr := tx.ValidateAndCreate(&addr) 521 r.NoError(addrErr) 522 addrCount, _ := tx.Count(&Address{}) 523 r.Zero(addrVerrs.Count()) 524 r.Equal(1, addrCount) 525 r.NotZero(addr.ID) 526 527 count, _ := tx.Count(&User{}) 528 user := User{ 529 Name: nulls.NewString("Mark 'Awesome' Bates"), 530 Books: Books{{Title: "Pop Book", Description: "Pop Book", Isbn: "PB1"}}, 531 FavoriteSong: Song{Title: "Hook - Blues Traveler"}, 532 Houses: Addresses{ 533 Address{HouseNumber: 86, Street: "Modelo"}, 534 addr, 535 }, 536 } 537 538 err := tx.Eager().Create(&user) 539 r.NoError(err) 540 r.NotEqual(user.ID, 0) 541 542 ctx, _ := tx.Count(&User{}) 543 r.Equal(count+1, ctx) 544 545 ctx, _ = tx.Count(&Book{}) 546 r.Equal(count+1, ctx) 547 548 ctx, _ = tx.Count(&Song{}) 549 r.Equal(count+1, ctx) 550 551 ctx, _ = tx.Count(&Address{}) 552 r.Equal(addrCount+1, ctx) 553 554 u := User{} 555 q := tx.Eager().Where("name = ?", "Mark 'Awesome' Bates") 556 err = q.First(&u) 557 r.NoError(err) 558 r.Equal(u.Name.String, "Mark 'Awesome' Bates") 559 r.Equal(1, len(u.Books)) 560 r.Equal(u.Books[0].Title, "Pop Book") 561 r.Equal(u.FavoriteSong.Title, "Hook - Blues Traveler") 562 r.Equal(2, len(u.Houses)) 563 if u.Houses[0].ID == addr.ID { 564 r.Equal(u.Houses[0].Street, "Life") 565 r.Equal(u.Houses[1].Street, "Modelo") 566 } else { 567 r.Equal(u.Houses[0].Street, "Modelo") 568 r.Equal(u.Houses[1].Street, "Life") 569 } 570 }) 571 } 572 573 func Test_Eager_Create_Has_Many_Reset_Eager_Mode_Connection(t *testing.T) { 574 if PDB == nil { 575 t.Skip("skipping integration tests") 576 } 577 transaction(func(tx *Connection) { 578 r := require.New(t) 579 count, _ := tx.Count(&User{}) 580 user1 := User{ 581 Name: nulls.NewString("Mark 'Awesome' Bates"), 582 Books: Books{{Title: "Pop Book", Description: "Pop Book", Isbn: "PB1"}}, 583 } 584 585 err := tx.Eager("Books").Create(&user1) 586 r.NoError(err) 587 ctx, _ := tx.Count(&User{}) 588 r.Equal(count+1, ctx) 589 ctx, _ = tx.Count(&Book{}) 590 r.Equal(count+1, ctx) 591 592 book := Book{Title: "Pop Book", Description: "Pop Book", Isbn: "PB1"} 593 594 err = tx.Eager().Create(&book) 595 r.NoError(err) 596 ctx, _ = tx.Count(&Book{}) 597 r.Equal(count+2, ctx) 598 }) 599 } 600 601 func Test_Eager_Validate_And_Create_Has_Many(t *testing.T) { 602 if PDB == nil { 603 t.Skip("skipping integration tests") 604 } 605 r := require.New(t) 606 transaction(func(tx *Connection) { 607 user := User{ 608 Name: nulls.NewString("Mark 'Awesome' Bates"), 609 Books: Books{{Title: "Pop Book", Isbn: "PB1"}}, 610 FavoriteSong: Song{Title: "Hook - Blues Traveler"}, 611 Houses: Addresses{ 612 Address{HouseNumber: 86, Street: "Modelo"}, 613 }, 614 } 615 616 verrs, err := tx.Eager().ValidateAndCreate(&user) 617 r.NoError(err) 618 ctx, _ := tx.Count(&User{}) 619 r.Zero(ctx) 620 r.Equal(1, verrs.Count()) // Missing Books.Description. 621 }) 622 } 623 624 func Test_Eager_Validate_And_Create_Parental(t *testing.T) { 625 if PDB == nil { 626 t.Skip("skipping integration tests") 627 } 628 r := require.New(t) 629 transaction(func(tx *Connection) { 630 user := User{ 631 Name: nulls.NewString(""), 632 Books: Books{{Title: "Pop Book", Isbn: "PB1", Description: "Awesome Book!"}}, 633 FavoriteSong: Song{Title: "Hook - Blues Traveler"}, 634 Houses: Addresses{ 635 Address{HouseNumber: 86, Street: "Modelo"}, 636 }, 637 } 638 639 verrs, err := tx.Eager().ValidateAndCreate(&user) 640 r.NoError(err) 641 ctx, _ := tx.Count(&User{}) 642 r.Zero(ctx) 643 r.Equal(1, verrs.Count()) // Missing Books.Description. 644 }) 645 } 646 647 func Test_Eager_Validate_And_Create_Parental_With_Existing(t *testing.T) { 648 if PDB == nil { 649 t.Skip("skipping integration tests") 650 } 651 r := require.New(t) 652 transaction(func(tx *Connection) { 653 addr := Address{HouseNumber: 42, Street: "Life"} 654 addrVerrs, addrErr := tx.ValidateAndCreate(&addr) 655 r.NoError(addrErr) 656 addrCount, _ := tx.Count(&Address{}) 657 r.Zero(addrVerrs.Count()) 658 r.Equal(1, addrCount) 659 r.NotZero(addr.ID) 660 661 m2mCount, m2mErr := tx.Count(&UsersAddress{}) 662 r.NoError(m2mErr) 663 r.Zero(m2mCount) 664 665 user := User{ 666 Name: nulls.NewString("Mark 'Awesome' Bates"), 667 Books: Books{{Title: "Pop Book", Isbn: "PB1", Description: "Awesome Book!"}}, 668 FavoriteSong: Song{Title: "Hook - Blues Traveler"}, 669 Houses: Addresses{ 670 Address{HouseNumber: 86, Street: "Modelo"}, 671 addr, 672 }, 673 } 674 count, _ := tx.Count(&User{}) 675 676 verrs, err := tx.Eager().ValidateAndCreate(&user) 677 r.NoError(err) 678 r.NotEqual(user.ID, 0) 679 r.Equal(0, verrs.Count()) 680 681 ctx, _ := tx.Count(&User{}) 682 r.Equal(count+1, ctx) 683 684 ctx, _ = tx.Count(&Address{}) 685 r.Equal(addrCount+1, ctx) 686 687 m2mCount, m2mErr = tx.Count(&UsersAddress{}) 688 r.NoError(m2mErr) 689 r.Equal(2, m2mCount) 690 691 u := User{} 692 q := tx.Eager().Where("name = ?", "Mark 'Awesome' Bates") 693 err = q.First(&u) 694 r.NoError(err) 695 r.Equal(u.Name.String, "Mark 'Awesome' Bates") 696 r.Equal(1, len(u.Books)) 697 r.Equal(u.Books[0].Title, "Pop Book") 698 r.Equal(u.FavoriteSong.Title, "Hook - Blues Traveler") 699 r.Equal(2, len(u.Houses)) 700 if u.Houses[0].ID == addr.ID { 701 r.Equal(u.Houses[0].Street, "Life") 702 r.Equal(u.Houses[1].Street, "Modelo") 703 } else { 704 r.Equal(u.Houses[1].ID, addr.ID) 705 r.Equal(u.Houses[0].Street, "Modelo") 706 r.Equal(u.Houses[1].Street, "Life") 707 } 708 }) 709 } 710 711 func Test_Eager_Validate_And_Create_Parental_With_Partial_Existing(t *testing.T) { 712 if PDB == nil { 713 t.Skip("skipping integration tests") 714 } 715 r := require.New(t) 716 transaction(func(tx *Connection) { 717 addr := Address{HouseNumber: 42, Street: "Life"} 718 addrVerrs, addrErr := tx.ValidateAndCreate(&addr) 719 r.NoError(addrErr) 720 addrCount, _ := tx.Count(&Address{}) 721 r.Zero(addrVerrs.Count()) 722 r.Equal(1, addrCount) 723 r.NotZero(addr.ID) 724 725 m2mCount, m2mErr := tx.Count(&UsersAddress{}) 726 r.NoError(m2mErr) 727 r.Zero(m2mCount) 728 729 user := User{ 730 Name: nulls.NewString("Mark 'Awesome' Bates"), 731 Books: Books{{Title: "Pop Book", Isbn: "PB1", Description: "Awesome Book!"}}, 732 FavoriteSong: Song{Title: "Hook - Blues Traveler"}, 733 Houses: Addresses{ 734 Address{HouseNumber: 86, Street: "Modelo"}, 735 Address{ID: addr.ID}, 736 }, 737 } 738 count, _ := tx.Count(&User{}) 739 740 verrs, err := tx.Eager().ValidateAndCreate(&user) 741 r.NoError(err) 742 r.NotEqual(user.ID, 0) 743 r.Equal(0, verrs.Count()) 744 745 ctx, _ := tx.Count(&User{}) 746 r.Equal(count+1, ctx) 747 748 ctx, _ = tx.Count(&Address{}) 749 r.Equal(addrCount+1, ctx) 750 751 m2mCount, m2mErr = tx.Count(&UsersAddress{}) 752 r.NoError(m2mErr) 753 r.Equal(2, m2mCount) 754 755 u := User{} 756 q := tx.Eager().Where("name = ?", "Mark 'Awesome' Bates") 757 err = q.First(&u) 758 r.NoError(err) 759 r.Equal(u.Name.String, "Mark 'Awesome' Bates") 760 r.Equal(1, len(u.Books)) 761 r.Equal(u.Books[0].Title, "Pop Book") 762 r.Equal(u.FavoriteSong.Title, "Hook - Blues Traveler") 763 r.Equal(2, len(u.Houses)) 764 if u.Houses[0].ID == addr.ID { 765 r.Equal("Life", u.Houses[0].Street) // Street is blanked out 766 r.Equal("Modelo", u.Houses[1].Street) 767 } else { 768 r.Equal(addr.ID, u.Houses[1].ID) 769 r.Equal("Modelo", u.Houses[0].Street) 770 r.Equal("Life", u.Houses[1].Street) // Street is blanked out 771 } 772 }) 773 } 774 775 func Test_Flat_Validate_And_Create_Parental_With_Existing(t *testing.T) { 776 if PDB == nil { 777 t.Skip("skipping integration tests") 778 } 779 r := require.New(t) 780 transaction(func(tx *Connection) { 781 addr := Address{HouseNumber: 42, Street: "Life"} 782 addrVerrs, addrErr := tx.ValidateAndCreate(&addr) 783 r.NoError(addrErr) 784 addrCount, _ := tx.Count(&Address{}) 785 r.Zero(addrVerrs.Count()) 786 r.Equal(1, addrCount) 787 r.NotZero(addr.ID) 788 789 book := Book{Title: "Pop Book", Isbn: "PB1", Description: "Awesome Book!"} 790 bookVerrs, bookErr := tx.ValidateAndCreate(&book) 791 r.NoError(bookErr) 792 r.Zero(bookVerrs.Count()) 793 r.NotZero(book.ID) 794 795 book2 := Book{Title: "Pop Book2", Isbn: "PB2", Description: "Awesome Book Also!"} 796 bookVerrs, bookErr = tx.ValidateAndCreate(&book2) 797 r.NoError(bookErr) 798 r.Zero(bookVerrs.Count()) 799 r.NotZero(book2.ID) 800 801 bookCount, _ := tx.Count(&Book{}) 802 r.Equal(2, bookCount) 803 804 song := Song{Title: "Hook - Blues Traveler"} 805 songVerrs, songErr := tx.ValidateAndCreate(&song) 806 r.NoError(songErr) 807 songCount, _ := tx.Count(&Song{}) 808 r.Zero(songVerrs.Count()) 809 r.Equal(1, songCount) 810 r.NotZero(song.ID) 811 812 m2mCount, m2mErr := tx.Count(&UsersAddress{}) 813 r.NoError(m2mErr) 814 r.Zero(m2mCount) 815 816 user := User{ 817 Name: nulls.NewString("Mark 'Awesome' Bates"), 818 Books: Books{book, book2}, 819 FavoriteSong: song, 820 Houses: Addresses{ 821 Address{HouseNumber: 86, Street: "Modelo"}, 822 addr, 823 }, 824 } 825 count, _ := tx.Count(&User{}) 826 827 verrs, err := tx.ValidateAndCreate(&user) 828 r.NoError(err) 829 r.NotEqual(user.ID, 0) 830 r.Equal(0, verrs.Count()) 831 832 ctx, _ := tx.Count(&User{}) 833 r.Equal(count+1, ctx) 834 835 ctx, _ = tx.Count(&Address{}) 836 r.Equal(addrCount, ctx) 837 838 ctx, _ = tx.Count(&Book{}) 839 r.Equal(bookCount, ctx) 840 841 ctx, _ = tx.Count(&Song{}) 842 r.Equal(songCount, ctx) 843 844 m2mCount, m2mErr = tx.Count(&UsersAddress{}) 845 r.NoError(m2mErr) 846 r.Equal(1, m2mCount) 847 848 u := User{} 849 q := tx.Eager().Where("name = ?", "Mark 'Awesome' Bates") 850 err = q.First(&u) 851 r.NoError(err) 852 r.Equal(u.Name.String, "Mark 'Awesome' Bates") 853 r.Equal(2, len(u.Books)) 854 if u.Books[0].ID == book.ID { 855 r.Equal(u.Books[0].Title, "Pop Book") 856 r.Equal(u.Books[1].Title, "Pop Book2") 857 } else { 858 r.Equal(u.Books[1].Title, "Pop Book") 859 r.Equal(u.Books[0].Title, "Pop Book2") 860 } 861 r.Equal(u.FavoriteSong.Title, "Hook - Blues Traveler") 862 r.Equal(1, len(u.Houses)) 863 r.Equal(addr.ID, u.Houses[0].ID) 864 r.Equal("Life", u.Houses[0].Street) 865 }) 866 } 867 868 func Test_Flat_Validate_And_Create_Parental_With_Partial_Existing(t *testing.T) { 869 if PDB == nil { 870 t.Skip("skipping integration tests") 871 } 872 r := require.New(t) 873 transaction(func(tx *Connection) { 874 addr := Address{HouseNumber: 42, Street: "Life"} 875 addrVerrs, addrErr := tx.ValidateAndCreate(&addr) 876 r.NoError(addrErr) 877 addrCount, _ := tx.Count(&Address{}) 878 r.Zero(addrVerrs.Count()) 879 r.Equal(1, addrCount) 880 r.NotZero(addr.ID) 881 882 book := Book{Title: "Pop Book", Isbn: "PB1", Description: "Awesome Book!"} 883 bookVerrs, bookErr := tx.ValidateAndCreate(&book) 884 r.NoError(bookErr) 885 bookCount, _ := tx.Count(&Book{}) 886 r.Zero(bookVerrs.Count()) 887 r.Equal(1, bookCount) 888 r.NotZero(book.ID) 889 890 song := Song{Title: "Hook - Blues Traveler"} 891 songVerrs, songErr := tx.ValidateAndCreate(&song) 892 r.NoError(songErr) 893 songCount, _ := tx.Count(&Song{}) 894 r.Zero(songVerrs.Count()) 895 r.Equal(1, songCount) 896 r.NotZero(song.ID) 897 898 m2mCount, m2mErr := tx.Count(&UsersAddress{}) 899 r.NoError(m2mErr) 900 r.Zero(m2mCount) 901 902 user := User{ 903 Name: nulls.NewString("Mark 'Awesome' Bates"), 904 //TODO: add another existing here and test for it to make sure this works with multiples (books) 905 Books: Books{Book{ID: book.ID}}, 906 FavoriteSong: Song{ID: song.ID}, 907 Houses: Addresses{ 908 Address{HouseNumber: 86, Street: "Modelo"}, 909 Address{ID: addr.ID}, 910 }, 911 } 912 count, _ := tx.Count(&User{}) 913 914 verrs, err := tx.ValidateAndCreate(&user) 915 r.NoError(err) 916 r.NotEqual(user.ID, 0) 917 r.Equal(0, verrs.Count()) 918 919 ctx, _ := tx.Count(&User{}) 920 r.Equal(count+1, ctx) 921 922 ctx, _ = tx.Count(&Address{}) 923 r.Equal(addrCount, ctx) 924 925 ctx, _ = tx.Where("user_id = ?", user.ID).Count(&Book{}) 926 r.Equal(bookCount, ctx) 927 928 ctx, _ = tx.Count(&Song{}) 929 r.Equal(songCount, ctx) 930 931 m2mCount, m2mErr = tx.Count(&UsersAddress{}) 932 r.NoError(m2mErr) 933 r.Equal(1, m2mCount) 934 935 u := User{} 936 q := tx.Eager().Where("name = ?", "Mark 'Awesome' Bates") 937 err = q.First(&u) 938 r.NoError(err) 939 r.Equal(u.Name.String, "Mark 'Awesome' Bates") 940 r.Equal(1, len(u.Books)) 941 r.Equal(u.Books[0].Title, "Pop Book") 942 r.Equal(u.FavoriteSong.Title, "Hook - Blues Traveler") 943 r.Equal(1, len(u.Houses)) 944 r.Equal(addr.ID, u.Houses[0].ID) 945 r.Equal("Life", u.Houses[0].Street) 946 }) 947 } 948 949 func Test_Eager_Create_Belongs_To(t *testing.T) { 950 if PDB == nil { 951 t.Skip("skipping integration tests") 952 } 953 transaction(func(tx *Connection) { 954 r := require.New(t) 955 book := Book{ 956 Title: "Pop Book", 957 Description: "Pop Book", 958 Isbn: "PB1", 959 User: User{ 960 Name: nulls.NewString("Larry"), 961 }, 962 } 963 964 err := tx.Eager().Create(&book) 965 r.NoError(err) 966 967 ctx, _ := tx.Count(&Book{}) 968 r.Equal(1, ctx) 969 970 ctx, _ = tx.Count(&User{}) 971 r.Equal(1, ctx) 972 973 car := Taxi{ 974 Model: "Fancy car", 975 Driver: User{ 976 Name: nulls.NewString("Larry 2"), 977 }, 978 } 979 980 err = tx.Eager().Create(&car) 981 r.NoError(err) 982 983 ctx, _ = tx.Count(&Taxi{}) 984 r.Equal(1, ctx) 985 986 err = tx.Eager().Find(&car, car.ID) 987 r.NoError(err) 988 989 r.Equal(nulls.NewString("Larry 2"), car.Driver.Name) 990 }) 991 } 992 993 func Test_Eager_Create_Belongs_To_Pointers(t *testing.T) { 994 if PDB == nil { 995 t.Skip("skipping integration tests") 996 } 997 transaction(func(tx *Connection) { 998 r := require.New(t) 999 // Create a body with a head 1000 body := Body{ 1001 Head: &Head{}, 1002 } 1003 1004 err := tx.Eager().Create(&body) 1005 r.NoError(err) 1006 r.NotZero(body.ID) 1007 r.NotZero(body.Head.ID) 1008 1009 ctx, _ := tx.Count(&Body{}) 1010 r.Equal(1, ctx) 1011 1012 ctx, _ = tx.Count(&Head{}) 1013 r.Equal(1, ctx) 1014 1015 // Create a body without a head: 1016 body = Body{ 1017 Head: nil, 1018 } 1019 1020 err = tx.Eager().Create(&body) 1021 r.NoError(err) 1022 r.NotZero(body.ID) 1023 r.Nil(body.Head) 1024 1025 ctx, _ = tx.Count(&Body{}) 1026 r.Equal(2, ctx) 1027 1028 ctx, _ = tx.Count(&Head{}) 1029 r.Equal(1, ctx) 1030 1031 err = tx.Eager().Create(&Head{ 1032 BodyID: body.ID, 1033 Body: nil, 1034 }) 1035 r.NoError(err) 1036 }) 1037 } 1038 1039 func Test_Create_Belongs_To_Pointers(t *testing.T) { 1040 if PDB == nil { 1041 t.Skip("skipping integration tests") 1042 } 1043 transaction(func(tx *Connection) { 1044 r := require.New(t) 1045 // Create a body without a head: 1046 body := Body{ 1047 Head: nil, 1048 } 1049 1050 err := tx.Create(&body) 1051 r.NoError(err) 1052 r.NotZero(body.ID) 1053 r.Nil(body.Head) 1054 1055 // Create a head with the associated model set but not the ID 1056 created := HeadPtr{ 1057 Body: &body, 1058 } 1059 err = tx.Create(&created) 1060 r.NoError(err) 1061 1062 found := HeadPtr{} 1063 err = tx.Find(&found, created.ID) 1064 r.NoError(err) 1065 r.Equal(body.ID, *found.BodyID) 1066 }) 1067 } 1068 1069 func Test_Flat_Create_Belongs_To(t *testing.T) { 1070 if PDB == nil { 1071 t.Skip("skipping integration tests") 1072 } 1073 transaction(func(tx *Connection) { 1074 r := require.New(t) 1075 user := User{ 1076 Name: nulls.NewString("Larry"), 1077 } 1078 1079 err := tx.Create(&user) 1080 r.NoError(err) 1081 ctx, _ := tx.Count(&User{}) 1082 r.Equal(1, ctx) 1083 1084 book := Book{ 1085 Title: "Pop Book", 1086 Description: "Pop Book", 1087 Isbn: "PB1", 1088 User: user, 1089 } 1090 1091 err = tx.Create(&book) 1092 r.NoError(err) 1093 1094 ctx, _ = tx.Count(&Book{}) 1095 r.Equal(1, ctx) 1096 1097 err = tx.Eager().Find(&book, book.ID) 1098 r.NoError(err) 1099 1100 r.Equal(nulls.NewString("Larry"), book.User.Name) 1101 1102 car := Taxi{ 1103 Model: "Fancy car", 1104 Driver: user, 1105 } 1106 1107 err = tx.Create(&car) 1108 r.NoError(err) 1109 1110 ctx, _ = tx.Count(&Taxi{}) 1111 r.Equal(1, ctx) 1112 1113 err = tx.Eager().Find(&car, car.ID) 1114 r.NoError(err) 1115 1116 r.Equal(nulls.NewString("Larry"), car.Driver.Name) 1117 }) 1118 } 1119 1120 func Test_Eager_Creation_Without_Associations(t *testing.T) { 1121 if PDB == nil { 1122 t.Skip("skipping integration tests") 1123 } 1124 transaction(func(tx *Connection) { 1125 r := require.New(t) 1126 code := CourseCode{ 1127 Course: Course{}, 1128 } 1129 1130 err := tx.Eager().Create(&code) 1131 r.NoError(err) 1132 1133 ctx, _ := tx.Count(&CourseCode{}) 1134 r.Equal(1, ctx) 1135 }) 1136 } 1137 1138 func Test_Create_UUID(t *testing.T) { 1139 if PDB == nil { 1140 t.Skip("skipping integration tests") 1141 } 1142 transaction(func(tx *Connection) { 1143 r := require.New(t) 1144 1145 count, _ := tx.Count(&Song{}) 1146 song := Song{Title: "Automatic Buffalo"} 1147 err := tx.Create(&song) 1148 r.NoError(err) 1149 r.NotZero(song.ID) 1150 1151 ctx, _ := tx.Count(&Song{}) 1152 r.Equal(count+1, ctx) 1153 1154 u := Song{} 1155 q := tx.Where("title = ?", "Automatic Buffalo") 1156 err = q.First(&u) 1157 r.NoError(err) 1158 }) 1159 } 1160 1161 func Test_Create_Existing_UUID(t *testing.T) { 1162 if PDB == nil { 1163 t.Skip("skipping integration tests") 1164 } 1165 transaction(func(tx *Connection) { 1166 r := require.New(t) 1167 id, err := uuid.NewV4() 1168 r.NoError(err) 1169 1170 count, _ := tx.Count(&Song{}) 1171 song := Song{ 1172 ID: id, 1173 Title: "Automatic Buffalo", 1174 } 1175 1176 err = tx.Create(&song) 1177 r.NoError(err) 1178 r.NotZero(song.ID) 1179 r.Equal(id.String(), song.ID.String()) 1180 1181 ctx, _ := tx.Count(&Song{}) 1182 r.Equal(count+1, ctx) 1183 1184 }) 1185 } 1186 1187 func Test_Create_Timestamps(t *testing.T) { 1188 if PDB == nil { 1189 t.Skip("skipping integration tests") 1190 } 1191 transaction(func(tx *Connection) { 1192 r := require.New(t) 1193 1194 user := User{Name: nulls.NewString("Mark 'Awesome' Bates")} 1195 r.Zero(user.CreatedAt) 1196 r.Zero(user.UpdatedAt) 1197 1198 err := tx.Create(&user) 1199 r.NoError(err) 1200 1201 r.NotZero(user.CreatedAt) 1202 r.NotZero(user.UpdatedAt) 1203 1204 friend := Friend{FirstName: "Ross", LastName: "Gellar"} 1205 err = tx.Create(&friend) 1206 r.NoError(err) 1207 }) 1208 } 1209 1210 func Test_Update(t *testing.T) { 1211 if PDB == nil { 1212 t.Skip("skipping integration tests") 1213 } 1214 transaction(func(tx *Connection) { 1215 r := require.New(t) 1216 1217 user := User{Name: nulls.NewString("Mark")} 1218 tx.Create(&user) 1219 1220 r.NotZero(user.CreatedAt) 1221 r.NotZero(user.UpdatedAt) 1222 1223 user.Name.String = "Marky" 1224 err := tx.Update(&user) 1225 r.NoError(err) 1226 1227 r.NoError(tx.Reload(&user)) 1228 r.Equal(user.Name.String, "Marky") 1229 }) 1230 } 1231 1232 func Test_UpdateColumns(t *testing.T) { 1233 if PDB == nil { 1234 t.Skip("skipping integration tests") 1235 } 1236 transaction(func(tx *Connection) { 1237 r := require.New(t) 1238 1239 user := User{Name: nulls.NewString("Mark")} 1240 tx.Create(&user) 1241 1242 r.NotZero(user.CreatedAt) 1243 r.NotZero(user.UpdatedAt) 1244 1245 user.Name.String = "Fulano" 1246 user.UserName = "Fulano" 1247 err := tx.UpdateColumns(&user, "user_name") // Update UserName field/column only 1248 r.NoError(err) 1249 1250 r.NoError(tx.Reload(&user)) 1251 r.Equal(user.Name.String, "Mark") // Name column should not be updated 1252 r.Equal(user.UserName, "Fulano") 1253 }) 1254 } 1255 1256 func Test_UpdateColumns_UpdatedAt(t *testing.T) { 1257 if PDB == nil { 1258 t.Skip("skipping integration tests") 1259 } 1260 transaction(func(tx *Connection) { 1261 r := require.New(t) 1262 1263 user := User{Name: nulls.NewString("Foo")} 1264 tx.Create(&user) 1265 1266 r.NotZero(user.CreatedAt) 1267 r.NotZero(user.UpdatedAt) 1268 updatedAtBefore := user.UpdatedAt 1269 1270 user.Name.String = "Bar" 1271 err := tx.UpdateColumns(&user, "name", "updated_at") // Update name and updated_at 1272 r.NoError(err) 1273 1274 r.NoError(tx.Reload(&user)) 1275 r.NotEqual(user.UpdatedAt, updatedAtBefore) // UpdatedAt should be updated automatically 1276 }) 1277 } 1278 1279 func Test_UpdateColumns_MultipleColumns(t *testing.T) { 1280 if PDB == nil { 1281 t.Skip("skipping integration tests") 1282 } 1283 transaction(func(tx *Connection) { 1284 r := require.New(t) 1285 1286 user := User{Name: nulls.NewString("Mark"), UserName: "Sagan", Email: "test@example.com"} 1287 tx.Create(&user) 1288 1289 r.NotZero(user.CreatedAt) 1290 r.NotZero(user.UpdatedAt) 1291 1292 user.Name.String = "Ping" 1293 user.UserName = "Pong" 1294 user.Email = "fulano@example" 1295 err := tx.UpdateColumns(&user, "name", "user_name") // Update multiple columns 1296 r.NoError(err) 1297 1298 r.NoError(tx.Reload(&user)) 1299 r.Equal(user.Name.String, "Ping") 1300 r.Equal(user.UserName, "Pong") 1301 r.Equal(user.Email, "test@example.com") // Email should not be updated 1302 }) 1303 } 1304 1305 func Test_UpdateColumns_All(t *testing.T) { 1306 if PDB == nil { 1307 t.Skip("skipping integration tests") 1308 } 1309 transaction(func(tx *Connection) { 1310 r := require.New(t) 1311 1312 user := User{Name: nulls.NewString("Mark"), UserName: "Sagan"} 1313 tx.Create(&user) 1314 1315 r.NotZero(user.CreatedAt) 1316 r.NotZero(user.UpdatedAt) 1317 1318 user.Name.String = "Ping" 1319 user.UserName = "Pong" 1320 user.Email = "ping@pong.com" 1321 err := tx.UpdateColumns(&user) // Update all columns 1322 r.NoError(err) 1323 1324 r.NoError(tx.Reload(&user)) 1325 r.Equal(user.Name.String, "Ping") 1326 r.Equal(user.UserName, "Pong") 1327 r.Equal(user.Email, "ping@pong.com") 1328 }) 1329 } 1330 1331 func Test_UpdateColumns_With_Slice(t *testing.T) { 1332 if PDB == nil { 1333 t.Skip("skipping integration tests") 1334 } 1335 transaction(func(tx *Connection) { 1336 r := require.New(t) 1337 1338 user := Users{ 1339 { 1340 Name: nulls.NewString("Mark"), 1341 UserName: "Ping", 1342 }, 1343 { 1344 Name: nulls.NewString("Larry"), 1345 UserName: "Pong", 1346 }, 1347 } 1348 tx.Create(&user) 1349 1350 r.NotZero(user[0].CreatedAt) 1351 r.NotZero(user[0].UpdatedAt) 1352 1353 r.NotZero(user[1].CreatedAt) 1354 r.NotZero(user[1].UpdatedAt) 1355 1356 user[0].Name.String = "Fulano" 1357 user[0].UserName = "Thor" 1358 user[1].Name.String = "Fulana" 1359 user[1].UserName = "Freya" 1360 1361 err := tx.UpdateColumns(&user, "name") // Update Name field/column only 1362 r.NoError(err) 1363 1364 r.NoError(tx.Reload(&user)) 1365 r.Equal(user[0].Name.String, "Fulano") 1366 r.Equal(user[0].UserName, "Ping") // UserName should not be updated 1367 r.Equal(user[1].Name.String, "Fulana") 1368 r.Equal(user[1].UserName, "Pong") // UserName should not be updated 1369 }) 1370 } 1371 1372 func Test_Update_With_Slice(t *testing.T) { 1373 if PDB == nil { 1374 t.Skip("skipping integration tests") 1375 } 1376 transaction(func(tx *Connection) { 1377 r := require.New(t) 1378 1379 user := Users{ 1380 {Name: nulls.NewString("Mark")}, 1381 {Name: nulls.NewString("Larry")}, 1382 } 1383 tx.Create(&user) 1384 1385 r.NotZero(user[0].CreatedAt) 1386 r.NotZero(user[0].UpdatedAt) 1387 1388 r.NotZero(user[1].CreatedAt) 1389 r.NotZero(user[1].UpdatedAt) 1390 1391 user[0].Name.String = "Marky" 1392 user[1].Name.String = "Lawrence" 1393 1394 err := tx.Update(&user) 1395 r.NoError(err) 1396 1397 r.NoError(tx.Reload(&user)) 1398 r.Equal(user[0].Name.String, "Marky") 1399 r.Equal(user[1].Name.String, "Lawrence") 1400 }) 1401 } 1402 1403 func Test_Update_UUID(t *testing.T) { 1404 if PDB == nil { 1405 t.Skip("skipping integration tests") 1406 } 1407 transaction(func(tx *Connection) { 1408 r := require.New(t) 1409 1410 song := Song{Title: "Automatic Buffalo"} 1411 err := tx.Create(&song) 1412 r.NoError(err) 1413 1414 r.NotZero(song.CreatedAt) 1415 r.NotZero(song.UpdatedAt) 1416 1417 song.Title = "Hum" 1418 err = tx.Update(&song) 1419 r.NoError(err) 1420 1421 err = tx.Reload(&song) 1422 r.NoError(err) 1423 r.Equal("Hum", song.Title) 1424 }) 1425 } 1426 1427 func Test_Destroy(t *testing.T) { 1428 if PDB == nil { 1429 t.Skip("skipping integration tests") 1430 } 1431 transaction(func(tx *Connection) { 1432 r := require.New(t) 1433 1434 count, err := tx.Count("users") 1435 r.NoError(err) 1436 user := User{Name: nulls.NewString("Mark")} 1437 err = tx.Create(&user) 1438 r.NoError(err) 1439 r.NotEqual(user.ID, 0) 1440 1441 ctx, err := tx.Count("users") 1442 r.NoError(err) 1443 r.Equal(count+1, ctx) 1444 1445 err = tx.Destroy(&user) 1446 r.NoError(err) 1447 1448 ctx, _ = tx.Count("users") 1449 r.Equal(count, ctx) 1450 }) 1451 } 1452 1453 func Test_Destroy_With_Slice(t *testing.T) { 1454 if PDB == nil { 1455 t.Skip("skipping integration tests") 1456 } 1457 transaction(func(tx *Connection) { 1458 r := require.New(t) 1459 1460 count, err := tx.Count("users") 1461 r.NoError(err) 1462 user := Users{ 1463 {Name: nulls.NewString("Mark")}, 1464 {Name: nulls.NewString("Larry")}, 1465 } 1466 err = tx.Create(&user) 1467 r.NoError(err) 1468 r.NotEqual(user[0].ID, 0) 1469 r.NotEqual(user[1].ID, 0) 1470 1471 ctx, err := tx.Count("users") 1472 r.NoError(err) 1473 r.Equal(count+2, ctx) 1474 1475 err = tx.Destroy(&user) 1476 r.NoError(err) 1477 1478 ctx, _ = tx.Count("users") 1479 r.Equal(count, ctx) 1480 }) 1481 } 1482 1483 func Test_Destroy_UUID(t *testing.T) { 1484 if PDB == nil { 1485 t.Skip("skipping integration tests") 1486 } 1487 transaction(func(tx *Connection) { 1488 r := require.New(t) 1489 1490 count, err := tx.Count("songs") 1491 r.NoError(err) 1492 song := Song{Title: "Automatic Buffalo"} 1493 err = tx.Create(&song) 1494 r.NoError(err) 1495 r.NotZero(song.ID) 1496 1497 ctx, err := tx.Count("songs") 1498 r.NoError(err) 1499 r.Equal(count+1, ctx) 1500 1501 err = tx.Destroy(&song) 1502 r.NoError(err) 1503 1504 ctx, _ = tx.Count("songs") 1505 r.Equal(count, ctx) 1506 }) 1507 } 1508 1509 func Test_TruncateAll(t *testing.T) { 1510 if PDB == nil { 1511 t.Skip("skipping integration tests") 1512 } 1513 count := int(0) 1514 transaction(func(tx *Connection) { 1515 r := require.New(t) 1516 1517 var err error 1518 count, err = tx.Count("users") 1519 r.NoError(err) 1520 user := User{Name: nulls.NewString("Mark")} 1521 err = tx.Create(&user) 1522 r.NoError(err) 1523 r.NotEqual(user.ID, 0) 1524 1525 ctx, err := tx.Count("users") 1526 r.NoError(err) 1527 r.Equal(count+1, ctx) 1528 1529 err = tx.TruncateAll() 1530 r.NoError(err) 1531 1532 ctx, _ = tx.Count("users") 1533 r.Equal(count, ctx) 1534 }) 1535 }