sentiment_agent_spec.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. require 'rails_helper'
  2. describe Agents::SentimentAgent do
  3. before do
  4. @valid_params = {
  5. :name => "somename",
  6. :options => {
  7. :content => "$.message",
  8. :expected_receive_period_in_days => 1
  9. }
  10. }
  11. @checker = Agents::SentimentAgent.new(@valid_params)
  12. @checker.user = users(:jane)
  13. @checker.save!
  14. @event = Event.new
  15. @event.agent = agents(:jane_weather_agent)
  16. @event.payload = {
  17. :message => "value1"
  18. }
  19. @event.save!
  20. end
  21. describe "#working?" do
  22. it "checks if events have been received within expected receive period" do
  23. expect(@checker).not_to be_working
  24. Agents::SentimentAgent.async_receive @checker.id, [@event.id]
  25. expect(@checker.reload).to be_working
  26. two_days_from_now = 2.days.from_now
  27. allow(Time).to receive(:now) { two_days_from_now }
  28. expect(@checker.reload).not_to be_working
  29. end
  30. end
  31. describe "validation" do
  32. before do
  33. expect(@checker).to be_valid
  34. end
  35. it "should validate presence of content key" do
  36. @checker.options[:content] = nil
  37. expect(@checker).not_to be_valid
  38. end
  39. it "should validate presence of expected_receive_period_in_days key" do
  40. @checker.options[:expected_receive_period_in_days] = nil
  41. expect(@checker).not_to be_valid
  42. end
  43. end
  44. describe "#receive" do
  45. it "checks if content key is working fine" do
  46. @checker.receive([@event])
  47. expect(Event.last.payload[:content]).to eq("value1")
  48. expect(Event.last.payload[:original_event]).to eq({ 'message' => "value1" })
  49. end
  50. it "should handle multiple events" do
  51. event1 = Event.new
  52. event1.agent = agents(:bob_weather_agent)
  53. event1.payload = {
  54. :message => "The quick brown fox jumps over the lazy dog"
  55. }
  56. event2 = Event.new
  57. event2.agent = agents(:jane_weather_agent)
  58. event2.payload = {
  59. :message => "The quick brown fox jumps over the lazy dog"
  60. }
  61. expect {
  62. @checker.receive([@event,event1,event2])
  63. }.to change { Event.count }.by(3)
  64. end
  65. end
  66. end