github.com/pjdufour-truss/pop@v4.11.2-0.20190705085848-4c90b0ff4d5a+incompatible/executors_test.go (about)

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