balinterdi-activewarehouse-etl 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/0.9-UPGRADE +6 -0
- data/CHANGELOG +198 -0
- data/HOW_TO_RELEASE +4 -0
- data/LICENSE +7 -0
- data/README +85 -0
- data/Rakefile +169 -0
- data/TODO +28 -0
- data/VERSION +1 -0
- data/active_support_logger.patch +78 -0
- data/balinterdi-activewarehouse-etl.gemspec +221 -0
- data/bin/etl +28 -0
- data/bin/etl.cmd +8 -0
- data/examples/database.example.yml +16 -0
- data/lib/etl.rb +78 -0
- data/lib/etl/batch.rb +2 -0
- data/lib/etl/batch/batch.rb +111 -0
- data/lib/etl/batch/directives.rb +55 -0
- data/lib/etl/builder.rb +2 -0
- data/lib/etl/builder/date_dimension_builder.rb +96 -0
- data/lib/etl/builder/time_dimension_builder.rb +31 -0
- data/lib/etl/commands/etl.rb +89 -0
- data/lib/etl/control.rb +3 -0
- data/lib/etl/control/control.rb +405 -0
- data/lib/etl/control/destination.rb +420 -0
- data/lib/etl/control/destination/database_destination.rb +95 -0
- data/lib/etl/control/destination/file_destination.rb +124 -0
- data/lib/etl/control/source.rb +109 -0
- data/lib/etl/control/source/database_source.rb +220 -0
- data/lib/etl/control/source/enumerable_source.rb +11 -0
- data/lib/etl/control/source/file_source.rb +90 -0
- data/lib/etl/control/source/model_source.rb +39 -0
- data/lib/etl/core_ext.rb +1 -0
- data/lib/etl/core_ext/time.rb +5 -0
- data/lib/etl/core_ext/time/calculations.rb +42 -0
- data/lib/etl/engine.rb +556 -0
- data/lib/etl/execution.rb +20 -0
- data/lib/etl/execution/base.rb +9 -0
- data/lib/etl/execution/batch.rb +8 -0
- data/lib/etl/execution/job.rb +8 -0
- data/lib/etl/execution/migration.rb +85 -0
- data/lib/etl/generator.rb +2 -0
- data/lib/etl/generator/generator.rb +20 -0
- data/lib/etl/generator/surrogate_key_generator.rb +39 -0
- data/lib/etl/http_tools.rb +139 -0
- data/lib/etl/parser.rb +11 -0
- data/lib/etl/parser/apache_combined_log_parser.rb +49 -0
- data/lib/etl/parser/delimited_parser.rb +75 -0
- data/lib/etl/parser/fixed_width_parser.rb +65 -0
- data/lib/etl/parser/parser.rb +41 -0
- data/lib/etl/parser/sax_parser.rb +218 -0
- data/lib/etl/parser/xml_parser.rb +65 -0
- data/lib/etl/processor.rb +11 -0
- data/lib/etl/processor/block_processor.rb +14 -0
- data/lib/etl/processor/bulk_import_processor.rb +81 -0
- data/lib/etl/processor/check_exist_processor.rb +80 -0
- data/lib/etl/processor/check_unique_processor.rb +35 -0
- data/lib/etl/processor/copy_field_processor.rb +26 -0
- data/lib/etl/processor/encode_processor.rb +55 -0
- data/lib/etl/processor/hierarchy_exploder_processor.rb +55 -0
- data/lib/etl/processor/print_row_processor.rb +12 -0
- data/lib/etl/processor/processor.rb +25 -0
- data/lib/etl/processor/rename_processor.rb +24 -0
- data/lib/etl/processor/require_non_blank_processor.rb +26 -0
- data/lib/etl/processor/row_processor.rb +17 -0
- data/lib/etl/processor/sequence_processor.rb +23 -0
- data/lib/etl/processor/surrogate_key_processor.rb +53 -0
- data/lib/etl/processor/truncate_processor.rb +35 -0
- data/lib/etl/row.rb +20 -0
- data/lib/etl/screen.rb +14 -0
- data/lib/etl/screen/row_count_screen.rb +20 -0
- data/lib/etl/transform.rb +2 -0
- data/lib/etl/transform/block_transform.rb +13 -0
- data/lib/etl/transform/date_to_string_transform.rb +20 -0
- data/lib/etl/transform/decode_transform.rb +51 -0
- data/lib/etl/transform/default_transform.rb +20 -0
- data/lib/etl/transform/foreign_key_lookup_transform.rb +151 -0
- data/lib/etl/transform/hierarchy_lookup_transform.rb +49 -0
- data/lib/etl/transform/ordinalize_transform.rb +12 -0
- data/lib/etl/transform/sha1_transform.rb +13 -0
- data/lib/etl/transform/string_to_date_transform.rb +16 -0
- data/lib/etl/transform/string_to_datetime_transform.rb +14 -0
- data/lib/etl/transform/string_to_time_transform.rb +11 -0
- data/lib/etl/transform/transform.rb +61 -0
- data/lib/etl/transform/trim_transform.rb +26 -0
- data/lib/etl/transform/type_transform.rb +35 -0
- data/lib/etl/util.rb +59 -0
- data/lib/etl/version.rb +9 -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/connection/native_mysql/connection.rb +9 -0
- data/test/connection/native_mysql/schema.sql +36 -0
- data/test/connection/postgresql/connection.rb +13 -0
- data/test/connection/postgresql/schema.sql +43 -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/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/database.example.yml +18 -0
- data/test/database.mysql.yml +18 -0
- data/test/database.postgres.yml +18 -0
- data/test/database.yml +18 -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_with_bulk_load.ctl +34 -0
- data/test/destination_test.rb +171 -0
- data/test/directive_test.rb +23 -0
- data/test/encode_processor_test.rb +31 -0
- data/test/engine_test.rb +32 -0
- data/test/errors.ctl +24 -0
- data/test/etl_test.rb +42 -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 +200 -0
- data/test/performance/delimited.ctl +30 -0
- data/test/processor_test.rb +38 -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 +263 -0
- data/test/scd_test_type_1.ctl +43 -0
- data/test/scd_test_type_2.ctl +42 -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 +33 -0
- data/test/transform_test.rb +101 -0
- data/test/xml.ctl +31 -0
- metadata +237 -0
@@ -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,31 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class EncodeProcessorTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
SOURCE = 'data/encode_source_latin1.txt'
|
6
|
+
TARGET = 'output/encode_destination_utf-8.txt'
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@control = flexmock("control")
|
10
|
+
@control.should_receive(:file).twice.and_return(File.dirname(__FILE__) + '/fake-control.ctl')
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_should_transform_a_latin1_file_to_utf8_with_grace
|
14
|
+
configuration = { :source_file => SOURCE, :source_encoding => 'latin1', :target_file => TARGET, :target_encoding => 'utf-8' }
|
15
|
+
ETL::Processor::EncodeProcessor.new(@control, configuration).process
|
16
|
+
assert_equal "éphémère has accents.\nlet's encode them.", IO.read(File.join(File.dirname(__FILE__),TARGET))
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_should_throw_exception_on_unsupported_encoding
|
20
|
+
configuration = { :source_file => SOURCE, :source_encoding => 'acme-encoding', :target_file => TARGET, :target_encoding => 'utf-8' }
|
21
|
+
error = assert_raise(ETL::ControlError) { ETL::Processor::EncodeProcessor.new(@control, configuration) }
|
22
|
+
assert_equal "Either the source encoding 'acme-encoding' or the target encoding 'utf-8' is not supported", error.message
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_should_throw_exception_when_target_and_source_are_the_same
|
26
|
+
configuration = { :source_file => SOURCE, :source_encoding => 'latin1', :target_file => SOURCE, :target_encoding => 'utf-8' }
|
27
|
+
error = assert_raise(ETL::ControlError) { ETL::Processor::EncodeProcessor.new(@control, configuration) }
|
28
|
+
assert_equal "Source and target file cannot currently point to the same file", error.message
|
29
|
+
end
|
30
|
+
|
31
|
+
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
|
@@ -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 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,200 @@
|
|
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 Apache combined log format parser
|
50
|
+
def test_apache_combined_log_parser
|
51
|
+
control = ETL::Control::Control.resolve(File.dirname(__FILE__) + '/apache_combined_log.ctl')
|
52
|
+
parser = ETL::Parser::ApacheCombinedLogParser.new(control.sources.first)
|
53
|
+
# first test the parse method
|
54
|
+
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)")
|
55
|
+
fields = parser.parse(line)
|
56
|
+
assert_equal '127.0.0.1', fields[:ip_address]
|
57
|
+
assert_equal 'frank', fields[:user]
|
58
|
+
assert_equal Time.mktime(2000, 10, 10, 13, 55, 36), fields[:timestamp]
|
59
|
+
assert_equal 'GET /apache_pb.gif HTTP/1.0', fields[:request]
|
60
|
+
assert_equal '200', fields[:response_code]
|
61
|
+
assert_equal '2326', fields[:bytes]
|
62
|
+
assert_equal 'http://www.example.com/start.html', fields[:referrer]
|
63
|
+
assert_equal 'Mozilla/4.08 [en] (Win98; I ;Nav)', fields[:user_agent]
|
64
|
+
#now test the each method
|
65
|
+
rows = parser.collect { |row| row }
|
66
|
+
assert_equal 3, rows.length
|
67
|
+
assert_equal({
|
68
|
+
:user_agent=>"Mozilla/4.08 [en] (Win98; I ;Nav)",
|
69
|
+
:browser_version_minor=>nil,
|
70
|
+
:timestamp=>Time.mktime(2000, 10, 10, 13, 55, 36),
|
71
|
+
:ostype=>"Windows",
|
72
|
+
:request=>"GET /apache_pb.gif HTTP/1.0",
|
73
|
+
:host=>"www.example.com",
|
74
|
+
:domain => "example.com",
|
75
|
+
:os=>"Win98",
|
76
|
+
:uri_path=>"/start.html",
|
77
|
+
:response_code=>"200",
|
78
|
+
:port=>80,
|
79
|
+
:os_version=>nil,
|
80
|
+
:ip_address=>"127.0.0.1",
|
81
|
+
:scheme=>"http",
|
82
|
+
:bytes=>"2326",
|
83
|
+
:browser=>"Mozilla",
|
84
|
+
:identd=>nil,
|
85
|
+
:referrer=>"http://www.example.com/start.html",
|
86
|
+
:browser_version_major=>"4",
|
87
|
+
:user=>"frank"}, rows.first, 'Failed on first row')
|
88
|
+
assert_equal({
|
89
|
+
:user_agent=>"Mozilla/4.08 [en] (Win98; I ;Nav)",
|
90
|
+
:port=>80,
|
91
|
+
:timestamp=>Time.mktime(2000, 10, 11, 5, 22, 2),
|
92
|
+
:browser_version_minor=>nil,
|
93
|
+
:os_version=>nil,
|
94
|
+
:request=>"GET /apache_pb.gif HTTP/1.1",
|
95
|
+
:ostype=>"Windows",
|
96
|
+
:scheme=>"http",
|
97
|
+
:response_code=>"200",
|
98
|
+
:host=>"www.foo.com",
|
99
|
+
:domain => "foo.com",
|
100
|
+
:ip_address=>"127.0.0.1",
|
101
|
+
:browser=>"Mozilla",
|
102
|
+
:bytes=>"2326",
|
103
|
+
:os=>"Win98",
|
104
|
+
:identd=>nil,
|
105
|
+
:browser_version_major=>"4",
|
106
|
+
:referrer=>"http://www.foo.com/",
|
107
|
+
:uri_path=>"/",
|
108
|
+
:user=>"bob"}, rows[1], 'Failed on second row')
|
109
|
+
assert_equal({
|
110
|
+
:browser_version_major=>"4",
|
111
|
+
:browser_version_minor=>nil,
|
112
|
+
:port=>nil,
|
113
|
+
:request=>"GET /apache_pb.gif HTTP/1.1",
|
114
|
+
:ostype=>"Windows",
|
115
|
+
:os_version=>nil,
|
116
|
+
:response_code=>"200",
|
117
|
+
:host=>nil,
|
118
|
+
:scheme=>nil,
|
119
|
+
:bytes=>"2326",
|
120
|
+
:ip_address=>"127.0.0.1",
|
121
|
+
:browser=>"Mozilla",
|
122
|
+
:referrer=>nil,
|
123
|
+
:os=>"Win98",
|
124
|
+
:user=>"bob",
|
125
|
+
:user_agent=>"Mozilla/4.08 [en] (Win98; I ;Nav)",
|
126
|
+
:identd=>nil,
|
127
|
+
:uri_path=>nil,
|
128
|
+
:timestamp=>Time.mktime(2000, 10, 11, 5, 52, 31)}, rows[2], 'Failed on third row')
|
129
|
+
end
|
130
|
+
|
131
|
+
# Test the user agent parser
|
132
|
+
def test_user_agent_parser
|
133
|
+
agents = <<-AGENTS
|
134
|
+
Mozilla/4.7 [en] (WinNT; U)
|
135
|
+
Mozilla/4.0 (compatible; MSIE 5.01; Windows NT)
|
136
|
+
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; T312461; .NET CLR 1.1.4322)
|
137
|
+
Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 4.0) Opera 5.11 [en]
|
138
|
+
Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02
|
139
|
+
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040612 Firefox/0.8
|
140
|
+
Mozilla/5.0 (compatible; Konqueror/3.2; Linux) (KHTML, like Gecko)
|
141
|
+
Lynx/2.8.4rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.6h
|
142
|
+
AGENTS
|
143
|
+
agents = agents.split("\n").collect { |s| s.strip }
|
144
|
+
|
145
|
+
control = ETL::Control::Control.resolve(File.dirname(__FILE__) + '/apache_combined_log.ctl')
|
146
|
+
parser = ETL::Parser::ApacheCombinedLogParser.new(control.sources.first)
|
147
|
+
rows = parser.collect { |row| row }
|
148
|
+
|
149
|
+
assert_equal({:browser_version_major=>"4",
|
150
|
+
:browser_version_minor=>nil,
|
151
|
+
:ostype=>"Windows",
|
152
|
+
:os=>"WinNT",
|
153
|
+
:os_version=>nil,
|
154
|
+
:browser=>"Mozilla"}, parser.parse_user_agent(agents[0]), 'Agent 0 invalid'
|
155
|
+
)
|
156
|
+
assert_equal({:browser_version_major=>"5",
|
157
|
+
:browser_version_minor=>"01",
|
158
|
+
:ostype=>"Windows",
|
159
|
+
:os=>"Windows NT",
|
160
|
+
:os_version=>nil,
|
161
|
+
:browser=>"MSIE"}, parser.parse_user_agent(agents[1]), 'Agent 1 invalid'
|
162
|
+
)
|
163
|
+
assert_equal({:browser_version_major=>"6",
|
164
|
+
:browser_version_minor=>"0",
|
165
|
+
:ostype=>"Windows",
|
166
|
+
:os=>"Windows NT 5.0",
|
167
|
+
:os_version=>"5.0",
|
168
|
+
:browser=>"MSIE"}, parser.parse_user_agent(agents[2]), 'Agent 2 invalid'
|
169
|
+
)
|
170
|
+
assert_equal({:browser_version_major=>"5",
|
171
|
+
:browser_version_minor=>"0",
|
172
|
+
:ostype=>"Windows",
|
173
|
+
:os=>"Windows NT 4.0",
|
174
|
+
:os_version=>"4.0",
|
175
|
+
:browser=>"MSIE"}, parser.parse_user_agent(agents[3]), 'Agent 3 invalid'
|
176
|
+
)
|
177
|
+
assert_equal({:browser_version_major=>"7",
|
178
|
+
:browser_version_minor=>nil,
|
179
|
+
:ostype=>"Windows",
|
180
|
+
:os=>"Windows NT 5.0",
|
181
|
+
:os_version=>"5.0",
|
182
|
+
:browser=>"Netscape"}, parser.parse_user_agent(agents[4]), 'Agent 4 invalid'
|
183
|
+
)
|
184
|
+
assert_equal({:browser_version_major=>"0.8",
|
185
|
+
:browser_version_minor=>nil,
|
186
|
+
:ostype=>"Linux",
|
187
|
+
:os=>"Linux i686",
|
188
|
+
:os_version=>nil,
|
189
|
+
:browser=>"Firefox"}, parser.parse_user_agent(agents[5]), 'Agent 5 invalid'
|
190
|
+
)
|
191
|
+
# test fails here
|
192
|
+
# assert_equal({:browser_version_major=>"6",
|
193
|
+
# :browser_version_minor=>nil,
|
194
|
+
# :ostype=>"Linux",
|
195
|
+
# :os=>"Linux",
|
196
|
+
# :os_version=>nil,
|
197
|
+
# :browser=>"Konquerer"}, parser.parse_user_agent(agents[6]), 'Agent 6 invalid'
|
198
|
+
# )
|
199
|
+
end
|
200
|
+
end
|