etl 0.9.5.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +12 -0
- data/.yardopts +5 -0
- data/0.9-UPGRADE +6 -0
- data/CHANGELOG +236 -0
- data/Gemfile +4 -0
- data/HOW_TO_RELEASE +13 -0
- data/LICENSE +7 -0
- data/README.textile +111 -0
- data/Rakefile +105 -0
- data/TODO +28 -0
- data/activewarehouse-etl.gemspec +38 -0
- data/bin/etl +28 -0
- data/bin/etl.cmd +8 -0
- data/examples/database.example.yml +16 -0
- data/lib/etl.rb +97 -0
- data/lib/etl/batch.rb +2 -0
- data/lib/etl/batch/batch.rb +111 -0
- data/lib/etl/batch/directives.rb +65 -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 +438 -0
- data/lib/etl/control/destination/csv_destination.rb +113 -0
- data/lib/etl/control/destination/database_destination.rb +97 -0
- data/lib/etl/control/destination/excel_destination.rb +91 -0
- data/lib/etl/control/destination/file_destination.rb +126 -0
- data/lib/etl/control/destination/insert_update_database_destination.rb +136 -0
- data/lib/etl/control/destination/update_database_destination.rb +109 -0
- data/lib/etl/control/destination/yaml_destination.rb +74 -0
- data/lib/etl/control/source.rb +132 -0
- data/lib/etl/control/source/database_source.rb +224 -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 +582 -0
- data/lib/etl/execution.rb +19 -0
- data/lib/etl/execution/base.rb +8 -0
- data/lib/etl/execution/batch.rb +10 -0
- data/lib/etl/execution/job.rb +8 -0
- data/lib/etl/execution/migration.rb +90 -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/csv_parser.rb +93 -0
- data/lib/etl/parser/excel_parser.rb +112 -0
- data/lib/etl/parser/fixed_width_parser.rb +65 -0
- data/lib/etl/parser/nokogiri_xml_parser.rb +83 -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 +94 -0
- data/lib/etl/processor/check_exist_processor.rb +80 -0
- data/lib/etl/processor/check_unique_processor.rb +39 -0
- data/lib/etl/processor/copy_field_processor.rb +26 -0
- data/lib/etl/processor/database_join_processor.rb +82 -0
- data/lib/etl/processor/encode_processor.rb +55 -0
- data/lib/etl/processor/ensure_fields_presence_processor.rb +24 -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/hierarchy_exploder_processor.rb +55 -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/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 +27 -0
- data/lib/etl/processor/sequence_processor.rb +23 -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/surrogate_key_processor.rb +53 -0
- data/lib/etl/processor/truncate_processor.rb +40 -0
- data/lib/etl/processor/zip_file_processor.rb +27 -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/calculation_transform.rb +71 -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 +211 -0
- data/lib/etl/transform/hierarchy_lookup_transform.rb +49 -0
- data/lib/etl/transform/md5_transform.rb +13 -0
- data/lib/etl/transform/ordinalize_transform.rb +14 -0
- data/lib/etl/transform/sha1_transform.rb +13 -0
- data/lib/etl/transform/split_fields_transform.rb +27 -0
- data/lib/etl/transform/string_to_date_time_transform.rb +14 -0
- data/lib/etl/transform/string_to_date_transform.rb +16 -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 +3 -0
- data/spec/fixtures/all.ebf +6 -0
- data/spec/fixtures/apache_combined_log.ctl +11 -0
- data/spec/fixtures/batch_with_error.ebf +6 -0
- data/spec/fixtures/batched1.ctl +0 -0
- data/spec/fixtures/batched2.ctl +0 -0
- data/spec/fixtures/block_processor.ctl +6 -0
- data/spec/fixtures/block_processor_error.ctl +1 -0
- data/spec/fixtures/block_processor_pre_post_process.ctl +4 -0
- data/spec/fixtures/block_processor_remove_rows.ctl +5 -0
- data/spec/fixtures/data/apache_combined_log.txt +3 -0
- data/spec/fixtures/data/bulk_import.txt +3 -0
- data/spec/fixtures/data/bulk_import_with_empties.txt +3 -0
- data/spec/fixtures/data/decode.txt +3 -0
- data/spec/fixtures/data/delimited.txt +3 -0
- data/spec/fixtures/data/encode_source_latin1.txt +2 -0
- data/spec/fixtures/data/excel.xls +0 -0
- data/spec/fixtures/data/excel2.xls +0 -0
- data/spec/fixtures/data/fixed_width.txt +3 -0
- data/spec/fixtures/data/multiple_delimited_1.txt +3 -0
- data/spec/fixtures/data/multiple_delimited_2.txt +3 -0
- data/spec/fixtures/data/nokogiri.xml +38 -0
- data/spec/fixtures/data/people.txt +3 -0
- data/spec/fixtures/data/sax.xml +14 -0
- data/spec/fixtures/data/xml.xml +16 -0
- data/spec/fixtures/delimited.ctl +30 -0
- data/spec/fixtures/delimited_absolute.ctl +31 -0
- data/spec/fixtures/delimited_destination_db.ctl +23 -0
- data/spec/fixtures/delimited_excel.ctl +31 -0
- data/spec/fixtures/delimited_insert_update.ctl +34 -0
- data/spec/fixtures/delimited_update.ctl +34 -0
- data/spec/fixtures/delimited_with_bulk_load.ctl +34 -0
- data/spec/fixtures/errors.ctl +24 -0
- data/spec/fixtures/excel.ctl +24 -0
- data/spec/fixtures/excel2.ctl +25 -0
- data/spec/fixtures/fixed_width.ctl +35 -0
- data/spec/fixtures/inline_parser.ctl +17 -0
- data/spec/fixtures/model_source.ctl +14 -0
- data/spec/fixtures/multiple_delimited.ctl +22 -0
- data/spec/fixtures/multiple_source_delimited.ctl +39 -0
- data/spec/fixtures/nokogiri_all.ctl +35 -0
- data/spec/fixtures/nokogiri_select.ctl +35 -0
- data/spec/fixtures/output/.ignore +1 -0
- data/spec/fixtures/output/delimited.txt +3 -0
- data/spec/fixtures/output/encode_destination_utf-8.txt +2 -0
- data/spec/fixtures/output/fixed_width.txt +3 -0
- data/spec/fixtures/output/inline_parser.txt +3 -0
- data/spec/fixtures/output/multiple_source_delimited.txt +6 -0
- data/spec/fixtures/output/test_excel_destination.xls +0 -0
- data/spec/fixtures/output/test_file_destination.2.txt +2 -0
- data/spec/fixtures/output/test_file_destination.txt +2 -0
- data/spec/fixtures/output/test_multiple_unique.txt +1 -0
- data/spec/fixtures/output/test_unique.txt +2 -0
- data/spec/fixtures/sax.ctl +26 -0
- data/spec/fixtures/scd/1.txt +1 -0
- data/spec/fixtures/scd/2.txt +1 -0
- data/spec/fixtures/scd/3.txt +1 -0
- data/spec/fixtures/scd_test_type_1.ctl +43 -0
- data/spec/fixtures/scd_test_type_2.ctl +34 -0
- data/spec/fixtures/screen_test_error.ctl +3 -0
- data/spec/fixtures/screen_test_fatal.ctl +3 -0
- data/spec/fixtures/xml.ctl +31 -0
- data/spec/quality_spec.rb +11 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/support/custom_fixtures.rb +54 -0
- data/spec/support/custom_matchers.rb +54 -0
- data/test-matrix.yml +10 -0
- data/test/.gitignore +1 -0
- data/test/.ignore +2 -0
- data/test/batch_test.rb +41 -0
- data/test/block_processor_test.rb +38 -0
- data/test/check_exist_processor_test.rb +92 -0
- data/test/check_unique_processor_test.rb +40 -0
- data/test/config/Gemfile.rails-2.3.x +3 -0
- data/test/config/Gemfile.rails-2.3.x.lock +53 -0
- data/test/config/Gemfile.rails-3.0.x +3 -0
- data/test/config/Gemfile.rails-3.0.x.lock +61 -0
- data/test/config/common.rb +29 -0
- data/test/connection/mysql/connection.rb +9 -0
- data/test/connection/mysql/schema.sql +37 -0
- data/test/connection/postgresql/connection.rb +13 -0
- data/test/connection/postgresql/schema.sql +40 -0
- data/test/control_test.rb +43 -0
- data/test/database_join_processor_test.rb +43 -0
- data/test/date_dimension_builder_test.rb +96 -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 +78 -0
- data/test/ensure_fields_presence_processor_test.rb +28 -0
- data/test/etl_test.rb +42 -0
- data/test/foreign_key_lookup_transform_test.rb +50 -0
- data/test/generator_test.rb +14 -0
- data/test/mocks/mock_destination.rb +26 -0
- data/test/mocks/mock_source.rb +25 -0
- data/test/nokogiri_test.rb +35 -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/scd_test.rb +257 -0
- data/test/screen_test.rb +9 -0
- data/test/source_test.rb +154 -0
- data/test/test_helper.rb +37 -0
- data/test/transform_test.rb +101 -0
- data/test/truncate_processor_test.rb +37 -0
- metadata +510 -0
data/test/engine_test.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class EngineTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context 'process' do
|
6
|
+
|
7
|
+
should 'raise an error when a file which does not exist is given' do
|
8
|
+
error = assert_raise(Errno::ENOENT) do
|
9
|
+
ETL::Engine.process('foo-bar.ctl')
|
10
|
+
end
|
11
|
+
|
12
|
+
assert_equal "No such file or directory - foo-bar.ctl", error.message
|
13
|
+
end
|
14
|
+
|
15
|
+
should 'raise an error when an unknown file type is given' do
|
16
|
+
error = assert_raise(RuntimeError) do
|
17
|
+
ETL::Engine.process(__FILE__)
|
18
|
+
end
|
19
|
+
|
20
|
+
assert_match /Unsupported file type/, error.message
|
21
|
+
end
|
22
|
+
|
23
|
+
should_eventually 'stop as soon as the error threshold is reached' do
|
24
|
+
engine = ETL::Engine.new
|
25
|
+
|
26
|
+
assert_equal 0, engine.errors.size
|
27
|
+
|
28
|
+
engine.process ETL::Control::Control.parse_text <<CTL
|
29
|
+
set_error_threshold 1
|
30
|
+
source :in, { :type => :enumerable, :enumerable => (1..100) }
|
31
|
+
after_read { |row| raise "Failure" }
|
32
|
+
CTL
|
33
|
+
|
34
|
+
assert_equal 1, engine.errors.size
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'connection' do
|
40
|
+
|
41
|
+
should 'return an ActiveRecord configuration by name' do
|
42
|
+
assert_not_nil ETL::Engine.connection(:data_warehouse)
|
43
|
+
end
|
44
|
+
|
45
|
+
should 'raise an error on non existent connection' do
|
46
|
+
error = assert_raise(ETL::ETLError) do
|
47
|
+
ETL::Engine.connection(:does_not_exist)
|
48
|
+
end
|
49
|
+
assert_equal "Cannot find connection named :does_not_exist", error.message
|
50
|
+
end
|
51
|
+
|
52
|
+
should 'raise an error when requesting a connection with no name' do
|
53
|
+
error = assert_raise(ETL::ETLError) do
|
54
|
+
ETL::Engine.connection(" ")
|
55
|
+
end
|
56
|
+
assert_equal "Connection with no name requested. Is there a missing :target parameter somewhere?", error.message
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'temp tables' do
|
61
|
+
attr_reader :connection
|
62
|
+
|
63
|
+
setup do
|
64
|
+
@connection = ETL::Engine.connection(:data_warehouse)
|
65
|
+
end
|
66
|
+
|
67
|
+
should 'return unmodified table name when temp tables are disabled' do
|
68
|
+
assert_equal 'foo', ETL::Engine.table('foo', ETL::Engine.connection(:data_warehouse))
|
69
|
+
end
|
70
|
+
|
71
|
+
should 'return temp table name instead of table name when temp tables are enabled' do
|
72
|
+
ETL::Engine.use_temp_tables = true
|
73
|
+
assert_equal 'tmp_people', ETL::Engine.table('people', connection)
|
74
|
+
ETL::Engine.use_temp_tables = false
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class EnsureFieldsPresenceProcessorTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def new_processor(options)
|
6
|
+
ETL::Processor::EnsureFieldsPresenceProcessor.new(nil, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
should 'raise an error unless :fields is specified' do
|
10
|
+
error = assert_raises(ETL::ControlError) { new_processor({}) }
|
11
|
+
assert_equal ":fields must be specified", error.message
|
12
|
+
end
|
13
|
+
|
14
|
+
should 'raise an error if a field is missing in the row' do
|
15
|
+
error = assert_raise(ETL::ControlError) do
|
16
|
+
processor = new_processor(:fields => [:key])
|
17
|
+
processor.process(ETL::Row[])
|
18
|
+
end
|
19
|
+
|
20
|
+
assert_match /missing required field\(s\)/, error.message
|
21
|
+
end
|
22
|
+
|
23
|
+
should 'return the row if the required fields are in the row' do
|
24
|
+
row = ETL::Row[:first => nil, :second => "Barry"]
|
25
|
+
assert_equal row, new_processor(:fields => [:first, :second]).process(row)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
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,50 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
# TODO - use flexmock instead, but I'm not sure how to handle the respond_to part yet
|
4
|
+
class TestResolver
|
5
|
+
attr_accessor :cache_loaded
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@cache_loaded = false
|
9
|
+
end
|
10
|
+
|
11
|
+
def load_cache
|
12
|
+
@cache_loaded = true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class ForeignKeyLookupTransformTest < Test::Unit::TestCase
|
17
|
+
|
18
|
+
context 'configuration' do
|
19
|
+
|
20
|
+
should 'enable cache by default' do
|
21
|
+
resolver = TestResolver.new
|
22
|
+
|
23
|
+
transform = ETL::Transform::ForeignKeyLookupTransform.new(nil, 'name',
|
24
|
+
{:resolver => resolver})
|
25
|
+
|
26
|
+
assert_equal true, resolver.cache_loaded
|
27
|
+
end
|
28
|
+
|
29
|
+
should 'allow to disable cache' do
|
30
|
+
resolver = TestResolver.new
|
31
|
+
|
32
|
+
transform = ETL::Transform::ForeignKeyLookupTransform.new(nil, 'name',
|
33
|
+
{:resolver => resolver, :cache => false})
|
34
|
+
|
35
|
+
assert_equal false, resolver.cache_loaded
|
36
|
+
end
|
37
|
+
|
38
|
+
should 'allow to enable cache' do
|
39
|
+
resolver = TestResolver.new
|
40
|
+
|
41
|
+
transform = ETL::Transform::ForeignKeyLookupTransform.new(nil, 'name',
|
42
|
+
{:resolver => resolver, :cache => true})
|
43
|
+
|
44
|
+
assert_equal true, resolver.cache_loaded
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
end
|
@@ -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,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,35 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
# Test the flat text parsers
|
4
|
+
class ParserTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
# Test the DOM-based Nokogiri XML parser. .
|
7
|
+
def test_nokogiri_xml_parser_for_all_nodes
|
8
|
+
control = ETL::Control::Control.resolve(
|
9
|
+
File.dirname(__FILE__) + '/nokogiri_all.ctl')
|
10
|
+
parser = ETL::Parser::NokogiriXmlParser.new(control.sources.first)
|
11
|
+
rows = parser.collect { |row| row }
|
12
|
+
assert_equal 3, rows.length
|
13
|
+
assert_equal(
|
14
|
+
{ :hair_colour=>"black",
|
15
|
+
:first_name=>"Bob",
|
16
|
+
:last_name=>"Smith",
|
17
|
+
:ssn=>"123456789", :age=>"24"}, rows.first)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Test the DOM-based Nokogiri XML parser. .
|
21
|
+
def test_nokogiri_xml_parser_for_selected_nodes
|
22
|
+
control = ETL::Control::Control.resolve(
|
23
|
+
File.dirname(__FILE__) + '/nokogiri_select.ctl')
|
24
|
+
parser = ETL::Parser::NokogiriXmlParser.new(control.sources.first)
|
25
|
+
rows = parser.collect { |row| row }
|
26
|
+
assert_equal 2, rows.length
|
27
|
+
assert_equal(
|
28
|
+
{ :age=>"37",
|
29
|
+
:hair_colour=>"black",
|
30
|
+
:first_name=>"Jake",
|
31
|
+
:last_name=>"Smithsonian",
|
32
|
+
:ssn=>"133244566"}, rows.last)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
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_csv_parser
|
7
|
+
control = ETL::Control::Control.resolve(File.dirname(__FILE__) + '/delimited.ctl')
|
8
|
+
parser = ETL::Parser::CsvParser.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
|