-
Notifications
You must be signed in to change notification settings - Fork 33
/
transformation_test.rb
287 lines (240 loc) · 8.15 KB
/
transformation_test.rb
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Encoding: UTF-8
require 'test_helper'
require 'traject/indexer'
# should be built into every indexer
describe "Traject::Macros::Transformation" do
before do
@indexer = Traject::Indexer.new
@record = nil
end
describe "translation_map" do
it "translates" do
@indexer.configure do
to_field "cataloging_agency", literal("DLC"), translation_map("marc_040a_translate_test")
end
output = @indexer.map_record(@record)
assert_equal ["Library of Congress"], output["cataloging_agency"]
end
it "can merge multiple" do
@indexer.configure do
to_field "result", literal("key_to_be_overridden"), translation_map("ruby_map", "yaml_map")
end
output = @indexer.map_record(@record)
assert_equal ["value_from_yaml"], output["result"]
end
it "can merge multiple with hash" do
@indexer.configure do
to_field "result", literal("key_to_be_overridden"), translation_map("ruby_map", "yaml_map", {"key_to_be_overridden" => "value_from_inline_hash"})
end
output = @indexer.map_record(@record)
assert_equal ["value_from_inline_hash"], output["result"]
end
end
describe "transform" do
it "transforms with block" do
@indexer.configure do
to_field "sample_field", literal("one"), literal("two"), transform(&:upcase)
end
output = @indexer.map_record(@record)
assert_equal ["ONE", "TWO"], output["sample_field"]
end
it "transforms with proc arg" do
@indexer.configure do
to_field "sample_field", literal("one"), literal("two"), transform(->(val) { val.tr('aeiou', '!') })
end
output = @indexer.map_record(@record)
assert_equal ["!n!", "tw!"], output["sample_field"]
end
it "transforms with both, in correct order" do
@indexer.configure do
to_field "sample_field", literal("one"), literal("two"), transform(->(val) { val.tr('aeiou', '!') }, &:upcase)
end
output = @indexer.map_record(@record)
assert_equal ["!N!", "TW!"], output["sample_field"]
end
end
describe "default" do
it "adds default to empty accumulator" do
@indexer.configure do
to_field "test", default("default")
end
output = @indexer.map_record(@record)
assert_equal ["default"], output["test"]
end
it "does not add default if value present" do
@indexer.configure do
to_field "test", literal("value"), default("defaut")
end
output = @indexer.map_record(@record)
assert_equal ["value"], output["test"]
end
end
describe "first_only" do
it "takes only first in multi-value" do
@indexer.configure do
to_field "test", literal("one"), literal("two"), literal("three"), first_only
end
output = @indexer.map_record(@record)
assert_equal ["one"], output["test"]
end
it "no-ops on nil" do
@indexer.configure do
to_field "test", first_only
end
output = @indexer.map_record(@record)
assert_nil output["test"]
end
it "no-ops on single value" do
@indexer.configure do
to_field "test", literal("one"), first_only
end
output = @indexer.map_record(@record)
assert_equal ["one"], output["test"]
end
end
describe "unique" do
it "uniqs" do
@indexer.configure do
to_field "test", literal("one"), literal("two"), literal("one"), literal("three"), unique
end
output = @indexer.map_record(@record)
assert_equal ["one", "two", "three"], output["test"]
end
end
describe "strip" do
it "strips" do
@indexer.configure do
to_field "test", literal(" one"), literal(" two "), strip
end
output = @indexer.map_record(@record)
assert_equal ["one", "two"], output["test"]
end
it "strips unicode whitespace" do
@indexer.configure do
to_field "test", literal(" \u00A0 \u2002 one \u202F "), strip
end
output = @indexer.map_record(@record)
assert_equal ["one"], output["test"]
end
end
describe "split" do
it "splits" do
@indexer.configure do
to_field "test", literal("one.two"), split(".")
end
output = @indexer.map_record(@record)
assert_equal ["one", "two"], output["test"]
end
end
describe "append" do
it "appends suffix" do
@indexer.configure do
to_field "test", literal("one"), literal("two"), append(".suffix")
end
output = @indexer.map_record(@record)
assert_equal ["one.suffix", "two.suffix"], output["test"]
end
end
describe "prepend" do
it "prepends prefix" do
@indexer.configure do
to_field "test", literal("one"), literal("two"), prepend("prefix.")
end
output = @indexer.map_record(@record)
assert_equal ["prefix.one", "prefix.two"], output["test"]
end
end
describe "gsub" do
it "gsubs" do
@indexer.configure do
to_field "test", literal("one1212two23three"), gsub(/\d+/, ' ')
end
output = @indexer.map_record(@record)
assert_equal ["one two three"], output["test"]
end
end
describe "delete_if" do
describe "argument is an Array" do
it "filters out selected values from accumulatd values" do
arg = [ "one", "three"]
@indexer.configure do
to_field "test", literal("one"), literal("two"), literal("three"), delete_if(arg)
end
output = @indexer.map_record(@record)
assert_equal ["two"], output["test"]
end
end
describe "argument is a Set" do
it "filters out selected values from accumulatd values" do
arg = [ "one", "three"].to_set
@indexer.configure do
to_field "test", literal("one"), literal("two"), literal("three"), delete_if(arg)
end
output = @indexer.map_record(@record)
assert_equal ["two"], output["test"]
end
end
describe "argument is a Regex" do
it "filters out selected values from accumulatd values" do
arg = /^t/
@indexer.configure do
to_field "test", literal("one"), literal("two"), literal("three"), delete_if(arg)
end
output = @indexer.map_record(@record)
assert_equal ["one"], output["test"]
end
end
describe "argument is a Procedure or Lambda" do
it "filters out selected values from accumulatd values" do
arg = ->(v) { v == "one" }
@indexer.configure do
to_field "test", literal("one"), literal("two"), literal("three"), delete_if(arg)
end
output = @indexer.map_record(@record)
assert_equal ["two", "three"], output["test"]
end
end
end
describe "select" do
describe "argument is an Array" do
it "selects a subset of values from accumulatd values" do
arg = [ "one", "three", "four"]
@indexer.configure do
to_field "test", literal("one"), literal("two"), literal("three"), select(arg)
end
output = @indexer.map_record(@record)
assert_equal ["one", "three"], output["test"]
end
end
describe "argument is a Set" do
it "selects a subset of values from accumulatd values" do
arg = [ "one", "three", "four"].to_set
@indexer.configure do
to_field "test", literal("one"), literal("two"), literal("three"), select(arg)
end
output = @indexer.map_record(@record)
assert_equal ["one", "three"], output["test"]
end
end
describe "argument is a Regex" do
it "selects a subset of values from accumulatd values" do
arg = /^t/
@indexer.configure do
to_field "test", literal("one"), literal("two"), literal("three"), select(arg)
end
output = @indexer.map_record(@record)
assert_equal ["two", "three"], output["test"]
end
end
describe "argument is a Procedure or Lambda" do
it "selects a subset of values from accumulatd values" do
arg = ->(v) { v != "one" }
@indexer.configure do
to_field "test", literal("one"), literal("two"), literal("three"), select(arg)
end
output = @indexer.map_record(@record)
assert_equal ["two", "three"], output["test"]
end
end
end
end