require './test/test_helper' require 'hover/client/static' describe Hover::Client::Static do let(:application) { 'hyperion' } let(:environment) { 'development' } before do @client = Hover::Client::Static.new(application, environment, 'access_id', 'secret_key') end it "inits" do assert @client.is_a?(Hover::Client::Static) end describe "param_name" do it "metric value" do Hover::Client::Static.param_name('metric', 'value').must_equal('metric[value]') end it "metric tags->order->practice" do Hover::Client::Static.param_name('metric', 'tags', 'order', 'practice').must_equal('metric[tags][order][practice]') end it "tags order->practice" do Hover::Client::Static.param_name('tags', 'order', 'practice').must_equal('tags[order][practice]') end end describe "tags_to_parameter" do it "flat hash" do input = { modeler: 'yanis@hover.to', server: 'production', order: 1234 } output = { 'metric[tags][modeler]' => 'yanis@hover.to', 'metric[tags][server]' => 'production', 'metric[tags][order]' => 1234 } Hover::Client::Static.tags_to_params(input).must_equal(output) end it "nested" do input = { a: 1, b: 2, c: { d: 4, e: ['a', 'b', 'c'] }, } output = { 'metric[tags][a]' => 1, 'metric[tags][b]' => 2, 'metric[tags][c][d]' => 4, 'metric[tags][c][e][]' => ['a', 'b', 'c'] } Hover::Client::Static.tags_to_params(input).must_equal(output) end end describe "metrics" do describe "create" do let(:happened_at) { Time.now.utc } it "reports to alooma" do @client.expects(:alooma_url).returns("https://localhost").at_least(1) http = mock Net::HTTP.expects(:new).returns(http) http.expects(:use_ssl=).with(true) response = mock response.expects(:code).returns(200) http.expects(:request).returns(response) @client.create_metric(name: 'foo', value: 1) end it "raises AloomaReportingError if alooma returns an error" do @client.expects(:alooma_url).returns("https://localhost").at_least(1) http = mock Net::HTTP.expects(:new).returns(http) http.expects(:use_ssl=).with(true) response = mock response.expects(:code).returns(503) http.expects(:request).returns(response) err = Proc.new { @client.create_metric(name: 'foo', value: 1) }.must_raise(Hover::Client::AloomaReportingError) err.message.must_match(/Error reporting to alooma/) end it "creates metric" do parameters = { "metric[name]" => 'state_waiting_labeling', 'metric[value]' => 133.2, 'metric[happened_at]' => happened_at.to_s, 'metric[environment]' => environment, 'metric[application]' => application, 'metric[tags][order_id]' => 2 } sorted_array = parameters.to_a.sort_by(&:first) metric_identifier = Digest::SHA256.hexdigest(sorted_array.to_json) parameters['metric[identifier]'] = metric_identifier @client.expects(:alooma_url).returns("https://localhost").at_least(1) @client.expects(:post_to_alooma).with(parameters, 'https://localhost') @client.create_metric(name: 'state_waiting_labeling', value: 133.2, happened_at: happened_at, tags: {order_id: 2}) end it "includes remote_record_id parameter" do parameters = { "metric[name]" => 'name', 'metric[value]' => 1, 'metric[happened_at]' => 'foo', 'metric[environment]' => environment, 'metric[application]' => application, 'metric[remote_record_id]' => 7 } sorted_array = parameters.to_a.sort_by(&:first) metric_identifier = Digest::SHA256.hexdigest(sorted_array.to_json) parameters['metric[identifier]'] = metric_identifier @client.expects(:alooma_url).returns("https://localhost").at_least(1) @client.expects(:post_to_alooma).with(parameters, 'https://localhost') @client.create_metric(name: 'name', value: 1, remote_record_id: 7, happened_at: 'foo') end it "creates metric with nested tags" do parameters = { "metric[name]" => 'state_labeling', 'metric[value]' => 3449, 'metric[happened_at]' => happened_at.to_s, 'metric[environment]' => environment, 'metric[application]' => application, 'metric[tags][order][id]' => 2, 'metric[tags][order][practice]' => false, 'metric[tags][user][email]' => 'modeler@hover.to', 'metric[tags][a][b][c][]' => [11, 27], } sorted_array = parameters.to_a.sort_by(&:first) metric_identifier = Digest::SHA256.hexdigest(sorted_array.to_json) parameters['metric[identifier]'] = metric_identifier @client.expects(:alooma_url).returns("https://localhost").at_least(1) @client.expects(:post_to_alooma).with(parameters, 'https://localhost') @client.create_metric(name: 'state_labeling', value: 3449, happened_at: happened_at, tags: { order: {id: 2, practice: false}, user: {email: 'modeler@hover.to'}, a: {b: {c: [11, 27]}} }) end end end end