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

- old
+ new

@@ -34,51 +34,143 @@ any_instance_of(MixpanelApi::Client) do |klass| DURATIONS.each do |duration| from_date = duration[:from_date] to_date = duration[:to_date] + params = params.merge(from_date: from_date, to_date: to_date) stub(klass).export(params) { records } end end end def setup_logger stub(Embulk).logger { ::Logger.new(IO::NULL) } end - def test_guess - expected = { - "columns" => [ - {name: "event", type: :string}, - {name: "foo", type: :string}, - {name: "time", type: :long}, - {name: "int", type: :long}, - ] - } + class GuessTest < self + def test_from_date_old_date + config = { + type: "mixpanel", + api_key: API_KEY, + api_secret: API_SECRET, + from_date: FROM_DATE, + } - actual = Mixpanel.guess(embulk_config) - assert_equal(expected, actual) + from_date = config[:from_date] + to_date = Date.parse(from_date) + Mixpanel::SLICE_DAYS_COUNT + stub_export(from_date, to_date) + + actual = Mixpanel.guess(embulk_config(config)) + assert_equal(expected, actual) + end + + def test_from_date_today + config = { + type: "mixpanel", + api_key: API_KEY, + api_secret: API_SECRET, + from_date: Date.today.to_s, + } + + assert_raise(ConfigError) do + Mixpanel.guess(embulk_config(config)) + end + end + + def test_from_date_yesterday + config = { + type: "mixpanel", + api_key: API_KEY, + api_secret: API_SECRET, + from_date: (Date.today - 1).to_s, + } + + from_date = config[:from_date] + to_date = from_date + stub_export(from_date, to_date) + + actual = Mixpanel.guess(embulk_config(config)) + assert_equal(expected, actual) + end + + def test_no_from_date + config = { + type: "mixpanel", + api_key: API_KEY, + api_secret: API_SECRET, + } + + from_date = Date.today - 1 - Mixpanel::SLICE_DAYS_COUNT + to_date = Date.today - 1 + stub_export(from_date, to_date) + + actual = Mixpanel.guess(embulk_config(config)) + assert_equal(expected, actual) + end + + private + + def stub_export(from_date, to_date) + params = { + api_key: API_KEY, + event: nil, + where: nil, + bucket: nil, + from_date: from_date.to_s, + to_date: to_date.to_s, + } + + any_instance_of(MixpanelApi::Client) do |klass| + stub(klass).export(params) { records } + end + end + + def embulk_config(config) + DataSource[*config.to_a.flatten(1)] + end + + def expected + { + "columns" => [ + {name: "event", type: :string}, + {name: "foo", type: :string}, + {name: "time", type: :long}, + {name: "int", type: :long}, + ] + } + end end - class GenerateDatesTest < self + class TransactionDateTest < self def test_valid_from_date from_date = "2015-08-14" - parsed_date = Date.parse(from_date) + mock(Mixpanel).resume(anything, anything, 1) - expected = (parsed_date..(parsed_date + DAYS)) - actual = Mixpanel.generate_dates(transaction_config(from_date)) - assert_equal(expected, actual) + Mixpanel.transaction(transaction_config(from_date)) end def test_invalid_from_date from_date = "2015-08-41" assert_raise(Embulk::ConfigError) do - Mixpanel.generate_dates(transaction_config(from_date)) + Mixpanel.transaction(transaction_config(from_date)) end end + def test_future + from_date = (Date.today + 10).to_s + mock(Mixpanel).resume(anything, anything, 1) + + Mixpanel.transaction(transaction_config(from_date)) + end + + def test_negative_days + assert_raise(Embulk::ConfigError) do + Mixpanel.transaction(transaction_config((Date.today - 1).to_s).merge(days: -1)) + end + end + private def transaction_config(from_date) _config = config.merge( from_date: from_date, @@ -89,10 +181,46 @@ end end class TransactionTest < self class FromDateTest < self + def setup + end + + def test_ignore_early_days + stub(Embulk).logger { Logger.new(File::NULL) } + + mock(Mixpanel).resume(task.merge(dates: target_dates), columns, 1, &control) + Mixpanel.transaction(transaction_config, &control) + end + + def test_warn + stub(Mixpanel).resume(task.merge(dates: target_dates), columns, 1, &control) + mock(Embulk.logger).warn(anything) + + Mixpanel.transaction(transaction_config, &control) + end + + private + + def dates + (Date.today - 10)..(Date.today + 10) + end + + def target_dates + dates.find_all{|d| d < Date.today}.map {|date| date.to_s} + end + + def transaction_config + _config = config.merge( + from_date: dates.first.to_s, + days: dates.to_a.size, + timezone: TIMEZONE, + columns: schema + ) + DataSource[*_config.to_a.flatten(1)] + end end class TimezoneTest < self def test_valid_timezone timezone = TIMEZONE @@ -221,10 +349,17 @@ assert_equal(expected, actual) end class RunTest < self + def setup_client + + any_instance_of(MixpanelApi::Client) do |klass| + stub(klass).export(anything) { records } + end + end + def setup super @page_builder = Object.new @plugin = Mixpanel.new(task, nil, nil, @page_builder) @@ -284,10 +419,10 @@ { api_key: API_KEY, api_secret: API_SECRET, timezone: TIMEZONE, schema: schema, - dates: DATES.to_a, + dates: DATES.to_a.map(&:to_s), params: Mixpanel.export_params(embulk_config), } end def records