Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.52 KB | None | 0 0
  1. require "spec_helper"
  2.  
  3. describe GridQuery::TileActions do
  4.   let!(:demo) { FactoryGirl.create :demo }
  5.   let!(:other_demo) { FactoryGirl.create :demo }
  6.   let!(:tile) { FactoryGirl.create :tile, demo: demo, multiple_choice_answers: ["Ham", "Eggs", "A V8 Buick"] }
  7.   let!(:other_tile) { FactoryGirl.create :tile, demo: demo, multiple_choice_answers: ["Good", "Bad", "Ugly"] }
  8.  
  9.   def user_actions user, tile, views = 1, interacted = false, answer_index = nil
  10.     FactoryGirl.create :tile_viewing, user: user, tile: tile, views: views
  11.     if interacted
  12.       FactoryGirl.create :tile_completion, user: user, tile: tile, answer_index: answer_index
  13.     end
  14.   end
  15.  
  16.   def create_users num, demo, name
  17.     (0..num).to_a.map do |i|
  18.       FactoryGirl.create :user, name: "#{name.humanize}#{i}", email: "#{name}#{i}@gmail.com", demo: demo
  19.     end
  20.   end
  21.  
  22.   def make_table query
  23.     query.map(&:attributes).map do |row|
  24.       row.delete("completion_date")
  25.       row.delete("user_id")
  26.       row
  27.     end.map(&:values)
  28.   end
  29.  
  30.   before do
  31.     @users = create_users 8, demo, "good_guy"
  32.     @other_users = create_users 2, other_demo, "other_guy"
  33.     # viewed and interacted
  34.     [0,1,2].each do |i|
  35.       user_actions @users[i], tile, i+1, true, i
  36.     end
  37.     user_actions @other_users[0], other_tile, 2, true, 0
  38.     # viewed only
  39.     [3,4,5].each do |i|
  40.       user_actions @users[i], tile, i
  41.     end
  42.     # didn't view
  43.     # users 6 7 8
  44.   end
  45.  
  46.   it "should return 'all'" do
  47.     # result is set of rows(arrays) with columns:
  48.     # user_name | user_email | tile_views | tile_answer_index
  49.     table = make_table(GridQuery::TileActions.new(tile, "all").query.order("users.id ASC"))
  50.     table.should == [
  51.       ["Good guy0", "good_guy0@gmail.com", "1", "0"],
  52.       ["Good guy1", "good_guy1@gmail.com", "2", "1"],
  53.       ["Good guy2", "good_guy2@gmail.com", "3", "2"],
  54.       ["Good guy3", "good_guy3@gmail.com", "3", nil],
  55.       ["Good guy4", "good_guy4@gmail.com", "4", nil],
  56.       ["Good guy5", "good_guy5@gmail.com", "5", nil],
  57.       ["Good guy6", "good_guy6@gmail.com", nil, nil],
  58.       ["Good guy7", "good_guy7@gmail.com", nil, nil],
  59.       ["Good guy8", "good_guy8@gmail.com", nil, nil]
  60.     ]
  61.   end
  62.  
  63.   it "should return 'viewed only'" do
  64.     # result is set of rows(arrays) with columns:
  65.     # user_name | user_email | tile_views | tile_answer_index
  66.     table = make_table(GridQuery::TileActions.new(tile, "viewed_only").query.order("users.id ASC"))
  67.     table.should == [
  68.       ["Good guy3", "good_guy3@gmail.com", "3", nil],
  69.       ["Good guy4", "good_guy4@gmail.com", "4", nil],
  70.       ["Good guy5", "good_guy5@gmail.com", "5", nil]
  71.     ]
  72.   end
  73.  
  74.   it "should return 'not_viewed'" do
  75.     # result is set of rows(arrays) with columns:
  76.     # user_name | user_email | tile_views | tile_answer_index
  77.     table = make_table(GridQuery::TileActions.new(tile, "not_viewed").query.order("users.id ASC"))
  78.     table.should == [
  79.       ["Good guy6", "good_guy6@gmail.com", nil, nil],
  80.       ["Good guy7", "good_guy7@gmail.com", nil, nil],
  81.       ["Good guy8", "good_guy8@gmail.com", nil, nil]
  82.     ]
  83.   end
  84.  
  85.   it "should return 'interacted'" do
  86.     # result is set of rows(arrays) with columns:
  87.     # user_name | user_email | tile_views | tile_answer_index
  88.     table = make_table(GridQuery::TileActions.new(tile, "interacted").query.order("users.id ASC"))
  89.     table.should == [
  90.       ["Good guy0", "good_guy0@gmail.com", "1", "0"],
  91.       ["Good guy1", "good_guy1@gmail.com", "2", "1"],
  92.       ["Good guy2", "good_guy2@gmail.com", "3", "2"]
  93.     ]
  94.   end
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement