rcsv 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,156 @@
1
+ require 'test/unit'
2
+ require 'rcsv'
3
+
4
+ class RcsvTest < Test::Unit::TestCase
5
+ def setup
6
+ @csv_data = File.read('test/test_rcsv.csv')
7
+ end
8
+
9
+ def test_rcsv
10
+ raw_parsed_csv_data = Rcsv.raw_parse(@csv_data)
11
+
12
+ assert_equal(raw_parsed_csv_data[0][2], 'EDADEDADEDADEDADEDADEDAD')
13
+ assert_equal(raw_parsed_csv_data[0][13], '$$$908080')
14
+ assert_equal(raw_parsed_csv_data[0][14], '"')
15
+ assert_equal(raw_parsed_csv_data[0][15], 'true/false')
16
+ assert_equal(raw_parsed_csv_data[0][16], nil)
17
+ assert_equal(raw_parsed_csv_data[9][2], nil)
18
+ assert_equal(raw_parsed_csv_data[3][6], '""C81E-=; **ECCB; .. 89')
19
+ assert_equal(raw_parsed_csv_data[888][13], 'Dallas, TX')
20
+ end
21
+
22
+ def test_rcsv_col_sep
23
+ tsv_data = @csv_data.tr(",", "\t")
24
+ raw_parsed_tsv_data = Rcsv.raw_parse(tsv_data, :col_sep => "\t")
25
+
26
+ assert_equal(raw_parsed_tsv_data[0][2], 'EDADEDADEDADEDADEDADEDAD')
27
+ assert_equal(raw_parsed_tsv_data[0][13], '$$$908080')
28
+ assert_equal(raw_parsed_tsv_data[0][14], '"')
29
+ assert_equal(raw_parsed_tsv_data[0][15], 'true/false')
30
+ assert_equal(raw_parsed_tsv_data[0][16], nil)
31
+ assert_equal(raw_parsed_tsv_data[9][2], nil)
32
+ assert_equal(raw_parsed_tsv_data[3][6], '""C81E-=; **ECCB; .. 89')
33
+ assert_equal(raw_parsed_tsv_data[888][13], "Dallas\t TX")
34
+ end
35
+
36
+ def test_broken_data
37
+ broken_data = @csv_data.sub(/"/, '')
38
+
39
+ assert_raise(Rcsv::ParseError) do
40
+ Rcsv.raw_parse(broken_data)
41
+ end
42
+ end
43
+
44
+ def test_broken_data_without_strict
45
+ broken_data = @csv_data.sub(/"/, '')
46
+
47
+ raw_parsed_csv_data = Rcsv.raw_parse(broken_data, :nostrict => true)
48
+ assert_equal(["DSAdsfksjh", "iii ooo iii", "EDADEDADEDADEDADEDADEDAD", "111 333 555", "NMLKTF", "---==---", "//", "###", "0000000000", "Asdad bvd qwert", ";'''sd", "@@@", "OCTZ", "$$$908080", "\",true/false\nC85A5B9F,85259637,,96,6838,1983-06-14,\"\"\"C4CA-=; **1679; .. 79", "210,11", "908e", "1281-03-09", "7257.4654049904275", "20efe749-50fe-4b6a-a603-7f9cd1dc6c6d", "3", "New York, NY", "u", "2.228169203286535", "t"], raw_parsed_csv_data.first)
49
+ end
50
+
51
+ def test_only_rows
52
+ raw_parsed_csv_data = Rcsv.raw_parse(@csv_data, :only_rows => ['GBP'])
53
+
54
+ assert_equal(raw_parsed_csv_data[0][0], 'GBP')
55
+ assert_equal(raw_parsed_csv_data[-1][3], '15')
56
+ assert_equal(raw_parsed_csv_data.size, 3)
57
+ end
58
+
59
+ def test_only_rows_with_nil_and_empty_string_filter
60
+ raw_parsed_csv_data = Rcsv.raw_parse(@csv_data, :only_rows => ['GBP', nil, '', '51'])
61
+
62
+ assert_equal(raw_parsed_csv_data[0][0], 'GBP')
63
+ assert_equal(raw_parsed_csv_data[0][2], nil)
64
+ assert_equal(raw_parsed_csv_data.size, 1)
65
+ end
66
+
67
+ def test_only_rows_with_nil_beginning
68
+ raw_parsed_csv_data = Rcsv.raw_parse(@csv_data, :only_rows => [nil, nil, nil, '96', nil])
69
+
70
+ assert_equal(raw_parsed_csv_data[1][0], 'C3B87A6B')
71
+ assert_equal(raw_parsed_csv_data[0][2], nil)
72
+ assert_equal(raw_parsed_csv_data[3][3], '96')
73
+ assert_equal(raw_parsed_csv_data.size, 5)
74
+ end
75
+
76
+ def test_row_defaults
77
+ raw_parsed_csv_data = Rcsv.raw_parse(@csv_data, :row_defaults => [nil, nil, :booya, nil, 'never ever'])
78
+
79
+ assert_equal(raw_parsed_csv_data[0][2], 'EDADEDADEDADEDADEDADEDAD')
80
+ assert_equal(raw_parsed_csv_data[1][2], :booya)
81
+ assert_equal(raw_parsed_csv_data[1][0], 'C85A5B9F')
82
+ assert_equal(raw_parsed_csv_data[2][4], '9134')
83
+ end
84
+
85
+ def test_row_conversions
86
+ raw_parsed_csv_data = Rcsv.raw_parse(@csv_data.each_line.to_a[1..-1].join, # skipping string headers
87
+ :row_conversions => 'sisiisssssfsissf')
88
+
89
+ assert_equal(raw_parsed_csv_data[0][2], nil)
90
+ assert_equal(raw_parsed_csv_data[0][3], 96)
91
+ assert_equal(raw_parsed_csv_data[0][8], '908e')
92
+ assert_equal(raw_parsed_csv_data[1][15], -9.549296585513721)
93
+ assert_equal(raw_parsed_csv_data[3][5], '2015-12-22')
94
+ end
95
+
96
+ def test_row_conversions_with_column_exclusions
97
+ raw_parsed_csv_data = Rcsv.raw_parse(@csv_data.each_line.to_a[1..-1].join, # skipping string headers
98
+ :row_conversions => 's f issss fsis fb')
99
+
100
+ assert_equal(raw_parsed_csv_data[0][1], nil)
101
+ assert_equal(raw_parsed_csv_data[0][2], 6838)
102
+ assert_equal(raw_parsed_csv_data[0][8], '20efe749-50fe-4b6a-a603-7f9cd1dc6c6d')
103
+ assert_equal(raw_parsed_csv_data[0][12], true)
104
+ assert_equal(raw_parsed_csv_data[0][13], nil)
105
+ assert_equal(raw_parsed_csv_data[4][3], '2020-12-09')
106
+ end
107
+
108
+ def test_offset_rows
109
+ raw_parsed_csv_data = Rcsv.raw_parse(@csv_data, :offset_rows => 51)
110
+
111
+ assert_equal('0FFDEA62', raw_parsed_csv_data[0][0])
112
+ assert_equal(889 - 51, raw_parsed_csv_data.count)
113
+ end
114
+
115
+ def test_rows_as_hash
116
+ raw_parsed_csv_data = Rcsv.raw_parse(@csv_data, :row_as_hash => true, :column_names => [
117
+ 'DSAdsfksjh',
118
+ 'iii ooo iii',
119
+ 'EDADEDADEDADEDADEDADEDAD',
120
+ '111 333 555',
121
+ 'NMLKTF',
122
+ '---==---',
123
+ '//',
124
+ '###',
125
+ '0000000000',
126
+ 'Asdad bvd qwert',
127
+ ";'''sd",
128
+ '@@@',
129
+ 'OCTZ',
130
+ '$$$908080',
131
+ '"',
132
+ 'noname',
133
+ 'booleator'
134
+ ])
135
+
136
+ assert_equal({
137
+ 'DSAdsfksjh' => 'C85A5B9F',
138
+ 'iii ooo iii' => '85259637',
139
+ 'EDADEDADEDADEDADEDADEDAD' => nil,
140
+ '111 333 555' => '96',
141
+ 'NMLKTF' => '6838',
142
+ '---==---' => '1983-06-14',
143
+ '//' => '""C4CA-=; **1679; .. 79',
144
+ '###' => '210,11',
145
+ '0000000000' => '908e',
146
+ 'Asdad bvd qwert' => '1281-03-09',
147
+ ";'''sd" => '7257.4654049904275',
148
+ '@@@' => '20efe749-50fe-4b6a-a603-7f9cd1dc6c6d',
149
+ 'OCTZ' => '3',
150
+ '$$$908080' => "New York, NY",
151
+ '"' => 'u',
152
+ 'noname' => '2.228169203286535',
153
+ 'booleator' => 't'
154
+ }, raw_parsed_csv_data[1])
155
+ end
156
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rcsv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Arthur Pirogovski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A libcsv-based CSV parser for Ruby
15
+ email:
16
+ - arthur@flyingtealeaf.com
17
+ executables: []
18
+ extensions:
19
+ - ext/rcsv/extconf.rb
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - COPYING.LESSER
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE
27
+ - README.md
28
+ - Rakefile
29
+ - bench.rb
30
+ - ext/rcsv/csv.h
31
+ - ext/rcsv/extconf.rb
32
+ - ext/rcsv/libcsv.c
33
+ - ext/rcsv/rcsv.c
34
+ - ext/rcsv/test.rb
35
+ - lib/lib_csv.rb
36
+ - lib/rcsv.rb
37
+ - lib/rcsv/version.rb
38
+ - rcsv.gemspec
39
+ - test/test_rcsv.csv
40
+ - test/test_rcsv_raw_parse.rb
41
+ homepage: http://github.com/fiksu/rcsv
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ - ext
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.24
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Fast CSV parsing library for MRI based on libcsv. Supports type conversion,
66
+ non-strict parsing and basic filtering.
67
+ test_files:
68
+ - test/test_rcsv.csv
69
+ - test/test_rcsv_raw_parse.rb
70
+ has_rdoc: