Нет описания http://j1x-huginn.herokuapp.com

human_task_agent_spec.rb 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. require 'spec_helper'
  2. describe Agents::HumanTaskAgent do
  3. before do
  4. @checker = Agents::HumanTaskAgent.new(:name => "my human task agent")
  5. @checker.options = @checker.default_options
  6. @checker.user = users(:bob)
  7. @checker.save!
  8. @event = Event.new
  9. @event.agent = agents(:bob_rain_notifier_agent)
  10. @event.payload = { 'foo' => { "bar" => { 'baz' => "a2b" } },
  11. 'name' => "Joe" }
  12. @event.id = 345
  13. @checker.should be_valid
  14. end
  15. describe "validations" do
  16. it "validates that trigger_on is 'schedule' or 'event'" do
  17. @checker.options['trigger_on'] = "foo"
  18. @checker.should_not be_valid
  19. end
  20. it "requires expected_receive_period_in_days when trigger_on is set to 'event'" do
  21. @checker.options['trigger_on'] = "event"
  22. @checker.options['expected_receive_period_in_days'] = nil
  23. @checker.should_not be_valid
  24. @checker.options['expected_receive_period_in_days'] = 2
  25. @checker.should be_valid
  26. end
  27. it "requires a positive submission_period when trigger_on is set to 'schedule'" do
  28. @checker.options['trigger_on'] = "schedule"
  29. @checker.options['submission_period'] = nil
  30. @checker.should_not be_valid
  31. @checker.options['submission_period'] = 2
  32. @checker.should be_valid
  33. end
  34. it "requires a hit.title" do
  35. @checker.options['hit']['title'] = ""
  36. @checker.should_not be_valid
  37. end
  38. it "requires a hit.description" do
  39. @checker.options['hit']['description'] = ""
  40. @checker.should_not be_valid
  41. end
  42. it "requires hit.assignments" do
  43. @checker.options['hit']['assignments'] = ""
  44. @checker.should_not be_valid
  45. @checker.options['hit']['assignments'] = 0
  46. @checker.should_not be_valid
  47. @checker.options['hit']['assignments'] = "moose"
  48. @checker.should_not be_valid
  49. @checker.options['hit']['assignments'] = "2"
  50. @checker.should be_valid
  51. end
  52. it "requires hit.questions" do
  53. old_questions = @checker.options['hit']['questions']
  54. @checker.options['hit']['questions'] = nil
  55. @checker.should_not be_valid
  56. @checker.options['hit']['questions'] = []
  57. @checker.should_not be_valid
  58. @checker.options['hit']['questions'] = [old_questions[0]]
  59. @checker.should be_valid
  60. end
  61. it "requires that all questions have key, name, required, type, and question" do
  62. old_questions = @checker.options['hit']['questions']
  63. @checker.options['hit']['questions'].first['key'] = ""
  64. @checker.should_not be_valid
  65. @checker.options['hit']['questions'] = old_questions
  66. @checker.options['hit']['questions'].first['name'] = ""
  67. @checker.should_not be_valid
  68. @checker.options['hit']['questions'] = old_questions
  69. @checker.options['hit']['questions'].first['required'] = nil
  70. @checker.should_not be_valid
  71. @checker.options['hit']['questions'] = old_questions
  72. @checker.options['hit']['questions'].first['type'] = ""
  73. @checker.should_not be_valid
  74. @checker.options['hit']['questions'] = old_questions
  75. @checker.options['hit']['questions'].first['question'] = ""
  76. @checker.should_not be_valid
  77. end
  78. it "requires that all questions of type 'selection' have a selections array with keys and text" do
  79. @checker.options['hit']['questions'][0]['selections'] = []
  80. @checker.should_not be_valid
  81. @checker.options['hit']['questions'][0]['selections'] = [{}]
  82. @checker.should_not be_valid
  83. @checker.options['hit']['questions'][0]['selections'] = [{ 'key' => "", 'text' => "" }]
  84. @checker.should_not be_valid
  85. @checker.options['hit']['questions'][0]['selections'] = [{ 'key' => "", 'text' => "hi" }]
  86. @checker.should_not be_valid
  87. @checker.options['hit']['questions'][0]['selections'] = [{ 'key' => "hi", 'text' => "" }]
  88. @checker.should_not be_valid
  89. @checker.options['hit']['questions'][0]['selections'] = [{ 'key' => "hi", 'text' => "hi" }]
  90. @checker.should be_valid
  91. @checker.options['hit']['questions'][0]['selections'] = [{ 'key' => "hi", 'text' => "hi" }, {}]
  92. @checker.should_not be_valid
  93. end
  94. it "requires that 'poll_options' be present and populated when 'combination_mode' is set to 'poll'" do
  95. @checker.options['combination_mode'] = "poll"
  96. @checker.should_not be_valid
  97. @checker.options['poll_options'] = {}
  98. @checker.should_not be_valid
  99. @checker.options['poll_options'] = { 'title' => "Take a poll about jokes",
  100. 'instructions' => "Rank these by how funny they are",
  101. 'assignments' => 3,
  102. 'row_template' => "{{joke}}" }
  103. @checker.should be_valid
  104. @checker.options['poll_options'] = { 'instructions' => "Rank these by how funny they are",
  105. 'assignments' => 3,
  106. 'row_template' => "{{joke}}" }
  107. @checker.should_not be_valid
  108. @checker.options['poll_options'] = { 'title' => "Take a poll about jokes",
  109. 'assignments' => 3,
  110. 'row_template' => "{{joke}}" }
  111. @checker.should_not be_valid
  112. @checker.options['poll_options'] = { 'title' => "Take a poll about jokes",
  113. 'instructions' => "Rank these by how funny they are",
  114. 'row_template' => "{{joke}}" }
  115. @checker.should_not be_valid
  116. @checker.options['poll_options'] = { 'title' => "Take a poll about jokes",
  117. 'instructions' => "Rank these by how funny they are",
  118. 'assignments' => 3}
  119. @checker.should_not be_valid
  120. end
  121. it "requires that all questions be of type 'selection' when 'combination_mode' is 'take_majority'" do
  122. @checker.options['combination_mode'] = "take_majority"
  123. @checker.should_not be_valid
  124. @checker.options['hit']['questions'][1]['type'] = "selection"
  125. @checker.options['hit']['questions'][1]['selections'] = @checker.options['hit']['questions'][0]['selections']
  126. @checker.should be_valid
  127. end
  128. it "accepts 'take_majority': 'true' for legacy support" do
  129. @checker.options['take_majority'] = "true"
  130. @checker.should_not be_valid
  131. @checker.options['hit']['questions'][1]['type'] = "selection"
  132. @checker.options['hit']['questions'][1]['selections'] = @checker.options['hit']['questions'][0]['selections']
  133. @checker.should be_valid
  134. end
  135. end
  136. describe "when 'trigger_on' is set to 'schedule'" do
  137. before do
  138. @checker.options['trigger_on'] = "schedule"
  139. @checker.options['submission_period'] = "2"
  140. @checker.options.delete('expected_receive_period_in_days')
  141. end
  142. it "should check for reviewable HITs frequently" do
  143. mock(@checker).review_hits.twice
  144. mock(@checker).create_basic_hit.once
  145. @checker.check
  146. @checker.check
  147. end
  148. it "should create HITs every 'submission_period' hours" do
  149. now = Time.now
  150. stub(Time).now { now }
  151. mock(@checker).review_hits.times(3)
  152. mock(@checker).create_basic_hit.twice
  153. @checker.check
  154. now += 1 * 60 * 60
  155. @checker.check
  156. now += 1 * 60 * 60
  157. @checker.check
  158. end
  159. it "should ignore events" do
  160. mock(@checker).create_basic_hit(anything).times(0)
  161. @checker.receive([events(:bob_website_agent_event)])
  162. end
  163. end
  164. describe "when 'trigger_on' is set to 'event'" do
  165. it "should not create HITs during check but should check for reviewable HITs" do
  166. @checker.options['submission_period'] = "2"
  167. now = Time.now
  168. stub(Time).now { now }
  169. mock(@checker).review_hits.times(3)
  170. mock(@checker).create_basic_hit.times(0)
  171. @checker.check
  172. now += 1 * 60 * 60
  173. @checker.check
  174. now += 1 * 60 * 60
  175. @checker.check
  176. end
  177. it "should create HITs based on events" do
  178. mock(@checker).create_basic_hit(events(:bob_website_agent_event)).times(1)
  179. @checker.receive([events(:bob_website_agent_event)])
  180. end
  181. end
  182. describe "creating hits" do
  183. it "can create HITs based on events, interpolating their values" do
  184. @checker.options['hit']['title'] = "Hi {{name}}"
  185. @checker.options['hit']['description'] = "Make something for {{name}}"
  186. @checker.options['hit']['questions'][0]['name'] = "{{name}} Question 1"
  187. question_form = nil
  188. hitInterface = OpenStruct.new
  189. hitInterface.id = 123
  190. mock(hitInterface).question_form(instance_of Agents::HumanTaskAgent::AgentQuestionForm) { |agent_question_form_instance| question_form = agent_question_form_instance }
  191. mock(RTurk::Hit).create(:title => "Hi Joe").yields(hitInterface) { hitInterface }
  192. @checker.send :create_basic_hit, @event
  193. hitInterface.max_assignments.should == @checker.options['hit']['assignments']
  194. hitInterface.reward.should == @checker.options['hit']['reward']
  195. hitInterface.description.should == "Make something for Joe"
  196. xml = question_form.to_xml
  197. xml.should include("<Title>Hi Joe</Title>")
  198. xml.should include("<Text>Make something for Joe</Text>")
  199. xml.should include("<DisplayName>Joe Question 1</DisplayName>")
  200. @checker.memory['hits'][123]['event_id'].should == @event.id
  201. end
  202. it "works without an event too" do
  203. @checker.options['hit']['title'] = "Hi {{name}}"
  204. hitInterface = OpenStruct.new
  205. hitInterface.id = 123
  206. mock(hitInterface).question_form(instance_of Agents::HumanTaskAgent::AgentQuestionForm)
  207. mock(RTurk::Hit).create(:title => "Hi").yields(hitInterface) { hitInterface }
  208. @checker.send :create_basic_hit
  209. hitInterface.max_assignments.should == @checker.options['hit']['assignments']
  210. hitInterface.reward.should == @checker.options['hit']['reward']
  211. end
  212. end
  213. describe "reviewing HITs" do
  214. class FakeHit
  215. def initialize(options = {})
  216. @options = options
  217. end
  218. def assignments
  219. @options[:assignments] || []
  220. end
  221. def max_assignments
  222. @options[:max_assignments] || 1
  223. end
  224. def dispose!
  225. @disposed = true
  226. end
  227. def disposed?
  228. @disposed
  229. end
  230. end
  231. class FakeAssignment
  232. attr_accessor :approved
  233. def initialize(options = {})
  234. @options = options
  235. end
  236. def answers
  237. @options[:answers] || {}
  238. end
  239. def status
  240. @options[:status] || ""
  241. end
  242. def approve!
  243. @approved = true
  244. end
  245. end
  246. it "should work on multiple HITs" do
  247. event2 = Event.new
  248. event2.agent = agents(:bob_rain_notifier_agent)
  249. event2.payload = { 'foo2' => { "bar2" => { 'baz2' => "a2b2" } },
  250. 'name2' => "Joe2" }
  251. event2.id = 3452
  252. # It knows about two HITs from two different events.
  253. @checker.memory['hits'] = {}
  254. @checker.memory['hits']["JH3132836336DHG"] = { 'event_id' => @event.id }
  255. @checker.memory['hits']["JH39AA63836DHG"] = { 'event_id' => event2.id }
  256. hit_ids = %w[JH3132836336DHG JH39AA63836DHG JH39AA63836DH12345]
  257. mock(RTurk::GetReviewableHITs).create { mock!.hit_ids { hit_ids } } # It sees 3 HITs.
  258. # It looksup the two HITs that it owns. Neither are ready yet.
  259. mock(RTurk::Hit).new("JH3132836336DHG") { FakeHit.new }
  260. mock(RTurk::Hit).new("JH39AA63836DHG") { FakeHit.new }
  261. @checker.send :review_hits
  262. end
  263. it "shouldn't do anything if an assignment isn't ready" do
  264. @checker.memory['hits'] = { "JH3132836336DHG" => { 'event_id' => @event.id } }
  265. mock(RTurk::GetReviewableHITs).create { mock!.hit_ids { %w[JH3132836336DHG JH39AA63836DHG JH39AA63836DH12345] } }
  266. assignments = [
  267. FakeAssignment.new(:status => "Accepted", :answers => {}),
  268. FakeAssignment.new(:status => "Submitted", :answers => {"sentiment"=>"happy", "feedback"=>"Take 2"})
  269. ]
  270. hit = FakeHit.new(:max_assignments => 2, :assignments => assignments)
  271. mock(RTurk::Hit).new("JH3132836336DHG") { hit }
  272. # One of the assignments isn't set to "Submitted", so this should get skipped for now.
  273. mock.any_instance_of(FakeAssignment).answers.times(0)
  274. @checker.send :review_hits
  275. assignments.all? {|a| a.approved == true }.should be_falsey
  276. @checker.memory['hits'].should == { "JH3132836336DHG" => { 'event_id' => @event.id } }
  277. end
  278. it "shouldn't do anything if an assignment is missing" do
  279. @checker.memory['hits'] = { "JH3132836336DHG" => { 'event_id' => @event.id } }
  280. mock(RTurk::GetReviewableHITs).create { mock!.hit_ids { %w[JH3132836336DHG JH39AA63836DHG JH39AA63836DH12345] } }
  281. assignments = [
  282. FakeAssignment.new(:status => "Submitted", :answers => {"sentiment"=>"happy", "feedback"=>"Take 2"})
  283. ]
  284. hit = FakeHit.new(:max_assignments => 2, :assignments => assignments)
  285. mock(RTurk::Hit).new("JH3132836336DHG") { hit }
  286. # One of the assignments hasn't shown up yet, so this should get skipped for now.
  287. mock.any_instance_of(FakeAssignment).answers.times(0)
  288. @checker.send :review_hits
  289. assignments.all? {|a| a.approved == true }.should be_falsey
  290. @checker.memory['hits'].should == { "JH3132836336DHG" => { 'event_id' => @event.id } }
  291. end
  292. it "should create events when all assignments are ready" do
  293. @checker.memory['hits'] = { "JH3132836336DHG" => { 'event_id' => @event.id } }
  294. mock(RTurk::GetReviewableHITs).create { mock!.hit_ids { %w[JH3132836336DHG JH39AA63836DHG JH39AA63836DH12345] } }
  295. assignments = [
  296. FakeAssignment.new(:status => "Submitted", :answers => {"sentiment"=>"neutral", "feedback"=>""}),
  297. FakeAssignment.new(:status => "Submitted", :answers => {"sentiment"=>"happy", "feedback"=>"Take 2"})
  298. ]
  299. hit = FakeHit.new(:max_assignments => 2, :assignments => assignments)
  300. hit.should_not be_disposed
  301. mock(RTurk::Hit).new("JH3132836336DHG") { hit }
  302. lambda {
  303. @checker.send :review_hits
  304. }.should change { Event.count }.by(1)
  305. assignments.all? {|a| a.approved == true }.should be_truthy
  306. hit.should be_disposed
  307. @checker.events.last.payload['answers'].should == [
  308. {'sentiment' => "neutral", 'feedback' => ""},
  309. {'sentiment' => "happy", 'feedback' => "Take 2"}
  310. ]
  311. @checker.memory['hits'].should == {}
  312. end
  313. describe "taking majority votes" do
  314. before do
  315. @checker.options['combination_mode'] = "take_majority"
  316. @checker.memory['hits'] = { "JH3132836336DHG" => { 'event_id' => @event.id } }
  317. mock(RTurk::GetReviewableHITs).create { mock!.hit_ids { %w[JH3132836336DHG JH39AA63836DHG JH39AA63836DH12345] } }
  318. end
  319. it "should take the majority votes of all questions" do
  320. @checker.options['hit']['questions'][1] = {
  321. 'type' => "selection",
  322. 'key' => "age_range",
  323. 'name' => "Age Range",
  324. 'required' => "true",
  325. 'question' => "Please select your age range:",
  326. 'selections' =>
  327. [
  328. { 'key' => "<50", 'text' => "50 years old or younger" },
  329. { 'key' => ">50", 'text' => "Over 50 years old" }
  330. ]
  331. }
  332. assignments = [
  333. FakeAssignment.new(:status => "Submitted", :answers => {"sentiment"=>"sad", "age_range"=>"<50"}),
  334. FakeAssignment.new(:status => "Submitted", :answers => {"sentiment"=>"neutral", "age_range"=>">50"}),
  335. FakeAssignment.new(:status => "Submitted", :answers => {"sentiment"=>"happy", "age_range"=>">50"}),
  336. FakeAssignment.new(:status => "Submitted", :answers => {"sentiment"=>"happy", "age_range"=>">50"})
  337. ]
  338. hit = FakeHit.new(:max_assignments => 4, :assignments => assignments)
  339. mock(RTurk::Hit).new("JH3132836336DHG") { hit }
  340. lambda {
  341. @checker.send :review_hits
  342. }.should change { Event.count }.by(1)
  343. assignments.all? {|a| a.approved == true }.should be_truthy
  344. @checker.events.last.payload['answers'].should == [
  345. { 'sentiment' => "sad", 'age_range' => "<50" },
  346. { 'sentiment' => "neutral", 'age_range' => ">50" },
  347. { 'sentiment' => "happy", 'age_range' => ">50" },
  348. { 'sentiment' => "happy", 'age_range' => ">50" }
  349. ]
  350. @checker.events.last.payload['counts'].should == { 'sentiment' => { 'happy' => 2, 'sad' => 1, 'neutral' => 1 }, 'age_range' => { ">50" => 3, "<50" => 1 } }
  351. @checker.events.last.payload['majority_answer'].should == { 'sentiment' => "happy", 'age_range' => ">50" }
  352. @checker.events.last.payload.should_not have_key('average_answer')
  353. @checker.memory['hits'].should == {}
  354. end
  355. it "should also provide an average answer when all questions are numeric" do
  356. # it should accept 'take_majority': 'true' as well for legacy support. Demonstrating that here.
  357. @checker.options.delete :combination_mode
  358. @checker.options['take_majority'] = "true"
  359. @checker.options['hit']['questions'] = [
  360. {
  361. 'type' => "selection",
  362. 'key' => "rating",
  363. 'name' => "Rating",
  364. 'required' => "true",
  365. 'question' => "Please select a rating:",
  366. 'selections' =>
  367. [
  368. { 'key' => "1", 'text' => "One" },
  369. { 'key' => "2", 'text' => "Two" },
  370. { 'key' => "3", 'text' => "Three" },
  371. { 'key' => "4", 'text' => "Four" },
  372. { 'key' => "5.1", 'text' => "Five Point One" }
  373. ]
  374. }
  375. ]
  376. assignments = [
  377. FakeAssignment.new(:status => "Submitted", :answers => { "rating"=>"1" }),
  378. FakeAssignment.new(:status => "Submitted", :answers => { "rating"=>"3" }),
  379. FakeAssignment.new(:status => "Submitted", :answers => { "rating"=>"5.1" }),
  380. FakeAssignment.new(:status => "Submitted", :answers => { "rating"=>"2" }),
  381. FakeAssignment.new(:status => "Submitted", :answers => { "rating"=>"2" })
  382. ]
  383. hit = FakeHit.new(:max_assignments => 5, :assignments => assignments)
  384. mock(RTurk::Hit).new("JH3132836336DHG") { hit }
  385. lambda {
  386. @checker.send :review_hits
  387. }.should change { Event.count }.by(1)
  388. assignments.all? {|a| a.approved == true }.should be_truthy
  389. @checker.events.last.payload['answers'].should == [
  390. { 'rating' => "1" },
  391. { 'rating' => "3" },
  392. { 'rating' => "5.1" },
  393. { 'rating' => "2" },
  394. { 'rating' => "2" }
  395. ]
  396. @checker.events.last.payload['counts'].should == { 'rating' => { "1" => 1, "2" => 2, "3" => 1, "4" => 0, "5.1" => 1 } }
  397. @checker.events.last.payload['majority_answer'].should == { 'rating' => "2" }
  398. @checker.events.last.payload['average_answer'].should == { 'rating' => (1 + 2 + 2 + 3 + 5.1) / 5.0 }
  399. @checker.memory['hits'].should == {}
  400. end
  401. end
  402. describe "creating and reviewing polls" do
  403. before do
  404. @checker.options['combination_mode'] = "poll"
  405. @checker.options['poll_options'] = {
  406. 'title' => "Hi!",
  407. 'instructions' => "hello!",
  408. 'assignments' => 2,
  409. 'row_template' => "This is {{sentiment}}"
  410. }
  411. @event.save!
  412. mock(RTurk::GetReviewableHITs).create { mock!.hit_ids { %w[JH3132836336DHG JH39AA63836DHG JH39AA63836DH12345] } }
  413. end
  414. it "creates a poll using the row_template, message, and correct number of assignments" do
  415. @checker.memory['hits'] = { "JH3132836336DHG" => { 'event_id' => @event.id } }
  416. # Mock out the HIT's submitted assignments.
  417. assignments = [
  418. FakeAssignment.new(:status => "Submitted", :answers => {"sentiment"=>"sad", "feedback"=>"This is my feedback 1"}),
  419. FakeAssignment.new(:status => "Submitted", :answers => {"sentiment"=>"neutral", "feedback"=>"This is my feedback 2"}),
  420. FakeAssignment.new(:status => "Submitted", :answers => {"sentiment"=>"happy", "feedback"=>"This is my feedback 3"}),
  421. FakeAssignment.new(:status => "Submitted", :answers => {"sentiment"=>"happy", "feedback"=>"This is my feedback 4"})
  422. ]
  423. hit = FakeHit.new(:max_assignments => 4, :assignments => assignments)
  424. mock(RTurk::Hit).new("JH3132836336DHG") { hit }
  425. @checker.memory['hits']["JH3132836336DHG"].should be_present
  426. # Setup mocks for HIT creation
  427. question_form = nil
  428. hitInterface = OpenStruct.new
  429. hitInterface.id = "JH39AA63836DH12345"
  430. mock(hitInterface).question_form(instance_of Agents::HumanTaskAgent::AgentQuestionForm) { |agent_question_form_instance| question_form = agent_question_form_instance }
  431. mock(RTurk::Hit).create(:title => "Hi!").yields(hitInterface) { hitInterface }
  432. # And finally, the test.
  433. lambda {
  434. @checker.send :review_hits
  435. }.should change { Event.count }.by(0) # it does not emit an event until all poll results are in
  436. # it approves the existing assignments
  437. assignments.all? {|a| a.approved == true }.should be_truthy
  438. hit.should be_disposed
  439. # it creates a new HIT for the poll
  440. hitInterface.max_assignments.should == @checker.options['poll_options']['assignments']
  441. hitInterface.description.should == @checker.options['poll_options']['instructions']
  442. xml = question_form.to_xml
  443. xml.should include("<Text>This is happy</Text>")
  444. xml.should include("<Text>This is neutral</Text>")
  445. xml.should include("<Text>This is sad</Text>")
  446. @checker.save
  447. @checker.reload
  448. @checker.memory['hits']["JH3132836336DHG"].should_not be_present
  449. @checker.memory['hits']["JH39AA63836DH12345"].should be_present
  450. @checker.memory['hits']["JH39AA63836DH12345"]['event_id'].should == @event.id
  451. @checker.memory['hits']["JH39AA63836DH12345"]['type'].should == "poll"
  452. @checker.memory['hits']["JH39AA63836DH12345"]['original_hit'].should == "JH3132836336DHG"
  453. @checker.memory['hits']["JH39AA63836DH12345"]['answers'].length.should == 4
  454. end
  455. it "emits an event when all poll results are in, containing the data from the best answer, plus all others" do
  456. original_answers = [
  457. { 'sentiment' => "sad", 'feedback' => "This is my feedback 1"},
  458. { 'sentiment' => "neutral", 'feedback' => "This is my feedback 2"},
  459. { 'sentiment' => "happy", 'feedback' => "This is my feedback 3"},
  460. { 'sentiment' => "happy", 'feedback' => "This is my feedback 4"}
  461. ]
  462. @checker.memory['hits'] = {
  463. 'JH39AA63836DH12345' => {
  464. 'type' => 'poll',
  465. 'original_hit' => "JH3132836336DHG",
  466. 'answers' => original_answers,
  467. 'event_id' => 345
  468. }
  469. }
  470. # Mock out the HIT's submitted assignments.
  471. assignments = [
  472. FakeAssignment.new(:status => "Submitted", :answers => {"1" => "2", "2" => "5", "3" => "3", "4" => "2"}),
  473. FakeAssignment.new(:status => "Submitted", :answers => {"1" => "3", "2" => "4", "3" => "1", "4" => "4"})
  474. ]
  475. hit = FakeHit.new(:max_assignments => 2, :assignments => assignments)
  476. mock(RTurk::Hit).new("JH39AA63836DH12345") { hit }
  477. @checker.memory['hits']["JH39AA63836DH12345"].should be_present
  478. lambda {
  479. @checker.send :review_hits
  480. }.should change { Event.count }.by(1)
  481. # It emits an event
  482. @checker.events.last.payload['answers'].should == original_answers
  483. @checker.events.last.payload['poll'].should == [{"1" => "2", "2" => "5", "3" => "3", "4" => "2"}, {"1" => "3", "2" => "4", "3" => "1", "4" => "4"}]
  484. @checker.events.last.payload['best_answer'].should == {'sentiment' => "neutral", 'feedback' => "This is my feedback 2"}
  485. # it approves the existing assignments
  486. assignments.all? {|a| a.approved == true }.should be_truthy
  487. hit.should be_disposed
  488. @checker.memory['hits'].should be_empty
  489. end
  490. end
  491. end
  492. end