-
Notifications
You must be signed in to change notification settings - Fork 10
/
update_record_test.go
67 lines (52 loc) · 2.13 KB
/
update_record_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package publish_test
import "testing"
func TestUpdateStructFromDraft(t *testing.T) {
name := "update_product_from_draft"
newName := name + "_v2"
product := Product{Name: name, Color: Color{Name: name}}
pbprod.Create(&product)
pbdraft.Model(&product).Update("name", newName)
pbdraft.First(&product, product.ID)
if !product.PublishStatus {
t.Errorf("Product's publish status should be DIRTY when updated from draft db")
}
if pbprod.First(&Product{}, "name = ?", name).RecordNotFound() {
t.Errorf("record should not be changed in production db")
}
if pbdraft.First(&Product{}, "name = ?", newName).RecordNotFound() {
t.Errorf("record should be changed in draft db")
}
if pbdraft.Model(&product).Related(&product.Color); product.Color.Name != name {
t.Errorf("should be able to find related struct")
}
}
func TestUpdateStructFromProduction(t *testing.T) {
name := "update_product_from_production"
newName := name + "_v2"
product := Product{Name: name, Color: Color{Name: name}}
pbprod.Create(&product)
pbprod.Model(&product).Update("name", newName)
if product.PublishStatus {
t.Errorf("Product's publish status should be PUBLISHED when updated from production db")
}
if pbprod.First(&Product{}, "name = ?", newName).RecordNotFound() {
t.Errorf("record should be changed in production db")
}
var productDraft Product
if pbdraft.First(&productDraft, "name = ?", newName).RecordNotFound() {
t.Errorf("record should be changed in draft db")
}
if productDraft.PublishStatus {
t.Errorf("Product's publish status should be PUBLISHED in draft when updated from production db")
}
if pbprod.Model(&product).Related(&product.Color); product.Color.Name != name {
t.Errorf("should be able to find related struct")
}
db.Model(&Product{}).Where("id = ?", product.ID).UpdateColumns(map[string]interface{}{"quantity": 5})
var newProduct, newDraftProduct Product
pbprod.Find(&newProduct, product.ID)
pbprod.Find(&newDraftProduct, product.ID)
if newProduct.Quantity != 5 || newDraftProduct.Quantity != 5 || newProduct.Name != newName || newDraftProduct.Name != newName {
t.Errorf("Sync update columns during production & draft db")
}
}