event_spec.rb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. require 'spec_helper'
  2. describe Event do
  3. describe ".with_location" do
  4. it "selects events with location" do
  5. event = events(:bob_website_agent_event)
  6. event.lat = 2
  7. event.lng = 3
  8. event.save!
  9. Event.with_location.pluck(:id).should == [event.id]
  10. event.lat = nil
  11. event.save!
  12. Event.with_location.should be_empty
  13. end
  14. end
  15. describe "#location" do
  16. it "returns a default hash when an event does not have a location" do
  17. event = events(:bob_website_agent_event)
  18. event.location.should == Location.new(
  19. lat: nil,
  20. lng: nil,
  21. radius: 0.0,
  22. speed: nil,
  23. course: nil)
  24. end
  25. it "returns a hash containing location information" do
  26. event = events(:bob_website_agent_event)
  27. event.lat = 2
  28. event.lng = 3
  29. event.payload = {
  30. radius: 300,
  31. speed: 0.5,
  32. course: 90.0,
  33. }
  34. event.save!
  35. event.location.should == Location.new(
  36. lat: 2.0,
  37. lng: 3.0,
  38. radius: 0.0,
  39. speed: 0.5,
  40. course: 90.0)
  41. end
  42. end
  43. describe "#reemit" do
  44. it "creates a new event identical to itself" do
  45. events(:bob_website_agent_event).lat = 2
  46. events(:bob_website_agent_event).lng = 3
  47. events(:bob_website_agent_event).created_at = 2.weeks.ago
  48. lambda {
  49. events(:bob_website_agent_event).reemit!
  50. }.should change { Event.count }.by(1)
  51. Event.last.payload.should == events(:bob_website_agent_event).payload
  52. Event.last.agent.should == events(:bob_website_agent_event).agent
  53. Event.last.lat.should == 2
  54. Event.last.lng.should == 3
  55. Event.last.created_at.to_i.should be_within(2).of(Time.now.to_i)
  56. end
  57. end
  58. describe ".cleanup_expired!" do
  59. it "removes any Events whose expired_at date is non-null and in the past, updating Agent counter caches" do
  60. half_hour_event = agents(:jane_weather_agent).create_event :expires_at => 20.minutes.from_now
  61. one_hour_event = agents(:bob_weather_agent).create_event :expires_at => 1.hours.from_now
  62. two_hour_event = agents(:jane_weather_agent).create_event :expires_at => 2.hours.from_now
  63. three_hour_event = agents(:jane_weather_agent).create_event :expires_at => 3.hours.from_now
  64. non_expiring_event = agents(:bob_weather_agent).create_event({})
  65. initial_bob_count = agents(:bob_weather_agent).reload.events_count
  66. initial_jane_count = agents(:jane_weather_agent).reload.events_count
  67. current_time = Time.now
  68. stub(Time).now { current_time }
  69. Event.cleanup_expired!
  70. Event.find_by_id(half_hour_event.id).should_not be_nil
  71. Event.find_by_id(one_hour_event.id).should_not be_nil
  72. Event.find_by_id(two_hour_event.id).should_not be_nil
  73. Event.find_by_id(three_hour_event.id).should_not be_nil
  74. Event.find_by_id(non_expiring_event.id).should_not be_nil
  75. agents(:bob_weather_agent).reload.events_count.should == initial_bob_count
  76. agents(:jane_weather_agent).reload.events_count.should == initial_jane_count
  77. current_time = 119.minutes.from_now # move almost 2 hours into the future
  78. Event.cleanup_expired!
  79. Event.find_by_id(half_hour_event.id).should be_nil
  80. Event.find_by_id(one_hour_event.id).should be_nil
  81. Event.find_by_id(two_hour_event.id).should_not be_nil
  82. Event.find_by_id(three_hour_event.id).should_not be_nil
  83. Event.find_by_id(non_expiring_event.id).should_not be_nil
  84. agents(:bob_weather_agent).reload.events_count.should == initial_bob_count - 1
  85. agents(:jane_weather_agent).reload.events_count.should == initial_jane_count - 1
  86. current_time = 2.minutes.from_now # move 2 minutes further into the future
  87. Event.cleanup_expired!
  88. Event.find_by_id(two_hour_event.id).should be_nil
  89. Event.find_by_id(three_hour_event.id).should_not be_nil
  90. Event.find_by_id(non_expiring_event.id).should_not be_nil
  91. agents(:bob_weather_agent).reload.events_count.should == initial_bob_count - 1
  92. agents(:jane_weather_agent).reload.events_count.should == initial_jane_count - 2
  93. end
  94. it "doesn't touch Events with no expired_at" do
  95. event = Event.new
  96. event.agent = agents(:jane_weather_agent)
  97. event.expires_at = nil
  98. event.save!
  99. current_time = Time.now
  100. stub(Time).now { current_time }
  101. Event.cleanup_expired!
  102. Event.find_by_id(event.id).should_not be_nil
  103. current_time = 2.days.from_now
  104. Event.cleanup_expired!
  105. Event.find_by_id(event.id).should_not be_nil
  106. end
  107. end
  108. describe "after destroy" do
  109. it "nullifies any dependent AgentLogs" do
  110. agent_logs(:log_for_jane_website_agent).outbound_event_id.should be_present
  111. agent_logs(:log_for_bob_website_agent).outbound_event_id.should be_present
  112. agent_logs(:log_for_bob_website_agent).outbound_event.destroy
  113. agent_logs(:log_for_jane_website_agent).reload.outbound_event_id.should be_present
  114. agent_logs(:log_for_bob_website_agent).reload.outbound_event_id.should be_nil
  115. end
  116. end
  117. describe "caches" do
  118. describe "when an event is created" do
  119. it "updates a counter cache on agent" do
  120. lambda {
  121. agents(:jane_weather_agent).events.create!(:user => users(:jane))
  122. }.should change { agents(:jane_weather_agent).reload.events_count }.by(1)
  123. end
  124. it "updates last_event_at on agent" do
  125. lambda {
  126. agents(:jane_weather_agent).events.create!(:user => users(:jane))
  127. }.should change { agents(:jane_weather_agent).reload.last_event_at }
  128. end
  129. end
  130. describe "when an event is updated" do
  131. it "does not touch the last_event_at on the agent" do
  132. event = agents(:jane_weather_agent).events.create!(:user => users(:jane))
  133. agents(:jane_weather_agent).update_attribute :last_event_at, 2.days.ago
  134. lambda {
  135. event.update_attribute :payload, { 'hello' => 'world' }
  136. }.should_not change { agents(:jane_weather_agent).reload.last_event_at }
  137. end
  138. end
  139. end
  140. end
  141. describe EventDrop do
  142. def interpolate(string, event)
  143. event.agent.interpolate_string(string, event.to_liquid)
  144. end
  145. before do
  146. @event = Event.new
  147. @event.agent = agents(:jane_weather_agent)
  148. @event.created_at = Time.now
  149. @event.payload = {
  150. 'title' => 'some title',
  151. 'url' => 'http://some.site.example.org/',
  152. }
  153. @event.lat = 2
  154. @event.lng = 3
  155. @event.save!
  156. end
  157. it 'should be created via Agent#to_liquid' do
  158. @event.to_liquid.class.should be(EventDrop)
  159. end
  160. it 'should have attributes of its payload' do
  161. t = '{{title}}: {{url}}'
  162. interpolate(t, @event).should eq('some title: http://some.site.example.org/')
  163. end
  164. it 'should use created_at from the payload if it exists' do
  165. created_at = @event.created_at - 86400
  166. # Avoid timezone issue by using %s
  167. @event.payload['created_at'] = created_at.strftime("%s")
  168. @event.save!
  169. t = '{{created_at | date:"%s" }}'
  170. interpolate(t, @event).should eq(created_at.strftime("%s"))
  171. end
  172. it 'should be iteratable' do
  173. # to_liquid returns self
  174. t = "{% for pair in to_liquid %}{{pair | join:':' }}\n{% endfor %}"
  175. interpolate(t, @event).should eq("title:some title\nurl:http://some.site.example.org/\n")
  176. end
  177. it 'should have agent' do
  178. t = '{{agent.name}}'
  179. interpolate(t, @event).should eq('SF Weather')
  180. end
  181. it 'should have created_at' do
  182. t = '{{created_at | date:"%FT%T%z" }}'
  183. interpolate(t, @event).should eq(@event.created_at.strftime("%FT%T%z"))
  184. end
  185. it 'should have _location_' do
  186. t = '{{_location_.lat}},{{_location_.lng}}'
  187. interpolate(t, @event).should eq("2.0,3.0")
  188. end
  189. end