test/embulk/input/test_mixpanel.rb in embulk-input-mixpanel-0.3.4 vs test/embulk/input/test_mixpanel.rb in embulk-input-mixpanel-0.4.0
- old
+ new
@@ -22,10 +22,11 @@
]
def setup
setup_client
setup_logger
+ stub(Embulk::Input::MixpanelApi::Client).mixpanel_available? { true }
end
def setup_client
params = {
api_key: API_KEY,
@@ -51,10 +52,11 @@
class GuessTest < self
def setup
# Do nothing from parent
mute_warn
+ stub(Embulk::Input::MixpanelApi::Client).mixpanel_available? { true }
end
def test_from_date_old_date
config = {
type: "mixpanel",
@@ -110,15 +112,37 @@
mock(Embulk.logger).info(/Guessing.*#{Regexp.escape Mixpanel.default_guess_start_date.to_s}/)
Mixpanel.guess(embulk_config(config))
end
+ def test_json_type
+ sample_records = records.map do |r|
+ r.merge("properties" => {"array" => [1,2], "hash" => {foo: "FOO"}})
+ end
+ actual = Mixpanel.guess_from_records(sample_records)
+ assert actual.include?(name: "array", type: :json)
+ assert actual.include?(name: "hash", type: :json)
+ end
+
+ def test_mixpanel_is_down
+ stub(Embulk::Input::MixpanelApi::Client).mixpanel_available? { false }
+ config = {
+ type: "mixpanel",
+ api_key: API_KEY,
+ api_secret: API_SECRET,
+ }
+
+ assert_raise(Embulk::DataError) do
+ Mixpanel.guess(embulk_config(config))
+ end
+ end
+
private
def stub_export_all
any_instance_of(MixpanelApi::Client) do |klass|
- stub(klass).export(anything) { records }
+ stub(klass).export_for_small_dataset(anything) { records }
end
end
def mute_warn
stub(Embulk.logger).warn(anything) {}
@@ -129,13 +153,13 @@
end
def expected
{
"columns" => [
+ {name: "time", type: :long},
{name: "event", type: :string},
{name: "foo", type: :string},
- {name: "time", type: :long},
{name: "int", type: :long},
]
}
end
end
@@ -362,10 +386,91 @@
actual = Mixpanel.export_params(config)
assert_equal(expected, actual)
end
+ sub_test_case "retry" do
+ def setup
+ @page_builder = Object.new
+ @plugin = Mixpanel.new(task, nil, nil, @page_builder)
+ @plugin.init
+ @httpclient = HTTPClient.new
+ stub(HTTPClient).new { @httpclient }
+ stub(@page_builder).add {}
+ stub(@page_builder).finish {}
+ stub(Embulk.logger).warn {}
+ stub(Embulk.logger).info {}
+ stub(Embulk::Input::MixpanelApi::Client).mixpanel_available? { true }
+ end
+
+ test "200" do
+ stub_response(200)
+ mock(Embulk.logger).warn(/Retrying/).never
+ mock(@page_builder).finish
+ @plugin.run
+ end
+
+ test "400" do
+ stub_response(400)
+ mock(Embulk.logger).warn(/Retrying/).never
+ assert_raise(Embulk::ConfigError) do
+ @plugin.run
+ end
+ end
+
+ test "401" do
+ stub_response(401)
+ mock(Embulk.logger).warn(/Retrying/).never
+ assert_raise(Embulk::ConfigError) do
+ @plugin.run
+ end
+ end
+
+ test "500" do
+ stub_response(500)
+ mock(Embulk.logger).warn(/Retrying/).times(task[:retry_limit])
+ assert_raise(PerfectRetry::TooManyRetry) do
+ @plugin.run
+ end
+ end
+
+ test "timeout" do
+ stub(@httpclient).get { raise HTTPClient::TimeoutError, "timeout" }
+ mock(Embulk.logger).warn(/Retrying/).times(task[:retry_limit])
+
+ assert_raise(PerfectRetry::TooManyRetry) do
+ @plugin.run
+ end
+ end
+
+ test "Mixpanel is down" do
+ stub(Embulk::Input::MixpanelApi::Client).mixpanel_available? { false }
+
+ assert_raise(Embulk::DataError) do
+ @plugin.run
+ end
+ end
+
+ def stub_response(code)
+ stub(@httpclient.test_loopback_http_response).shift { "HTTP/1.1 #{code}\r\n\r\n" }
+ end
+
+ def task
+ {
+ api_key: API_KEY,
+ api_secret: API_SECRET,
+ timezone: TIMEZONE,
+ schema: schema,
+ dates: DATES.to_a.map(&:to_s),
+ params: Mixpanel.export_params(embulk_config),
+ fetch_unknown_columns: false,
+ retry_initial_wait_sec: 0,
+ retry_limit: 3,
+ }
+ end
+ end
+
class RunTest < self
def setup_client
any_instance_of(MixpanelApi::Client) do |klass|
stub(klass).request { records_raw_response }
@@ -375,17 +480,9 @@
def setup
super
@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)