Commit 61e5fa30 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

removed _spec

parent 0f313a17
Loading
Loading
Loading
Loading

_spec/app_spec.rb

deleted100644 → 0
+0 −178
Original line number Diff line number Diff line
require ::File.expand_path('../spec_helper.rb', __FILE__)

include Rack::Test::Methods

describe "app" do

  before(:each) do
    FnordMetric::Event.destroy_all
  end

  def app
    @app ||= FnordMetric::App
  end

  it "should redirect to the default dashboard" do
  	get "/"
  	last_response.status.should == 302
  	last_response.location.should == "http://example.org/dashboard/default"
  end

  it "should render the dashboard" do
    FnordMetric.dashboard("Deine Mama"){|dash|}
  	get "/dashboard/default"
  	last_response.status.should == 200
    last_response.body.should include("Deine Mama")
  end

  it "should render the right dashboard" do
    FnordMetric.dashboard("Deine Mama"){|dash|}
    FnordMetric.dashboard("My Dashboard"){|dash|}
    get "/dashboard/DeineMama"
    last_response.status.should == 200
    last_response.body.should include("Deine Mama")
    get "/dashboard/MyDashboard"
    last_response.status.should == 200
    last_response.body.should include("My Dashboard")
  end

  it "should track an event without auth" do
  	post "/events", :type => "myevent", :fnord => "foobar"
  	last_response.status.should == 200
  	FnordMetric::Event.last.type.should == "myevent"
  	FnordMetric::Event.last.fnord.should == "foobar"
  end

  it "should return 400 if no type is provided" do
  	post "/events", :fnord => "foobar"
  	last_response.status.should == 400  	
  	last_response.body.should == "please specify the event_type"
  end

  it "should track an event in the past" do
  	my_time = (Time.now-3.years).to_i
  	post "/events", :type => "myevent", :time => my_time
  	last_response.status.should == 200
  	FnordMetric::Event.last.type.should == "myevent"
  	FnordMetric::Event.last.time.should == my_time
  end

  it "should track an event with integer data" do
  	post "/events", :type => "myevent", :blubb => "123"
  	last_response.status.should == 200
  	FnordMetric::Event.last.type.should == "myevent"
  	FnordMetric::Event.last.blubb.should == 123
  end
  
  it "should track an event with float data" do
  	post "/events", :type => "myevent", :blubb => "42.23"
  	last_response.status.should == 200
  	FnordMetric::Event.last.type.should == "myevent"
  	FnordMetric::Event.last.blubb.should == 42.23
  end


  describe "metrics api" do

    before(:each) do
      FnordMetric::Event.destroy_all
      FnordMetric.metric('my_event_count', :count => true, :types => [:my_event_type])
      FnordMetric.track('my_event_type', :time => 33.hours.ago)
      FnordMetric.track('my_event_type', :time => 32.hours.ago)
      FnordMetric.track('my_event_type', :time => 29.hours.ago)
      FnordMetric.track('my_event_type', :time => 27.hours.ago)
      FnordMetric.track('my_event_type', :time => 26.hours.ago)
      FnordMetric.track('my_event_type', :time => 13.hours.ago)
      FnordMetric.track('my_event_type', :time => 12.hours.ago)
      FnordMetric.track('my_event_type', :time => 2.hours.ago)      
      FnordMetric.track('my_event_type', :time => 3.hours.ago)  
    end

    it "should return the right answer for: /metric/:name" do      
      get "/metric/my_event_count"
      JSON.parse(last_response.body)["value"].to_i.should == 9      
    end

    it "should return the right answer for: /metric/:name?at=timestamp" do      
      get "/metric/my_event_count", :at => 18.hours.ago.to_i.to_s
      JSON.parse(last_response.body)["value"].to_i.should == 5
    end

    it "should return the right answer for: /metric/:name?at=timestamp-timstamp" do      
      get "/metric/my_event_count", :at => "#{30.hours.ago.to_i}-#{20.hours.ago.to_i}"
      JSON.parse(last_response.body)["value"].to_i.should == 3
    end   

    it "should return the right answer for: /metric/:name?at=timestamp-timstamp&tick=seconds" do
      get "/metric/my_event_count", :at => "#{34.hours.ago.to_i}-#{1.hour.ago.to_i}", :tick => 6.hours.to_i, :delta => true
      JSON.parse(last_response.body)["values"].length.should == 6
    end

    it "should return the right answer for: /metric/:name?at=timestamp-timstamp&tick=seconds" do
      get "/metric/my_event_count", :at => "#{34.hours.ago.to_i}-#{1.hour.ago.to_i}", :tick => 6.hours.to_i, :delta => true
      JSON.parse(last_response.body)["values"][0].first.should == 34.hours.ago.to_i
      JSON.parse(last_response.body)["values"][0].last.should == 3
    end

    it "should return the right answer for: /metric/:name?at=timestamp-timstamp&tick=seconds" do
      get "/metric/my_event_count", :at => "#{34.hours.ago.to_i}-#{1.hour.ago.to_i}", :tick => 6.hours.to_i, :delta => true
      JSON.parse(last_response.body)["values"][1].first.should == 28.hours.ago.to_i
      JSON.parse(last_response.body)["values"][1].last.should == 2
    end

    it "should return the right answer for: /metric/:name?at=timestamp-timstamp&tick=seconds" do
      get "/metric/my_event_count", :at => "#{34.hours.ago.to_i}-#{1.hour.ago.to_i}", :tick => 6.hours.to_i, :delta => true
      JSON.parse(last_response.body)["values"][2].first.should == 22.hours.ago.to_i
      JSON.parse(last_response.body)["values"][2].last.should == 0
    end

    it "should return the right answer for: /metric/:name?at=timestamp-timstamp&tick=seconds" do
      get "/metric/my_event_count", :at => "#{34.hours.ago.to_i}-#{1.hour.ago.to_i}", :tick => 6.hours.to_i, :delta => true
      JSON.parse(last_response.body)["values"][3].first.should == 16.hours.ago.to_i
      JSON.parse(last_response.body)["values"][3].last.should == 2
    end

    it "should return the right answer for: /metric/:name?at=timestamp-timstamp&tick=seconds" do
      get "/metric/my_event_count", :at => "#{34.hours.ago.to_i}-#{1.hour.ago.to_i}", :tick => 6.hours.to_i, :delta => true
      JSON.parse(last_response.body)["values"][5].first.should == 4.hours.ago.to_i
      JSON.parse(last_response.body)["values"][5].last.should == 2
    end

    it "should return the right answer for: /metric/:name?at=timestamp-timstamp&tick=seconds" do
      get "/metric/my_event_count", :at => "#{34.hours.ago.to_i}-#{1.hour.ago.to_i}", :tick => 6.hours.to_i
      JSON.parse(last_response.body)["values"].length.should == 6
    end

    it "should return the right answer for: /metric/:name?at=timestamp-timstamp&tick=seconds" do
      get "/metric/my_event_count", :at => "#{34.hours.ago.to_i}-#{1.hour.ago.to_i}", :tick => 6.hours.to_i
      JSON.parse(last_response.body)["values"][0].first.should == 34.hours.ago.to_i
      JSON.parse(last_response.body)["values"][0].last.should == 0
    end

    it "should return the right answer for: /metric/:name?at=timestamp-timstamp&tick=seconds" do
      get "/metric/my_event_count", :at => "#{34.hours.ago.to_i}-#{1.hour.ago.to_i}", :tick => 6.hours.to_i
      JSON.parse(last_response.body)["values"][1].first.should == 28.hours.ago.to_i
      JSON.parse(last_response.body)["values"][1].last.should == 3
    end

    it "should return the right answer for: /metric/:name?at=timestamp-timstamp&tick=seconds" do
      get "/metric/my_event_count", :at => "#{34.hours.ago.to_i}-#{1.hour.ago.to_i}", :tick => 6.hours.to_i
      JSON.parse(last_response.body)["values"][2].first.should == 22.hours.ago.to_i
      JSON.parse(last_response.body)["values"][2].last.should == 5
    end

    it "should return the right answer for: /metric/:name?at=timestamp-timstamp&tick=seconds" do
      get "/metric/my_event_count", :at => "#{34.hours.ago.to_i}-#{1.hour.ago.to_i}", :tick => 6.hours.to_i
      JSON.parse(last_response.body)["values"][3].first.should == 16.hours.ago.to_i
      JSON.parse(last_response.body)["values"][3].last.should == 5
    end

    it "should return the right answer for: /metric/:name?at=timestamp-timstamp&tick=seconds" do
      get "/metric/my_event_count", :at => "#{34.hours.ago.to_i}-#{1.hour.ago.to_i}", :tick => 6.hours.to_i
      JSON.parse(last_response.body)["values"][5].first.should == 4.hours.ago.to_i
      JSON.parse(last_response.body)["values"][5].last.should == 7
    end

  end
 
end
 No newline at end of file

_spec/cache_spec.rb

deleted100644 → 0
+0 −53
Original line number Diff line number Diff line
require ::File.expand_path('../spec_helper.rb', __FILE__)

describe FnordMetric::Cache do

  before(:each) do
    FnordMetric::Cache.destroy_all
  end

  it "should store a cache item with a integer value" do
    FnordMetric::Cache.store!('mykey', 123)
    FnordMetric::Cache.last[:data].should be_a(Hash)
    FnordMetric::Cache.last[:data].keys.should == ["value"]
    FnordMetric::Cache.last[:data]["value"].to_i.should == 123
  end

  it "should store a cache item with a float value" do
    FnordMetric::Cache.store!('mykey', 123.5)
    FnordMetric::Cache.last[:data].should be_a(Hash)
    FnordMetric::Cache.last[:data].keys.should == ["value"]
    FnordMetric::Cache.last[:data]["value"].to_f.should == 123.5
  end

  it "should store a cache item with a hash value" do
    FnordMetric::Cache.store!('mykey', :foobar => "fnord", :blubb => "asd")
    FnordMetric::Cache.last[:data].should be_a(Hash)
    FnordMetric::Cache.last[:data].keys.length.should == 2
    FnordMetric::Cache.last[:data].keys.should include("foobar")
    FnordMetric::Cache.last[:data].keys.should include("blubb")
    FnordMetric::Cache.last[:data]["foobar"].should == "fnord"
    FnordMetric::Cache.last[:data]["blubb"].should == "asd"
  end

  it "should get a cache item with a integer value" do
    FnordMetric::Cache.store!('mykey', 123)
    FnordMetric::Cache.get('mykey').should == 123
  end

  it "should store a cache item with a float value" do
    FnordMetric::Cache.store!('mykey', 42.5)
    FnordMetric::Cache.get('mykey').should == 42.5
  end

  it "should store a cache item with a hash value" do
    FnordMetric::Cache.store!('mykey', :foobar => "fnord", :blubb => "asd")
    FnordMetric::Cache.get('mykey').should be_a(Hash)
    FnordMetric::Cache.get('mykey').keys.length.should == 2
    FnordMetric::Cache.get('mykey').keys.should include("foobar")
    FnordMetric::Cache.get('mykey').keys.should include("blubb")
    FnordMetric::Cache.get('mykey')["foobar"].should == "fnord"
    FnordMetric::Cache.get('mykey')["blubb"].should == "asd"
  end

end

_spec/combine_metric_spec.rb

deleted100644 → 0
+0 −19
Original line number Diff line number Diff line
require ::File.expand_path('../spec_helper.rb', __FILE__)

describe FnordMetric::CombineMetric do

  it "should return the block return" do
    metric = FnordMetric.metric('my_event_count', :combine => lambda{ |time_or_range|
      2323
    })
    metric.current.should == 2323
  end

  it "should pass the time_or_range to the bloc" do
    metric = FnordMetric.metric('my_event_count', :combine => lambda{ |time_or_range|
      time_or_range.to_i
    })
    metric.current.should == Time.now.to_i
  end

end
 No newline at end of file

_spec/core_spec.rb

deleted100644 → 0
+0 −50
Original line number Diff line number Diff line
require ::File.expand_path('../spec_helper.rb', __FILE__)

describe FnordMetric do

  before(:each) do
    FnordMetric::Event.destroy_all
  end

  it "should define a new metric" do
    FnordMetric.metric(:myfield_total, :sum => :myfield)
    FnordMetric.metrics.keys.should include(:myfield_total)
    FnordMetric.metrics[:myfield_total].should be_a(FnordMetric::Metric)
  end

  it "should define a new dashboard" do
    FnordMetric.dashboard('My Dashboard'){ |dash| }
    dashboard = FnordMetric.dashboards.last
    dashboard.should be_a(FnordMetric::Dashboard)
    dashboard.title.should == 'My Dashboard'
  end

  it "should define a new dashboard and call the config block" do
    block_called = false
    FnordMetric.dashboard 'My Dashboard' do |dash|
      block_called = true
      dash.should be_a(FnordMetric::Dashboard)
    end
    block_called.should be_true
  end

  it "should define a new widget" do
    FnordMetric.metric(:my_metric, :sum => :my_field)
    FnordMetric.widget(:my_widget, :metrics => :my_metric, :title => "My Widget", :type => :timeline)
    FnordMetric.widgets[:my_widget].title.should == "My Widget"
  end

  it "should raise an error if no type option is provided" do
    FnordMetric.metric(:my_metric, :sum => :my_field)
    lambda{
      FnordMetric.widget(:my_widget, :metrics => :my_metric, :title => "My Widget")      
    }.should raise_error(RuntimeError)
  end

  it "should raise an error if an unknown metric is added to a widget" do
    lambda{
      FnordMetric.widget(:my_widget, :metrics => :my_unknown_metric, :title => "My Widget", :type => :timeline)      
    }.should raise_error(RuntimeError)
  end

end
 No newline at end of file

_spec/count_metric_spec.rb

deleted100644 → 0
+0 −32
Original line number Diff line number Diff line
require ::File.expand_path('../spec_helper.rb', __FILE__)

describe FnordMetric::CountMetric do

  before(:each) do
    FnordMetric::Event.destroy_all
    FnordMetric.track('my_event_type', :time => 33.hours.ago)
    FnordMetric.track('my_event_type', :time => 32.hours.ago)
    FnordMetric.track('my_event_type', :time => 28.hours.ago)
    FnordMetric.track('my_event_type', :time => 27.hours.ago)
    FnordMetric.track('my_event_type', :time => 26.hours.ago)
    FnordMetric.track('my_event_type', :time => 13.hours.ago)
    FnordMetric.track('my_event_type', :time => 12.hours.ago)
    FnordMetric.track('my_event_type', :time => 11.hours.ago)
  end

  it "should count events until now" do
    metric = FnordMetric.metric('my_event_count', :count => true, :types => [:my_event_type])
    metric.current.should == 8
  end
  
  it "should count events until 18 hours ago" do
    metric = FnordMetric.metric('my_event_count', :count => true, :types => [:my_event_type])
    metric.at(18.hours.ago).should == 5
  end

  it "should count events from 30 to 20 hours ago" do
    metric = FnordMetric.metric('my_event_count', :count => true, :types => [:my_event_type])
    metric.at(30.hours.ago..20.hours.ago).should == 3
  end

end
 No newline at end of file
Loading