(t *testing.T)
| 95 | } |
| 96 | |
| 97 | func TestRunCallbacks(t *testing.T) { |
| 98 | DB.Migrator().DropTable(&Product{}) |
| 99 | DB.AutoMigrate(&Product{}) |
| 100 | |
| 101 | p := Product{Code: "unique_code", Price: 100} |
| 102 | DB.Save(&p) |
| 103 | |
| 104 | if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 1, 0, 1, 1, 0, 0, 0, 0}) { |
| 105 | t.Fatalf("Callbacks should be invoked successfully, %v", p.GetCallTimes()) |
| 106 | } |
| 107 | |
| 108 | DB.Where("Code = ?", "unique_code").First(&p) |
| 109 | if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 1, 0, 1, 0, 0, 0, 0, 1}) { |
| 110 | t.Fatalf("After callbacks values are not saved, %v", p.GetCallTimes()) |
| 111 | } |
| 112 | |
| 113 | p.Price = 200 |
| 114 | DB.Save(&p) |
| 115 | if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 1, 1, 1, 0, 0, 1}) { |
| 116 | t.Fatalf("After update callbacks should be invoked successfully, %v", p.GetCallTimes()) |
| 117 | } |
| 118 | |
| 119 | var products []Product |
| 120 | DB.Find(&products, "code = ?", "unique_code") |
| 121 | if products[0].AfterFindCallTimes != 2 { |
| 122 | t.Fatalf("AfterFind callbacks should work with slice, called %v", products[0].AfterFindCallTimes) |
| 123 | } |
| 124 | |
| 125 | DB.Where("Code = ?", "unique_code").First(&p) |
| 126 | if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 1, 0, 0, 0, 0, 2}) { |
| 127 | t.Fatalf("After update callbacks values are not saved, %v", p.GetCallTimes()) |
| 128 | } |
| 129 | |
| 130 | DB.Delete(&p) |
| 131 | if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 1, 0, 0, 1, 1, 2}) { |
| 132 | t.Fatalf("After delete callbacks should be invoked successfully, %v", p.GetCallTimes()) |
| 133 | } |
| 134 | |
| 135 | if DB.Where("Code = ?", "unique_code").First(&p).Error == nil { |
| 136 | t.Fatalf("Can't find a deleted record") |
| 137 | } |
| 138 | |
| 139 | beforeCallTimes := p.AfterFindCallTimes |
| 140 | if DB.Where("Code = ?", "unique_code").Find(&p).Error != nil { |
| 141 | t.Fatalf("Find don't raise error when record not found") |
| 142 | } |
| 143 | |
| 144 | if p.AfterFindCallTimes != beforeCallTimes { |
| 145 | t.Fatalf("AfterFind should not be called") |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestCallbacksWithErrors(t *testing.T) { |
| 150 | DB.Migrator().DropTable(&Product{}) |
nothing calls this directly
no test coverage detected