1
0

agent_log_spec.rb 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. require 'spec_helper'
  2. describe AgentLog do
  3. describe "validations" do
  4. before do
  5. @log = AgentLog.new(:agent => agents(:jane_website_agent), :message => "The agent did something", :level => 3)
  6. @log.should be_valid
  7. end
  8. it "requires an agent" do
  9. @log.agent = nil
  10. @log.should_not be_valid
  11. @log.should have(1).error_on(:agent)
  12. end
  13. it "requires a message" do
  14. @log.message = ""
  15. @log.should_not be_valid
  16. @log.message = nil
  17. @log.should_not be_valid
  18. @log.should have(1).error_on(:message)
  19. end
  20. it "requires a valid log level" do
  21. @log.level = nil
  22. @log.should_not be_valid
  23. @log.should have(1).error_on(:level)
  24. @log.level = -1
  25. @log.should_not be_valid
  26. @log.should have(1).error_on(:level)
  27. @log.level = 5
  28. @log.should_not be_valid
  29. @log.should have(1).error_on(:level)
  30. @log.level = 4
  31. @log.should be_valid
  32. @log.level = 0
  33. @log.should be_valid
  34. end
  35. end
  36. it "truncates message to a reasonable length" do
  37. log = AgentLog.new(:agent => agents(:jane_website_agent), :level => 3)
  38. log.message = "a" * 3000
  39. log.save!
  40. log.message.length.should == 2048
  41. end
  42. describe "#log_for_agent" do
  43. it "creates AgentLogs" do
  44. log = AgentLog.log_for_agent(agents(:jane_website_agent), "some message", :level => 4, :outbound_event => events(:jane_website_agent_event))
  45. log.should_not be_new_record
  46. log.agent.should == agents(:jane_website_agent)
  47. log.outbound_event.should == events(:jane_website_agent_event)
  48. log.message.should == "some message"
  49. log.level.should == 4
  50. end
  51. it "cleans up old logs when there are more than log_length" do
  52. stub(AgentLog).log_length { 4 }
  53. AgentLog.log_for_agent(agents(:jane_website_agent), "message 1")
  54. AgentLog.log_for_agent(agents(:jane_website_agent), "message 2")
  55. AgentLog.log_for_agent(agents(:jane_website_agent), "message 3")
  56. AgentLog.log_for_agent(agents(:jane_website_agent), "message 4")
  57. agents(:jane_website_agent).logs.order("agent_logs.id desc").first.message.should == "message 4"
  58. agents(:jane_website_agent).logs.order("agent_logs.id desc").last.message.should == "message 1"
  59. AgentLog.log_for_agent(agents(:jane_website_agent), "message 5")
  60. agents(:jane_website_agent).logs.order("agent_logs.id desc").first.message.should == "message 5"
  61. agents(:jane_website_agent).logs.order("agent_logs.id desc").last.message.should == "message 2"
  62. AgentLog.log_for_agent(agents(:jane_website_agent), "message 6")
  63. agents(:jane_website_agent).logs.order("agent_logs.id desc").first.message.should == "message 6"
  64. agents(:jane_website_agent).logs.order("agent_logs.id desc").last.message.should == "message 3"
  65. end
  66. it "updates Agents' last_error_log_at when an error is logged" do
  67. AgentLog.log_for_agent(agents(:jane_website_agent), "some message", :level => 3, :outbound_event => events(:jane_website_agent_event))
  68. agents(:jane_website_agent).reload.last_error_log_at.should be_nil
  69. AgentLog.log_for_agent(agents(:jane_website_agent), "some message", :level => 4, :outbound_event => events(:jane_website_agent_event))
  70. agents(:jane_website_agent).reload.last_error_log_at.to_i.should be_within(2).of(Time.now.to_i)
  71. end
  72. end
  73. describe "#log_length" do
  74. it "defaults to 200" do
  75. AgentLog.log_length.should == 200
  76. end
  77. end
  78. end