gooddata_datawarehouse 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c02001e54624b97d0dc6ce317eea0141af49e6d7
4
- data.tar.gz: f136f74d59e63b43be394b0cf57e99c7805a7c75
3
+ metadata.gz: e6c19b17e345eb5ea10f389067f689544bc49ce1
4
+ data.tar.gz: 2d7bae04776af43c98eccd798352954d5678e7e2
5
5
  SHA512:
6
- metadata.gz: 37307c29876afa00c48e2a0402ff648d52a9037ce6f040f355d4a8cfaaf0e7b99847c2192537481f82d9daa025a94c5384b2fe4a2ccf2c019abfa5a5dc01fa45
7
- data.tar.gz: 4f25f05dec8b8ac1f78f1ae75b924b5792d860228043d403e72fe5f4c48f8aeab1a7db836f17b2ba9220e373e14f3ec54c1ac7ed20eae474e9ecd1c6a5720643
6
+ metadata.gz: 926f231df825fdb1077d270a8944a9d2631d10f1ca15b1ee4d5a19fb31505a2325088872ff1a6a56b4598e197d90a7410cfaf15685fa0506e372e3829fb9839f
7
+ data.tar.gz: 20505562823f04ffb717260115bff12dd649ae0a81331012793fc370de1bc05e37ec73a6fbf7f7040d119e4576ccfc4bb7e8be5cf343bd720327f333949cdd9f
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ # automatic ruby preselection
4
+ #ruby=jruby-1.7.15
5
+
3
6
  # Specify your gem's dependencies in gooddata_datawarehouse.gemspec
4
7
  gemspec
@@ -52,25 +52,31 @@ module GoodData
52
52
 
53
53
  def csv_to_new_table(table_name, csv_path, opts={})
54
54
  cols = create_table_from_csv_header(table_name, csv_path, opts)
55
- load_data_from_csv(table_name, csv_path, columns: cols)
55
+ load_data_from_csv(table_name, csv_path, opts.merge(columns: cols))
56
56
  end
57
57
 
58
58
  def load_data_from_csv(table_name, csv_path, opts={})
59
59
  columns = opts[:columns] || get_csv_headers(csv_path)
60
60
 
61
- # temporary files to get the excepted records
62
- exc = opts[:exceptions_file] ||= Tempfile.new('exceptions')
63
- rej = opts[:rejections_file] ||= Tempfile.new('rejections')
64
- exc = File.new(exc) unless exc.is_a?(File)
65
- rej = File.new(rej) unless exc.is_a?(File)
61
+ if opts[:ignore_parse_errors] && opts[:exceptions_file].nil? && opts[:rejections_file].nil?
62
+ exc = nil
63
+ rej = nil
64
+ else
65
+ # temporary files to get the excepted records (if not given)
66
+ exc = opts[:exceptions_file] ||= Tempfile.new('exceptions')
67
+ rej = opts[:rejections_file] ||= Tempfile.new('rejections')
68
+ exc = File.new(exc) unless exc.is_a?(File)
69
+ rej = File.new(rej) unless rej.is_a?(File)
70
+ end
66
71
 
67
72
  # execute the load
68
73
  execute(GoodData::SQLGenerator.load_data(table_name, csv_path, columns, opts))
69
74
 
70
- exc.close
71
- rej.close
75
+ exc.close if exc
76
+ rej.close if rej
72
77
 
73
- if exc.size > 0 || rej.size > 0
78
+ # if there was something rejected and it shouldn't be ignored, raise an error
79
+ if ((exc && File.size?(exc)) || (rej && File.size?(rej))) && (! opts[:ignore_parse_errors])
74
80
  fail ArgumentError, "Some lines in the CSV didn't go through. Exceptions: #{IO.read(exc)}\nRejected records: #{IO.read(rej)}"
75
81
  end
76
82
  end
@@ -25,7 +25,11 @@ module GoodData
25
25
  parser = opts[:parser] || 'GdcCsvParser()'
26
26
  escape_as = opts[:escape_as] || '"'
27
27
 
28
- exc_rej = opts[:ignore_parse_errors] ? '' : "EXCEPTIONS '#{File.absolute_path(opts[:exceptions_file])}' REJECTED DATA '#{File.absolute_path(opts[:rejections_file])}'"
28
+ exc_rej = if opts[:ignore_parse_errors] && opts[:exceptions_file].nil? && opts[:rejections_file].nil?
29
+ ''
30
+ else
31
+ "EXCEPTIONS '#{File.absolute_path(opts[:exceptions_file])}' REJECTED DATA '#{File.absolute_path(opts[:rejections_file])}'"
32
+ end
29
33
 
30
34
  %Q{COPY #{table} (#{col_list})
31
35
  FROM LOCAL '#{File.absolute_path(csv)}' WITH PARSER #{parser}
@@ -1,5 +1,5 @@
1
1
  module GoodData
2
2
  class Datawarehouse
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -2,6 +2,9 @@ require 'tempfile'
2
2
  require 'gooddata_datawarehouse/datawarehouse'
3
3
  require_relative 'spec_helper'
4
4
 
5
+ CSV_PATH = 'spec/data/bike.csv'
6
+ WRONG_CSV_PATH = 'spec/data/bike-wrong.csv'
7
+
5
8
  describe GoodData::Datawarehouse do
6
9
  before(:each) do
7
10
  @dwh = SpecHelper::create_default_connection
@@ -100,22 +103,80 @@ describe GoodData::Datawarehouse do
100
103
 
101
104
  describe '#csv_to_new_table' do
102
105
  it 'creates a new table from csv' do
103
- path = 'spec/data/bike.csv'
104
- @dwh.csv_to_new_table(@random_table_name, path)
106
+ @dwh.csv_to_new_table(@random_table_name, CSV_PATH)
107
+
108
+ # table exists
109
+ expect(@dwh.table_exists?(@random_table_name)).to eq true
110
+
111
+ # cols are the same as in the csv
112
+ expected_cols = File.open(CSV_PATH, &:gets).strip.split(',')
113
+ expect(Set.new(@dwh.get_columns(@random_table_name))).to eq Set.new(expected_cols.map {|c| {:column_name => c, :data_type => GoodData::SQLGenerator::DEFAULT_TYPE}})
114
+ end
115
+
116
+ it 'writes exceptions and rejections to files at given path, passed strings' do
117
+ rej = Tempfile.new('rejections.csv')
118
+ exc = Tempfile.new('exceptions.csv')
119
+
120
+ @dwh.csv_to_new_table(@random_table_name, CSV_PATH, :exceptions_file => exc.path, :rejections_file => rej.path)
121
+
122
+ expect(File.size(rej)).to eq 0
123
+ expect(File.size(exc)).to eq 0
124
+ end
125
+
126
+ it 'writes exceptions and rejections to files at given path, passed files' do
127
+ rej = Tempfile.new('rejections.csv')
128
+ exc = Tempfile.new('exceptions.csv')
129
+
130
+ @dwh.csv_to_new_table(@random_table_name, CSV_PATH, :exceptions_file => exc, :rejections_file => rej)
131
+
132
+ expect(File.size(rej)).to eq 0
133
+ expect(File.size(exc)).to eq 0
134
+ end
135
+
136
+ it "writes exceptions and rejections to files at given absolute path, when it's wrong there's something" do
137
+ rej = Tempfile.new('rejections.csv')
138
+ exc = Tempfile.new('exceptions.csv')
139
+
140
+ @dwh.csv_to_new_table(@random_table_name, WRONG_CSV_PATH, :exceptions_file => exc.path, :rejections_file => rej.path, :ignore_parse_errors => true)
141
+
142
+ expect(File.size(rej)).to be > 0
143
+ expect(File.size(exc)).to be > 0
144
+ end
145
+
146
+ it "writes exceptions and rejections to files at given relative path, when it's wrong there's something" do
147
+ rej = Tempfile.new('rejections.csv')
148
+ exc = Tempfile.new('exceptions.csv')
149
+
150
+ if File.dirname(rej) != File.dirname(exc)
151
+ raise "two directories for tempfiles!"
152
+ end
153
+
154
+ csv_path = File.expand_path(WRONG_CSV_PATH)
155
+
156
+ Dir.chdir(File.dirname(rej)) do
157
+ @dwh.csv_to_new_table(@random_table_name, csv_path, :exceptions_file => File.basename(exc), :rejections_file => File.basename(rej), :ignore_parse_errors => true)
158
+ end
159
+
160
+
161
+ expect(File.size(rej)).to be > 0
162
+ expect(File.size(exc)).to be > 0
163
+ end
164
+
165
+ it "does something when ignoring errors and not passing files" do
166
+ @dwh.csv_to_new_table(@random_table_name, CSV_PATH, :ignore_parse_errors => true)
105
167
 
106
168
  # table exists
107
169
  expect(@dwh.table_exists?(@random_table_name)).to eq true
108
170
 
109
171
  # cols are the same as in the csv
110
- expected_cols = File.open(path, &:gets).strip.split(',')
172
+ expected_cols = File.open(CSV_PATH, &:gets).strip.split(',')
111
173
  expect(Set.new(@dwh.get_columns(@random_table_name))).to eq Set.new(expected_cols.map {|c| {:column_name => c, :data_type => GoodData::SQLGenerator::DEFAULT_TYPE}})
112
174
  end
113
175
  end
114
176
 
115
177
  describe '#export_table' do
116
178
  it 'exports a created table' do
117
- path = 'spec/data/bike.csv'
118
- @dwh.csv_to_new_table(@random_table_name, path)
179
+ @dwh.csv_to_new_table(@random_table_name, CSV_PATH)
119
180
 
120
181
  # table exists
121
182
  expect(@dwh.table_exists?(@random_table_name)).to eq true
@@ -125,7 +186,7 @@ describe GoodData::Datawarehouse do
125
186
  @dwh.export_table(@random_table_name, f)
126
187
 
127
188
  # should be the same except for order of the lines
128
- imported = Set.new(CSV.read(path))
189
+ imported = Set.new(CSV.read(CSV_PATH))
129
190
  exported = Set.new(CSV.read(f))
130
191
 
131
192
  expect(exported).to eq imported
@@ -134,39 +195,42 @@ describe GoodData::Datawarehouse do
134
195
 
135
196
  describe '#load_data_from_csv' do
136
197
  it 'loads data from csv to existing table' do
137
- path = 'spec/data/bike.csv'
138
-
139
198
  # create the table
140
- @dwh.create_table_from_csv_header(@random_table_name, path)
199
+ @dwh.create_table_from_csv_header(@random_table_name, CSV_PATH)
141
200
  expect(@dwh.table_exists?(@random_table_name)).to eq true
142
201
 
143
- expected_cols = File.open(path, &:gets).strip.split(',')
202
+ expected_cols = File.open(CSV_PATH, &:gets).strip.split(',')
144
203
  expect(Set.new(@dwh.get_columns(@random_table_name))).to eq Set.new(expected_cols.map {|c| {:column_name => c, :data_type => GoodData::SQLGenerator::DEFAULT_TYPE}})
145
204
 
146
205
  # load the data there
147
- @dwh.load_data_from_csv(@random_table_name, path)
206
+ @dwh.load_data_from_csv(@random_table_name, CSV_PATH)
148
207
 
149
208
  # export it
150
209
  f = Tempfile.new('bike.csv')
151
210
  @dwh.export_table(@random_table_name, f)
152
211
 
153
212
  # should be the same except for order of the lines
154
- imported = Set.new(CSV.read(path))
213
+ imported = Set.new(CSV.read(CSV_PATH))
155
214
  exported = Set.new(CSV.read(f))
156
215
 
157
216
  expect(exported).to eq imported
158
217
  end
159
218
 
160
219
  it 'fails for a wrong csv' do
161
- path_wrong = 'spec/data/bike-wrong.csv'
162
-
163
220
  # create the table
164
- @dwh.create_table_from_csv_header(@random_table_name, path_wrong)
221
+ @dwh.create_table_from_csv_header(@random_table_name, WRONG_CSV_PATH)
165
222
  expect(@dwh.table_exists?(@random_table_name)).to eq true
166
223
 
167
224
  # load the data there - expect fail
168
- expect{@dwh.load_data_from_csv(@random_table_name, path_wrong)}.to raise_error(ArgumentError)
225
+ expect{@dwh.load_data_from_csv(@random_table_name, WRONG_CSV_PATH)}.to raise_error(ArgumentError)
226
+ end
227
+ end
169
228
 
229
+ describe '#get_columns' do
230
+ it 'gives you the right list of columns' do
231
+ expected_cols = File.open(CSV_PATH, &:gets).strip.split(',')
232
+ @dwh.create_table_from_csv_header(@random_table_name, CSV_PATH)
170
233
  end
234
+ # TODO more tests
171
235
  end
172
236
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gooddata_datawarehouse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Petr Cvengros
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-07 00:00:00.000000000 Z
11
+ date: 2015-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement