test/plugin/test_out_mixpanel.rb in fluent-plugin-mixpanel-0.0.8 vs test/plugin/test_out_mixpanel.rb in fluent-plugin-mixpanel-0.0.9

- old
+ new

@@ -1,7 +1,8 @@ require 'helper' require 'uri' +require 'msgpack' class MixpanelOutputTest < Test::Unit::TestCase def setup Fluent::Test.setup @@ -68,26 +69,26 @@ end def test_write stub_mixpanel d = create_driver(CONFIG + "event_key event") - time = Time.new('2014-01-01T01:23:45+00:00') + time = Time.new('2014-01-01T01:23:45+00:00').to_i d.emit(sample_record, time) d.run assert_equal "test_token", @out[0]['properties']['token'] assert_equal "123", @out[0]['properties']['distinct_id'] assert_equal "event1", @out[0]['event'] - assert_equal time.to_i, @out[0]['properties']['time'] + assert_equal time, @out[0]['properties']['time'] assert_equal "value1", @out[0]['properties']['key1'] assert_equal "value2", @out[0]['properties']['key2'] end def test_write_setting_time_via_export stub_mixpanel_import d = create_driver(CONFIG + "use_import true\nevent_key event") - time = Time.new('2014-01-01T01:23:45+00:00') + time = Time.new('2014-01-01T01:23:45+00:00').to_i d.emit(sample_record.merge!('time' => 1435707767), time) d.run assert_equal "test_token", @out[0]['properties']['token'] assert_equal "123", @out[0]['properties']['distinct_id'] @@ -98,181 +99,181 @@ end def test_write_multi_request stub_mixpanel_import d = create_driver(IMPORT_CONFIG + "event_key event") - time1 = Time.new('2014-01-01T01:23:45+00:00') - time2 = Time.new('2014-01-02T01:23:45+00:00') + time1 = Time.new('2014-01-01T01:23:45+00:00').to_i + time2 = Time.new('2014-01-02T01:23:45+00:00').to_i d.emit(sample_record, time1) d.emit(sample_record.merge(key3: "value3"), time2) d.run assert_equal "123", @out[0]['properties']['distinct_id'] assert_equal "event1", @out[0]['event'] - assert_equal time1.to_i, @out[0]['properties']['time'] + assert_equal time1, @out[0]['properties']['time'] assert_equal "value1", @out[0]['properties']['key1'] assert_equal "value2", @out[0]['properties']['key2'] assert_equal "123", @out[1]['properties']['distinct_id'] assert_equal "event1", @out[1]['event'] - assert_equal time2.to_i, @out[1]['properties']['time'] + assert_equal time2, @out[1]['properties']['time'] assert_equal "value1", @out[1]['properties']['key1'] assert_equal "value2", @out[1]['properties']['key2'] assert_equal "value2", @out[1]['properties']['key2'] end def test_write_with_ip_key stub_mixpanel d = create_driver(CONFIG + "event_key event\n ip_key ip_address") - time = Time.new('2014-01-01T01:23:45+00:00') + time = Time.new('2014-01-01T01:23:45+00:00').to_i d.emit(sample_record.merge('ip_address' => '192.168.0.2'), time) d.run assert_equal "123", @out[0]['properties']['distinct_id'] assert_equal "event1", @out[0]['event'] - assert_equal time.to_i, @out[0]['properties']['time'] + assert_equal time, @out[0]['properties']['time'] assert_equal "192.168.0.2", @out[0]['properties']['ip'] assert_equal "value1", @out[0]['properties']['key1'] assert_equal "value2", @out[0]['properties']['key2'] end def test_write_with_no_tag_manipulation stub_mixpanel d = create_driver(CONFIG + "event_map_tag true") - time = Time.new('2014-01-01T01:23:45+00:00') + time = Time.new('2014-01-01T01:23:45+00:00').to_i d.emit(sample_record, time) d.run assert_equal "123", @out[0]['properties']['distinct_id'] assert_equal "mixpanel.test", @out[0]['event'] - assert_equal time.to_i, @out[0]['properties']['time'] + assert_equal time, @out[0]['properties']['time'] assert_equal "value1", @out[0]['properties']['key1'] assert_equal "value2", @out[0]['properties']['key2'] end def test_write_with_event_map_tag_removing_prefix stub_mixpanel d = create_driver(CONFIG + "remove_tag_prefix mixpanel.\n event_map_tag true") - time = Time.new('2014-01-01T01:23:45+00:00') + time = Time.new('2014-01-01T01:23:45+00:00').to_i d.emit(sample_record, time) d.run assert_equal "123", @out[0]['properties']['distinct_id'] assert_equal "test", @out[0]['event'] - assert_equal time.to_i, @out[0]['properties']['time'] + assert_equal time, @out[0]['properties']['time'] assert_equal "value1", @out[0]['properties']['key1'] assert_equal "value2", @out[0]['properties']['key2'] end def test_write_with_event_map_tag_removing_prefix_LEGACY stub_mixpanel d = create_driver(CONFIG + "remove_tag_prefix mixpanel\n event_map_tag true\n use_legacy_prefix_behavior true") - time = Time.new('2014-01-01T01:23:45+00:00') + time = Time.new('2014-01-01T01:23:45+00:00').to_i d.emit(sample_record, time) d.run assert_equal "123", @out[0]['properties']['distinct_id'] assert_equal "test", @out[0]['event'] - assert_equal time.to_i, @out[0]['properties']['time'] + assert_equal time, @out[0]['properties']['time'] assert_equal "value1", @out[0]['properties']['key1'] assert_equal "value2", @out[0]['properties']['key2'] end def test_write_with_event_map_tag_removing_prefix_LEGACY_with_dot stub_mixpanel d = create_driver(CONFIG + "remove_tag_prefix mixpanel.\n event_map_tag true\n use_legacy_prefix_behavior true") - time = Time.new('2014-01-01T01:23:45+00:00') + time = Time.new('2014-01-01T01:23:45+00:00').to_i d.emit(sample_record, time) d.run assert_equal "123", @out[0]['properties']['distinct_id'] assert_equal "test", @out[0]['event'] - assert_equal time.to_i, @out[0]['properties']['time'] + assert_equal time, @out[0]['properties']['time'] assert_equal "value1", @out[0]['properties']['key1'] assert_equal "value2", @out[0]['properties']['key2'] end def test_write_with_event_map_tag_removing_suffix stub_mixpanel d = create_driver(CONFIG + "remove_tag_suffix .test\n event_map_tag true") - time = Time.new('2014-01-01T01:23:45+00:00') + time = Time.new('2014-01-01T01:23:45+00:00').to_i d.emit(sample_record, time) d.run assert_equal "123", @out[0]['properties']['distinct_id'] assert_equal "mixpanel", @out[0]['event'] - assert_equal time.to_i, @out[0]['properties']['time'] + assert_equal time, @out[0]['properties']['time'] assert_equal "value1", @out[0]['properties']['key1'] assert_equal "value2", @out[0]['properties']['key2'] end def test_write_with_event_map_tag_adding_prefix stub_mixpanel d = create_driver(CONFIG + "add_tag_prefix foo.\n event_map_tag true") - time = Time.new('2014-01-01T01:23:45+00:00') + time = Time.new('2014-01-01T01:23:45+00:00').to_i d.emit(sample_record, time) d.run assert_equal "123", @out[0]['properties']['distinct_id'] assert_equal "foo.mixpanel.test", @out[0]['event'] - assert_equal time.to_i, @out[0]['properties']['time'] + assert_equal time, @out[0]['properties']['time'] assert_equal "value1", @out[0]['properties']['key1'] assert_equal "value2", @out[0]['properties']['key2'] end def test_write_with_event_map_tag_adding_suffix stub_mixpanel d = create_driver(CONFIG + "add_tag_suffix .foo\n event_map_tag true") - time = Time.new('2014-01-01T01:23:45+00:00') + time = Time.new('2014-01-01T01:23:45+00:00').to_i d.emit(sample_record, time) d.run assert_equal "123", @out[0]['properties']['distinct_id'] assert_equal "mixpanel.test.foo", @out[0]['event'] - assert_equal time.to_i, @out[0]['properties']['time'] + assert_equal time, @out[0]['properties']['time'] assert_equal "value1", @out[0]['properties']['key1'] assert_equal "value2", @out[0]['properties']['key2'] end def test_write_ignore_special_event stub_mixpanel d = create_driver(CONFIG + "event_key event") - time = Time.new('2014-01-01T01:23:45+00:00') + time = Time.new('2014-01-01T01:23:45+00:00').to_i d.emit({ user_id: '123', event: 'mp_page_view' }, time) d.run assert_equal 0, @out.length end def test_write_ignore_special_property stub_mixpanel d = create_driver(CONFIG + "event_key event") - time = Time.new('2014-01-01T01:23:45+00:00') + time = Time.new('2014-01-01T01:23:45+00:00').to_i d.emit(sample_record.merge('mp_event' => '3'), time) d.run assert_equal "test_token", @out[0]['properties']['token'] assert_equal "123", @out[0]['properties']['distinct_id'] assert_equal "event1", @out[0]['event'] - assert_equal time.to_i, @out[0]['properties']['time'] + assert_equal time, @out[0]['properties']['time'] assert_equal "value1", @out[0]['properties']['key1'] assert_equal "value2", @out[0]['properties']['key2'] assert_equal false, @out[0]['properties'].key?('mp_event') end def test_write_delete_supried_token stub_mixpanel d = create_driver(CONFIG + "event_key event") - time = Time.new('2014-01-01T01:23:45+00:00') + time = Time.new('2014-01-01T01:23:45+00:00').to_i d.emit(sample_record.merge('token' => '123'), time) d.run assert_equal "test_token", @out[0]['properties']['token'] assert_equal "123", @out[0]['properties']['distinct_id'] assert_equal "event1", @out[0]['event'] - assert_equal time.to_i, @out[0]['properties']['time'] + assert_equal time, @out[0]['properties']['time'] assert_equal "value1", @out[0]['properties']['key1'] assert_equal "value2", @out[0]['properties']['key2'] assert_equal false, @out[0]['properties'].key?('mp_event') end @@ -281,7 +282,90 @@ d = create_driver(CONFIG + "event_key event") d.emit(sample_record) assert_raise(Fluent::MixpanelOutput::MixpanelError) { d.run } + end + + def test_multiple_records_1_missing_event + stub_mixpanel + d = create_driver(CONFIG + "event_key event") + time = Time.new('2014-01-01T01:23:45+00:00').to_i + d.emit(sample_record, time) + + broken_record = sample_record.dup.delete(:event) + d.emit(broken_record, time) + + d.run + + assert_equal 1, @out.length + + assert_equal "test_token", @out[0]['properties']['token'] + assert_equal "123", @out[0]['properties']['distinct_id'] + assert_equal "event1", @out[0]['event'] + assert_equal time, @out[0]['properties']['time'] + assert_equal "value1", @out[0]['properties']['key1'] + assert_equal "value2", @out[0]['properties']['key2'] + + end + + def test_multiple_records_1_missing_distinct_id + stub_mixpanel + d = create_driver(CONFIG + "event_key event") + time = Time.new('2014-01-01T01:23:45+00:00').to_i + d.emit(sample_record, time) + + broken_record = sample_record.dup.delete(:user_id) + d.emit(broken_record, time) + + d.run + + assert_equal 1, @out.length + + assert_equal "test_token", @out[0]['properties']['token'] + assert_equal "123", @out[0]['properties']['distinct_id'] + assert_equal "event1", @out[0]['event'] + assert_equal time, @out[0]['properties']['time'] + assert_equal "value1", @out[0]['properties']['key1'] + assert_equal "value2", @out[0]['properties']['key2'] + + end + + def test_multiple_records_1_having_mp + stub_mixpanel + d = create_driver(CONFIG + "event_key event") + time = Time.new('2014-01-01T01:23:45+00:00').to_i + d.emit(sample_record, time) + + broken_record = sample_record.dup.merge({ event: 'mp_foo'}) + d.emit(broken_record, time) + + d.run + + assert_equal 1, @out.length + + assert_equal "test_token", @out[0]['properties']['token'] + assert_equal "123", @out[0]['properties']['distinct_id'] + assert_equal "event1", @out[0]['event'] + assert_equal time, @out[0]['properties']['time'] + assert_equal "value1", @out[0]['properties']['key1'] + assert_equal "value2", @out[0]['properties']['key2'] + + end + + def test_request_error_discard + stub_mixpanel_unavailable + d = create_driver(CONFIG + "event_key event\ndiscard_event_on_send_error true") + time = Time.new('2014-01-01T01:23:45+00:00').to_i + d.emit(sample_record, time) + d.run + + logs = d.instance.log.logs + + assert_match "MixpanelOutputErrorHandler:", logs[0] + assert_match "Class: Mixpanel::ServerError", logs[0] + assert_match "Message: Could not write to Mixpanel, server responded with 503 returning: 'Service Unavailable", logs[0] + assert_match "Backtrace", logs[0] + assert_match "Failed to track event to mixpanel", logs[1] + assert_match 'Record: {"properties":{"key1":"value1","key2":"value2","time":' + time.to_s + '},"event":"event1","distinct_id":"123"}', logs[1] end end