-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathhistory_test.go
144 lines (113 loc) · 3.19 KB
/
history_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"path/filepath"
"testing"
)
func Test_saveHistoryItems(t *testing.T) {
setup(t)
defer teardown(t)
err := saveHistoryItems([]*FileAction{})
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
items, _ := allHistoryItems()
if len(items) > 0 {
t.Errorf("Expected no items, got %d", len(items))
}
var fileActions []*FileAction
var fileAction *FileAction
fileAction = NewFileAction()
fileAction = NewFileAction()
fileAction.oldPath = "one"
fileAction.newPath = "1"
fileActions = append(fileActions, fileAction)
fileAction = NewFileAction()
fileAction.oldPath = "two"
fileAction.newPath = "2"
fileActions = append(fileActions, fileAction)
saveHistoryItems(fileActions)
items, _ = allHistoryItems()
if len(items) != 2 {
t.Errorf("Expected 2 items, got %d", len(items))
}
for _, item := range items {
if (filepath.Base(item.Source) == "one" && filepath.Base(item.Dest) != "1") || (filepath.Base(item.Source) == "two" && filepath.Base(item.Dest) != "2") {
t.Error("Source and destination do not match.")
}
}
fileActions = []*FileAction{}
fileAction = NewFileAction()
fileAction.oldPath = "three"
fileAction.newPath = "3"
fileActions = append(fileActions, fileAction)
saveHistoryItems(fileActions)
items, _ = allHistoryItems()
if len(items) != 3 {
t.Errorf("Expected 3 items, got %d", len(items))
}
profileDb_.Close()
err = saveHistoryItems(fileActions)
if err == nil {
t.Error("Expected error, got nil")
}
}
func Test_deleteHistoryItems(t *testing.T) {
setup(t)
defer teardown(t)
var fileActions []*FileAction
var fileAction *FileAction
fileAction = NewFileAction()
fileAction.oldPath = "one"
fileAction.newPath = "1"
fileActions = append(fileActions, fileAction)
fileAction = NewFileAction()
fileAction.oldPath = "two"
fileAction.newPath = "2"
fileActions = append(fileActions, fileAction)
fileAction = NewFileAction()
fileAction.oldPath = "three"
fileAction.newPath = "3"
fileActions = append(fileActions, fileAction)
saveHistoryItems(fileActions)
items, _ := allHistoryItems()
deleteHistoryItems([]HistoryItem{items[0], items[1]})
items, _ = allHistoryItems()
if len(items) != 1 {
t.Errorf("Expected 1 item, got %d", len(items))
} else {
if filepath.Base(items[0].Source) != "three" {
t.Error("Incorrect item in history")
}
}
}
func Test_deleteOldHistoryItems(t *testing.T) {
setup(t)
defer teardown(t)
now := 1000
for i := 0; i < 5; i++ {
profileDb_.Exec("INSERT INTO history (source, destination, timestamp) VALUES (?, ?, ?)", "a", "b", now+i)
}
deleteOldHistoryItems(int64(now + 2))
items, _ := allHistoryItems()
if len(items) != 3 {
t.Errorf("Expected 3 items, got %d", len(items))
}
}
func Test_latestHistoryItemsByDestinations(t *testing.T) {
setup(t)
defer teardown(t)
now := 1000
for i := 0; i < 5; i++ {
profileDb_.Exec("INSERT INTO history (source, destination, timestamp) VALUES (?, ?, ?)", "a", "b", now+i)
}
items, _ := allHistoryItems()
dest := items[0].Dest
items, _ = latestHistoryItemsByDestinations([]string{dest})
if len(items) != 1 {
t.Errorf("Expected 1 item, got %d", len(items))
} else {
if items[0].Timestamp != 1004 {
t.Error("Did not get the right item")
}
}
}