event_spec.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. require 'rails_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. expect(Event.with_location.pluck(:id)).to eq([event.id])
  10. event.lat = nil
  11. event.save!
  12. expect(Event.with_location).to 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. expect(event.location).to eq(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. expect(event.location).to eq(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. expect {
  49. events(:bob_website_agent_event).reemit!
  50. }.to change { Event.count }.by(1)
  51. expect(Event.last.payload).to eq(events(:bob_website_agent_event).payload)
  52. expect(Event.last.agent).to eq(events(:bob_website_agent_event).agent)
  53. expect(Event.last.lat).to eq(2)
  54. expect(Event.last.lng).to eq(3)
  55. expect(Event.last.created_at.to_i).to 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. expect(Event.find_by_id(half_hour_event.id)).not_to be_nil
  71. expect(Event.find_by_id(one_hour_event.id)).not_to be_nil
  72. expect(Event.find_by_id(two_hour_event.id)).not_to be_nil
  73. expect(Event.find_by_id(three_hour_event.id)).not_to be_nil
  74. expect(Event.find_by_id(non_expiring_event.id)).not_to be_nil
  75. expect(agents(:bob_weather_agent).reload.events_count).to eq(initial_bob_count)
  76. expect(agents(:jane_weather_agent).reload.events_count).to eq(initial_jane_count)
  77. current_time = 119.minutes.from_now # move almost 2 hours into the future
  78. Event.cleanup_expired!
  79. expect(Event.find_by_id(half_hour_event.id)).to be_nil
  80. expect(Event.find_by_id(one_hour_event.id)).to be_nil
  81. expect(Event.find_by_id(two_hour_event.id)).not_to be_nil
  82. expect(Event.find_by_id(three_hour_event.id)).not_to be_nil
  83. expect(Event.find_by_id(non_expiring_event.id)).not_to be_nil
  84. expect(agents(:bob_weather_agent).reload.events_count).to eq(initial_bob_count - 1)
  85. expect(agents(:jane_weather_agent).reload.events_count).to eq(initial_jane_count - 1)
  86. current_time = 2.minutes.from_now # move 2 minutes further into the future
  87. Event.cleanup_expired!
  88. expect(Event.find_by_id(two_hour_event.id)).to be_nil
  89. expect(Event.find_by_id(three_hour_event.id)).not_to be_nil
  90. expect(Event.find_by_id(non_expiring_event.id)).not_to be_nil
  91. expect(agents(:bob_weather_agent).reload.events_count).to eq(initial_bob_count - 1)
  92. expect(agents(:jane_weather_agent).reload.events_count).to eq(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. expect(Event.find_by_id(event.id)).not_to be_nil
  103. current_time = 2.days.from_now
  104. Event.cleanup_expired!
  105. expect(Event.find_by_id(event.id)).not_to be_nil
  106. end
  107. it "always keeps the latest Event regardless of its expires_at value only if the database is MySQL" do
  108. Event.delete_all
  109. event1 = agents(:jane_weather_agent).create_event expires_at: 1.minute.ago
  110. event2 = agents(:bob_weather_agent).create_event expires_at: 1.minute.ago
  111. Event.cleanup_expired!
  112. case ActiveRecord::Base.connection.adapter_name
  113. when /\Amysql/i
  114. expect(Event.all.pluck(:id)).to eq([event2.id])
  115. else
  116. expect(Event.all.pluck(:id)).to be_empty
  117. end
  118. end
  119. end
  120. describe "after destroy" do
  121. it "nullifies any dependent AgentLogs" do
  122. expect(agent_logs(:log_for_jane_website_agent).outbound_event_id).to be_present
  123. expect(agent_logs(:log_for_bob_website_agent).outbound_event_id).to be_present
  124. agent_logs(:log_for_bob_website_agent).outbound_event.destroy
  125. expect(agent_logs(:log_for_jane_website_agent).reload.outbound_event_id).to be_present
  126. expect(agent_logs(:log_for_bob_website_agent).reload.outbound_event_id).to be_nil
  127. end
  128. end
  129. describe "caches" do
  130. describe "when an event is created" do
  131. it "updates a counter cache on agent" do
  132. expect {
  133. agents(:jane_weather_agent).events.create!(:user => users(:jane))
  134. }.to change { agents(:jane_weather_agent).reload.events_count }.by(1)
  135. end
  136. it "updates last_event_at on agent" do
  137. expect {
  138. agents(:jane_weather_agent).events.create!(:user => users(:jane))
  139. }.to change { agents(:jane_weather_agent).reload.last_event_at }
  140. end
  141. end
  142. describe "when an event is updated" do
  143. it "does not touch the last_event_at on the agent" do
  144. event = agents(:jane_weather_agent).events.create!(:user => users(:jane))
  145. agents(:jane_weather_agent).update_attribute :last_event_at, 2.days.ago
  146. expect {
  147. event.update_attribute :payload, { 'hello' => 'world' }
  148. }.not_to change { agents(:jane_weather_agent).reload.last_event_at }
  149. end
  150. end
  151. end
  152. end
  153. describe EventDrop do
  154. def interpolate(string, event)
  155. event.agent.interpolate_string(string, event.to_liquid)
  156. end
  157. before do
  158. @event = Event.new
  159. @event.agent = agents(:jane_weather_agent)
  160. @event.created_at = Time.now
  161. @event.payload = {
  162. 'title' => 'some title',
  163. 'url' => 'http://some.site.example.org/',
  164. }
  165. @event.lat = 2
  166. @event.lng = 3
  167. @event.save!
  168. end
  169. it 'should be created via Agent#to_liquid' do
  170. expect(@event.to_liquid.class).to be(EventDrop)
  171. end
  172. it 'should have attributes of its payload' do
  173. t = '{{title}}: {{url}}'
  174. expect(interpolate(t, @event)).to eq('some title: http://some.site.example.org/')
  175. end
  176. it 'should use created_at from the payload if it exists' do
  177. created_at = @event.created_at - 86400
  178. # Avoid timezone issue by using %s
  179. @event.payload['created_at'] = created_at.strftime("%s")
  180. @event.save!
  181. t = '{{created_at | date:"%s" }}'
  182. expect(interpolate(t, @event)).to eq(created_at.strftime("%s"))
  183. end
  184. it 'should be iteratable' do
  185. # to_liquid returns self
  186. t = "{% for pair in to_liquid %}{{pair | join:':' }}\n{% endfor %}"
  187. expect(interpolate(t, @event)).to eq("title:some title\nurl:http://some.site.example.org/\n")
  188. end
  189. it 'should have agent' do
  190. t = '{{agent.name}}'
  191. expect(interpolate(t, @event)).to eq('SF Weather')
  192. end
  193. it 'should have created_at' do
  194. t = '{{created_at | date:"%FT%T%z" }}'
  195. expect(interpolate(t, @event)).to eq(@event.created_at.strftime("%FT%T%z"))
  196. end
  197. it 'should have _location_' do
  198. t = '{{_location_.lat}},{{_location_.lng}}'
  199. expect(interpolate(t, @event)).to eq("2.0,3.0")
  200. end
  201. end