activewarehouse-etl 0.9.1 → 0.9.5.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/0.9-UPGRADE +6 -0
- data/CHANGELOG +182 -150
- data/Gemfile +4 -0
- data/HOW_TO_RELEASE +9 -0
- data/README +18 -2
- data/Rakefile +35 -91
- data/active_support_logger.patch +78 -0
- data/activewarehouse-etl.gemspec +30 -0
- data/lib/etl.rb +10 -2
- data/lib/etl/batch/directives.rb +11 -1
- data/lib/etl/control/control.rb +2 -2
- data/lib/etl/control/destination.rb +27 -7
- data/lib/etl/control/destination/database_destination.rb +8 -6
- data/lib/etl/control/destination/excel_destination.rb +91 -0
- data/lib/etl/control/destination/file_destination.rb +6 -4
- data/lib/etl/control/destination/insert_update_database_destination.rb +133 -0
- data/lib/etl/control/destination/update_database_destination.rb +109 -0
- data/lib/etl/control/source.rb +3 -2
- data/lib/etl/control/source/database_source.rb +14 -10
- data/lib/etl/control/source/file_source.rb +2 -2
- data/lib/etl/engine.rb +17 -15
- data/lib/etl/execution.rb +0 -1
- data/lib/etl/execution/batch.rb +3 -1
- data/lib/etl/execution/migration.rb +5 -0
- data/lib/etl/parser/delimited_parser.rb +20 -1
- data/lib/etl/parser/excel_parser.rb +112 -0
- data/lib/etl/processor/bulk_import_processor.rb +4 -2
- data/lib/etl/processor/database_join_processor.rb +68 -0
- data/lib/etl/processor/escape_csv_processor.rb +77 -0
- data/lib/etl/processor/filter_row_processor.rb +51 -0
- data/lib/etl/processor/ftp_downloader_processor.rb +68 -0
- data/lib/etl/processor/ftp_uploader_processor.rb +65 -0
- data/lib/etl/processor/imapattachment_downloader_processor.rb +91 -0
- data/lib/etl/processor/pop3attachment_downloader_processor.rb +90 -0
- data/lib/etl/processor/sftp_downloader_processor.rb +63 -0
- data/lib/etl/processor/sftp_uploader_processor.rb +63 -0
- data/lib/etl/processor/zip_file_processor.rb +27 -0
- data/lib/etl/transform/calculation_transform.rb +71 -0
- data/lib/etl/transform/foreign_key_lookup_transform.rb +25 -7
- data/lib/etl/transform/ordinalize_transform.rb +3 -1
- data/lib/etl/transform/split_fields_transform.rb +27 -0
- data/lib/etl/version.rb +1 -7
- data/test-matrix.yml +10 -0
- data/test/.gitignore +1 -0
- data/test/.ignore +2 -0
- data/test/all.ebf +6 -0
- data/test/apache_combined_log.ctl +11 -0
- data/test/batch_test.rb +41 -0
- data/test/batch_with_error.ebf +6 -0
- data/test/batched1.ctl +0 -0
- data/test/batched2.ctl +0 -0
- data/test/block_processor.ctl +6 -0
- data/test/block_processor_error.ctl +1 -0
- data/test/block_processor_pre_post_process.ctl +4 -0
- data/test/block_processor_remove_rows.ctl +5 -0
- data/test/block_processor_test.rb +38 -0
- data/test/config/Gemfile.rails-2.3.x +3 -0
- data/test/config/Gemfile.rails-2.3.x.lock +38 -0
- data/test/config/Gemfile.rails-3.0.x +3 -0
- data/test/config/Gemfile.rails-3.0.x.lock +49 -0
- data/test/config/common.rb +21 -0
- data/test/connection/mysql/connection.rb +9 -0
- data/test/connection/mysql/schema.sql +36 -0
- data/test/connection/postgresql/connection.rb +13 -0
- data/test/connection/postgresql/schema.sql +39 -0
- data/test/control_test.rb +43 -0
- data/test/data/apache_combined_log.txt +3 -0
- data/test/data/bulk_import.txt +3 -0
- data/test/data/bulk_import_with_empties.txt +3 -0
- data/test/data/decode.txt +3 -0
- data/test/data/delimited.txt +3 -0
- data/test/data/encode_source_latin1.txt +2 -0
- data/test/data/excel.xls +0 -0
- data/test/data/excel2.xls +0 -0
- data/test/data/fixed_width.txt +3 -0
- data/test/data/multiple_delimited_1.txt +3 -0
- data/test/data/multiple_delimited_2.txt +3 -0
- data/test/data/people.txt +3 -0
- data/test/data/sax.xml +14 -0
- data/test/data/xml.xml +16 -0
- data/test/date_dimension_builder_test.rb +96 -0
- data/test/delimited.ctl +30 -0
- data/test/delimited_absolute.ctl +33 -0
- data/test/delimited_destination_db.ctl +25 -0
- data/test/delimited_excel.ctl +31 -0
- data/test/delimited_insert_update.ctl +34 -0
- data/test/delimited_update.ctl +34 -0
- data/test/delimited_with_bulk_load.ctl +34 -0
- data/test/destination_test.rb +275 -0
- data/test/directive_test.rb +23 -0
- data/test/encode_processor_test.rb +32 -0
- data/test/engine_test.rb +32 -0
- data/test/errors.ctl +24 -0
- data/test/etl_test.rb +42 -0
- data/test/excel.ctl +24 -0
- data/test/excel2.ctl +25 -0
- data/test/fixed_width.ctl +35 -0
- data/test/generator_test.rb +14 -0
- data/test/inline_parser.ctl +17 -0
- data/test/mocks/mock_destination.rb +26 -0
- data/test/mocks/mock_source.rb +25 -0
- data/test/model_source.ctl +14 -0
- data/test/multiple_delimited.ctl +22 -0
- data/test/multiple_source_delimited.ctl +39 -0
- data/test/parser_test.rb +224 -0
- data/test/performance/delimited.ctl +30 -0
- data/test/processor_test.rb +44 -0
- data/test/row_processor_test.rb +17 -0
- data/test/sax.ctl +26 -0
- data/test/scd/1.txt +1 -0
- data/test/scd/2.txt +1 -0
- data/test/scd/3.txt +1 -0
- data/test/scd_test.rb +257 -0
- data/test/scd_test_type_1.ctl +43 -0
- data/test/scd_test_type_2.ctl +34 -0
- data/test/screen_test.rb +9 -0
- data/test/screen_test_error.ctl +3 -0
- data/test/screen_test_fatal.ctl +3 -0
- data/test/source_test.rb +139 -0
- data/test/test_helper.rb +34 -0
- data/test/transform_test.rb +101 -0
- data/test/vendor/adapter_extensions-0.5.0/CHANGELOG +26 -0
- data/test/vendor/adapter_extensions-0.5.0/LICENSE +16 -0
- data/test/vendor/adapter_extensions-0.5.0/README +7 -0
- data/test/vendor/adapter_extensions-0.5.0/Rakefile +158 -0
- data/test/vendor/adapter_extensions-0.5.0/lib/adapter_extensions.rb +12 -0
- data/test/vendor/adapter_extensions-0.5.0/lib/adapter_extensions/connection_adapters/abstract_adapter.rb +44 -0
- data/test/vendor/adapter_extensions-0.5.0/lib/adapter_extensions/connection_adapters/mysql_adapter.rb +63 -0
- data/test/vendor/adapter_extensions-0.5.0/lib/adapter_extensions/connection_adapters/postgresql_adapter.rb +52 -0
- data/test/vendor/adapter_extensions-0.5.0/lib/adapter_extensions/connection_adapters/sqlserver_adapter.rb +44 -0
- data/test/vendor/adapter_extensions-0.5.0/lib/adapter_extensions/version.rb +10 -0
- data/test/xml.ctl +31 -0
- metadata +229 -70
- data/lib/etl/execution/record.rb +0 -18
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class BadDirective < ETL::Batch::Directive
|
4
|
+
|
5
|
+
end
|
6
|
+
|
7
|
+
class BatchTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
attr_reader :file
|
10
|
+
attr_reader :engine
|
11
|
+
def setup
|
12
|
+
@file = File.dirname(__FILE__) + '/all.ebf'
|
13
|
+
@engine = ETL::Engine.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_directive_without_implementation_should_fail
|
17
|
+
batch = ETL::Batch::Batch.resolve(file, engine)
|
18
|
+
assert_raise RuntimeError do
|
19
|
+
d = BadDirective.new(batch)
|
20
|
+
d.execute
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.dirname(__FILE__) + '/test_helper'
|
3
|
+
|
4
|
+
class EncodeProcessorTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
SOURCE = 'data/encode_source_latin1.txt'
|
7
|
+
TARGET = 'output/encode_destination_utf-8.txt'
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@control = flexmock("control")
|
11
|
+
@control.should_receive(:file).twice.and_return(File.dirname(__FILE__) + '/fake-control.ctl')
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_should_transform_a_latin1_file_to_utf8_with_grace
|
15
|
+
configuration = { :source_file => SOURCE, :source_encoding => 'latin1', :target_file => TARGET, :target_encoding => 'utf-8' }
|
16
|
+
ETL::Processor::EncodeProcessor.new(@control, configuration).process
|
17
|
+
assert_equal "éphémère has accents.\nlet's encode them.", IO.read(File.join(File.dirname(__FILE__),TARGET))
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_throw_exception_on_unsupported_encoding
|
21
|
+
configuration = { :source_file => SOURCE, :source_encoding => 'acme-encoding', :target_file => TARGET, :target_encoding => 'utf-8' }
|
22
|
+
error = assert_raise(ETL::ControlError) { ETL::Processor::EncodeProcessor.new(@control, configuration) }
|
23
|
+
assert_equal "Either the source encoding 'acme-encoding' or the target encoding 'utf-8' is not supported", error.message
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_should_throw_exception_when_target_and_source_are_the_same
|
27
|
+
configuration = { :source_file => SOURCE, :source_encoding => 'latin1', :target_file => SOURCE, :target_encoding => 'utf-8' }
|
28
|
+
error = assert_raise(ETL::ControlError) { ETL::Processor::EncodeProcessor.new(@control, configuration) }
|
29
|
+
assert_equal "Source and target file cannot currently point to the same file", error.message
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/test/engine_test.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class EngineTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_connections
|
6
|
+
assert_equal 3, ActiveRecord::Base.configurations.length
|
7
|
+
conn = ETL::Engine.connection(:data_warehouse)
|
8
|
+
assert_not_nil conn
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_non_existent_connection
|
12
|
+
assert_raise ETL::ETLError do
|
13
|
+
conn = ETL::Engine.connection(:does_not_exist)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_engine_table_method_should_return_same_name_when_not_using_temp_tables
|
18
|
+
assert_equal 'foo', ETL::Engine.table('foo', connection)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_engine_table_method_should_return_temp_name_when_using_temp_tables
|
22
|
+
ETL::Engine.use_temp_tables = true
|
23
|
+
assert_equal 'tmp_people', ETL::Engine.table('people', connection)
|
24
|
+
ETL::Engine.use_temp_tables = false
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
def connection
|
29
|
+
ETL::Engine.connection(:data_warehouse)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/test/errors.ctl
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
class ErrorProcessor < ETL::Processor::RowProcessor
|
2
|
+
def initialize(control, configuration)
|
3
|
+
super
|
4
|
+
end
|
5
|
+
def process(row)
|
6
|
+
raise RuntimeError, "Generated error"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
set_error_threshold 1
|
11
|
+
|
12
|
+
source :in, {
|
13
|
+
:type => :enumerable,
|
14
|
+
:enumerable => [
|
15
|
+
{:first_name => 'Bob',:last_name => 'Smith'},
|
16
|
+
{:first_name => 'Joe', :last_name => 'Thompson'}
|
17
|
+
]
|
18
|
+
},
|
19
|
+
[
|
20
|
+
:first_name,
|
21
|
+
:last_name
|
22
|
+
]
|
23
|
+
|
24
|
+
after_read ErrorProcessor
|
data/test/etl_test.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
# This is an integration test
|
4
|
+
class ETLTest < Test::Unit::TestCase
|
5
|
+
# Test end-to-end integration of ETL engine processing for the delimited.ctl control file
|
6
|
+
def test_delimited_single_file_load
|
7
|
+
#ETL::Engine.logger = Logger.new(STDOUT)
|
8
|
+
#ETL::Engine.logger.level = Logger::DEBUG
|
9
|
+
|
10
|
+
ETL::Engine.init(:config => File.dirname(__FILE__) + '/database.yml')
|
11
|
+
ETL::Engine.process(File.dirname(__FILE__) + '/delimited.ctl')
|
12
|
+
lines = open(File.dirname(__FILE__) + '/output/delimited.txt').readlines
|
13
|
+
assert_equal 3, lines.length
|
14
|
+
|
15
|
+
data = lines[0].split(',')
|
16
|
+
assert_equal '1', data[0]
|
17
|
+
assert_equal 'Chris', data[1]
|
18
|
+
assert_equal 'Smith', data[2]
|
19
|
+
assert_equal '23cc5914d48b146f0fbb73c4', data[3]
|
20
|
+
assert_equal '24', data[4]
|
21
|
+
assert_equal 'Male', data[5]
|
22
|
+
assert_equal 'test!', data[6]
|
23
|
+
assert_nothing_raised { Time.parse(data[7]) }
|
24
|
+
|
25
|
+
data = lines[1].split(',')
|
26
|
+
assert_equal '2', data[0]
|
27
|
+
assert_equal 'Jim', data[1]
|
28
|
+
assert_equal 'Foxworthy', data[2]
|
29
|
+
assert_equal '596e3534978b8c2b47851e37', data[3]
|
30
|
+
assert_equal '51', data[4]
|
31
|
+
assert_equal 'Male', data[5]
|
32
|
+
assert_equal 'test!', data[6]
|
33
|
+
assert_nothing_raised { Time.parse(data[7]) }
|
34
|
+
end
|
35
|
+
|
36
|
+
# Test end-to-end integration of ETL engine processing for the fixed_width.ctl control file
|
37
|
+
def test_fixed_width_single_file_load
|
38
|
+
ETL::Engine.process(File.dirname(__FILE__) + '/fixed_width.ctl')
|
39
|
+
lines = open(File.dirname(__FILE__) + '/output/delimited.txt').readlines
|
40
|
+
assert_equal 3, lines.length
|
41
|
+
end
|
42
|
+
end
|
data/test/excel.ctl
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
source :in, {
|
2
|
+
:file => 'data/excel.xls',
|
3
|
+
:parser => :excel
|
4
|
+
},
|
5
|
+
{
|
6
|
+
:ignore_blank_line => false,
|
7
|
+
:fields => [
|
8
|
+
:first_name,
|
9
|
+
:last_name,
|
10
|
+
:ssn,
|
11
|
+
:age
|
12
|
+
]
|
13
|
+
}
|
14
|
+
|
15
|
+
transform :ssn, :sha1
|
16
|
+
transform(:ssn){ |n, v, r| v[0,24] }
|
17
|
+
|
18
|
+
|
19
|
+
destination :out, {
|
20
|
+
:file => 'output/excel.out.txt'
|
21
|
+
},
|
22
|
+
{
|
23
|
+
:order => [:first_name, :last_name, :ssn, :age]
|
24
|
+
}
|
data/test/excel2.ctl
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
source :in, {
|
2
|
+
:file => 'data/excel2.xls',
|
3
|
+
:parser => :excel
|
4
|
+
},
|
5
|
+
{
|
6
|
+
:ignore_blank_line => true,
|
7
|
+
:worksheets => [ 1 ],
|
8
|
+
:fields => [
|
9
|
+
:first_name,
|
10
|
+
:last_name,
|
11
|
+
:ssn,
|
12
|
+
:age
|
13
|
+
]
|
14
|
+
}
|
15
|
+
|
16
|
+
transform :ssn, :sha1
|
17
|
+
transform(:ssn){ |n, v, r| v[0,24] }
|
18
|
+
|
19
|
+
|
20
|
+
destination :out, {
|
21
|
+
:file => 'output/excel2.out.txt'
|
22
|
+
},
|
23
|
+
{
|
24
|
+
:order => [:first_name, :last_name, :ssn, :age]
|
25
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# puts "executing fixed_width.ctl"
|
2
|
+
|
3
|
+
source :in, {
|
4
|
+
:file => 'data/fixed_width.txt',
|
5
|
+
:parser => :fixed_width
|
6
|
+
},
|
7
|
+
{
|
8
|
+
:first_name => {
|
9
|
+
:start => 1,
|
10
|
+
:length => 9
|
11
|
+
},
|
12
|
+
:last_name => {
|
13
|
+
:start => 10,
|
14
|
+
:length => 12
|
15
|
+
},
|
16
|
+
:ssn => {
|
17
|
+
:start => 22,
|
18
|
+
:length => 9
|
19
|
+
},
|
20
|
+
:age => {
|
21
|
+
:start => 31,
|
22
|
+
:length => 2,
|
23
|
+
:type => :integer
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
transform :ssn, :sha1
|
28
|
+
transform(:ssn){ |n, v, r| v[0,24] }
|
29
|
+
|
30
|
+
destination :out, {
|
31
|
+
:file => 'output/fixed_width.txt'
|
32
|
+
},
|
33
|
+
{
|
34
|
+
:order => [:first_name, :last_name, :ssn, :age]
|
35
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
# Test generators
|
4
|
+
class GeneratorTest < Test::Unit::TestCase
|
5
|
+
# Test the surrogate key generator
|
6
|
+
def test_surrogate_key_generator
|
7
|
+
generator_class = ETL::Generator::Generator.class_for_name(:surrogate_key)
|
8
|
+
assert_equal ETL::Generator::SurrogateKeyGenerator, generator_class
|
9
|
+
generator = generator_class.new
|
10
|
+
1.upto(10) do |i|
|
11
|
+
assert_equal i, generator.next
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class MyParser < ETL::Parser::Parser
|
2
|
+
def each
|
3
|
+
[{:name => 'foo'},{:name => 'bar'},{:name => 'baz'}].each do |row|
|
4
|
+
yield row
|
5
|
+
end
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
source :in, {
|
10
|
+
:file => '',
|
11
|
+
:parser => MyParser
|
12
|
+
},
|
13
|
+
[
|
14
|
+
:name
|
15
|
+
]
|
16
|
+
|
17
|
+
destination :out, {:file => 'output/inline_parser.txt'},{:order => [:name]}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ETL
|
2
|
+
module Control
|
3
|
+
# Usage:
|
4
|
+
# - declare in the ctl file:
|
5
|
+
# destination :out, { :type => :mock, :name => :my_mock_output }
|
6
|
+
# - run the .ctl from your test
|
7
|
+
# - then assert the content of the rows
|
8
|
+
# assert_equal [{:name => 'John Barry'},{:name => 'Gary Moore'}], MockDestination[:my_mock_output]
|
9
|
+
class MockDestination < Destination
|
10
|
+
def initialize(control, configuration, mapping={})
|
11
|
+
super
|
12
|
+
@mock_destination_name = configuration[:name] || 'mock_destination'
|
13
|
+
@@registry ||= {}
|
14
|
+
@@registry[@mock_destination_name] ||= []
|
15
|
+
end
|
16
|
+
def self.[](mock_destination_name)
|
17
|
+
@@registry[mock_destination_name]
|
18
|
+
end
|
19
|
+
def write(row)
|
20
|
+
@@registry[@mock_destination_name] << row
|
21
|
+
end
|
22
|
+
# the presence of close is asserted - just do nothing
|
23
|
+
def close; end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ETL
|
2
|
+
module Control
|
3
|
+
# Usage:
|
4
|
+
# - first set the data in your test setup
|
5
|
+
# MockSource[:my_input] = [ { :first_name => 'John', :last_name => 'Barry' }, { ...} ]
|
6
|
+
# - then declare in the ctl file:
|
7
|
+
# source :in, { :type => :mock, :name => :my_input }
|
8
|
+
class MockSource < EnumerableSource
|
9
|
+
def initialize(control, configuration, definition)
|
10
|
+
super
|
11
|
+
mock_source_name = configuration[:name] || 'mock_source'
|
12
|
+
throw "No mock source data set for mock source '#{mock_source_name}'" if @@registry[mock_source_name].nil?
|
13
|
+
configuration[:enumerable] = @@registry[mock_source_name]
|
14
|
+
end
|
15
|
+
def self.[]=(mock_source_name,mock_source_data)
|
16
|
+
@@registry ||= {}
|
17
|
+
@@registry[mock_source_name] = mock_source_data
|
18
|
+
end
|
19
|
+
def self.[](mock_source_name)
|
20
|
+
@@registry[mock_source_name]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# puts "executing delimited.ctl"
|
2
|
+
|
3
|
+
source :in, {
|
4
|
+
:file => 'data/multiple_delimited_*.txt',
|
5
|
+
:parser => :delimited
|
6
|
+
},
|
7
|
+
[
|
8
|
+
:first_name,
|
9
|
+
:last_name,
|
10
|
+
:ssn,
|
11
|
+
{
|
12
|
+
:name => :age,
|
13
|
+
:type => :integer
|
14
|
+
}
|
15
|
+
]
|
16
|
+
|
17
|
+
destination :out, {
|
18
|
+
:file => 'output/multiple_delimited.txt'
|
19
|
+
},
|
20
|
+
{
|
21
|
+
:order => [:first_name, :last_name, :ssn, :age]
|
22
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# puts "executing delimited.ctl"
|
2
|
+
|
3
|
+
source :source1, {
|
4
|
+
:file => 'data/multiple_delimited_*.txt',
|
5
|
+
:parser => :delimited
|
6
|
+
},
|
7
|
+
[
|
8
|
+
:first_name,
|
9
|
+
:last_name,
|
10
|
+
:ssn,
|
11
|
+
{
|
12
|
+
:name => :age,
|
13
|
+
:type => :integer
|
14
|
+
}
|
15
|
+
]
|
16
|
+
|
17
|
+
source :source2, {
|
18
|
+
:file => 'data/multiple_delimited_*.txt',
|
19
|
+
:parser => :delimited
|
20
|
+
},
|
21
|
+
[
|
22
|
+
:first_name,
|
23
|
+
:last_name,
|
24
|
+
:ssn,
|
25
|
+
{
|
26
|
+
:name => :age,
|
27
|
+
:type => :integer
|
28
|
+
}
|
29
|
+
]
|
30
|
+
|
31
|
+
transform :ssn, :sha1
|
32
|
+
transform(:ssn){ |v| v[0,24] }
|
33
|
+
|
34
|
+
destination :out, {
|
35
|
+
:file => 'output/multiple_source_delimited.txt'
|
36
|
+
},
|
37
|
+
{
|
38
|
+
:order => [:first_name, :last_name, :ssn, :age]
|
39
|
+
}
|
data/test/parser_test.rb
ADDED
@@ -0,0 +1,224 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
# Test the flat text parsers
|
4
|
+
class ParserTest < Test::Unit::TestCase
|
5
|
+
# Test parsing delimited data
|
6
|
+
def test_delimited_parser
|
7
|
+
control = ETL::Control::Control.resolve(File.dirname(__FILE__) + '/delimited.ctl')
|
8
|
+
parser = ETL::Parser::DelimitedParser.new(control.sources.first)
|
9
|
+
rows = parser.collect { |row| row }
|
10
|
+
assert_equal 3, rows.length
|
11
|
+
assert_equal({:first_name=>"Chris", :last_name=>"Smith", :ssn=>"111223333", :age=>"24", :sex => 'M'}, rows.first)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Test parsing fixed-width data
|
15
|
+
def test_fixed_width_parser
|
16
|
+
control = ETL::Control::Control.resolve(File.dirname(__FILE__) + '/fixed_width.ctl')
|
17
|
+
parser = ETL::Parser::FixedWidthParser.new(control.sources.first)
|
18
|
+
rows = parser.collect { |row| row }
|
19
|
+
assert_equal 3, rows.length
|
20
|
+
assert_equal({:first_name=>"Bob", :last_name=>"Smith", :ssn=>"123445555", :age=>"23"}, rows.first)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Test the DOM-based XML parser. Note that the DOM parser is slow and should
|
24
|
+
# probably be removed.
|
25
|
+
def test_xml_parser
|
26
|
+
control = ETL::Control::Control.resolve(File.dirname(__FILE__) + '/xml.ctl')
|
27
|
+
parser = ETL::Parser::XmlParser.new(control.sources.first)
|
28
|
+
rows = parser.collect { |row| row }
|
29
|
+
assert_equal 2, rows.length
|
30
|
+
assert_equal({:first_name=>"Bob", :last_name=>"Smith", :ssn=>"123456789", :age=>"24"}, rows.first)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Test an inline parser
|
34
|
+
def test_inline_parser
|
35
|
+
ETL::Engine.process(File.dirname(__FILE__) + '/inline_parser.ctl')
|
36
|
+
lines = open(File.dirname(__FILE__) + '/output/inline_parser.txt').readlines
|
37
|
+
assert_equal 3, lines.length
|
38
|
+
end
|
39
|
+
|
40
|
+
# Test the SAX parser (preferred for XML parsing)
|
41
|
+
def test_sax_parser
|
42
|
+
control = ETL::Control::Control.resolve(File.dirname(__FILE__) + '/sax.ctl')
|
43
|
+
parser = control.sources.first.parser
|
44
|
+
rows = parser.collect { |row| row }
|
45
|
+
assert_equal 2, rows.length
|
46
|
+
assert_equal({:first_name=>"Bob", :last_name=>"Smith", :ssn=>"123456789", :age=>"24"}, rows.first)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Test the Excel parser
|
50
|
+
def test_excel_parser
|
51
|
+
control = ETL::Control::Control.resolve(File.dirname(__FILE__) + '/excel.ctl')
|
52
|
+
parser = control.sources.first.parser
|
53
|
+
rows = parser.collect { |row| row }
|
54
|
+
assert_equal 2, rows.length
|
55
|
+
assert_equal({:first_name=>"Bob", :last_name=>"Smith", :ssn=>123456789, :age=>24}, rows.first)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Test 2 the Excel parser
|
59
|
+
def test_excel2_parser
|
60
|
+
control = ETL::Control::Control.resolve(File.dirname(__FILE__) + '/excel2.ctl')
|
61
|
+
parser = control.sources.first.parser
|
62
|
+
rows = parser.collect { |row| row }
|
63
|
+
assert_equal 2, rows.length
|
64
|
+
assert_equal({:first_name=>"John", :last_name=>"Doe", :ssn=>222114545, :age=>31}, rows[1])
|
65
|
+
end
|
66
|
+
|
67
|
+
# Test the Apache combined log format parser
|
68
|
+
def test_apache_combined_log_parser
|
69
|
+
control = ETL::Control::Control.resolve(File.dirname(__FILE__) + '/apache_combined_log.ctl')
|
70
|
+
parser = ETL::Parser::ApacheCombinedLogParser.new(control.sources.first)
|
71
|
+
# first test the parse method
|
72
|
+
line = %Q(127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "http://www.example.com/start.html" "Mozilla/4.08 [en] (Win98; I ;Nav)")
|
73
|
+
fields = parser.parse(line)
|
74
|
+
assert_equal '127.0.0.1', fields[:ip_address]
|
75
|
+
assert_equal 'frank', fields[:user]
|
76
|
+
assert_equal Time.mktime(2000, 10, 10, 13, 55, 36), fields[:timestamp]
|
77
|
+
assert_equal 'GET /apache_pb.gif HTTP/1.0', fields[:request]
|
78
|
+
assert_equal '200', fields[:response_code]
|
79
|
+
assert_equal '2326', fields[:bytes]
|
80
|
+
assert_equal 'http://www.example.com/start.html', fields[:referrer]
|
81
|
+
assert_equal 'Mozilla/4.08 [en] (Win98; I ;Nav)', fields[:user_agent]
|
82
|
+
#now test the each method
|
83
|
+
rows = parser.collect { |row| row }
|
84
|
+
assert_equal 3, rows.length
|
85
|
+
assert_equal({
|
86
|
+
:user_agent=>"Mozilla/4.08 [en] (Win98; I ;Nav)",
|
87
|
+
:browser_version_minor=>nil,
|
88
|
+
:timestamp=>Time.mktime(2000, 10, 10, 13, 55, 36),
|
89
|
+
:ostype=>"Windows",
|
90
|
+
:request=>"GET /apache_pb.gif HTTP/1.0",
|
91
|
+
:referrer_host=>"www.example.com",
|
92
|
+
:referrer_domain=>"example.com",
|
93
|
+
:os=>"Win98",
|
94
|
+
:referrer_uri_path=>"/start.html",
|
95
|
+
:method=>"GET",
|
96
|
+
:response_code=>"200",
|
97
|
+
:referrer_port=>80,
|
98
|
+
:os_version=>nil,
|
99
|
+
:ip_address=>"127.0.0.1",
|
100
|
+
:referrer_scheme=>"http",
|
101
|
+
:bytes=>"2326",
|
102
|
+
:browser=>"Mozilla",
|
103
|
+
:identd=>nil,
|
104
|
+
:path=>"/apache_pb.gif",
|
105
|
+
:referrer=>"http://www.example.com/start.html",
|
106
|
+
:browser_version_major=>"4",
|
107
|
+
:user=>"frank"}, rows.first, 'Failed on first row')
|
108
|
+
assert_equal({
|
109
|
+
:user_agent=>"Mozilla/4.08 [en] (Win98; I ;Nav)",
|
110
|
+
:referrer_port=>80,
|
111
|
+
:timestamp=>Time.mktime(2000, 10, 11, 5, 22, 2),
|
112
|
+
:browser_version_minor=>nil,
|
113
|
+
:os_version=>nil,
|
114
|
+
:request=>"GET /apache_pb.gif HTTP/1.1",
|
115
|
+
:ostype=>"Windows",
|
116
|
+
:referrer_scheme=>"http",
|
117
|
+
:response_code=>"200",
|
118
|
+
:referrer_host=>"www.foo.com",
|
119
|
+
:referrer_domain=>"foo.com",
|
120
|
+
:ip_address=>"127.0.0.1",
|
121
|
+
:browser=>"Mozilla",
|
122
|
+
:bytes=>"2326",
|
123
|
+
:os=>"Win98",
|
124
|
+
:identd=>nil,
|
125
|
+
:browser_version_major=>"4",
|
126
|
+
:referrer=>"http://www.foo.com/",
|
127
|
+
:referrer_uri_path=>"/",
|
128
|
+
:method=>"GET",
|
129
|
+
:path=>"/apache_pb.gif",
|
130
|
+
:user=>"bob"}, rows[1], 'Failed on second row')
|
131
|
+
assert_equal({
|
132
|
+
:browser_version_major=>"4",
|
133
|
+
:browser_version_minor=>nil,
|
134
|
+
:referrer_port=>nil,
|
135
|
+
:request=>"GET /apache_pb.gif HTTP/1.1",
|
136
|
+
:ostype=>"Windows",
|
137
|
+
:os_version=>nil,
|
138
|
+
:response_code=>"200",
|
139
|
+
:referrer_host=>nil,
|
140
|
+
:referrer_scheme=>nil,
|
141
|
+
:bytes=>"2326",
|
142
|
+
:ip_address=>"127.0.0.1",
|
143
|
+
:browser=>"Mozilla",
|
144
|
+
:referrer=>nil,
|
145
|
+
:os=>"Win98",
|
146
|
+
:user=>"bob",
|
147
|
+
:user_agent=>"Mozilla/4.08 [en] (Win98; I ;Nav)",
|
148
|
+
:identd=>nil,
|
149
|
+
:referrer_uri_path=>nil,
|
150
|
+
:path=>"/apache_pb.gif",
|
151
|
+
:method=>"GET",
|
152
|
+
:timestamp=>Time.mktime(2000, 10, 11, 5, 52, 31)}, rows[2], 'Failed on third row')
|
153
|
+
end
|
154
|
+
|
155
|
+
# Test the user agent parser
|
156
|
+
def test_user_agent_parser
|
157
|
+
agents = <<-AGENTS
|
158
|
+
Mozilla/4.7 [en] (WinNT; U)
|
159
|
+
Mozilla/4.0 (compatible; MSIE 5.01; Windows NT)
|
160
|
+
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; T312461; .NET CLR 1.1.4322)
|
161
|
+
Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 4.0) Opera 5.11 [en]
|
162
|
+
Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02
|
163
|
+
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040612 Firefox/0.8
|
164
|
+
Mozilla/5.0 (compatible; Konqueror/3.2; Linux) (KHTML, like Gecko)
|
165
|
+
Lynx/2.8.4rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.6h
|
166
|
+
AGENTS
|
167
|
+
agents = agents.split("\n").collect { |s| s.strip }
|
168
|
+
|
169
|
+
control = ETL::Control::Control.resolve(File.dirname(__FILE__) + '/apache_combined_log.ctl')
|
170
|
+
parser = ETL::Parser::ApacheCombinedLogParser.new(control.sources.first)
|
171
|
+
rows = parser.collect { |row| row }
|
172
|
+
|
173
|
+
assert_equal({:browser_version_major=>"4",
|
174
|
+
:browser_version_minor=>nil,
|
175
|
+
:ostype=>"Windows",
|
176
|
+
:os=>"WinNT",
|
177
|
+
:os_version=>nil,
|
178
|
+
:browser=>"Mozilla"}, parser.parse_user_agent(agents[0]), 'Agent 0 invalid'
|
179
|
+
)
|
180
|
+
assert_equal({:browser_version_major=>"5",
|
181
|
+
:browser_version_minor=>"01",
|
182
|
+
:ostype=>"Windows",
|
183
|
+
:os=>"Windows NT",
|
184
|
+
:os_version=>nil,
|
185
|
+
:browser=>"MSIE"}, parser.parse_user_agent(agents[1]), 'Agent 1 invalid'
|
186
|
+
)
|
187
|
+
assert_equal({:browser_version_major=>"6",
|
188
|
+
:browser_version_minor=>"0",
|
189
|
+
:ostype=>"Windows",
|
190
|
+
:os=>"Windows NT 5.0",
|
191
|
+
:os_version=>"5.0",
|
192
|
+
:browser=>"MSIE"}, parser.parse_user_agent(agents[2]), 'Agent 2 invalid'
|
193
|
+
)
|
194
|
+
assert_equal({:browser_version_major=>"5",
|
195
|
+
:browser_version_minor=>"0",
|
196
|
+
:ostype=>"Windows",
|
197
|
+
:os=>"Windows NT 4.0",
|
198
|
+
:os_version=>"4.0",
|
199
|
+
:browser=>"MSIE"}, parser.parse_user_agent(agents[3]), 'Agent 3 invalid'
|
200
|
+
)
|
201
|
+
assert_equal({:browser_version_major=>"7",
|
202
|
+
:browser_version_minor=>nil,
|
203
|
+
:ostype=>"Windows",
|
204
|
+
:os=>"Windows NT 5.0",
|
205
|
+
:os_version=>"5.0",
|
206
|
+
:browser=>"Netscape"}, parser.parse_user_agent(agents[4]), 'Agent 4 invalid'
|
207
|
+
)
|
208
|
+
assert_equal({:browser_version_major=>"0.8",
|
209
|
+
:browser_version_minor=>nil,
|
210
|
+
:ostype=>"Linux",
|
211
|
+
:os=>"Linux i686",
|
212
|
+
:os_version=>nil,
|
213
|
+
:browser=>"Firefox"}, parser.parse_user_agent(agents[5]), 'Agent 5 invalid'
|
214
|
+
)
|
215
|
+
# test fails here
|
216
|
+
# assert_equal({:browser_version_major=>"6",
|
217
|
+
# :browser_version_minor=>nil,
|
218
|
+
# :ostype=>"Linux",
|
219
|
+
# :os=>"Linux",
|
220
|
+
# :os_version=>nil,
|
221
|
+
# :browser=>"Konquerer"}, parser.parse_user_agent(agents[6]), 'Agent 6 invalid'
|
222
|
+
# )
|
223
|
+
end
|
224
|
+
end
|