twitter_stream_agent_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. require 'spec_helper'
  2. describe Agents::TwitterStreamAgent do
  3. before do
  4. @opts = {
  5. :consumer_key => "---",
  6. :consumer_secret => "---",
  7. :oauth_token => "---",
  8. :oauth_token_secret => "---",
  9. :filters => %w[keyword1 keyword2],
  10. :expected_update_period_in_days => "2",
  11. :generate => "events"
  12. }
  13. @agent = Agents::TwitterStreamAgent.new(:name => "HuginnBot", :options => @opts)
  14. @agent.service = services(:generic)
  15. @agent.user = users(:bob)
  16. @agent.save!
  17. end
  18. describe '#process_tweet' do
  19. context "when generate is set to 'counts'" do
  20. before do
  21. @agent.options[:generate] = 'counts'
  22. end
  23. it 'records counts' do
  24. @agent.process_tweet('keyword1', {:text => "something", :user => {:name => "Mr. Someone"}})
  25. @agent.process_tweet('keyword2', {:text => "something", :user => {:name => "Mr. Someone"}})
  26. @agent.process_tweet('keyword1', {:text => "something", :user => {:name => "Mr. Someone"}})
  27. @agent.reload
  28. expect(@agent.memory[:filter_counts][:keyword1]).to eq(2)
  29. expect(@agent.memory[:filter_counts][:keyword2]).to eq(1)
  30. end
  31. it 'records counts for keyword sets as well' do
  32. @agent.options[:filters][0] = %w[keyword1-1 keyword1-2 keyword1-3]
  33. @agent.save!
  34. @agent.process_tweet('keyword2', {:text => "something", :user => {:name => "Mr. Someone"}})
  35. @agent.process_tweet('keyword2', {:text => "something", :user => {:name => "Mr. Someone"}})
  36. @agent.process_tweet('keyword1-1', {:text => "something", :user => {:name => "Mr. Someone"}})
  37. @agent.process_tweet('keyword1-2', {:text => "something", :user => {:name => "Mr. Someone"}})
  38. @agent.process_tweet('keyword1-3', {:text => "something", :user => {:name => "Mr. Someone"}})
  39. @agent.process_tweet('keyword1-1', {:text => "something", :user => {:name => "Mr. Someone"}})
  40. @agent.reload
  41. expect(@agent.memory[:filter_counts][:'keyword1-1']).to eq(4) # it stores on the first keyword
  42. expect(@agent.memory[:filter_counts][:keyword2]).to eq(2)
  43. end
  44. it 'removes unused keys' do
  45. @agent.memory[:filter_counts] = {:keyword1 => 2, :keyword2 => 3, :keyword3 => 4}
  46. @agent.save!
  47. @agent.process_tweet('keyword1', {:text => "something", :user => {:name => "Mr. Someone"}})
  48. expect(@agent.reload.memory[:filter_counts]).to eq({ 'keyword1' => 3, 'keyword2' => 3 })
  49. end
  50. end
  51. context "when generate is set to 'events'" do
  52. it 'emits events immediately' do
  53. expect {
  54. @agent.process_tweet('keyword1', {:text => "something", :user => {:name => "Mr. Someone"}})
  55. }.to change { @agent.events.count }.by(1)
  56. expect(@agent.events.last.payload).to eq({
  57. 'filter' => 'keyword1',
  58. 'text' => "something",
  59. 'user' => { 'name' => "Mr. Someone" }
  60. })
  61. end
  62. it 'handles keyword sets too' do
  63. @agent.options[:filters][0] = %w[keyword1-1 keyword1-2 keyword1-3]
  64. @agent.save!
  65. expect {
  66. @agent.process_tweet('keyword1-2', {:text => "something", :user => {:name => "Mr. Someone"}})
  67. }.to change { @agent.events.count }.by(1)
  68. expect(@agent.events.last.payload).to eq({
  69. 'filter' => 'keyword1-1',
  70. 'text' => "something",
  71. 'user' => { 'name' => "Mr. Someone" }
  72. })
  73. end
  74. end
  75. end
  76. describe '#check' do
  77. context "when generate is set to 'counts'" do
  78. before do
  79. @agent.options[:generate] = 'counts'
  80. @agent.save!
  81. end
  82. it 'emits events' do
  83. @agent.process_tweet('keyword1', {:text => "something", :user => {:name => "Mr. Someone"}})
  84. @agent.process_tweet('keyword2', {:text => "something", :user => {:name => "Mr. Someone"}})
  85. @agent.process_tweet('keyword1', {:text => "something", :user => {:name => "Mr. Someone"}})
  86. expect {
  87. @agent.reload.check
  88. }.to change { @agent.events.count }.by(2)
  89. expect(@agent.events[-1].payload[:filter]).to eq('keyword1')
  90. expect(@agent.events[-1].payload[:count]).to eq(2)
  91. expect(@agent.events[-2].payload[:filter]).to eq('keyword2')
  92. expect(@agent.events[-2].payload[:count]).to eq(1)
  93. expect(@agent.memory[:filter_counts]).to eq({})
  94. end
  95. end
  96. context "when generate is not set to 'counts'" do
  97. it 'does nothing' do
  98. @agent.memory[:filter_counts] = { :keyword1 => 2 }
  99. @agent.save!
  100. expect {
  101. @agent.reload.check
  102. }.not_to change { Event.count }
  103. expect(@agent.memory[:filter_counts]).to eq({})
  104. end
  105. end
  106. end
  107. end