-
Notifications
You must be signed in to change notification settings - Fork 10
/
publish_event_test.go
87 lines (69 loc) · 2.67 KB
/
publish_event_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package publish_test
import (
"fmt"
"testing"
"github.com/jinzhu/gorm"
"github.com/qor/publish"
)
type createResourcePublishInterface struct {
}
func (createResourcePublishInterface) Publish(db *gorm.DB, event publish.PublishEventInterface) error {
if event, ok := event.(*publish.PublishEvent); ok {
var product Product
db.Set("publish:draft_mode", true).First(&product, event.Argument)
pb.Publish(&product)
}
return nil
}
func (createResourcePublishInterface) Discard(db *gorm.DB, event publish.PublishEventInterface) error {
if event, ok := event.(*publish.PublishEvent); ok {
var product Product
db.Set("publish:draft_mode", true).First(&product, event.Argument)
pb.Discard(&product)
}
return nil
}
type publishAllResourcesInterface struct {
}
func (publishAllResourcesInterface) Publish(db *gorm.DB, event publish.PublishEventInterface) error {
return nil
}
func (publishAllResourcesInterface) Discard(db *gorm.DB, event publish.PublishEventInterface) error {
return nil
}
func init() {
publish.RegisterEvent("create_product", createResourcePublishInterface{})
publish.RegisterEvent("publish_all_resources", publishAllResourcesInterface{})
}
func TestCreateNewEvent(t *testing.T) {
product1 := Product{Name: "event_1"}
pbdraft.Set("publish:publish_event", true).Save(&product1)
event := publish.PublishEvent{Name: "create_product", Argument: fmt.Sprintf("%v", product1.ID)}
db.Save(&event)
if !pbprod.First(&Product{}, "name = ?", product1.Name).RecordNotFound() {
t.Errorf("created resource in draft db with event should not be published to production db")
}
var productDraft Product
if pbdraft.First(&productDraft, "name = ?", product1.Name).RecordNotFound() {
t.Errorf("created resource in draft db with event should exist in draft db")
}
if productDraft.PublishStatus == publish.DIRTY {
t.Errorf("product's publish status should not be DIRTY before publish event")
}
var publishEvent publish.PublishEvent
if pbdraft.First(&publishEvent, "name = ?", "create_product").Error != nil {
t.Errorf("created resource in draft db with event should create the event in db")
}
if !pbprod.First(&Product{}, "name = ?", product1.Name).RecordNotFound() {
t.Errorf("product should not be published to production db before publish event")
}
publishEvent.Publish(db)
if pbprod.First(&Product{}, "name = ?", product1.Name).RecordNotFound() {
t.Errorf("product should be published to production db after publish event")
}
}
func TestCreateProductWithPublishAllEvent(t *testing.T) {
product1 := Product{Name: "event_1"}
event := &publish.PublishEvent{Name: "publish_all_resources", Argument: "products"}
pbdraft.Set("publish:publish_event", event).Save(&product1)
}