test/embulk/input/test_mixpanel.rb in embulk-input-mixpanel-0.1.0 vs test/embulk/input/test_mixpanel.rb in embulk-input-mixpanel-0.2.0

- old
+ new

@@ -7,10 +7,13 @@ class MixpanelTest < Test::Unit::TestCase API_KEY = "api_key".freeze API_SECRET = "api_secret".freeze FROM_DATE = "2015-02-22".freeze TO_DATE = "2015-03-02".freeze + DAYS = 8 + DATES = Date.parse(FROM_DATE)..(Date.parse(FROM_DATE) + DAYS) + TIMEZONE = "Asia/Tokyo".freeze DURATIONS = [ {from_date: FROM_DATE, to_date: "2015-02-28"}, # It has 7 days between 2015-02-22 and 2015-02-28 {from_date: "2015-03-01", to_date: TO_DATE}, ] @@ -54,10 +57,148 @@ actual = Mixpanel.guess(embulk_config) assert_equal(expected, actual) end + class GenerateDatesTest < self + def test_valid_from_date + from_date = "2015-08-14" + parsed_date = Date.parse(from_date) + + expected = (parsed_date..(parsed_date + DAYS)) + actual = Mixpanel.generate_dates(transaction_config(from_date)) + assert_equal(expected, actual) + end + + def test_invalid_from_date + from_date = "2015-08-41" + + assert_raise(Embulk::ConfigError) do + Mixpanel.generate_dates(transaction_config(from_date)) + end + end + + private + + def transaction_config(from_date) + _config = config.merge( + from_date: from_date, + timezone: TIMEZONE, + columns: schema, + ) + DataSource[*_config.to_a.flatten(1)] + end + end + + class TransactionTest < self + class FromDateTest < self + end + + class TimezoneTest < self + def test_valid_timezone + timezone = TIMEZONE + mock(Mixpanel).resume(transaction_task(timezone), columns, 1, &control) + + Mixpanel.transaction(transaction_config(timezone), &control) + end + + def test_invalid_timezone + timezone = "#{TIMEZONE}ooooo" + + assert_raise(Embulk::ConfigError) do + Mixpanel.transaction(transaction_config(timezone), &control) + end + end + + private + + def transaction_task(timezone) + task.merge( + dates: DATES.map {|date| date.to_s}, + api_key: API_KEY, + api_secret: API_SECRET, + timezone: timezone, + schema: schema + ) + end + + def transaction_config(timezone) + _config = config.merge( + timezone: timezone, + columns: schema, + ) + DataSource[*_config.to_a.flatten(1)] + end + end + + class DaysTest < self + def test_valid_days + days = 5 + + mock(Mixpanel).resume(transaction_task(days), columns, 1, &control) + Mixpanel.transaction(transaction_config(days), &control) + end + + def test_invalid_days + days = 0 + + assert_raise(Embulk::ConfigError) do + Mixpanel.transaction(transaction_config(days), &control) + end + end + + private + + def transaction_task(days) + from_date = Date.parse(FROM_DATE) + task.merge( + dates: (from_date..(from_date + days)).map {|date| date.to_s}, + api_key: API_KEY, + api_secret: API_SECRET, + timezone: TIMEZONE, + schema: schema + ) + end + + def transaction_config(days) + _config = config.merge( + days: days, + columns: schema, + timezone: TIMEZONE, + ) + DataSource[*_config.to_a.flatten(1)] + end + end + + def test_resume + today = Date.today + control = proc { [{to_date: today.to_s}] } + actual = Mixpanel.resume(transaction_task, columns, 1, &control) + assert_equal({from_date: today.next.to_s}, actual) + end + + def control + proc {} # dummy + end + + def transaction_task + task.merge( + dates: DATES.map {|date| date.to_s}, + api_key: API_KEY, + api_secret: API_SECRET, + timezone: TIMEZONE, + schema: schema + ) + end + + def columns + schema.map do |col| + Column.new(nil, col["name"], col["type"].to_sym) + end + end + end + def test_export_params config_params = [ :type, "mixpanel", :api_key, API_KEY, :api_secret, API_SECRET, @@ -87,10 +228,18 @@ @page_builder = Object.new @plugin = Mixpanel.new(task, nil, nil, @page_builder) end + def test_preview_check + mock(@plugin).preview? { true } + stub(@page_builder).add(anything) + stub(@page_builder).finish + + @plugin.run + end + def test_preview stub(@plugin).preview? { true } mock(@page_builder).add(anything).times(records.length) mock(@page_builder).finish @@ -112,39 +261,38 @@ mock(@page_builder).finish @plugin.run end - def test_invalid_timezone - assert_raise(TZInfo::InvalidTimezoneIdentifier) do - Mixpanel.new(task.merge(timezone: "Asia/Tokyooooooooo"), nil, nil, @page_builder).run - end - end - private - def task - { - api_key: API_KEY, - api_secret: API_SECRET, - timezone: "Asia/Tokyo", - schema: [ - {"name" => "foo", "type" => "long"}, - {"name" => "time", "type" => "long"}, - ], - dates: (Date.parse(FROM_DATE)..Date.parse(TO_DATE)).to_a, - params: Mixpanel.export_params(embulk_config), - } - end - def timezone_offset_seconds 60 * 60 * 9 # Asia/Tokyo end end private + def schema + [ + {"name" => "foo", "type" => "long"}, + {"name" => "time", "type" => "long"}, + {"name" => "event", "type" => "string"}, + ] + end + + def task + { + api_key: API_KEY, + api_secret: API_SECRET, + timezone: TIMEZONE, + schema: schema, + dates: DATES.to_a, + params: Mixpanel.export_params(embulk_config), + } + end + def records [ { "event" => "event", "properties" => { @@ -158,17 +306,20 @@ def record_epoch 1234567890 end - def embulk_config - config = { + def config + { type: "mixpanel", api_key: API_KEY, api_secret: API_SECRET, from_date: FROM_DATE, - to_date: TO_DATE, + days: DAYS, } + end + + def embulk_config DataSource[*config.to_a.flatten(1)] end end end end