-
Notifications
You must be signed in to change notification settings - Fork 5
/
tag_test.go
232 lines (190 loc) · 5.13 KB
/
tag_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package sncli
import (
"testing"
"github.com/jonhadfield/gosn-v2/cache"
"github.com/jonhadfield/gosn-v2/common"
"github.com/jonhadfield/gosn-v2/items"
"github.com/stretchr/testify/require"
)
func TestAddDeleteTagByTitle(t *testing.T) {
defer cleanUp(*testSession)
testDelay()
addTagConfig := AddTagsInput{
Session: testSession,
Tags: []string{"TestTagOne", "TestTagTwo"},
}
ato, err := addTagConfig.Run()
require.NoError(t, err)
require.Contains(t, ato.Added, "TestTagOne")
require.Contains(t, ato.Added, "TestTagTwo")
require.Empty(t, ato.Existing)
deleteTagConfig := DeleteTagConfig{
Session: testSession,
TagTitles: []string{"TestTagOne", "TestTagTwo"},
}
var noDeleted int
noDeleted, err = deleteTagConfig.Run()
require.Equal(t, 2, noDeleted)
require.NoError(t, err)
}
func TestAddTagWithParent(t *testing.T) {
defer cleanUp(*testSession)
testDelay()
addTagConfigParent := AddTagsInput{
Session: testSession,
Tags: []string{"TestTagParent"},
}
ato, err := addTagConfigParent.Run()
require.NoError(t, err)
require.Contains(t, ato.Added, "TestTagParent")
require.Empty(t, ato.Existing)
addTagConfigChild := AddTagsInput{
Session: testSession,
Tags: []string{"TestTagChild"},
Parent: "TestTagParent",
}
ato, err = addTagConfigChild.Run()
require.NoError(t, err)
require.Contains(t, ato.Added, "TestTagChild")
require.Empty(t, ato.Existing)
deleteTagConfig := DeleteTagConfig{
Session: testSession,
TagTitles: []string{"TestTagParent", "TestTagChild"},
}
var noDeleted int
noDeleted, err = deleteTagConfig.Run()
require.Equal(t, 2, noDeleted)
require.NoError(t, err)
}
func TestGetTag(t *testing.T) {
testDelay()
defer cleanUp(*testSession)
testTagTitles := []string{"TestTagOne", "TestTagTwo"}
addTagInput := AddTagsInput{
Session: testSession,
Tags: testTagTitles,
}
ato, err := addTagInput.Run()
require.NoError(t, err)
require.NoError(t, err)
require.Contains(t, ato.Added, "TestTagOne")
require.Contains(t, ato.Added, "TestTagTwo")
require.Empty(t, ato.Existing)
// create filters
getTagFilters := items.ItemFilters{
MatchAny: true,
}
for _, testTagTitle := range testTagTitles {
getTagFilters.Filters = append(getTagFilters.Filters, items.Filter{
Key: "Title",
Value: testTagTitle,
Type: common.SNItemTypeTag,
Comparison: "==",
})
}
getTagConfig := GetTagConfig{
Session: testSession,
Filters: getTagFilters,
}
var output items.Items
output, err = getTagConfig.Run()
require.NoError(t, err)
require.EqualValues(t, len(output), 2, "expected two items but got: %+v", output)
}
func _addNotes(session cache.Session, i map[string]string) error {
for k, v := range i {
addNoteConfig := AddNoteInput{
Session: &session,
Title: k,
Text: v,
}
err := addNoteConfig.Run()
if err != nil {
return err
}
}
return nil
}
func _deleteNotesByTitle(session cache.Session, input map[string]string) (noDeleted int, err error) {
var noteTitles []string
for k := range input {
noteTitles = append(noteTitles, k)
}
deleteNoteConfig := DeleteNoteConfig{
Session: &session,
NoteTitles: noteTitles,
}
noDeleted, err = deleteNoteConfig.Run()
if err != nil {
return noDeleted, err
}
return noDeleted, deleteNoteConfig.Session.CacheDB.Close()
}
func _deleteTagsByTitle(session cache.Session, input []string) (noDeleted int, err error) {
deleteTagConfig := DeleteTagConfig{
Session: &session,
TagTitles: input,
}
return deleteTagConfig.Run()
}
func TestTaggingOfNotes(t *testing.T) {
testDelay()
defer cleanUp(*testSession)
// create four notes
notes := map[string]string{
"noteOneTitle": "noteOneText example",
"noteTwoTitle": "noteTwoText",
"noteThreeTitle": "noteThreeText",
"noteFourTitle": "noteFourText example",
}
err := _addNotes(*testSession, notes)
require.NoError(t, err)
// tag new notes with 'testTag'
tags := []string{"testTag"}
tni := TagItemsConfig{
Session: testSession,
FindText: "example",
NewTags: tags,
}
err = tni.Run()
require.NoError(t, err)
filterNotesByTagName := items.Filter{
Type: common.SNItemTypeNote,
Key: "TagTitle",
Comparison: "==",
Value: "testTag",
}
itemFilters := items.ItemFilters{
Filters: []items.Filter{filterNotesByTagName},
MatchAny: true,
}
gnc := GetNoteConfig{
Session: testSession,
Filters: itemFilters,
}
var retNotes items.Items
retNotes, err = gnc.Run()
require.NoError(t, err)
if len(retNotes) != 2 {
t.Errorf("expected two notes but got: %d", len(retNotes))
}
require.NoError(t, testSession.CacheDB.Close())
nd, err := _deleteNotesByTitle(*testSession, notes)
require.NoError(t, err)
require.Equal(t, 4, nd)
var deletedTags int
deletedTags, err = _deleteTagsByTitle(*testSession, tags)
require.NoError(t, err)
require.Equal(t, 1, deletedTags)
}
func TestGetTagsByTitleAndUUID(t *testing.T) {
addTagConfig := AddTagsInput{
Session: testSession,
Tags: []string{"TestTagOne", "TestTagTwo"},
}
ato, err := addTagConfig.Run()
require.NoError(t, err)
require.Contains(t, ato.Added, "TestTagOne")
require.Contains(t, ato.Added, "TestTagTwo")
require.Empty(t, ato.Existing)
}