-
Notifications
You must be signed in to change notification settings - Fork 113
/
updatevalues_test.go
187 lines (170 loc) · 5.7 KB
/
updatevalues_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
// modifyvalues_test.go - test keyvalues.go methods
package mxj
import (
"fmt"
"testing"
)
func TestUVHeader(t *testing.T) {
fmt.Println("\n---------------- updatevalues_test.go ...")
}
func TestUpdateValuesForPath_Author(t *testing.T) {
m, merr := NewMapXml(doc1)
if merr != nil {
t.Fatal("merr:", merr.Error())
}
fmt.Println("m:", m)
ss, _ := m.ValuesForPath("doc.books.book.author")
for _, v := range ss {
fmt.Println("v:", v)
}
fmt.Println("m.UpdateValuesForPath(\"author:NoName\", \"doc.books.book.author\")")
n, err := m.UpdateValuesForPath("author:NoName", "doc.books.book.author")
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println(n, "updates")
ss, _ = m.ValuesForPath("doc.books.book.author")
for _, v := range ss {
fmt.Println("v:", v)
}
fmt.Println("m.UpdateValuesForPath(\"author:William Gadddis\", \"doc.books.book.author\", \"title:The Recognitions\")")
n, err = m.UpdateValuesForPath("author:William Gadddis", "doc.books.book.author", "title:The Recognitions")
o, _ := m.UpdateValuesForPath("author:Austin Tappen Wright", "doc.books.book", "title:Islandia")
p, _ := m.UpdateValuesForPath("author:John Hawkes", "doc.books.book", "title:The Beetle Leg")
q, _ := m.UpdateValuesForPath("author:T. E. Porter", "doc.books.book", "title:King's Day")
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println(n+o+p+q, "updates")
ss, _ = m.ValuesForPath("doc.books.book.author")
for _, v := range ss {
fmt.Println("v:", v)
}
fmt.Println("m.UpdateValuesForPath(\"author:William T. Gaddis\", \"doc.books.book.*\", \"title:The Recognitions\")")
n, _ = m.UpdateValuesForPath("author:William T. Gaddis", "doc.books.book.*", "title:The Recognitions")
fmt.Println(n, "updates")
ss, _ = m.ValuesForPath("doc.books.book.author")
for _, v := range ss {
fmt.Println("v:", v)
}
fmt.Println("m.UpdateValuesForPath(\"title:The Cannibal\", \"doc.books.book.title\", \"author:John Hawkes\")")
n, _ = m.UpdateValuesForPath("title:The Cannibal", "doc.books.book.title", "author:John Hawkes")
o, _ = m.UpdateValuesForPath("review:A novel on his experiences in WWII.", "doc.books.book.review", "title:The Cannibal")
fmt.Println(n+o, "updates")
ss, _ = m.ValuesForPath("doc.books.book")
for _, v := range ss {
fmt.Println("v:", v)
}
fmt.Println("m.UpdateValuesForPath(\"books:\", \"doc.books\")")
n, _ = m.UpdateValuesForPath("books:", "doc.books")
fmt.Println(n, "updates")
fmt.Println("m:", m)
fmt.Println("m.UpdateValuesForPath(mm, \"*\")")
mm, _ := NewMapXml(doc1)
n, err = m.UpdateValuesForPath(mm, "*")
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println(n, "updates")
fmt.Println("m:", m)
// ---------------------- newDoc
var newDoc = []byte(`<tag color="green" shape="square">simple element</tag>`)
m, merr = NewMapXml(newDoc)
if merr != nil {
t.Fatal("merr:", merr.Error())
}
fmt.Println("\nnewDoc:", string(newDoc))
fmt.Println("m:", m)
fmt.Println("m.UpdateValuesForPath(\"" + textK + ":maybe not so simple element\", \"tag\")")
n, _ = m.UpdateValuesForPath(textK + ":maybe not so simple element", "tag")
fmt.Println("n:", n, "m:", m)
fmt.Println("m.UpdateValuesForPath(\"" + textK + ":simple element again\", \"*\")")
n, _ = m.UpdateValuesForPath(textK + ":simple element again", "*")
fmt.Println("n:", n, "m:", m)
/*
fmt.Println("UpdateValuesForPath, doc.books.book, title:The Recognitions : NoBook")
n, err = m.UpdateValuesForPath("NoBook", "doc.books.book", "title:The Recognitions")
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println(n, "updates")
ss, _ = m.ValuesForPath("doc.books.book")
for _, v := range ss {
fmt.Println("v:", v)
}
fmt.Println("UpdateValuesForPath, doc.books.book.title -seq=3: The Blood Oranges")
n, err = m.UpdateValuesForPath("The Blood Oranges", "doc.books.book.title", "-seq:3")
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println(n, "updates")
ss, _ = m.ValuesForPath("doc.books.book.title")
for _, v := range ss {
fmt.Println("v:", v)
}
*/
}
var authorDoc = []byte(`
<biblio>
<author>
<name>William Gaddis</name>
<books>
<book>
<title>The Recognitions</title>
<date>1955</date>
<review>A novel that changed the face of American literature.</review>
</book>
<book>
<title>JR</title>
<date>1975</date>
<review>Winner of National Book Award for Fiction.</review>
</book>
</books>
</author>
<author>
<name>John Hawkes</name>
<books>
<book>
<title>The Cannibal</title>
<date>1949</date>
<review>A novel on his experiences in WWII.</review>
</book>
<book>
<title>The Beetle Leg</title>
<date>1951</date>
<review>A lyrical novel about the construction of Ft. Peck Dam in Montana.</review>
</book>
<book>
<title>The Blood Oranges</title>
<date>1970</date>
<review>Where everyone wants to vacation.</review>
</book>
</books>
</author>
</biblio>`)
func TestAuthorDoc(t *testing.T) {
m, merr := NewMapXml(authorDoc)
if merr != nil {
t.Fatal("merr:", merr.Error())
}
fmt.Println(m.StringIndent())
fmt.Println("m.UpdateValuesForPath(\"review:National Book Award winner.\", \"*.*.*.*\", \"title:JR\")")
n, _ := m.UpdateValuesForPath("review:National Book Award winner.", "*.*.*.*", "title:JR")
fmt.Println(n, "updates")
ss, _ := m.ValuesForPath("biblio.author", "name:William Gaddis")
for _, v := range ss {
fmt.Println("v:", v)
}
fmt.Println("m.UpdateValuesForPath(newVal, path, oldVal)")
path := m.PathForKeyShortest("date")
v, _ := m.ValuesForPath(path)
var counter int
for _, vv := range v {
oldVal := "date:" + vv.(string)
newVal := "date:" + vv.(string) + ":num"
n, _ = m.UpdateValuesForPath(newVal, path, oldVal)
counter += n
}
fmt.Println(counter, "updates")
fmt.Println(m.StringIndent())
}