github.com/dolanor/pop@v4.13.0+incompatible/pop_test.go (about) 1 package pop 2 3 import ( 4 stdlog "log" 5 "os" 6 "testing" 7 "time" 8 9 "github.com/gobuffalo/nulls" 10 "github.com/gobuffalo/pop/logging" 11 "github.com/gobuffalo/validate" 12 "github.com/gobuffalo/validate/validators" 13 "github.com/gofrs/uuid" 14 "github.com/stretchr/testify/suite" 15 ) 16 17 var PDB *Connection 18 19 type PostgreSQLSuite struct { 20 suite.Suite 21 } 22 23 type MySQLSuite struct { 24 suite.Suite 25 } 26 27 type SQLiteSuite struct { 28 suite.Suite 29 } 30 31 func TestSpecificSuites(t *testing.T) { 32 switch os.Getenv("SODA_DIALECT") { 33 case "postgres": 34 suite.Run(t, &PostgreSQLSuite{}) 35 case "mysql", "mysql_travis": 36 suite.Run(t, &MySQLSuite{}) 37 case "sqlite": 38 suite.Run(t, &SQLiteSuite{}) 39 } 40 } 41 42 func init() { 43 Debug = false 44 AddLookupPaths("./") 45 46 dialect := os.Getenv("SODA_DIALECT") 47 48 if dialect != "" { 49 if err := LoadConfigFile(); err != nil { 50 stdlog.Panic(err) 51 } 52 var err error 53 PDB, err = Connect(dialect) 54 log(logging.Info, "Run test with dialect %v", dialect) 55 if err != nil { 56 stdlog.Panic(err) 57 } 58 } else { 59 log(logging.Info, "Skipping integration tests") 60 } 61 } 62 63 func transaction(fn func(tx *Connection)) { 64 err := PDB.Rollback(func(tx *Connection) { 65 fn(tx) 66 }) 67 if err != nil { 68 stdlog.Fatal(err) 69 } 70 } 71 72 func ts(s string) string { 73 return PDB.Dialect.TranslateSQL(s) 74 } 75 76 type User struct { 77 ID int `db:"id"` 78 UserName string `db:"user_name"` 79 Email string `db:"email"` 80 Name nulls.String `db:"name"` 81 Alive nulls.Bool `db:"alive"` 82 CreatedAt time.Time `db:"created_at"` 83 UpdatedAt time.Time `db:"updated_at"` 84 BirthDate nulls.Time `db:"birth_date"` 85 Bio nulls.String `db:"bio"` 86 Price nulls.Float64 `db:"price"` 87 FullName nulls.String `db:"full_name" select:"name as full_name"` 88 Books Books `has_many:"books" order_by:"title asc"` 89 FavoriteSong Song `has_one:"song" fk_id:"u_id"` 90 Houses Addresses `many_to_many:"users_addresses"` 91 } 92 93 // Validate gets run every time you call a "Validate*" (ValidateAndSave, ValidateAndCreate, ValidateAndUpdate) method. 94 // This method is not required and may be deleted. 95 func (u *User) Validate(tx *Connection) (*validate.Errors, error) { 96 return validate.Validate( 97 &validators.StringIsPresent{Field: u.Name.String, Name: "Name"}, 98 ), nil 99 } 100 101 type Users []User 102 103 type UserAttribute struct { 104 ID int `db:"id"` 105 UserName string `db:"user_name"` 106 NickName string `db:"nick_name"` 107 108 User User `json:"user" belongs_to:"user" fk_id:"UserName" primary_id:"UserName"` 109 } 110 111 type Book struct { 112 ID int `db:"id"` 113 Title string `db:"title"` 114 Isbn string `db:"isbn"` 115 UserID nulls.Int `db:"user_id"` 116 User User `belongs_to:"user"` 117 Description string `db:"description"` 118 Writers Writers `has_many:"writers"` 119 CreatedAt time.Time `db:"created_at"` 120 UpdatedAt time.Time `db:"updated_at"` 121 } 122 123 type Taxi struct { 124 ID int `db:"id"` 125 Model string `db:"model"` 126 UserID nulls.Int `db:"user_id"` 127 Driver User `belongs_to:"user" fk_id:"UserID"` 128 CreatedAt time.Time `db:"created_at"` 129 UpdatedAt time.Time `db:"updated_at"` 130 } 131 132 // Validate gets run every time you call a "Validate*" (ValidateAndSave, ValidateAndCreate, ValidateAndUpdate) method. 133 // This method is not required and may be deleted. 134 func (b *Book) Validate(tx *Connection) (*validate.Errors, error) { 135 return validate.Validate( 136 &validators.StringIsPresent{Field: b.Description, Name: "Description"}, 137 ), nil 138 } 139 140 type Books []Book 141 142 type Writer struct { 143 ID int `db:"id"` 144 Name string `db:"name"` 145 BookID int `db:"book_id"` 146 Book Book `belongs_to:"book"` 147 CreatedAt time.Time `db:"created_at"` 148 UpdatedAt time.Time `db:"updated_at"` 149 } 150 151 type Writers []Writer 152 153 type Address struct { 154 ID int `db:"id"` 155 Street string `db:"street"` 156 HouseNumber int `db:"house_number"` 157 CreatedAt time.Time `db:"created_at"` 158 UpdatedAt time.Time `db:"updated_at"` 159 } 160 161 type Addresses []Address 162 163 type UsersAddress struct { 164 ID int `db:"id"` 165 UserID int `db:"user_id"` 166 AddressID int `db:"address_id"` 167 CreatedAt time.Time `db:"created_at"` 168 UpdatedAt time.Time `db:"updated_at"` 169 } 170 171 type UsersAddressQuery struct { 172 ID int `db:"id"` 173 UserID int `db:"user_id"` 174 AddressID int `db:"address_id"` 175 CreatedAt time.Time `db:"created_at"` 176 UpdatedAt time.Time `db:"updated_at"` 177 178 UserName *string `db:"name" json:"user_name"` 179 UserEmail *string `db:"email" json:"user_email"` 180 } 181 182 func (UsersAddressQuery) TableName() string { 183 return "users_addresses" 184 } 185 186 type Friend struct { 187 ID int `db:"id"` 188 FirstName string `db:"first_name"` 189 LastName string `db:"last_name"` 190 CreatedAt time.Time `db:"created_at"` 191 UpdatedAt time.Time `db:"updated_at"` 192 } 193 194 func (Friend) TableName() string { 195 return "good_friends" 196 } 197 198 type Family struct { 199 ID int `db:"id"` 200 FirstName string `db:"first_name"` 201 LastName string `db:"last_name"` 202 CreatedAt time.Time `db:"created_at"` 203 UpdatedAt time.Time `db:"updated_at"` 204 } 205 206 func (Family) TableName() string { 207 // schema.table_name 208 return "family.members" 209 } 210 211 type Enemy struct { 212 A string 213 } 214 215 type Song struct { 216 ID uuid.UUID `db:"id"` 217 Title string `db:"title"` 218 UserID int `db:"u_id"` 219 CreatedAt time.Time `json:"created_at" db:"created_at"` 220 UpdatedAt time.Time `json:"updated_at" db:"updated_at"` 221 ComposedByID int `json:"composed_by_id" db:"composed_by_id"` 222 ComposedBy Composer `belongs_to:"composer"` 223 } 224 225 type Composer struct { 226 ID int `db:"id"` 227 Name string `db:"name"` 228 CreatedAt time.Time `db:"created_at"` 229 UpdatedAt time.Time `db:"updated_at"` 230 } 231 232 type Course struct { 233 ID uuid.UUID `json:"id" db:"id"` 234 CreatedAt time.Time `json:"created_at" db:"created_at"` 235 UpdatedAt time.Time `json:"updated_at" db:"updated_at"` 236 } 237 238 type CourseCode struct { 239 ID uuid.UUID `json:"id" db:"id"` 240 CreatedAt time.Time `json:"created_at" db:"created_at"` 241 UpdatedAt time.Time `json:"updated_at" db:"updated_at"` 242 CourseID uuid.UUID `json:"course_id" db:"course_id"` 243 Course Course `json:"-" db:"-"` 244 // Course Course `belongs_to:"course"` 245 } 246 247 type ValidatableCar struct { 248 ID int64 `db:"id"` 249 Name string `db:"name"` 250 CreatedAt time.Time `json:"created_at" db:"created_at"` 251 UpdatedAt time.Time `json:"updated_at" db:"updated_at"` 252 } 253 254 var validationLogs []string 255 256 func (v *ValidatableCar) Validate(tx *Connection) (*validate.Errors, error) { 257 validationLogs = append(validationLogs, "Validate") 258 verrs := validate.Validate(&validators.StringIsPresent{Field: v.Name, Name: "Name"}) 259 return verrs, nil 260 } 261 262 func (v *ValidatableCar) ValidateSave(tx *Connection) (*validate.Errors, error) { 263 validationLogs = append(validationLogs, "ValidateSave") 264 return nil, nil 265 } 266 267 func (v *ValidatableCar) ValidateUpdate(tx *Connection) (*validate.Errors, error) { 268 validationLogs = append(validationLogs, "ValidateUpdate") 269 return nil, nil 270 } 271 272 func (v *ValidatableCar) ValidateCreate(tx *Connection) (*validate.Errors, error) { 273 validationLogs = append(validationLogs, "ValidateCreate") 274 return nil, nil 275 } 276 277 type NotValidatableCar struct { 278 ID int `db:"id"` 279 Name string `db:"name"` 280 CreatedAt time.Time `json:"created_at" db:"created_at"` 281 UpdatedAt time.Time `json:"updated_at" db:"updated_at"` 282 } 283 284 type CallbacksUser struct { 285 ID int `db:"id"` 286 BeforeS string `db:"before_s"` 287 BeforeC string `db:"before_c"` 288 BeforeU string `db:"before_u"` 289 BeforeD string `db:"before_d"` 290 BeforeV string `db:"before_v"` 291 AfterS string `db:"after_s"` 292 AfterC string `db:"after_c"` 293 AfterU string `db:"after_u"` 294 AfterD string `db:"after_d"` 295 AfterF string `db:"after_f"` 296 CreatedAt time.Time `json:"created_at" db:"created_at"` 297 UpdatedAt time.Time `json:"updated_at" db:"updated_at"` 298 } 299 300 type CallbacksUsers []CallbacksUser 301 302 func (u *CallbacksUser) BeforeSave(tx *Connection) error { 303 u.BeforeS = "BeforeSave" 304 return nil 305 } 306 307 func (u *CallbacksUser) BeforeUpdate(tx *Connection) error { 308 u.BeforeU = "BeforeUpdate" 309 return nil 310 } 311 312 func (u *CallbacksUser) BeforeCreate(tx *Connection) error { 313 u.BeforeC = "BeforeCreate" 314 return nil 315 } 316 317 func (u *CallbacksUser) BeforeDestroy(tx *Connection) error { 318 u.BeforeD = "BeforeDestroy" 319 return nil 320 } 321 322 func (u *CallbacksUser) BeforeValidate(tx *Connection) error { 323 u.BeforeV = "BeforeValidate" 324 return nil 325 } 326 327 func (u *CallbacksUser) AfterSave(tx *Connection) error { 328 u.AfterS = "AfterSave" 329 return nil 330 } 331 332 func (u *CallbacksUser) AfterUpdate(tx *Connection) error { 333 u.AfterU = "AfterUpdate" 334 return nil 335 } 336 337 func (u *CallbacksUser) AfterCreate(tx *Connection) error { 338 u.AfterC = "AfterCreate" 339 return nil 340 } 341 342 func (u *CallbacksUser) AfterDestroy(tx *Connection) error { 343 u.AfterD = "AfterDestroy" 344 return nil 345 } 346 347 func (u *CallbacksUser) AfterFind(tx *Connection) error { 348 u.AfterF = "AfterFind" 349 return nil 350 } 351 352 type Label struct { 353 ID string `db:"id"` 354 } 355 356 type SingleID struct { 357 ID int `db:"id"` 358 } 359 360 type Body struct { 361 ID int `json:"id" db:"id"` 362 Head *Head `json:"head" has_one:"head"` 363 } 364 365 type Head struct { 366 ID int `json:"id,omitempty" db:"id"` 367 BodyID int `json:"-" db:"body_id"` 368 Body *Body `json:"body,omitempty" belongs_to:"body"` 369 } 370 371 type HeadPtr struct { 372 ID int `json:"id,omitempty" db:"id"` 373 BodyID *int `json:"-" db:"body_id"` 374 Body *Body `json:"body,omitempty" belongs_to:"body"` 375 } 376 377 type Student struct { 378 ID uuid.UUID `json:"id" db:"id"` 379 CreatedAt time.Time `json:"created_at" db:"created_at"` 380 UpdatedAt time.Time `json:"updated_at" db:"updated_at"` 381 } 382 383 // https://github.com/gobuffalo/pop/issues/302 384 type Parent struct { 385 ID uuid.UUID `json:"id" db:"id"` 386 CreatedAt time.Time `json:"created_at" db:"created_at"` 387 UpdatedAt time.Time `json:"updated_at" db:"updated_at"` 388 Students []*Student `many_to_many:"parents_students"` 389 }