smarter_csv 1.4.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ fixture_path = 'spec/fixtures'
4
+
5
+ describe 'can handle the difficult CSV file' do
6
+
7
+ it 'loads the data with default values' do
8
+ data = SmarterCSV.process("#{fixture_path}/hard_sample.csv")
9
+ data.size.should eq 1
10
+ item = data.first
11
+ item.keys.count.should == 48
12
+ item[:name].should == '#MR1220817'
13
+ item[:shipping_method].should == 'Livraison Standard GRATUITE, 2-5 jours avec suivi'
14
+ item[:lineitem_name].should == 'Cire Épilation Nacrée'
15
+ item[:phone].should == 3366012111111
16
+ end
17
+
18
+ # the main problem is the data line starting with a # character, but not being a comment
19
+ it 'fails to load the CSV file with incorrectly set comment_regexp' do
20
+ options = {comment_regexp: /\A#/ }
21
+ data = SmarterCSV.process("#{fixture_path}/hard_sample.csv", options)
22
+ data.size.should eq 0
23
+ end
24
+ end
@@ -1,30 +1,45 @@
1
- require 'spec_helper'
2
-
3
- fixture_path = 'spec/fixtures'
4
-
5
- describe 'be_able_to' do
6
- it 'ignore comments in CSV files' do
7
- options = {}
8
- data = SmarterCSV.process("#{fixture_path}/ignore_comments.csv", options)
9
-
10
- data.size.should eq 5
11
-
12
- # all the keys should be symbols
13
- data.each{|item| item.keys.each{|x| x.is_a?(Symbol).should be_truthy}}
14
- data.each do |h|
15
- h.keys.each do |key|
16
- [:"not_a_comment#first_name", :last_name, :dogs, :cats, :birds, :fish].should include( key )
17
- end
18
- end
19
- end
20
-
21
- it 'ignore comments in CSV files with CRLF' do
22
- options = {row_sep: "\r\n"}
23
- data = SmarterCSV.process("#{fixture_path}/ignore_comments2.csv", options)
24
-
25
- # all the keys should be symbols
26
- data.size.should eq 1
27
- data.first[:h1].should eq 'a'
28
- data.first[:h2].should eq "b\r\n#c"
29
- end
30
- end
1
+ require 'spec_helper'
2
+
3
+ fixture_path = 'spec/fixtures'
4
+
5
+ describe 'be_able_to' do
6
+ it 'by default does not ignore comments in CSV files' do
7
+ options = {}
8
+ data = SmarterCSV.process("#{fixture_path}/ignore_comments.csv", options)
9
+
10
+ data.size.should eq 8
11
+
12
+ # all the keys should be symbols
13
+ data.each{|item| item.keys.each{|x| x.is_a?(Symbol).should be_truthy}}
14
+ data.each do |h|
15
+ h.keys.each do |key|
16
+ [:"not_a_comment#first_name", :last_name, :dogs, :cats, :birds, :fish].should include( key )
17
+ end
18
+ end
19
+ end
20
+
21
+ it 'ignore comments in CSV files using comment_regexp' do
22
+ options = {comment_regexp: /\A#/}
23
+ data = SmarterCSV.process("#{fixture_path}/ignore_comments.csv", options)
24
+
25
+ data.size.should eq 5
26
+
27
+ # all the keys should be symbols
28
+ data.each{|item| item.keys.each{|x| x.is_a?(Symbol).should be_truthy}}
29
+ data.each do |h|
30
+ h.keys.each do |key|
31
+ [:"not_a_comment#first_name", :last_name, :dogs, :cats, :birds, :fish].should include( key )
32
+ end
33
+ end
34
+ end
35
+
36
+ it 'ignore comments in CSV files with CRLF' do
37
+ options = {row_sep: "\r\n"}
38
+ data = SmarterCSV.process("#{fixture_path}/ignore_comments2.csv", options)
39
+
40
+ # all the keys should be symbols
41
+ data.size.should eq 1
42
+ data.first[:h1].should eq 'a'
43
+ data.first[:h2].should eq "b\r\n#c"
44
+ end
45
+ end
@@ -3,28 +3,6 @@ require 'spec_helper'
3
3
  fixture_path = 'spec/fixtures'
4
4
 
5
5
  describe 'test exceptions for invalid headers' do
6
- it 'raises error on duplicate headers' do
7
- expect {
8
- SmarterCSV.process("#{fixture_path}/duplicate_headers.csv", {})
9
- }.to raise_exception(SmarterCSV::DuplicateHeaders)
10
- end
11
-
12
- it 'raises error on duplicate given headers' do
13
- expect {
14
- options = {:user_provided_headers => [:a,:b,:c,:d,:a]}
15
- SmarterCSV.process("#{fixture_path}/duplicate_headers.csv", options)
16
- }.to raise_exception(SmarterCSV::DuplicateHeaders)
17
- end
18
-
19
- it 'raises error on duplicate mapped headers' do
20
- expect {
21
- # the mapping is right, but the underlying csv file is bad
22
- options = {:key_mapping => {:email => :a, :firstname => :b, :lastname => :c, :manager_email => :d, :age => :e} }
23
- SmarterCSV.process("#{fixture_path}/duplicate_headers.csv", options)
24
- }.to raise_exception(SmarterCSV::DuplicateHeaders)
25
- end
26
-
27
-
28
6
  it 'does not raise an error if no required headers are given' do
29
7
  options = {:required_headers => nil} # order does not matter
30
8
  data = SmarterCSV.process("#{fixture_path}/user_import.csv", options)
@@ -49,4 +27,12 @@ describe 'test exceptions for invalid headers' do
49
27
  SmarterCSV.process("#{fixture_path}/user_import.csv", options)
50
28
  }.to raise_exception(SmarterCSV::MissingHeaders)
51
29
  end
30
+
31
+ it 'raises error on missing mapped headers' do
32
+ expect {
33
+ # :age does not exist in the CSV header
34
+ options = {:key_mapping => {:email => :a, :firstname => :b, :lastname => :c, :manager_email => :d, :age => :e} }
35
+ SmarterCSV.process("#{fixture_path}/user_import.csv", options)
36
+ }.to raise_exception(SmarterCSV::KeyMappingError)
37
+ end
52
38
  end
@@ -2,23 +2,28 @@ require 'spec_helper'
2
2
 
3
3
  fixture_path = 'spec/fixtures'
4
4
 
5
- describe 'be_able_to' do
6
- it 'loads_csv_file_without_header' do
7
- options = {:headers_in_file => false, :user_provided_headers => [:a,:b,:c,:d,:e,:f]}
8
- data = SmarterCSV.process("#{fixture_path}/no_header.csv", options)
5
+ describe 'no header in file' do
6
+ let(:headers) { [:a,:b,:c,:d,:e,:f] }
7
+ let(:options) { {:headers_in_file => false, :user_provided_headers => headers} }
8
+ subject(:data) { SmarterCSV.process("#{fixture_path}/no_header.csv", options) }
9
+
10
+ it 'load the correct number of records' do
9
11
  data.size.should == 5
10
- # all the keys should be symbols
11
- data.each{|item| item.keys.each{|x| x.class.should be == Symbol}}
12
+ end
12
13
 
13
- data.each do |item|
14
+ it 'uses given symbols for all records' do
15
+ data.each do |item|
14
16
  item.keys.each do |key|
15
17
  [:a,:b,:c,:d,:e,:f].should include( key )
16
18
  end
17
19
  end
18
-
19
- data.each do |h|
20
- h.size.should <= 6
21
- end
22
20
  end
23
21
 
22
+ it 'loads the correct data' do
23
+ data[0].should == {a: "Dan", b: "McAllister", c: 2, d: 0}
24
+ data[1].should == {a: "Lucy", b: "Laweless", d: 5, e: 0}
25
+ data[2].should == {a: "Miles", b: "O'Brian", c: 0, d: 0, e: 0, f: 21}
26
+ data[3].should == {a: "Nancy", b: "Homes", c: 2, d: 0, e: 1}
27
+ data[4].should == {a: "Hernán", b: "Curaçon", c: 3, d: 0, e: 0}
28
+ end
24
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smarter_csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tilo Sloboda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-11 00:00:00.000000000 Z
11
+ date: 2022-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: simplecov
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description: Ruby Gem for smarter importing of CSV Files as Array(s) of Hashes, with
28
42
  optional features for processing large files in parallel, embedded comments, unusual
29
43
  field- and record-separators, flexible mapping of CSV-headers to Hash-keys
@@ -38,6 +52,7 @@ files:
38
52
  - ".rvmrc"
39
53
  - ".travis.yml"
40
54
  - CHANGELOG.md
55
+ - CONTRIBUTORS.md
41
56
  - Gemfile
42
57
  - LICENSE.txt
43
58
  - README.md
@@ -47,6 +62,7 @@ files:
47
62
  - lib/smarter_csv/smarter_csv.rb
48
63
  - lib/smarter_csv/version.rb
49
64
  - smarter_csv.gemspec
65
+ - spec/fixtures/additional_separator.csv
50
66
  - spec/fixtures/basic.csv
51
67
  - spec/fixtures/binary.csv
52
68
  - spec/fixtures/carriage_returns_n.csv
@@ -58,6 +74,7 @@ files:
58
74
  - spec/fixtures/empty.csv
59
75
  - spec/fixtures/empty_columns_1.csv
60
76
  - spec/fixtures/empty_columns_2.csv
77
+ - spec/fixtures/hard_sample.csv
61
78
  - spec/fixtures/ignore_comments.csv
62
79
  - spec/fixtures/ignore_comments2.csv
63
80
  - spec/fixtures/key_mapping.csv
@@ -86,6 +103,7 @@ files:
86
103
  - spec/fixtures/valid_unicode.csv
87
104
  - spec/fixtures/with_dashes.csv
88
105
  - spec/fixtures/with_dates.csv
106
+ - spec/smarter_csv/additional_separator_spec.rb
89
107
  - spec/smarter_csv/binary_file2_spec.rb
90
108
  - spec/smarter_csv/binary_file_spec.rb
91
109
  - spec/smarter_csv/blank_spec.rb
@@ -94,8 +112,10 @@ files:
94
112
  - spec/smarter_csv/close_file_spec.rb
95
113
  - spec/smarter_csv/column_separator_spec.rb
96
114
  - spec/smarter_csv/convert_values_to_numeric_spec.rb
115
+ - spec/smarter_csv/duplicate_headers_spec.rb
97
116
  - spec/smarter_csv/empty_columns_spec.rb
98
117
  - spec/smarter_csv/extenstions_spec.rb
118
+ - spec/smarter_csv/hard_sample_spec.rb
99
119
  - spec/smarter_csv/header_transformation_spec.rb
100
120
  - spec/smarter_csv/ignore_comments_spec.rb
101
121
  - spec/smarter_csv/invalid_headers_spec.rb
@@ -143,12 +163,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
163
  version: '0'
144
164
  requirements:
145
165
  - csv
146
- rubygems_version: 3.1.4
166
+ rubygems_version: 3.1.6
147
167
  signing_key:
148
168
  specification_version: 4
149
169
  summary: Ruby Gem for smarter importing of CSV Files (and CSV-like files), with lots
150
170
  of optional features, e.g. chunked processing for huge CSV files
151
171
  test_files:
172
+ - spec/fixtures/additional_separator.csv
152
173
  - spec/fixtures/basic.csv
153
174
  - spec/fixtures/binary.csv
154
175
  - spec/fixtures/carriage_returns_n.csv
@@ -160,6 +181,7 @@ test_files:
160
181
  - spec/fixtures/empty.csv
161
182
  - spec/fixtures/empty_columns_1.csv
162
183
  - spec/fixtures/empty_columns_2.csv
184
+ - spec/fixtures/hard_sample.csv
163
185
  - spec/fixtures/ignore_comments.csv
164
186
  - spec/fixtures/ignore_comments2.csv
165
187
  - spec/fixtures/key_mapping.csv
@@ -188,6 +210,7 @@ test_files:
188
210
  - spec/fixtures/valid_unicode.csv
189
211
  - spec/fixtures/with_dashes.csv
190
212
  - spec/fixtures/with_dates.csv
213
+ - spec/smarter_csv/additional_separator_spec.rb
191
214
  - spec/smarter_csv/binary_file2_spec.rb
192
215
  - spec/smarter_csv/binary_file_spec.rb
193
216
  - spec/smarter_csv/blank_spec.rb
@@ -196,8 +219,10 @@ test_files:
196
219
  - spec/smarter_csv/close_file_spec.rb
197
220
  - spec/smarter_csv/column_separator_spec.rb
198
221
  - spec/smarter_csv/convert_values_to_numeric_spec.rb
222
+ - spec/smarter_csv/duplicate_headers_spec.rb
199
223
  - spec/smarter_csv/empty_columns_spec.rb
200
224
  - spec/smarter_csv/extenstions_spec.rb
225
+ - spec/smarter_csv/hard_sample_spec.rb
201
226
  - spec/smarter_csv/header_transformation_spec.rb
202
227
  - spec/smarter_csv/ignore_comments_spec.rb
203
228
  - spec/smarter_csv/invalid_headers_spec.rb