mirror of
https://github.com/geektutu/7days-golang.git
synced 2024-04-21 12:32:11 +00:00
38 lines
733 B
Go
38 lines
733 B
Go
package session
|
|
|
|
import (
|
|
"geeorm/log"
|
|
"testing"
|
|
)
|
|
|
|
type Account struct {
|
|
ID int `geeorm:"PRIMARY KEY"`
|
|
Password string
|
|
}
|
|
|
|
func (account *Account) BeforeInsert(s *Session) error {
|
|
log.Info("before inert", account)
|
|
account.ID += 1000
|
|
return nil
|
|
}
|
|
|
|
func (account *Account) AfterQuery(s *Session) error {
|
|
log.Info("after query", account)
|
|
account.Password = "******"
|
|
return nil
|
|
}
|
|
|
|
func TestSession_CallMethod(t *testing.T) {
|
|
s := NewSession().Model(&Account{})
|
|
_ = s.DropTable()
|
|
_ = s.CreateTable()
|
|
_, _ = s.Insert(&Account{1, "123456"}, &Account{2, "qwerty"})
|
|
|
|
u := &Account{}
|
|
|
|
err := s.First(u)
|
|
if err != nil || u.ID != 1001 || u.Password != "******" {
|
|
t.Fatal("Failed to call hooks after query, got", u)
|
|
}
|
|
}
|