check_thread_result

in spec/spec_helper.rb [160:228]


def check_thread_result(user, thread, hash, is_json=false)
  expected_keys = %w(id thread_type title body course_id commentable_id created_at updated_at context)
  expected_keys += %w(anonymous anonymous_to_peers at_position_list closed user_id)
  expected_keys += %w(username votes abuse_flaggers tags type group_id pinned)
  expected_keys += %w(comments_count unread_comments_count read endorsed last_activity_at)
  
  actual_keys = hash.keys - [
      "children", "endorsed_responses", "non_endorsed_responses", "resp_skip",
      "resp_limit", "resp_total", "non_endorsed_resp_total"
  ]
  actual_keys.sort.should == expected_keys.sort

  hash["title"].should == thread.title
  hash["body"].should == thread.body
  hash["course_id"].should == thread.course_id
  hash["anonymous"].should == thread.anonymous
  hash["anonymous_to_peers"].should == thread.anonymous_to_peers
  hash["commentable_id"].should == thread.commentable_id
  hash["at_position_list"].should == thread.at_position_list
  hash["closed"].should == thread.closed
  hash["user_id"].should == thread.author.id
  hash["username"].should == thread.author.username
  hash["votes"]["point"].should == thread.votes["point"]
  hash["votes"]["count"].should == thread.votes["count"]
  hash["votes"]["up_count"].should == thread.votes["up_count"]
  hash["votes"]["down_count"].should == thread.votes["down_count"]
  hash["abuse_flaggers"].should == thread.abuse_flaggers
  hash["tags"].should == []
  hash["type"].should == "thread"
  hash["group_id"].should == thread.group_id
  hash["pinned"].should == thread.pinned?
  hash["endorsed"].should == thread.endorsed?
  hash["comments_count"].should == thread.comments.length
  hash["context"] = thread.context

  if is_json
    hash["id"].should == thread._id.to_s
    hash["created_at"].should == thread.created_at.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
    hash["updated_at"].should == thread.updated_at.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
    hash["last_activity_at"].should == thread.last_activity_at.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
  else
    hash["created_at"].should == thread.created_at
    hash["updated_at"].should == thread.updated_at
    hash["last_activity_at"].should == thread.last_activity_at
  end

  if user.nil?
    hash["unread_comments_count"].should == thread.comments.length
    hash["read"].should == false
  else
    expected_unread_cnt = thread.comments.length 
    read_states = user.read_states.where(course_id: thread.course_id).to_a
    if read_states.length == 1
      read_date = read_states.first.last_read_times[thread.id.to_s]
      if read_date
        thread.comments.each do |c|
          if c.created_at < read_date
            expected_unread_cnt -= 1
          end
        end
        hash["read"].should == (read_date >= thread.last_activity_at)
      else
        hash["read"].should == false
      end
    end
    hash["unread_comments_count"].should == expected_unread_cnt
  end
end