forked from qor/publish2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared_version_test.go
99 lines (81 loc) · 2.58 KB
/
shared_version_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
package publish2_test
import (
"testing"
"github.com/jinzhu/gorm"
"github.com/qor/publish2"
)
type SharedVersionProduct struct {
gorm.Model
Name string
ColorVariations []SharedVersionColorVariation
publish2.Version
}
type SharedVersionColorVariation struct {
gorm.Model
Name string
SharedVersionProductID uint
SizeVariations []SharedVersionSizeVariation
publish2.SharedVersion
}
type SharedVersionSizeVariation struct {
gorm.Model
Name string
SharedVersionColorVariationID uint
publish2.SharedVersion
}
func prepareSharedVersionProduct() *SharedVersionProduct {
product := SharedVersionProduct{
Name: "shared product 1",
ColorVariations: []SharedVersionColorVariation{
{
Name: "cv1",
},
{
Name: "cv2",
},
},
}
DB.Create(&product)
product.SetVersionName("v1")
product.ColorVariations[0].SetSharedVersionName("v1")
DB.Save(&product)
product.SetVersionName("v2")
product.ColorVariations[0].SetSharedVersionName("")
colorVariation := SharedVersionColorVariation{
Name: "cv3",
}
colorVariation.SetSharedVersionName("v2")
product.ColorVariations = append(product.ColorVariations, colorVariation)
DB.Save(&product)
return &product
}
func TestSharedVersions(t *testing.T) {
product1 := prepareSharedVersionProduct()
product2 := prepareSharedVersionProduct()
var product1V1 SharedVersionProduct
DB.Set(publish2.VersionNameMode, "v1").Preload("ColorVariations").Find(&product1V1, "id = ?", product1.ID)
if len(product1V1.ColorVariations) != 2 {
t.Errorf("Preload: Should have 2 color variations for product v1, but got %v", len(product1V1.ColorVariations))
}
var colorVariations1V1 []SharedVersionColorVariation
DB.Model(&product1V1).Related(&colorVariations1V1)
if len(colorVariations1V1) != 2 {
t.Errorf("Related: Should have 2 color variations for product v1, but got %v", len(colorVariations1V1))
}
var product1V2 SharedVersionProduct
DB.Set(publish2.VersionNameMode, "v2").Preload("ColorVariations").Find(&product1V2, "id = ?", product1.ID)
if len(product1V2.ColorVariations) != 3 {
t.Errorf("Preload: Should have 3 color variations for product v2, but got %v", len(product1V2.ColorVariations))
}
var products []SharedVersionProduct
DB.Preload("ColorVariations").Find(&products)
var product2V2 SharedVersionProduct
for _, p := range products {
if p.ID == product2.ID && p.VersionName == "v2" {
product2V2 = p
}
}
if len(product2V2.ColorVariations) != 3 {
t.Errorf("Preload: Should have 3 color variations for product v2, but got %v", len(product2V2.ColorVariations))
}
}