-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_test.go
117 lines (104 loc) · 2.59 KB
/
sync_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
112
113
114
115
116
117
package crm_test
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
crm "github.com/vertoforce/generic-crm"
)
const (
testSyncItemCount = 3
)
func TestSync(t *testing.T) {
testCRMs, err := getTestCRMs()
if err != nil {
t.Error(err)
return
}
// Create some items
ctx := context.Background()
for i := 0; i < testSyncItemCount; i++ {
for _, testCRM := range testCRMs {
testCRM.CreateItem(ctx, &crm.DefaultItem{
Fields: map[string]interface{}{
"Name": fmt.Sprintf("Name %d", i),
},
})
}
}
// Build stream of updates
// These updates will update Name 1, and Name 2, and create a Name 3
newItems := make(chan crm.Item)
go func() {
for i := 1; i < testSyncItemCount+1; i++ {
newItems <- &crm.DefaultItem{
Fields: map[string]interface{}{
"Name": fmt.Sprintf("Name %d", i),
"Item": "Updated content",
},
}
}
close(newItems)
}()
// Build sync machine
syncMachine := crm.NewSyncMachine().
SetDeleteUntouchedItems(true).
WithCRMs(testCRMs...).
WithSearchFunction(func(i crm.Item) map[string]interface{} {
return map[string]interface{}{
"Name": i.GetFields()["Name"],
}
})
err = syncMachine.Sync(ctx, newItems)
if err != nil {
t.Error(err)
return
}
// Check if the CRMs are in the state we'd expect
for _, testCRM := range testCRMs {
items := make(chan crm.Item)
go func() {
defer close(items)
err := testCRM.GetItems(ctx, items)
require.NoError(t, err)
}()
foundNames := map[string]bool{}
toDelete := []crm.Item{}
for item := range items {
toDelete = append(toDelete, item)
foundNames[item.GetFields()["Name"].(string)] = true
}
if len(foundNames) > testSyncItemCount {
t.Errorf("too many items in CRM")
}
for i := 1; i < testSyncItemCount+1; i++ {
if _, ok := foundNames[fmt.Sprintf("Name %d", i)]; !ok {
t.Errorf("CRM does not have the expected values")
}
}
// Delete all items
testCRM.RemoveItems(ctx, toDelete...)
}
}
func TestForgivingEqual(t *testing.T) {
tests := []struct {
A interface{}
B interface{}
Equal bool
}{
{A: float64(1), B: int64(1), Equal: true},
{A: int64(1), B: float64(1), Equal: true},
{A: "1", B: int64(1), Equal: false},
{A: 1, B: 2, Equal: false},
{A: "1", B: "1", Equal: true},
{A: "1", B: "2", Equal: false},
{A: fmt.Sprintf("%s", interface{}(nil)), B: nil, Equal: true},
{A: interface{}("200"), B: "200", Equal: true},
}
for i, test := range tests {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
result := crm.ForgivingEqual(test.A, test.B)
require.Equal(t, test.Equal, result)
})
}
}