spec/inputs/jdbc_spec.rb in logstash-input-jdbc-3.1.0 vs spec/inputs/jdbc_spec.rb in logstash-input-jdbc-4.0.0

- old
+ new

@@ -1,6 +1,5 @@ -# encoding: utf-8 require "logstash/devutils/rspec/spec_helper" require "logstash/inputs/jdbc" require "jdbc/derby" require "sequel" require "sequel/adapters/jdbc" @@ -28,11 +27,10 @@ before :each do Jdbc::Derby.load_driver db.create_table :test_table do DateTime :created_at Integer :num - String :string DateTime :custom_time end end after :each do @@ -81,45 +79,10 @@ end let(:config) { mixin_settings.merge(settings) } end end - context "when both jdbc_password and jdbc_password_filepath arguments are passed" do - let(:statement) { "SELECT * from test_table" } - let(:jdbc_password) { "secret" } - let(:jdbc_password_file_path) { Stud::Temporary.pathname } - let(:settings) { { "jdbc_password_filepath" => jdbc_password_file_path, - "jdbc_password" => jdbc_password, - "statement" => statement } } - - it "should fail to register" do - expect{ plugin.register }.to raise_error(LogStash::ConfigurationError) - end - end - - context "when jdbc_password is passed in from a file" do - let(:statement) { "SELECT * from test_table" } - let(:jdbc_password) { "secret" } - let(:jdbc_password_file_path) { Stud::Temporary.pathname } - let(:settings) { { "jdbc_password_filepath" => jdbc_password_file_path, - "statement" => statement } } - - before do - File.write(jdbc_password_file_path, jdbc_password) - plugin.register - end - - after do - plugin.stop - end - - it "should read in jdbc_password from file" do - expect(plugin.jdbc_password).to eq(jdbc_password) - end - end - - context "when neither statement and statement_filepath arguments are passed" do it "should fail to register" do expect{ plugin.register }.to raise_error(LogStash::ConfigurationError) end end @@ -169,11 +132,11 @@ plugin.stop end it "should retrieve params correctly from Event" do plugin.run(queue) - expect(queue.pop['num_param']).to eq(settings['parameters']['num_param']) + expect(queue.pop.get('num_param')).to eq(settings['parameters']['num_param']) end end context "when scheduling" do let(:settings) { {"statement" => "SELECT 1 as num_param FROM SYSIBM.SYSDUMMY1", "schedule" => "* * * * * UTC"} } @@ -287,11 +250,11 @@ end it "should convert it to LogStash::Timestamp " do plugin.run(queue) event = queue.pop - expect(event["custom_time"]).to be_a(LogStash::Timestamp) + expect(event.get("custom_time")).to be_a(LogStash::Timestamp) end end context "when fetching time data with jdbc_default_timezone set" do let(:mixin_settings) do @@ -323,11 +286,11 @@ it "should convert the time to reflect the timezone " do plugin.run(queue) event = queue.pop # This reflects a 6 hour time difference between UTC and America/Chicago - expect(event["custom_time"].time).to eq(Time.iso8601("2015-01-01T18:00:00Z")) + expect(event.get("custom_time").time).to eq(Time.iso8601("2015-01-01T18:00:00Z")) end end context "when fetching time data without jdbc_default_timezone set" do let(:mixin_settings) do @@ -358,11 +321,11 @@ it "should not convert the time to reflect the timezone " do plugin.run(queue) event = queue.pop # With no timezone set, no change should occur - expect(event["custom_time"].time).to eq(Time.iso8601("2015-01-01T12:00:00Z")) + expect(event.get("custom_time").time).to eq(Time.iso8601("2015-01-01T12:00:00Z")) end end context "when iteratively running plugin#run" do let(:settings) do @@ -391,11 +354,11 @@ test_table.insert(:num => nums[4], :created_at => Time.now.utc) plugin.run(queue) actual_sum = 0 until queue.empty? do - actual_sum += queue.pop['num'] + actual_sum += queue.pop.get('num') end expect(actual_sum).to eq(nums.inject{|sum,x| sum + x }) end end @@ -870,113 +833,8 @@ it "should not fail when passed a non-positive value" do mixin_settings['connection_retry_attempts'] = -2 expect { plugin.register }.to_not raise_error plugin.stop - end - end - - context "when encoding of some columns need to be changed" do - - let(:settings) {{ "statement" => "SELECT * from test_table" }} - let(:events) { [] } - let(:row) do - { - "column0" => "foo", - "column1" => "bar".force_encoding(Encoding::ISO_8859_1), - "column2" => 3 - } - end - - before(:each) do - allow_any_instance_of(Sequel::JDBC::Derby::Dataset).to receive(:each).and_yield(row) - plugin.register - end - - after(:each) do - plugin.stop - end - - it "should not convert any column by default" do - encoded_row = { - "column0" => "foo", - "column1" => "bar".force_encoding(Encoding::ISO_8859_1), - "column2" => 3 - } - expect(LogStash::Event).to receive(:new) do |row| - row.each do |k, v| - next unless v.is_a?(String) - expect(row[k].encoding).to eq(encoded_row[k].encoding) - end - end - plugin.run(events) - end - - context "when all string columns should be encoded" do - - let(:settings) do - { - "statement" => "SELECT * from test_table", - "charset" => "ISO-8859-1" - } - end - - let(:row) do - { - "column0" => "foo".force_encoding(Encoding::ISO_8859_1), - "column1" => "bar".force_encoding(Encoding::ISO_8859_1), - "column2" => 3 - } - end - - it "should transform all column string to UTF-8, default encoding" do - encoded_row = { - "column0" => "foo", - "column1" => "bar", - "column2" => 3 - } - expect(LogStash::Event).to receive(:new) do |row| - row.each do |k, v| - next unless v.is_a?(String) - expect(row[k].encoding).to eq(encoded_row[k].encoding) - end - end - plugin.run(events) - end - end - - context "when only an specific column should be converted" do - - let(:settings) do - { - "statement" => "SELECT * from test_table", - "columns_charset" => { "column1" => "ISO-8859-1" } - } - end - - let(:row) do - { - "column0" => "foo", - "column1" => "bar".force_encoding(Encoding::ISO_8859_1), - "column2" => 3, - "column3" => "berlin".force_encoding(Encoding::ASCII_8BIT) - } - end - - it "should only convert the selected column" do - encoded_row = { - "column0" => "foo", - "column1" => "bar", - "column2" => 3, - "column3" => "berlin".force_encoding(Encoding::ASCII_8BIT) - } - expect(LogStash::Event).to receive(:new) do |row| - row.each do |k, v| - next unless v.is_a?(String) - expect(row[k].encoding).to eq(encoded_row[k].encoding) - end - end - plugin.run(events) - end end end end