unipept 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,6 @@ module Unipept
14
14
  assert_equal(%w(a b c), runner.arguments)
15
15
  assert(!runner.configuration.nil?)
16
16
  assert_equal('http://test_host/api/v1/test.json', runner.url)
17
- assert_equal('http://test_host/api/v1/messages.json', runner.message_url)
18
17
  assert(/Unipept CLI - unipept [0-9]\.[0-9]\.[0-9]/.match runner.user_agent)
19
18
  end
20
19
 
@@ -108,8 +107,35 @@ module Unipept
108
107
  assert_equal(%w(a b c), output)
109
108
  end
110
109
 
110
+ def test_default_batch_size
111
+ assert_raises NotImplementedError do
112
+ new_runner.default_batch_size
113
+ end
114
+ end
115
+
111
116
  def test_batch_size
112
- assert_equal(100, new_runner.batch_size)
117
+ r = new_runner
118
+ def r.default_batch_size
119
+ 100
120
+ end
121
+ assert_equal(100, r.batch_size)
122
+ end
123
+
124
+ def test_argument_batch_size
125
+ runner = new_runner('test', host: 'http://param_host', batch: '123')
126
+ assert_equal(123, runner.batch_size)
127
+ end
128
+
129
+ def test_number_of_parallel_requests
130
+ assert_equal(10, new_runner.concurrent_requests)
131
+ runner = new_runner('test', host: 'http://param_host', parallel: '123')
132
+ assert_equal(123, runner.concurrent_requests)
133
+ end
134
+
135
+ def test_queue_size
136
+ assert_equal(200, new_runner.queue_size)
137
+ runner = new_runner('test', host: 'http://param_host', parallel: '100')
138
+ assert_equal(2000, runner.queue_size)
113
139
  end
114
140
 
115
141
  def test_default_formatter
@@ -152,24 +178,6 @@ module Unipept
152
178
  assert_equal([/^field.*$/], runner.selected_fields)
153
179
  end
154
180
 
155
- def test_never_recently_fetched
156
- runner = new_runner
157
- runner.configuration.delete('last_fetch_date')
158
- assert(!runner.recently_fetched?)
159
- end
160
-
161
- def test_old_recently_fetched
162
- runner = new_runner
163
- runner.configuration['last_fetch_date'] = Time.now - 60 * 60 * 25
164
- assert(!runner.recently_fetched?)
165
- end
166
-
167
- def test_recently_recently_fetched
168
- runner = new_runner
169
- runner.configuration['last_fetch_date'] = Time.now - 60 * 60 * 1
170
- assert(runner.recently_fetched?)
171
- end
172
-
173
181
  def test_basic_construct_request_body
174
182
  runner = new_runner('test', host: 'http://param_host')
175
183
  body = runner.construct_request_body('test')
@@ -215,104 +223,6 @@ module Unipept
215
223
  assert_equal(false, body[:names])
216
224
  end
217
225
 
218
- def test_print_server_message
219
- runner = new_runner
220
- runner.stub(:recently_fetched?, false) do
221
- runner.stub(:fetch_server_message, 'message') do
222
- out, _err = capture_io_while do
223
- def $stdout.tty?
224
- true
225
- end
226
- runner.print_server_message
227
- end
228
- assert_equal('message', out.chomp)
229
- end
230
- end
231
- end
232
-
233
- def test_quiet_print_server_message
234
- runner = new_runner('test', host: 'bla', quiet: true)
235
- runner.stub(:recently_fetched?, false) do
236
- runner.stub(:fetch_server_message, 'message') do
237
- out, _err = capture_io_while do
238
- def $stdout.tty?
239
- true
240
- end
241
- $stdout.tty?
242
- runner.print_server_message
243
- end
244
- assert_equal('', out)
245
- end
246
- end
247
- end
248
-
249
- def test_no_tty_print_server_message
250
- runner = new_runner
251
- runner.stub(:recently_fetched?, false) do
252
- runner.stub(:fetch_server_message, 'message') do
253
- out, _err = capture_io_while do
254
- def $stdout.tty?
255
- false
256
- end
257
- runner.print_server_message
258
- end
259
- assert_equal('', out)
260
- end
261
- end
262
- end
263
-
264
- def test_recent_print_server_message
265
- runner = new_runner
266
- runner.stub(:recently_fetched?, true) do
267
- runner.stub(:fetch_server_message, 'message') do
268
- out, _err = capture_io_while do
269
- def $stdout.tty?
270
- true
271
- end
272
- runner.print_server_message
273
- end
274
- assert_equal('', out)
275
- end
276
- end
277
- end
278
-
279
- def test_empty_print_server_message
280
- runner = new_runner
281
- runner.stub(:recently_fetched?, false) do
282
- runner.stub(:fetch_server_message, '') do
283
- out, _err = capture_io_while do
284
- def $stdout.tty?
285
- true
286
- end
287
- runner.print_server_message
288
- end
289
- assert_equal('', out)
290
- end
291
- end
292
- end
293
-
294
- def test_fetch_server_message
295
- runner = new_runner('test', host: 'http://api.unipept.ugent.be')
296
- assert(!runner.fetch_server_message.nil?)
297
- end
298
-
299
- def test_stdout_write_to_output
300
- runner = new_runner
301
- out, _err = capture_io_while do
302
- runner.write_to_output('hello world')
303
- end
304
- assert_equal('hello world', out.chomp)
305
- end
306
-
307
- def test_file_write_to_output
308
- runner = new_runner('test', host: 'test', output: 'output_file')
309
- out, _err = capture_io_while do
310
- runner.write_to_output('hello world')
311
- end
312
- assert_equal('', out)
313
- assert_equal('hello world', IO.foreach('output_file').next.chomp)
314
- end
315
-
316
226
  def test_glob_to_regex
317
227
  runner = new_runner
318
228
  assert(/^simple$/, runner.glob_to_regex('simple'))
@@ -418,9 +328,6 @@ module Unipept
418
328
  def test_run
419
329
  runner = new_runner('taxonomy', host: 'http://api.unipept.ugent.be')
420
330
  out, err = capture_io_while do
421
- def runner.print_server_message
422
- puts 'server message'
423
- end
424
331
  def runner.input_iterator
425
332
  %w(0 1 2).each
426
333
  end
@@ -431,7 +338,6 @@ module Unipept
431
338
  end
432
339
  lines = out.each_line
433
340
  assert_equal('', err)
434
- assert_equal('server message', lines.next.chomp)
435
341
  assert(lines.next.start_with? 'taxon_id')
436
342
  assert(lines.next.start_with? '1,root')
437
343
  assert(lines.next.start_with? '2,Bacteria')
@@ -2,12 +2,24 @@ require_relative '../../../lib/commands'
2
2
 
3
3
  module Unipept
4
4
  class UnipeptPept2lcaTestCase < Unipept::TestCase
5
+ def test_default_batch_size
6
+ command = Cri::Command.define { name 'pept2lca' }
7
+ pept2lca = Commands::Pept2lca.new({ host: 'http://api.unipept.ugent.be' }, [], command)
8
+ assert_equal(1000, pept2lca.default_batch_size)
9
+ pept2lca.options[:all] = true
10
+ assert_equal(100, pept2lca.default_batch_size)
11
+ end
12
+
13
+ def test_argument_batch_size
14
+ command = Cri::Command.define { name 'pept2lca' }
15
+ pept2lca = Commands::Pept2lca.new({ host: 'http://api.unipept.ugent.be', batch: '123' }, [], command)
16
+ assert_equal(123, pept2lca.batch_size)
17
+ end
18
+
5
19
  def test_batch_size
6
20
  command = Cri::Command.define { name 'pept2lca' }
7
21
  pept2lca = Commands::Pept2lca.new({ host: 'http://api.unipept.ugent.be' }, [], command)
8
22
  assert_equal(1000, pept2lca.batch_size)
9
- pept2lca.options[:all] = true
10
- assert_equal(100, pept2lca.batch_size)
11
23
  end
12
24
 
13
25
  def test_help
@@ -36,5 +48,43 @@ module Unipept
36
48
  assert(lines.next.start_with? 'AALTER,1,root,no rank')
37
49
  assert_raises(StopIteration) { lines.next }
38
50
  end
51
+
52
+ def test_run_with_fasta_multiple_batches
53
+ out, err = capture_io_while do
54
+ Commands::Unipept.run(%w(pept2lca --host http://api.unipept.ugent.be --batch 2 >test AALTER AALER >tost AALTER))
55
+ end
56
+ lines = out.each_line
57
+ assert_equal('', err)
58
+ assert(lines.next.start_with? 'fasta_header,peptide,taxon_id')
59
+ assert(lines.next.start_with? '>test,AALTER,1,root,no rank')
60
+ assert(lines.next.start_with? '>test,AALER,1,root,no rank')
61
+ assert(lines.next.start_with? '>tost,AALTER,1,root,no rank')
62
+ assert_raises(StopIteration) { lines.next }
63
+ end
64
+
65
+ def test_run_with_fasta_multiple_batches_json
66
+ out, err = capture_io_while do
67
+ Commands::Unipept.run(%w(pept2lca --host http://api.unipept.ugent.be --batch 2 --format json >test AALTER AALER >tost AALTER))
68
+ end
69
+ lines = out.each_line
70
+ assert_equal('', err)
71
+ output = lines.to_a.join('').chomp
72
+ assert(output.start_with? '[')
73
+ assert(output.end_with? ']')
74
+ assert(!output.include?('}{'))
75
+ assert(output.include? 'fasta_header')
76
+ end
77
+
78
+ def test_run_with_fasta_multiple_batches_xml
79
+ out, err = capture_io_while do
80
+ Commands::Unipept.run(%w(pept2lca --host http://api.unipept.ugent.be --batch 2 --format xml >test AALTER AALER >tost AALTER))
81
+ end
82
+ lines = out.each_line
83
+ assert_equal('', err)
84
+ output = lines.to_a.join('').chomp
85
+ assert(output.start_with? '<results>')
86
+ assert(output.end_with? '</results>')
87
+ assert(output.include? '<fasta_header>')
88
+ end
39
89
  end
40
90
  end
@@ -2,12 +2,12 @@ require_relative '../../../lib/commands'
2
2
 
3
3
  module Unipept
4
4
  class UnipeptPept2protTestCase < Unipept::TestCase
5
- def test_batch_size
5
+ def test_default_batch_size
6
6
  command = Cri::Command.define { name 'pept2prot' }
7
7
  pept2prot = Commands::Pept2prot.new({ host: 'http://api.unipept.ugent.be' }, [], command)
8
- assert_equal(10, pept2prot.batch_size)
8
+ assert_equal(10, pept2prot.default_batch_size)
9
9
  pept2prot.options[:all] = true
10
- assert_equal(5, pept2prot.batch_size)
10
+ assert_equal(5, pept2prot.default_batch_size)
11
11
  end
12
12
 
13
13
  def test_help
@@ -35,5 +35,42 @@ module Unipept
35
35
  assert(lines.next.start_with? 'peptide,uniprot_id,taxon_id')
36
36
  assert(lines.next.start_with? 'ENFVYIAK,')
37
37
  end
38
+
39
+ def test_run_with_fasta_multiple_batches
40
+ out, err = capture_io_while do
41
+ Commands::Unipept.run(%w(pept2prot --host http://api.unipept.ugent.be --batch 2 >test EGGAGSSTGQR ENFVYIAK >tost EGGAGSSTGQR))
42
+ end
43
+ lines = out.each_line
44
+ assert_equal('', err)
45
+ assert(lines.next.start_with? 'fasta_header,peptide,uniprot_id,taxon_id')
46
+ assert(lines.select { |line| line.start_with? '>test,EGGAGSSTGQR,' }.size >= 1)
47
+ assert(lines.select { |line| line.start_with? '>test,ENFVYIAK,' }.size >= 1)
48
+ assert(lines.select { |line| line.start_with? '>tost,EGGAGSSTGQR,' }.size >= 1)
49
+ end
50
+
51
+ def test_run_with_fasta_multiple_batches_json
52
+ out, err = capture_io_while do
53
+ Commands::Unipept.run(%w(pept2prot --host http://api.unipept.ugent.be --batch 2 --format json >test EGGAGSSTGQR ENFVYIAK >tost EGGAGSSTGQR))
54
+ end
55
+ lines = out.each_line
56
+ assert_equal('', err)
57
+ output = lines.to_a.join('').chomp
58
+ assert(output.start_with? '[')
59
+ assert(output.end_with? ']')
60
+ assert(!output.include?('}{'))
61
+ assert(output.include? 'fasta_header')
62
+ end
63
+
64
+ def test_run_with_fasta_multiple_batches_xml
65
+ out, err = capture_io_while do
66
+ Commands::Unipept.run(%w(pept2prot --host http://api.unipept.ugent.be --batch 2 --format xml >test EGGAGSSTGQR ENFVYIAK >tost EGGAGSSTGQR))
67
+ end
68
+ lines = out.each_line
69
+ assert_equal('', err)
70
+ output = lines.to_a.join('').chomp
71
+ assert(output.start_with? '<results>')
72
+ assert(output.end_with? '</results>')
73
+ assert(output.include? '<fasta_header>')
74
+ end
38
75
  end
39
76
  end
@@ -2,12 +2,12 @@ require_relative '../../../lib/commands'
2
2
 
3
3
  module Unipept
4
4
  class UnipeptPept2taxaTestCase < Unipept::TestCase
5
- def test_batch_size
5
+ def test_default_batch_size
6
6
  command = Cri::Command.define { name 'pept2taxa' }
7
7
  pept2taxa = Commands::Pept2taxa.new({ host: 'http://api.unipept.ugent.be' }, [], command)
8
- assert_equal(10, pept2taxa.batch_size)
8
+ assert_equal(10, pept2taxa.default_batch_size)
9
9
  pept2taxa.options[:all] = true
10
- assert_equal(5, pept2taxa.batch_size)
10
+ assert_equal(5, pept2taxa.default_batch_size)
11
11
  end
12
12
 
13
13
  def test_help
@@ -35,5 +35,42 @@ module Unipept
35
35
  assert(lines.next.start_with? 'peptide,taxon_id,taxon_name,taxon_rank')
36
36
  assert(lines.next.start_with? 'ENFVYIAK,')
37
37
  end
38
+
39
+ def test_run_with_fasta_multiple_batches
40
+ out, err = capture_io_while do
41
+ Commands::Unipept.run(%w(pept2taxa --host http://api.unipept.ugent.be --batch 2 >test EGGAGSSTGQR ENFVYIAK >tost EGGAGSSTGQR))
42
+ end
43
+ lines = out.each_line
44
+ assert_equal('', err)
45
+ assert(lines.next.start_with? 'fasta_header,peptide,taxon_id,taxon_name,taxon_rank')
46
+ assert(lines.select { |line| line.start_with? '>test,EGGAGSSTGQR,' }.size >= 1)
47
+ assert(lines.select { |line| line.start_with? '>test,ENFVYIAK,' }.size >= 1)
48
+ assert(lines.select { |line| line.start_with? '>tost,EGGAGSSTGQR,' }.size >= 1)
49
+ end
50
+
51
+ def test_run_with_fasta_multiple_batches_json
52
+ out, err = capture_io_while do
53
+ Commands::Unipept.run(%w(pept2taxa --host http://api.unipept.ugent.be --batch 2 --format json >test EGGAGSSTGQR ENFVYIAK >tost EGGAGSSTGQR))
54
+ end
55
+ lines = out.each_line
56
+ assert_equal('', err)
57
+ output = lines.to_a.join('').chomp
58
+ assert(output.start_with? '[')
59
+ assert(output.end_with? ']')
60
+ assert(!output.include?('}{'))
61
+ assert(output.include? 'fasta_header')
62
+ end
63
+
64
+ def test_run_with_fasta_multiple_batches_xml
65
+ out, err = capture_io_while do
66
+ Commands::Unipept.run(%w(pept2taxa --host http://api.unipept.ugent.be --batch 2 --format xml >test EGGAGSSTGQR ENFVYIAK >tost EGGAGSSTGQR))
67
+ end
68
+ lines = out.each_line
69
+ assert_equal('', err)
70
+ output = lines.to_a.join('').chomp
71
+ assert(output.start_with? '<results>')
72
+ assert(output.end_with? '</results>')
73
+ assert(output.include? '<fasta_header>')
74
+ end
38
75
  end
39
76
  end
@@ -2,11 +2,11 @@ require_relative '../../../lib/commands'
2
2
 
3
3
  module Unipept
4
4
  class UnipeptTaxa2lcaTestCase < Unipept::TestCase
5
- def test_batch_size
5
+ def test_default_batch_size
6
6
  command = Cri::Command.define { name 'taxa2lca' }
7
7
  taxa2lca = Commands::Taxa2lca.new({ host: 'http://api.unipept.ugent.be' }, [], command)
8
8
  assert_raises RuntimeError do
9
- taxa2lca.batch_size
9
+ taxa2lca.default_batch_size
10
10
  end
11
11
  end
12
12
 
@@ -35,5 +35,29 @@ module Unipept
35
35
  assert(lines.next.start_with? 'taxon_id,taxon_name,taxon_rank')
36
36
  assert(lines.next.start_with? '1678,Bifidobacterium,genus')
37
37
  end
38
+
39
+ def test_run_xml
40
+ out, err = capture_io_while do
41
+ Commands::Unipept.run(%w(taxa2lca --host http://api.unipept.ugent.be --format xml 216816 1680))
42
+ end
43
+ lines = out.each_line
44
+ output = lines.to_a.join('').chomp
45
+ assert_equal('', err)
46
+ assert(output.start_with? '<results>')
47
+ assert(output.end_with? '</results>')
48
+ end
49
+
50
+ def test_run_json
51
+ out, err = capture_io_while do
52
+ Commands::Unipept.run(%w(taxa2lca --host http://api.unipept.ugent.be --format json 216816 1680))
53
+ end
54
+ lines = out.each_line
55
+ output = lines.to_a.join('').chomp
56
+ assert_equal('', err)
57
+ assert(output.start_with? '[')
58
+ assert(output.end_with? ']')
59
+ assert(!output.include?('}{'))
60
+ assert(!output.include?(']['))
61
+ end
38
62
  end
39
63
  end
@@ -2,10 +2,10 @@ require_relative '../../../lib/commands'
2
2
 
3
3
  module Unipept
4
4
  class UnipeptTaxonomyTestCase < Unipept::TestCase
5
- def test_batch_size
5
+ def test_default_batch_size
6
6
  command = Cri::Command.define { name 'taxonomy' }
7
7
  taxonomy = Commands::Taxonomy.new({ host: 'http://api.unipept.ugent.be' }, [], command)
8
- assert_equal(100, taxonomy.batch_size)
8
+ assert_equal(100, taxonomy.default_batch_size)
9
9
  end
10
10
 
11
11
  def test_help
@@ -33,5 +33,42 @@ module Unipept
33
33
  assert(lines.next.start_with? 'taxon_id,taxon_name,taxon_rank')
34
34
  assert(lines.next.start_with? '1,root,no rank')
35
35
  end
36
+
37
+ def test_run_with_fasta_multiple_batches
38
+ out, err = capture_io_while do
39
+ Commands::Unipept.run(%w(taxonomy --host http://api.unipept.ugent.be --batch 2 >test 1 216816 >tost 1))
40
+ end
41
+ lines = out.each_line
42
+ assert_equal('', err)
43
+ assert(lines.next.start_with? 'fasta_header,taxon_id,taxon_name,taxon_rank')
44
+ assert(lines.select { |line| line.start_with? '>test,1,' }.size >= 1)
45
+ assert(lines.select { |line| line.start_with? '>test,216816,' }.size >= 1)
46
+ assert(lines.select { |line| line.start_with? '>tost,1,' }.size >= 1)
47
+ end
48
+
49
+ def test_run_with_fasta_multiple_batches_json
50
+ out, err = capture_io_while do
51
+ Commands::Unipept.run(%w(taxonomy --host http://api.unipept.ugent.be --batch 2 --format json >test 1 216816 >tost 1))
52
+ end
53
+ lines = out.each_line
54
+ assert_equal('', err)
55
+ output = lines.to_a.join('').chomp
56
+ assert(output.start_with? '[')
57
+ assert(output.end_with? ']')
58
+ assert(!output.include?('}{'))
59
+ assert(output.include? 'fasta_header')
60
+ end
61
+
62
+ def test_run_with_fasta_multiple_batches_xml
63
+ out, err = capture_io_while do
64
+ Commands::Unipept.run(%w(taxonomy --host http://api.unipept.ugent.be --batch 2 --format xml >test 1 216816 >tost 1))
65
+ end
66
+ lines = out.each_line
67
+ assert_equal('', err)
68
+ output = lines.to_a.join('').chomp
69
+ assert(output.start_with? '<results>')
70
+ assert(output.end_with? '</results>')
71
+ assert(output.include? '<fasta_header>')
72
+ end
36
73
  end
37
74
  end
@@ -33,20 +33,60 @@ module Unipept
33
33
  assert_equal('csv', formatter.type)
34
34
  end
35
35
 
36
+ def test_group_by_first_key
37
+ array = [{ key1: 'v1', key2: 'v2' }, { key1: 'v1', key2: 'v3' }, { key1: 'v4', key2: 'v2' }]
38
+ grouped = formatter.group_by_first_key(array)
39
+ assert_equal({ 'v1' => [{ key1: 'v1', key2: 'v2' }, { key1: 'v1', key2: 'v3' }], 'v4' => [{ key1: 'v4', key2: 'v2' }] }, grouped)
40
+ end
41
+
42
+ def test_integrate_fasta_headers
43
+ fasta = [['>test', '5']]
44
+ object = [TestObject.test_object, TestObject.test_object]
45
+ integrated = Array.new(2, { fasta_header: '>test' }.merge(TestObject.test_object))
46
+ assert_equal(integrated, formatter.integrate_fasta_headers(object, fasta))
47
+ end
48
+
36
49
  def formatter
37
50
  Formatter.new
38
51
  end
39
52
 
40
53
  def test_header
41
- assert_equal('', formatter.header(TestObject.test_object))
54
+ assert_raises NotImplementedError do
55
+ formatter.header(TestObject.test_object, nil)
56
+ end
57
+ end
58
+
59
+ def test_footer
60
+ assert_raises NotImplementedError do
61
+ formatter.footer
62
+ end
42
63
  end
43
64
 
44
65
  def test_type
45
- assert_equal('', formatter.type)
66
+ assert_raises NotImplementedError do
67
+ formatter.type
68
+ end
46
69
  end
47
70
 
48
71
  def test_format
49
- assert_equal(TestObject.test_object, formatter.format(TestObject.test_object))
72
+ f = formatter
73
+ def f.integrate_fasta_headers(_a, _b)
74
+ puts 'header'
75
+ end
76
+ def f.convert(_a, _b)
77
+ 'body'
78
+ end
79
+ assert_equal('body', f.format(TestObject.test_object, nil, false))
80
+ out, _err = capture_io_while do
81
+ assert_equal('body', f.format(TestObject.test_object, [], false))
82
+ end
83
+ assert_equal('header', out.chomp)
84
+ end
85
+
86
+ def test_convert
87
+ assert_raises NotImplementedError do
88
+ formatter.convert(TestObject.test_object, false)
89
+ end
50
90
  end
51
91
  end
52
92
 
@@ -56,15 +96,27 @@ module Unipept
56
96
  end
57
97
 
58
98
  def test_header
59
- assert_equal('', formatter.header(TestObject.test_object))
99
+ assert_equal('[', formatter.header(TestObject.test_object, nil))
100
+ end
101
+
102
+ def test_footer
103
+ assert_equal("]\n", formatter.footer)
60
104
  end
61
105
 
62
106
  def test_type
63
107
  assert_equal('json', formatter.type)
64
108
  end
65
109
 
66
- def test_format
67
- assert_equal(TestObject.as_json, formatter.format(TestObject.test_object))
110
+ def test_convert
111
+ assert_equal(TestObject.as_json, formatter.convert([TestObject.test_object], true))
112
+ assert_equal(',' + TestObject.as_json, formatter.convert([TestObject.test_object], false))
113
+ end
114
+
115
+ def test_format_with_fasta
116
+ fasta = [['>test', '5']]
117
+ output = formatter.format([TestObject.test_object], fasta, true)
118
+ json = '{"fasta_header":">test","integer":5,"string":"string","list":["a",2,false]}'
119
+ assert_equal(json, output)
68
120
  end
69
121
  end
70
122
 
@@ -80,21 +132,26 @@ module Unipept
80
132
  assert_equal('fasta_header,' + TestObject.as_csv_header, formatter.header(object, fasta))
81
133
  end
82
134
 
135
+ def test_footer
136
+ assert_equal('', formatter.footer)
137
+ end
138
+
83
139
  def test_type
84
140
  assert_equal('csv', formatter.type)
85
141
  end
86
142
 
87
- def test_format
143
+ def test_convert
88
144
  object = [TestObject.test_object, TestObject.test_object]
89
145
  csv = [TestObject.as_csv, TestObject.as_csv, ''].join("\n")
90
- assert_equal(csv, formatter.format(object))
146
+ assert_equal(csv, formatter.convert(object, true))
147
+ assert_equal(csv, formatter.convert(object, false))
91
148
  end
92
149
 
93
150
  def test_format_with_fasta
94
151
  fasta = [['>test', '5']]
95
152
  object = [TestObject.test_object, TestObject.test_object]
96
153
  csv = ['>test,' + TestObject.as_csv, '>test,' + TestObject.as_csv, ''].join("\n")
97
- assert_equal(csv, formatter.format(object, fasta))
154
+ assert_equal(csv, formatter.format(object, fasta, false))
98
155
  end
99
156
  end
100
157
 
@@ -104,15 +161,28 @@ module Unipept
104
161
  end
105
162
 
106
163
  def test_header
107
- assert_equal('', formatter.header(TestObject.test_object))
164
+ assert_equal('<results>', formatter.header(TestObject.test_object))
165
+ end
166
+
167
+ def test_footer
168
+ assert_equal("</results>\n", formatter.footer)
108
169
  end
109
170
 
110
171
  def test_type
111
172
  assert_equal('xml', formatter.type)
112
173
  end
113
174
 
114
- def test_format
115
- assert_equal(TestObject.as_xml, formatter.format(TestObject.test_object))
175
+ def test_convert
176
+ xml = '<result>' + TestObject.as_xml + '</result>'
177
+ assert_equal(xml, formatter.convert([TestObject.test_object], true))
178
+ assert_equal(xml, formatter.convert([TestObject.test_object], false))
179
+ end
180
+
181
+ def test_format_with_fasta
182
+ fasta = [['>test', '5']]
183
+ output = formatter.format([TestObject.test_object], fasta, false)
184
+ xml = '<result><fasta_header>>test</fasta_header>' + TestObject.as_xml + '</result>'
185
+ assert_equal(xml, output)
116
186
  end
117
187
  end
118
188
 
@@ -126,7 +196,7 @@ module Unipept
126
196
  end
127
197
 
128
198
  def self.as_xml
129
- '<integer>5</integer><string>string</string><list size="3"><item>a</item><item>2</item><item>false</item></list>'
199
+ '<integer>5</integer><string>string</string><list><item>a</item><item>2</item><item>false</item></list>'
130
200
  end
131
201
 
132
202
  def self.as_csv
@@ -0,0 +1,29 @@
1
+ require_relative '../lib/output_writer'
2
+
3
+ module Unipept
4
+ class OutputWriterTestCase < Unipept::TestCase
5
+ def test_init
6
+ assert_equal($stdout, OutputWriter.new(nil).output)
7
+ assert_equal(File, OutputWriter.new('output.txt').output.class)
8
+ end
9
+
10
+ def test_stdout_write_to_output
11
+ out, _err = capture_io_while do
12
+ writer = OutputWriter.new(nil)
13
+ writer.write_line('hello world')
14
+ writer.output.flush
15
+ end
16
+ assert_equal('hello world', out.chomp)
17
+ end
18
+
19
+ def test_file_write_to_output
20
+ out, _err = capture_io_while do
21
+ writer = OutputWriter.new('output_file')
22
+ writer.write_line('hello world')
23
+ writer.output.flush
24
+ end
25
+ assert_equal('', out)
26
+ assert_equal('hello world', IO.foreach('output_file').next.chomp)
27
+ end
28
+ end
29
+ end