func DoTestValidation(assert *assrt.Assert, info dialectInfo) { mg, q := setupDb(assert, info) valid := new(ValidatorTable) mg.dropTableIfExists(valid) mg.CreateTableIfNotExists(valid) valid.Name = "ok" q.Save(valid) valid.Id = 0 _, err := q.Save(valid) assert.MustNotNil(err) assert.Equal("name already taken", err.Error()) }
func DoTestForeignKey(assert *assrt.Assert, info dialectInfo) { mg, q := setupDb(assert, info) type user struct { Id Id Name string } type post struct { Id Id Title string AuthorId int64 Author *user } aUser := &user{ Name: "john", } aPost := &post{ Title: "A Title", } mg.dropTableIfExists(aPost) mg.dropTableIfExists(aUser) mg.CreateTableIfNotExists(aUser) mg.CreateTableIfNotExists(aPost) uid, err := q.Save(aUser) assert.Nil(err) aPost.AuthorId = int64(uid) affected, err := q.Save(aPost) assert.Equal(1, affected) pst := new(post) pst.Id = aPost.Id err = q.Find(pst) assert.MustNil(err) assert.MustNotNil(pst) assert.Equal(uid, pst.Id) assert.Equal("john", pst.Author.Name) }