rbbt-util 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/bin/tchash.rb +15 -0
- data/bin/tsv.rb +14 -0
- data/lib/rbbt/util/cachehelper.rb +100 -0
- data/lib/rbbt/util/cmd.rb +140 -0
- data/lib/rbbt/util/data_module.rb +81 -0
- data/lib/rbbt/util/excel2tsv.rb +32 -0
- data/lib/rbbt/util/filecache.rb +58 -0
- data/lib/rbbt/util/log.rb +50 -0
- data/lib/rbbt/util/misc.rb +158 -0
- data/lib/rbbt/util/open.rb +200 -0
- data/lib/rbbt/util/pkg_config.rb +78 -0
- data/lib/rbbt/util/pkg_data.rb +110 -0
- data/lib/rbbt/util/pkg_software.rb +130 -0
- data/lib/rbbt/util/simpleDSL.rb +92 -0
- data/lib/rbbt/util/simpleopt.rb +56 -0
- data/lib/rbbt/util/tc_hash.rb +124 -0
- data/lib/rbbt/util/tmpfile.rb +42 -0
- data/lib/rbbt/util/tsv.rb +804 -0
- data/lib/rbbt-util.rb +13 -0
- data/lib/rbbt.rb +15 -0
- data/share/install/software/lib/install_helpers +257 -0
- data/test/rbbt/util/test_cmd.rb +30 -0
- data/test/rbbt/util/test_data_module.rb +45 -0
- data/test/rbbt/util/test_excel2tsv.rb +10 -0
- data/test/rbbt/util/test_filecache.rb +36 -0
- data/test/rbbt/util/test_misc.rb +22 -0
- data/test/rbbt/util/test_open.rb +89 -0
- data/test/rbbt/util/test_simpleDSL.rb +55 -0
- data/test/rbbt/util/test_simpleopt.rb +10 -0
- data/test/rbbt/util/test_tc_hash.rb +18 -0
- data/test/rbbt/util/test_tmpfile.rb +20 -0
- data/test/rbbt/util/test_tsv.rb +652 -0
- data/test/test_helper.rb +9 -0
- data/test/test_pkg.rb +38 -0
- data/test/test_rbbt.rb +90 -0
- metadata +185 -0
@@ -0,0 +1,652 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
|
2
|
+
require 'rbbt/util/tsv'
|
3
|
+
require 'rbbt/util/tmpfile'
|
4
|
+
|
5
|
+
class TestTSV < Test::Unit::TestCase
|
6
|
+
def test_keep_empty
|
7
|
+
content =<<-EOF
|
8
|
+
#Id ValueA ValueB Comment
|
9
|
+
row1 a|aa|aaa b c
|
10
|
+
row2 A B
|
11
|
+
EOF
|
12
|
+
|
13
|
+
TmpFile.with_file(content) do |filename|
|
14
|
+
data = {}
|
15
|
+
key_field, fields = TSV.parse(data, File.open(filename), :sep => /\s+/, :keep_empty => true)
|
16
|
+
assert_equal ["ValueA", "ValueB", "Comment"], fields
|
17
|
+
assert_equal ["c"], data["row1"][2]
|
18
|
+
assert_equal [""], data["row2"][2]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_slice
|
23
|
+
content =<<-EOF
|
24
|
+
#ID ValueA ValueB Comment
|
25
|
+
row1 a b c
|
26
|
+
row2 A B C
|
27
|
+
EOF
|
28
|
+
|
29
|
+
TmpFile.with_file(content) do |filename|
|
30
|
+
tsv = TSV.new(File.open(filename), :sep => /\s/)
|
31
|
+
assert_equal [["a"],["c"]], tsv.reorder(:main, ["ValueA", "Comment"])["row1"]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_headers
|
36
|
+
content =<<-EOF
|
37
|
+
#ID ValueA ValueB Comment
|
38
|
+
row1 a b c
|
39
|
+
row2 A B C
|
40
|
+
EOF
|
41
|
+
|
42
|
+
TmpFile.with_file(content) do |filename|
|
43
|
+
assert_equal ['ID', 'ValueA', 'ValueB', 'Comment'], TSV.headers(filename, :sep => ' ')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_headerless
|
48
|
+
content =<<-EOF
|
49
|
+
row1 a b c
|
50
|
+
row2 A B C
|
51
|
+
EOF
|
52
|
+
|
53
|
+
TmpFile.with_file(content) do |filename|
|
54
|
+
assert_equal 3, TSV.new(filename, :sep => ' ')['row1'].length
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_hash
|
59
|
+
content =<<-EOF
|
60
|
+
#Id ValueA ValueB
|
61
|
+
row1 a|aa|aaa b
|
62
|
+
row2 A B
|
63
|
+
EOF
|
64
|
+
|
65
|
+
TmpFile.with_file(content) do |filename|
|
66
|
+
data = {}
|
67
|
+
key_field, fields = TSV.parse(data, File.open(filename), :sep => /\s+/)
|
68
|
+
assert_equal "Id", key_field
|
69
|
+
assert_equal ["ValueA", "ValueB"], fields
|
70
|
+
assert_equal ["a", "aa", "aaa"], data["row1"][0]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_large
|
75
|
+
content =<<-EOF
|
76
|
+
#Id ValueA ValueB OtherID
|
77
|
+
row1 a|aa|aaa b Id1|Id2
|
78
|
+
row2 A B Id3
|
79
|
+
EOF
|
80
|
+
|
81
|
+
TmpFile.with_file(content) do |filename|
|
82
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :native => "OtherID", :large => true)
|
83
|
+
assert_equal "OtherID", tsv.key_field
|
84
|
+
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
85
|
+
assert_equal ["a", "aa", "aaa"], tsv["Id2"][1]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_tsv
|
90
|
+
content =<<-EOF
|
91
|
+
#Id ValueA ValueB OtherID
|
92
|
+
row1 a|aa|aaa b Id1|Id2
|
93
|
+
row2 A B Id3
|
94
|
+
EOF
|
95
|
+
|
96
|
+
TmpFile.with_file(content) do |filename|
|
97
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :native => "OtherID")
|
98
|
+
assert_equal "OtherID", tsv.key_field
|
99
|
+
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
100
|
+
assert_equal ["a", "aa", "aaa"], tsv["Id1"][1]
|
101
|
+
assert_equal ["a", "aa", "aaa"], tsv["Id2"][1]
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_open_file
|
106
|
+
content =<<-EOF
|
107
|
+
#Id ValueA ValueB OtherID
|
108
|
+
row1 a|aa|aaa b Id1|Id2
|
109
|
+
row2 A B Id3
|
110
|
+
row3 a C Id4
|
111
|
+
EOF
|
112
|
+
|
113
|
+
TmpFile.with_file(content) do |filename|
|
114
|
+
tsv = TSV.open_file(filename + '#:sep=/\s+/#:native=OtherID')
|
115
|
+
assert_equal "OtherID", tsv.key_field
|
116
|
+
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
117
|
+
assert_equal ["a", "aa", "aaa"], tsv["Id1"][1]
|
118
|
+
assert_equal ["a", "aa", "aaa"], tsv["Id2"][1]
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
def test_extra
|
125
|
+
content =<<-EOF
|
126
|
+
#Id ValueA ValueB OtherID
|
127
|
+
row1 a|aa|aaa b Id1|Id2
|
128
|
+
row2 A B Id3
|
129
|
+
EOF
|
130
|
+
|
131
|
+
TmpFile.with_file(content) do |filename|
|
132
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :native => "OtherID", :extra => 2)
|
133
|
+
assert_equal ["b"], tsv["Id2"][0]
|
134
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :native => "OtherID", :extra => 'ValueB')
|
135
|
+
assert_equal ["b"], tsv["Id2"][0]
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_case
|
140
|
+
content =<<-EOF
|
141
|
+
#Id ValueA ValueB OtherID
|
142
|
+
row1 a|aa|aaa b Id1|Id2
|
143
|
+
row2 A B Id3
|
144
|
+
EOF
|
145
|
+
|
146
|
+
TmpFile.with_file(content) do |filename|
|
147
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :native => "OtherID", :case_insensitive => true)
|
148
|
+
assert_equal "OtherID", tsv.key_field
|
149
|
+
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
150
|
+
assert_equal ["a", "aa", "aaa"], tsv["id1"][1]
|
151
|
+
assert_equal ["a", "aa", "aaa"], tsv["Id2"][1]
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_persistence
|
156
|
+
content =<<-EOF
|
157
|
+
#Id ValueA ValueB OtherID
|
158
|
+
row1 a|aa|aaa b Id1|Id2
|
159
|
+
row2 A B Id3
|
160
|
+
EOF
|
161
|
+
|
162
|
+
TmpFile.with_file(content) do |filename|
|
163
|
+
tsv = TSV.new(filename, :sep => /\s+/, :native => "OtherID", :persistence => true)
|
164
|
+
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
165
|
+
tsv['Id4'] = [["row3"],["aA"],["bB","bbBB"]]
|
166
|
+
assert_equal ["aA"], tsv["Id4"][1]
|
167
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :native => "OtherID", :persistence => true)
|
168
|
+
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
169
|
+
assert_equal ["aA"], tsv["Id4"][1]
|
170
|
+
assert_equal [["b"],["B"]], tsv.values_at("Id1", "Id3").collect{|values| values[2]}
|
171
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :native => "OtherID", :persistence => true, :flatten => true)
|
172
|
+
assert(tsv["Id3"].include? "A")
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_index_headerless
|
177
|
+
content =<<-EOF
|
178
|
+
row1 a|aa|aaa b Id1|Id2
|
179
|
+
row2 A B Id3
|
180
|
+
EOF
|
181
|
+
|
182
|
+
TmpFile.with_file(content) do |filename|
|
183
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
184
|
+
index = tsv.index(:case_insensitive => true, :field => 2)
|
185
|
+
assert index["row1"].include? "Id1"
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
def test_index
|
191
|
+
content =<<-EOF
|
192
|
+
#Id ValueA ValueB OtherID
|
193
|
+
row1 a|aa|aaa b Id1|Id2
|
194
|
+
row2 A B Id3
|
195
|
+
EOF
|
196
|
+
|
197
|
+
TmpFile.with_file(content) do |filename|
|
198
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :native => "OtherID", :persistence => false)
|
199
|
+
index = tsv.index(:case_insensitive => true)
|
200
|
+
assert index["row1"].include? "Id1"
|
201
|
+
assert_equal "OtherID", index.key_field
|
202
|
+
end
|
203
|
+
|
204
|
+
TmpFile.with_file(content) do |filename|
|
205
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :native => "OtherID")
|
206
|
+
index = tsv.index(:case_insensitive => true)
|
207
|
+
assert index["row1"].include? "Id1"
|
208
|
+
assert_equal "OtherID", index.key_field
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_best_index
|
213
|
+
content =<<-EOF
|
214
|
+
#Id ValueA ValueB OtherID
|
215
|
+
row1 a|aa|aaa b|A Id1
|
216
|
+
row2 A a|B Id3
|
217
|
+
EOF
|
218
|
+
|
219
|
+
TmpFile.with_file(content) do |filename|
|
220
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :native => "OtherID", :persistence => true)
|
221
|
+
index = tsv.index(:case_insensitive => false, :order => true)
|
222
|
+
assert_equal "Id1", index['a'].first
|
223
|
+
assert_equal "Id3", index['A'].first
|
224
|
+
assert_equal "OtherID", index.key_field
|
225
|
+
end
|
226
|
+
|
227
|
+
TmpFile.with_file(content) do |filename|
|
228
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :native => "OtherID")
|
229
|
+
index = tsv.index(:case_insensitive => true)
|
230
|
+
assert index["row1"].include? "Id1"
|
231
|
+
assert_equal "OtherID", index.key_field
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_values_at
|
236
|
+
content =<<-EOF
|
237
|
+
#Id ValueA ValueB OtherID
|
238
|
+
row1 a|aa|aaa b Id1|Id2
|
239
|
+
row2 A B Id3
|
240
|
+
EOF
|
241
|
+
|
242
|
+
TmpFile.with_file(content) do |filename|
|
243
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :native => "OtherID", :persistence => true)
|
244
|
+
index = tsv.index(:case_insensitive => true)
|
245
|
+
assert index.values_at(*["row1"]).first.include? "Id1"
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
def test_named_array
|
250
|
+
content =<<-EOF
|
251
|
+
#Id ValueA ValueB OtherID
|
252
|
+
row1 a|aa|aaa b Id1|Id2
|
253
|
+
row2 A B Id3
|
254
|
+
EOF
|
255
|
+
|
256
|
+
TmpFile.with_file(content) do |filename|
|
257
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :native => "OtherID", :case_insensitive => true)
|
258
|
+
assert_equal "OtherID", tsv.key_field
|
259
|
+
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
260
|
+
assert_equal ["a", "aa", "aaa"], tsv["id1"][1]
|
261
|
+
assert_equal ["a", "aa", "aaa"], tsv["Id2"]["ValueA"]
|
262
|
+
|
263
|
+
tsv_sliced = tsv.reorder(:main, ["ValueA", "ValueB"])
|
264
|
+
|
265
|
+
assert_equal ["ValueA", "ValueB"], tsv_sliced.fields
|
266
|
+
assert_equal ["a", "aa", "aaa"], tsv_sliced["id1"][0]
|
267
|
+
assert_equal ["a", "aa", "aaa"], tsv_sliced["Id2"]["ValueA"]
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_helpers
|
272
|
+
begin
|
273
|
+
require 'rbbt/sources/organism'
|
274
|
+
filename = File.join(Organism.datadir('Sce'), 'identifiers')
|
275
|
+
missing = true
|
276
|
+
index = TSV.index(filename, :persistence => true, :native => "Associated Gene Name")
|
277
|
+
assert index['1020'].include? 'CDK5'
|
278
|
+
index = TSV.index(filename, :persistence => true, :native => "Associated Gene Name")
|
279
|
+
assert index[[nil,'1020']].include? 'CDK5'
|
280
|
+
index = TSV.index(filename, :persistence => true, :native => "Associated Gene Name")
|
281
|
+
assert index[['MISSING','1020']].include? 'CDK5'
|
282
|
+
rescue Exception
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
|
287
|
+
def test_sort
|
288
|
+
content =<<-EOF
|
289
|
+
#Id ValueA ValueB OtherID
|
290
|
+
row1 a|aa|aaa b Id1|Id2
|
291
|
+
row2 A B Id3
|
292
|
+
EOF
|
293
|
+
|
294
|
+
TmpFile.with_file(content) do |filename|
|
295
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
296
|
+
assert_equal "row2", tsv.sort{|a,b| a[1]["ValueB"] <=> b[1]["ValueA"] }.first[0]
|
297
|
+
assert_equal "B", tsv.sort{|a,b| a[1]["ValueB"] <=> b[1]["ValueA"] }.first[1]["ValueB"].first
|
298
|
+
end
|
299
|
+
|
300
|
+
TmpFile.with_file(content) do |filename|
|
301
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
302
|
+
assert_equal "row2", tsv.sort_by{|k,v| v["ValueB"]}.first[0]
|
303
|
+
assert_equal "B", tsv.sort_by{|k,v| v["ValueB"]}.first[1]["ValueB"].first
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_to_s
|
308
|
+
content =<<-EOF
|
309
|
+
#Id ValueA ValueB OtherID
|
310
|
+
row1 a|aa|aaa b Id1|Id2
|
311
|
+
row2 A B Id3
|
312
|
+
EOF
|
313
|
+
TmpFile.with_file(content) do |filename|
|
314
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
315
|
+
assert_equal content, tsv.to_s
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
|
320
|
+
def test_smart_merge_single
|
321
|
+
content1 =<<-EOF
|
322
|
+
#Id ValueA ValueB
|
323
|
+
row1 a|aa|aaa b
|
324
|
+
row2 A B
|
325
|
+
EOF
|
326
|
+
|
327
|
+
content2 =<<-EOF
|
328
|
+
#ValueC ValueB OtherID
|
329
|
+
c|cc|ccc b Id1|Id2
|
330
|
+
C B Id3
|
331
|
+
EOF
|
332
|
+
|
333
|
+
tsv1 = tsv2 = nil
|
334
|
+
TmpFile.with_file(content1) do |filename|
|
335
|
+
tsv1 = TSV.new(File.open(filename), :sep => /\s+/, :unique => true)
|
336
|
+
end
|
337
|
+
|
338
|
+
TmpFile.with_file(content2) do |filename|
|
339
|
+
tsv2 = TSV.new(File.open(filename), :sep => /\s+/, :unique => true)
|
340
|
+
end
|
341
|
+
|
342
|
+
tsv1.smart_merge tsv2, "ValueB"
|
343
|
+
|
344
|
+
assert_equal "C", tsv1["row2"]["ValueC"]
|
345
|
+
assert %w(c cc ccc).include? tsv1["row1"]["ValueC"]
|
346
|
+
assert_equal "Id1", tsv1["row1"]["OtherID"]
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_smart_merge
|
350
|
+
content1 =<<-EOF
|
351
|
+
#Id ValueA ValueB
|
352
|
+
row1 a|aa|aaa b
|
353
|
+
row2 A B
|
354
|
+
EOF
|
355
|
+
|
356
|
+
content2 =<<-EOF
|
357
|
+
#ValueC ValueB OtherID
|
358
|
+
c|cc|ccc b Id1|Id2
|
359
|
+
C B Id3
|
360
|
+
EOF
|
361
|
+
|
362
|
+
tsv1 = tsv2 = nil
|
363
|
+
TmpFile.with_file(content1) do |filename|
|
364
|
+
tsv1 = TSV.new(File.open(filename), :sep => /\s+/)
|
365
|
+
end
|
366
|
+
|
367
|
+
TmpFile.with_file(content2) do |filename|
|
368
|
+
tsv2 = TSV.new(File.open(filename), :sep => /\s+/)
|
369
|
+
end
|
370
|
+
|
371
|
+
tsv1.smart_merge tsv2, "ValueB"
|
372
|
+
|
373
|
+
assert_equal %w(C), tsv1["row2"]["ValueC"]
|
374
|
+
assert_equal %w(Id1 Id2), tsv1["row1"]["OtherID"]
|
375
|
+
end
|
376
|
+
|
377
|
+
def test_smart_merge_through_index_find_headers
|
378
|
+
content1 =<<-EOF
|
379
|
+
#Id ValueA ValueBB
|
380
|
+
row1 a|aa|aaa bb
|
381
|
+
row2 A BB
|
382
|
+
EOF
|
383
|
+
|
384
|
+
content2 =<<-EOF
|
385
|
+
#ValueC ValueB OtherID ValueA
|
386
|
+
c|cc|ccc b Id1|Id2 aaaa
|
387
|
+
C B Id3 AA
|
388
|
+
EOF
|
389
|
+
|
390
|
+
index =<<-EOF
|
391
|
+
#ValueB ValueBB
|
392
|
+
b bb
|
393
|
+
B BB
|
394
|
+
EOF
|
395
|
+
|
396
|
+
tsv1 = tsv2 = nil
|
397
|
+
TmpFile.with_file(content1) do |filename|
|
398
|
+
tsv1 = TSV.new(File.open(filename), :sep => /\s+/)
|
399
|
+
end
|
400
|
+
|
401
|
+
TmpFile.with_file(content2) do |filename|
|
402
|
+
tsv2 = TSV.new(File.open(filename), :sep => /\s+/)
|
403
|
+
end
|
404
|
+
|
405
|
+
TmpFile.with_file(index) do |filename|
|
406
|
+
index = TSV.index(filename, :sep => /\s+/)
|
407
|
+
end
|
408
|
+
|
409
|
+
tsv1.smart_merge tsv2, index
|
410
|
+
|
411
|
+
assert_equal %w(Id1 Id2), tsv1["row1"]["OtherID"]
|
412
|
+
assert_equal %w(C), tsv1["row2"]["ValueC"]
|
413
|
+
|
414
|
+
assert_equal %w(a aa aaa aaaa), tsv1["row1"]["ValueA"]
|
415
|
+
end
|
416
|
+
|
417
|
+
|
418
|
+
def test_smart_merge_through_string_find_headers
|
419
|
+
content1 =<<-EOF
|
420
|
+
#Id ValueA ValueBB
|
421
|
+
row1 a|aa|aaa bb
|
422
|
+
row2 A BB
|
423
|
+
EOF
|
424
|
+
|
425
|
+
content2 =<<-EOF
|
426
|
+
#ValueC ValueB OtherID ValueA
|
427
|
+
c|cc|ccc b Id1|Id2 aaaa
|
428
|
+
C B Id3 AA
|
429
|
+
EOF
|
430
|
+
|
431
|
+
index =<<-EOF
|
432
|
+
#ValueB ValueBB
|
433
|
+
b bb
|
434
|
+
B BB
|
435
|
+
EOF
|
436
|
+
|
437
|
+
tsv1 = tsv2 = nil
|
438
|
+
TmpFile.with_file(content1) do |filename|
|
439
|
+
tsv1 = TSV.new(File.open(filename), :sep => /\s+/)
|
440
|
+
end
|
441
|
+
|
442
|
+
TmpFile.with_file(content2) do |filename|
|
443
|
+
tsv2 = TSV.new(File.open(filename), :sep => /\s+/)
|
444
|
+
end
|
445
|
+
|
446
|
+
TmpFile.with_file(index) do |filename|
|
447
|
+
tsv1.smart_merge tsv2, "through:#{filename}#:sep=/\\s+/"
|
448
|
+
end
|
449
|
+
|
450
|
+
assert_equal %w(Id1 Id2), tsv1["row1"]["OtherID"]
|
451
|
+
assert_equal %w(C), tsv1["row2"]["ValueC"]
|
452
|
+
|
453
|
+
assert_equal %w(a aa aaa aaaa), tsv1["row1"]["ValueA"]
|
454
|
+
end
|
455
|
+
|
456
|
+
def test_smart_merge_through_string
|
457
|
+
content1 =<<-EOF
|
458
|
+
#Id ValueA ValueBB
|
459
|
+
row1 a|aa|aaa bb
|
460
|
+
row2 A BB
|
461
|
+
EOF
|
462
|
+
|
463
|
+
content2 =<<-EOF
|
464
|
+
#ValueC ValueB OtherID ValueA
|
465
|
+
c|cc|ccc b Id1|Id2 aaaa
|
466
|
+
C B Id3 AA
|
467
|
+
EOF
|
468
|
+
|
469
|
+
index =<<-EOF
|
470
|
+
#ValueB ValueBB
|
471
|
+
b bb
|
472
|
+
B BB
|
473
|
+
EOF
|
474
|
+
|
475
|
+
tsv1 = tsv2 = nil
|
476
|
+
TmpFile.with_file(content1) do |filename|
|
477
|
+
tsv1 = TSV.new(File.open(filename), :sep => /\s+/)
|
478
|
+
end
|
479
|
+
|
480
|
+
TmpFile.with_file(content2) do |filename|
|
481
|
+
tsv2 = TSV.new(File.open(filename), :sep => /\s+/)
|
482
|
+
end
|
483
|
+
|
484
|
+
TmpFile.with_file(index) do |filename|
|
485
|
+
tsv1.smart_merge tsv2, "through:#{filename}#:sep=/\\s+/#using:ValueBB"
|
486
|
+
end
|
487
|
+
|
488
|
+
assert_equal %w(Id1 Id2), tsv1["row1"]["OtherID"]
|
489
|
+
assert_equal %w(C), tsv1["row2"]["ValueC"]
|
490
|
+
|
491
|
+
assert_equal %w(a aa aaa aaaa), tsv1["row1"]["ValueA"]
|
492
|
+
end
|
493
|
+
def test_smart_merge_common_fields
|
494
|
+
content1 =<<-EOF
|
495
|
+
#Id ValueA ValueB
|
496
|
+
row1 a|aa|aaa b
|
497
|
+
row2 A B
|
498
|
+
EOF
|
499
|
+
|
500
|
+
content2 =<<-EOF
|
501
|
+
#ValueC ValueB OtherID ValueA
|
502
|
+
c|cc|ccc b Id1|Id2 aaaa
|
503
|
+
C B Id3 AA
|
504
|
+
EOF
|
505
|
+
|
506
|
+
tsv1 = tsv2 = nil
|
507
|
+
TmpFile.with_file(content1) do |filename|
|
508
|
+
tsv1 = TSV.new(File.open(filename), :sep => /\s+/)
|
509
|
+
end
|
510
|
+
|
511
|
+
TmpFile.with_file(content2) do |filename|
|
512
|
+
tsv2 = TSV.new(File.open(filename), :sep => /\s+/)
|
513
|
+
end
|
514
|
+
|
515
|
+
tsv1.smart_merge tsv2, "ValueB"
|
516
|
+
|
517
|
+
assert_equal %w(Id1 Id2), tsv1["row1"]["OtherID"]
|
518
|
+
assert_equal %w(C), tsv1["row2"]["ValueC"]
|
519
|
+
|
520
|
+
assert_equal %w(a aa aaa aaaa), tsv1["row1"]["ValueA"]
|
521
|
+
end
|
522
|
+
|
523
|
+
def test_smart_merge_headerless
|
524
|
+
content1 =<<-EOF
|
525
|
+
row1 a|aa|aaa b
|
526
|
+
row2 A B
|
527
|
+
EOF
|
528
|
+
|
529
|
+
content2 =<<-EOF
|
530
|
+
c|cc|ccc b Id1|Id2
|
531
|
+
C B Id3
|
532
|
+
EOF
|
533
|
+
|
534
|
+
tsv1 = tsv2 = nil
|
535
|
+
TmpFile.with_file(content1) do |filename|
|
536
|
+
tsv1 = TSV.new(File.open(filename), :sep => /\s+/)
|
537
|
+
end
|
538
|
+
|
539
|
+
TmpFile.with_file(content2) do |filename|
|
540
|
+
tsv2 = TSV.new(File.open(filename), :sep => /\s+/)
|
541
|
+
end
|
542
|
+
|
543
|
+
tsv1.smart_merge tsv2, 1
|
544
|
+
|
545
|
+
assert_equal %w(C), tsv1["row2"][2]
|
546
|
+
assert_equal %w(Id1 Id2), tsv1["row1"][3]
|
547
|
+
end
|
548
|
+
|
549
|
+
|
550
|
+
def test_reorder_simple
|
551
|
+
content =<<-EOF
|
552
|
+
#Id ValueA ValueB OtherID
|
553
|
+
row1 a|aa|aaa b Id1|Id2
|
554
|
+
row2 A B Id3
|
555
|
+
row3 a C Id4
|
556
|
+
EOF
|
557
|
+
|
558
|
+
TmpFile.with_file(content) do |filename|
|
559
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
560
|
+
|
561
|
+
tsv1 = tsv.reorder("ValueA")
|
562
|
+
|
563
|
+
assert_equal "ValueA", tsv1.key_field
|
564
|
+
assert_equal %w(Id ValueB OtherID), tsv1.fields
|
565
|
+
assert_equal ["B"], tsv1["A"]["ValueB"]
|
566
|
+
assert_equal ["b","C"], tsv1["a"]["ValueB"]
|
567
|
+
assert_equal ["b"], tsv1["aa"]["ValueB"]
|
568
|
+
|
569
|
+
end
|
570
|
+
end
|
571
|
+
|
572
|
+
def test_reorder_simple_headerless
|
573
|
+
content =<<-EOF
|
574
|
+
row1 a|aa|aaa b Id1|Id2
|
575
|
+
row2 A B Id3
|
576
|
+
row3 a C Id4
|
577
|
+
EOF
|
578
|
+
|
579
|
+
TmpFile.with_file(content) do |filename|
|
580
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
581
|
+
|
582
|
+
tsv1 = tsv.reorder(0)
|
583
|
+
|
584
|
+
assert_nil tsv1.key_field
|
585
|
+
assert_equal ["B"], tsv1["A"][1]
|
586
|
+
assert_equal ["b","C"], tsv1["a"][1]
|
587
|
+
assert_equal ["b"], tsv1["aa"][1]
|
588
|
+
assert_equal ["row1"], tsv1["aa"][0]
|
589
|
+
assert_equal ["row1","row3"], tsv1["a"][0]
|
590
|
+
end
|
591
|
+
end
|
592
|
+
|
593
|
+
|
594
|
+
def test_reorder_remove_field
|
595
|
+
content =<<-EOF
|
596
|
+
#Id ValueA ValueB OtherID
|
597
|
+
row1 a|aa|aaa b Id1|Id2
|
598
|
+
row2 A B Id3
|
599
|
+
row3 a C Id4
|
600
|
+
EOF
|
601
|
+
|
602
|
+
TmpFile.with_file(content) do |filename|
|
603
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
604
|
+
|
605
|
+
tsv1 = tsv.reorder("ValueA", ["ValueB", "Id"])
|
606
|
+
|
607
|
+
assert_equal "ValueA", tsv1.key_field
|
608
|
+
assert_equal %w(ValueB Id), tsv1.fields
|
609
|
+
assert_equal ["B"], tsv1["A"]["ValueB"]
|
610
|
+
assert_equal ["b","C"], tsv1["a"]["ValueB"]
|
611
|
+
assert_equal ["row1"], tsv1["aa"]["Id"]
|
612
|
+
assert_equal ["row1","row3"], tsv1["a"]["Id"]
|
613
|
+
end
|
614
|
+
end
|
615
|
+
|
616
|
+
def test_through
|
617
|
+
content =<<-EOF
|
618
|
+
#Id ValueA ValueB OtherID
|
619
|
+
row1 a|aa|aaa b Id1|Id2
|
620
|
+
row2 A B Id3
|
621
|
+
row3 a C Id4
|
622
|
+
EOF
|
623
|
+
|
624
|
+
TmpFile.with_file(content) do |filename|
|
625
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
626
|
+
|
627
|
+
tsv.through "ValueA" do |key, values|
|
628
|
+
assert(tsv.keys.include? values["Id"].first)
|
629
|
+
end
|
630
|
+
end
|
631
|
+
end
|
632
|
+
|
633
|
+
def test_process
|
634
|
+
content =<<-EOF
|
635
|
+
#Id ValueA ValueB OtherID
|
636
|
+
row1 a|aa|aaa b Id1|Id2
|
637
|
+
row2 A B Id3
|
638
|
+
row3 a C Id4
|
639
|
+
EOF
|
640
|
+
|
641
|
+
TmpFile.with_file(content) do |filename|
|
642
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
643
|
+
|
644
|
+
tsv.process "ValueA" do |field_values,key,values|
|
645
|
+
field_values.collect{|v| "Pref:#{v}"}
|
646
|
+
end
|
647
|
+
|
648
|
+
assert_equal ["Pref:A"], tsv["row2"]["ValueA"]
|
649
|
+
end
|
650
|
+
end
|
651
|
+
end
|
652
|
+
|
data/test/test_helper.rb
ADDED
data/test/test_pkg.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
require 'rbbt'
|
3
|
+
require 'rbbt/util/pkg_data'
|
4
|
+
require 'rbbt/util/pkg_config'
|
5
|
+
require 'rbbt/util/data_module'
|
6
|
+
require 'yaml'
|
7
|
+
|
8
|
+
module A
|
9
|
+
extend PKGConfig
|
10
|
+
extend PKGData
|
11
|
+
|
12
|
+
self.load_cfg(%w(datadir), {"datadir" => "/tmp/A"}.to_yaml)
|
13
|
+
end
|
14
|
+
|
15
|
+
module B
|
16
|
+
extend PKGConfig
|
17
|
+
extend PKGData
|
18
|
+
|
19
|
+
self.load_cfg(%w(datadir), {"datadir" => "/tmp/B"}.to_yaml)
|
20
|
+
end
|
21
|
+
|
22
|
+
module DA
|
23
|
+
PKG=A
|
24
|
+
extend DataModule
|
25
|
+
end
|
26
|
+
|
27
|
+
module DB
|
28
|
+
PKG=B
|
29
|
+
extend DataModule
|
30
|
+
end
|
31
|
+
|
32
|
+
class TestPKG < Test::Unit::TestCase
|
33
|
+
def test_datadir
|
34
|
+
assert_equal "/tmp/A", A.datadir
|
35
|
+
assert_equal "/tmp/B", B.datadir
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|