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