libis-tools 0.9.53 → 0.9.54

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 667255e887aea8734fdf80072ee77be74e273d71
4
- data.tar.gz: ca652b57931ae459cb239ad9f4f453f0615d588e
3
+ metadata.gz: ae389ef2e44feca0bd78fd597d72d6739e78fa04
4
+ data.tar.gz: ab0cb4d78009eb1bf655d6c7827b3a6ca3617a9e
5
5
  SHA512:
6
- metadata.gz: 43623c49d7afe2e8db4ee7a0413555053253141d01c62230df19d67901663116711eac7dc0870d2614b55ee9695f04887cb9b493aeaed746381fd90bc584452d
7
- data.tar.gz: aeb56f8c2f27da55b40e5c288f2bcffdcf8a8d5699fa7c71ef031d4457fe8cce95f0de6ec0c4d17cf89c67451ecfc700157cb699a438671b5912beb1466b60fe
6
+ metadata.gz: fca49440deb07099c2e8c4ae630930cc07c15ad740632bb71f5834e0bd6c9524ceacdbe5d7cdbbd6ee8c681c502750159123377c2808312fd68dd8fdbbde64d3
7
+ data.tar.gz: d0b17ee117419c096f35a99fb959dc75dcf33270a515e1b439d24516ce630be01ad5e727670e58a59fcd65f540f8514a59f985428646347918904d2ea926747c
@@ -38,14 +38,14 @@ module Libis
38
38
  # @param [Hash] opts
39
39
  def initialize(file_name, opts = {})
40
40
  options = {
41
- csv_options: [:encoding, :col_sep, :quote_char].inject({}) do |h, k|
42
- h[k] = opts.delete(k) if opts[k]
43
- h
44
- end.merge(
41
+ csv_options: {
45
42
  encoding: 'UTF-8',
46
43
  col_sep: ',',
47
44
  quote_char: '"',
48
- ),
45
+ }.merge([:encoding, :col_sep, :quote_char].inject({}) do |h, k|
46
+ h[k] = opts.delete(k) if opts[k]
47
+ h
48
+ end)
49
49
  }.merge(opts)
50
50
 
51
51
  required_headers = options.delete(:required) || []
@@ -1,5 +1,5 @@
1
1
  module Libis
2
2
  module Tools
3
- VERSION = '0.9.53'
3
+ VERSION = '0.9.54'
4
4
  end
5
5
  end
@@ -0,0 +1,2 @@
1
+ FirstName LastName address
2
+ John Smith mystreet 1, myplace
@@ -0,0 +1 @@
1
+ John Smith mystreet 1, myplace
@@ -6,11 +6,13 @@ require 'libis/tools/spreadsheet'
6
6
  describe 'Libis::Tools::Spreadsheet' do
7
7
 
8
8
  let(:path) {File.absolute_path('data', File.dirname(__FILE__))}
9
+ let(:options) { {} }
9
10
  let(:ss) {
10
11
  Libis::Tools::Spreadsheet.new(
11
12
  File.join(path, file_name),
12
- required: required_headers,
13
+ { required: required_headers,
13
14
  optional: optional_headers
15
+ }.merge(options)
14
16
  )
15
17
  }
16
18
 
@@ -240,6 +242,237 @@ describe 'Libis::Tools::Spreadsheet' do
240
242
 
241
243
  end
242
244
 
245
+ context 'TSV file' do
246
+
247
+ let(:options) { {
248
+ col_sep: "\t",
249
+ extension: 'csv'
250
+ }}
251
+
252
+ context 'with headers' do
253
+
254
+ let(:file_name) {'test-headers.tsv'}
255
+
256
+ context 'well-formed' do
257
+
258
+ let(:required_headers) {%w'FirstName LastName'}
259
+
260
+ it 'opens correctly' do
261
+ expect {ss}.not_to raise_error
262
+ end
263
+
264
+ it 'contains expected headers' do
265
+ required_headers.each do |header|
266
+ expect(ss.headers).to include header
267
+ end
268
+ expect(ss.headers).to eq %w'FirstName LastName address'
269
+ end
270
+
271
+ it '#shift returns Hash object' do
272
+ row = ss.shift
273
+ expect(row).to be_a Hash
274
+ expect(row['FirstName']).to eq 'John'
275
+ expect(row['LastName']).to eq 'Smith'
276
+ expect(row['address']).to eq 'mystreet 1, myplace'
277
+ expect(row['phone']).to be_nil
278
+ end
279
+
280
+ it '#parse returns Array of Hash objects' do
281
+ rows = ss.parse
282
+ expect(rows).to be_a Array
283
+ expect(rows.size).to eq 1
284
+ row = rows[0]
285
+ expect(row).to be_a Hash
286
+ expect(row['FirstName']).to eq 'John'
287
+ expect(row['LastName']).to eq 'Smith'
288
+ expect(row['address']).to eq 'mystreet 1, myplace'
289
+ expect(row['phone']).to be_nil
290
+ end
291
+
292
+ end
293
+
294
+ context 'not specified' do
295
+
296
+ let(:required_headers) {[]}
297
+
298
+ it 'opens correctly' do
299
+ expect {ss}.not_to raise_error
300
+ end
301
+
302
+ it 'contains expected headers' do
303
+ expect(ss.headers).to eq %w'FirstName LastName address'
304
+ end
305
+
306
+ it '#shift returns Hash object' do
307
+ row = ss.shift
308
+ expect(row).to be_a Hash
309
+ expect(row['FirstName']).to eq 'John'
310
+ expect(row['LastName']).to eq 'Smith'
311
+ expect(row['address']).to eq 'mystreet 1, myplace'
312
+ expect(row['phone']).to be_nil
313
+ end
314
+
315
+ it '#parse returns Array of Hash objects' do
316
+ rows = ss.parse
317
+ expect(rows).to be_a Array
318
+ expect(rows.size).to eq 1
319
+ row = rows[0]
320
+ expect(row).to be_a Hash
321
+ expect(row['FirstName']).to eq 'John'
322
+ expect(row['LastName']).to eq 'Smith'
323
+ expect(row['address']).to eq 'mystreet 1, myplace'
324
+ expect(row['phone']).to be_nil
325
+ end
326
+
327
+ end
328
+
329
+ context 'not well-formed' do
330
+
331
+ let(:required_headers) {%w'FirstName LastName address phone'}
332
+
333
+ it 'throws error when opened' do
334
+ expect {ss}.to raise_error(RuntimeError, 'Headers not found: ["phone"].')
335
+ end
336
+ end
337
+
338
+ end
339
+
340
+ context 'without headers' do
341
+ let(:file_name) {'test-noheaders.tsv'}
342
+
343
+ context 'well-formed and strict' do
344
+ let(:required_headers) {%w'FirstName LastName'}
345
+
346
+ it 'opens correctly' do
347
+ expect {ss}.not_to raise_error
348
+ end
349
+
350
+ it 'contains only required headers' do
351
+ required_headers.each do |header|
352
+ expect(ss.headers).to include header
353
+ end
354
+ expect(ss.headers).to eq %w'FirstName LastName'
355
+ end
356
+
357
+ it '#shift returns Hash object' do
358
+ row = ss.shift
359
+ expect(row).to be_a Hash
360
+ expect(row['FirstName']).to eq 'John'
361
+ expect(row['LastName']).to eq 'Smith'
362
+ expect(row['address']).to be_nil
363
+ expect(row['phone']).to be_nil
364
+ end
365
+
366
+ it '#parse returns Array of Hash objects' do
367
+ rows = ss.parse
368
+ expect(rows).to be_a Array
369
+ expect(rows.size).to eq 1
370
+ row = rows[0]
371
+ expect(row).to be_a Hash
372
+ expect(row['FirstName']).to eq 'John'
373
+ expect(row['LastName']).to eq 'Smith'
374
+ expect(row['address']).to be_nil
375
+ expect(row['phone']).to be_nil
376
+ end
377
+
378
+ end
379
+
380
+ context 'well-formed with optional headers' do
381
+ let(:required_headers) {%w'FirstName LastName'}
382
+ let(:optional_headers) {%w'address'}
383
+
384
+ it 'opens correctly' do
385
+ expect {ss}.not_to raise_error
386
+ end
387
+
388
+ it 'contains required and optional headers' do
389
+ required_headers.each do |header|
390
+ expect(ss.headers).to include header
391
+ end
392
+ optional_headers.each do |header|
393
+ expect(ss.headers).to include header
394
+ end
395
+ expect(ss.headers).to eq %w'FirstName LastName address'
396
+ end
397
+
398
+ it '#shift returns Hash object' do
399
+ row = ss.shift
400
+ expect(row).to be_a Hash
401
+ expect(row['FirstName']).to eq 'John'
402
+ expect(row['LastName']).to eq 'Smith'
403
+ expect(row['address']).to eq 'mystreet 1, myplace'
404
+ expect(row['phone']).to be_nil
405
+ end
406
+
407
+ it '#parse returns Array of Hash objects' do
408
+ rows = ss.parse
409
+ expect(rows).to be_a Array
410
+ expect(rows.size).to eq 1
411
+ row = rows[0]
412
+ expect(row).to be_a Hash
413
+ expect(row['FirstName']).to eq 'John'
414
+ expect(row['LastName']).to eq 'Smith'
415
+ expect(row['address']).to eq 'mystreet 1, myplace'
416
+ expect(row['phone']).to be_nil
417
+ end
418
+
419
+ end
420
+
421
+ context 'missing optional headers' do
422
+
423
+ let(:required_headers) {%w'FirstName LastName address'}
424
+ let(:optional_headers) {%w'phone'}
425
+
426
+ it 'opens correctly' do
427
+ expect {ss}.not_to raise_error
428
+ end
429
+
430
+ it 'contains only required headers' do
431
+ required_headers.each do |header|
432
+ expect(ss.headers).to include header
433
+ end
434
+ optional_headers.each do |header|
435
+ expect(ss.headers).not_to include header
436
+ end
437
+ expect(ss.headers).to eq %w'FirstName LastName address'
438
+ end
439
+
440
+ it '#shift returns Hash object' do
441
+ row = ss.shift
442
+ expect(row).to be_a Hash
443
+ expect(row['FirstName']).to eq 'John'
444
+ expect(row['LastName']).to eq 'Smith'
445
+ expect(row['address']).to eq 'mystreet 1, myplace'
446
+ expect(row['phone']).to be_nil
447
+ end
448
+
449
+ it '#parse returns Array of Hash objects' do
450
+ rows = ss.parse
451
+ expect(rows).to be_a Array
452
+ expect(rows.size).to eq 1
453
+ row = rows[0]
454
+ expect(row).to be_a Hash
455
+ expect(row['FirstName']).to eq 'John'
456
+ expect(row['LastName']).to eq 'Smith'
457
+ expect(row['address']).to eq 'mystreet 1, myplace'
458
+ expect(row['phone']).to be_nil
459
+ end
460
+
461
+ end
462
+
463
+ context 'missing required header' do
464
+ let(:required_headers) {%w'FirstName LastName address phone'}
465
+
466
+ it 'throws error when opened' do
467
+ expect {ss}.to raise_error(RuntimeError, 'Sheet does not contain enough columns.')
468
+ end
469
+
470
+ end
471
+
472
+ end
473
+
474
+ end
475
+
243
476
  context 'XLSX file' do
244
477
 
245
478
  let(:real_headers) {%w'Date Amount Code Remark'}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libis-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.53
4
+ version: 0.9.54
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kris Dekeyser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-22 00:00:00.000000000 Z
11
+ date: 2017-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -332,7 +332,9 @@ files:
332
332
  - spec/csv_spec.rb
333
333
  - spec/data/MetadataMapping.xlsx
334
334
  - spec/data/test-headers.csv
335
+ - spec/data/test-headers.tsv
335
336
  - spec/data/test-noheaders.csv
337
+ - spec/data/test-noheaders.tsv
336
338
  - spec/data/test.data
337
339
  - spec/data/test.xlsx
338
340
  - spec/data/test.xml
@@ -395,7 +397,9 @@ test_files:
395
397
  - spec/csv_spec.rb
396
398
  - spec/data/MetadataMapping.xlsx
397
399
  - spec/data/test-headers.csv
400
+ - spec/data/test-headers.tsv
398
401
  - spec/data/test-noheaders.csv
402
+ - spec/data/test-noheaders.tsv
399
403
  - spec/data/test.data
400
404
  - spec/data/test.xlsx
401
405
  - spec/data/test.xml