-
Notifications
You must be signed in to change notification settings - Fork 10
/
l10_test.go
111 lines (92 loc) · 2.32 KB
/
l10_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package publish_test
import (
"testing"
"github.com/jinzhu/gorm"
"github.com/qor/l10n"
"github.com/qor/publish"
)
type Book struct {
gorm.Model
l10n.Locale
publish.Status
Name string
CategoryID uint
Category Category
PublisherID uint
Publisher Publisher
Comments []Comment
Authors []Author `gorm:"many2many:author_books;ForeignKey:ID;AssociationForeignKey:ID"`
}
type Publisher struct {
gorm.Model
publish.Status
Name string
}
type Comment struct {
gorm.Model
l10n.Locale
publish.Status
Content string
BookID uint
}
type Author struct {
gorm.Model
l10n.Locale
Name string
}
func generateBook(name string) *Book {
book := Book{
Name: name,
Category: Category{
Name: name + "_category",
},
Publisher: Publisher{
Name: name + "_publisher",
},
Comments: []Comment{
{Content: name + "_comment1"},
{Content: name + "_comment2"},
},
Authors: []Author{
{Name: name + "_author1"},
{Name: name + "_author2"},
},
}
return &book
}
func TestBelongsToForL10nResource(t *testing.T) {
name := "belongs_to_for_l10n"
book := generateBook(name)
pbdraft.Save(book)
pb.Publish(book)
if pbprod.Where("id = ?", book.ID).First(&Book{}).RecordNotFound() {
t.Errorf("should find book from production db")
}
if pbprod.Where("name LIKE ?", name+"%").First(&Publisher{}).RecordNotFound() {
t.Errorf("should find publisher from production db")
}
if pbprod.Where("name LIKE ?", name+"%").First(&Category{}).RecordNotFound() {
t.Errorf("should find category from production db")
}
}
func TestMany2ManyForL10nResource(t *testing.T) {
name := "many2many_for_l10n"
book := generateBook(name)
pbdraft.Save(book)
if pbdraft.Model(book).Association("Authors").Count() != 2 {
t.Errorf("should find two authors from draft db before publish")
}
if pbprod.Model(book).Association("Authors").Count() != 0 {
t.Errorf("should find none author from production db before publish")
}
pb.Publish(book)
if pbprod.Where("id = ?", book.ID).First(&Book{}).RecordNotFound() {
t.Errorf("should find book from production db")
}
if pbdraft.Model(book).Association("Authors").Count() != 2 {
t.Errorf("should find two authors from draft db after publish")
}
if pbprod.Model(book).Association("Authors").Count() != 2 {
t.Errorf("should find two authors from draft db after publish")
}
}