Refactor UserSearch tests

This commit is contained in:
Mike Moore 2013-02-07 06:50:59 -05:00
parent e41b6537f9
commit d72c26ff92
4 changed files with 142 additions and 127 deletions

View file

@ -1,19 +1,19 @@
class UserSearch
def self.search term, topic_id
def self.search term, topic_id = nil
User.find_by_sql sql(term, topic_id)
end
private
def self.sql term, topic_id
sql = "select username, name, email from users u "
sql = "select id, username, name, email from users u "
if topic_id
sql << "left join (select distinct p.user_id from posts p where topic_id = :topic_id) s on
s.user_id = u.id "
end
if term.length > 0
if term.present?
sql << "where username ilike :term_like or
to_tsvector('simple', name) @@
to_tsquery('simple',

View file

@ -1,100 +0,0 @@
require 'spec_helper'
describe UsersController, :search_users do
let(:topic) { Fabricate :topic }
let(:topic2) { Fabricate :topic }
let(:topic3) { Fabricate :topic }
let(:user1) { Fabricate :user, username: "mrblonde", name: "Michael Madsen" }
let(:user2) { Fabricate :user, username: "mrblue", name: "Eddie Bunker" }
let(:user3) { Fabricate :user, username: "mrorange", name: "Tim Roth" }
let(:user4) { Fabricate :user, username: "mrpink", name: "Steve Buscemi" }
let(:user5) { Fabricate :user, username: "mrbrown", name: "Quentin Tarantino" }
let(:user6) { Fabricate :user, username: "mrwhite", name: "Harvey Keitel" }
before do
Fabricate :post, user: user1, topic: topic
Fabricate :post, user: user2, topic: topic2
Fabricate :post, user: user3, topic: topic
Fabricate :post, user: user4, topic: topic
Fabricate :post, user: user5, topic: topic3
Fabricate :post, user: user6, topic: topic
end
context "all user search" do
it "searches the user's name" do
xhr :post, :search_users, term: user1.name.split(" ").first
json = JSON.parse(response.body)
json["users"].size.should == 1
json["users"].first.should == user_json(user1)
end
it "searches the user's name case insensitive" do
xhr :post, :search_users, term: user1.name.split(" ").first.downcase
json = JSON.parse(response.body)
json["users"].size.should == 1
json["users"].first.should == user_json(user1)
end
it "searches the user's username" do
xhr :post, :search_users, term: user4.username
json = JSON.parse(response.body)
json["users"].size.should == 1
json["users"].first.should == user_json(user4)
end
it "searches the user's username case insensitive" do
xhr :post, :search_users, term: user4.username.upcase
json = JSON.parse(response.body)
json["users"].size.should == 1
json["users"].first.should == user_json(user4)
end
it "searches the user's username substring" do
xhr :post, :search_users, term: "mr"
json = JSON.parse(response.body)
json["users"].size.should == 6
xhr :post, :search_users, term: "mrb"
json = JSON.parse(response.body)
json["users"].size.should == 3
end
it "searches the user's username substring upper case" do
xhr :post, :search_users, term: "MR"
json = JSON.parse(response.body)
json["users"].size.should == 6
xhr :post, :search_users, term: "MRB"
json = JSON.parse(response.body)
json["users"].size.should == 3
end
end
context "sort order respects users with posts on the topic" do
it "Mr. Blond is first when searching his topic" do
xhr :post, :search_users, topic_id: topic.id, term: "mrb"
json = JSON.parse(response.body)
json["users"].first.should == user_json(user1)
end
it "Mr. Blue is first when searching his topic" do
xhr :post, :search_users, topic_id: topic2.id, term: "mrb"
json = JSON.parse(response.body)
json["users"].first.should == user_json(user2)
end
it "Mr. Brown is first when searching his topic" do
xhr :post, :search_users, topic_id: topic3.id, term: "mrb"
json = JSON.parse(response.body)
json["users"].first.should == user_json(user5)
end
end
def user_json user
{ "avatar_template" => user.avatar_template,
"name" => user.name,
"username" => user.username }
end
end

View file

@ -671,4 +671,36 @@ describe UsersController do
end
describe "search_users" do
let(:topic) { Fabricate :topic }
let(:user) { Fabricate :user, username: "joecabot", name: "Lawrence Tierney" }
before do
Fabricate :post, user: user, topic: topic
end
it "searches when provided the term only" do
xhr :post, :search_users, term: user.name.split(" ").last
response.should be_success
json = JSON.parse(response.body)
json["users"].map { |u| u["username"] }.should include(user.username)
end
it "searches when provided the topic only" do
xhr :post, :search_users, topic_id: topic.id
response.should be_success
json = JSON.parse(response.body)
json["users"].map { |u| u["username"] }.should include(user.username)
end
it "searches when provided the term and topic" do
xhr :post, :search_users, term: user.name.split(" ").last, topic_id: topic.id
response.should be_success
json = JSON.parse(response.body)
json["users"].map { |u| u["username"] }.should include(user.username)
end
end
end

View file

@ -0,0 +1,83 @@
require 'spec_helper'
describe UserSearch do
let(:topic) { Fabricate :topic }
let(:topic2) { Fabricate :topic }
let(:topic3) { Fabricate :topic }
let(:user1) { Fabricate :user, username: "mrblonde", name: "Michael Madsen" }
let(:user2) { Fabricate :user, username: "mrblue", name: "Eddie Bunker" }
let(:user3) { Fabricate :user, username: "mrorange", name: "Tim Roth" }
let(:user4) { Fabricate :user, username: "mrpink", name: "Steve Buscemi" }
let(:user5) { Fabricate :user, username: "mrbrown", name: "Quentin Tarantino" }
let(:user6) { Fabricate :user, username: "mrwhite", name: "Harvey Keitel" }
before do
Fabricate :post, user: user1, topic: topic
Fabricate :post, user: user2, topic: topic2
Fabricate :post, user: user3, topic: topic
Fabricate :post, user: user4, topic: topic
Fabricate :post, user: user5, topic: topic3
Fabricate :post, user: user6, topic: topic
end
context "all user search" do
it "searches the user's name" do
results = UserSearch.search user1.name.split(" ").first
results.size.should == 1
results.first.should == user1
end
it "searches the user's name case insensitive" do
results = UserSearch.search user1.name.split(" ").first.downcase
results.size.should == 1
results.first.should == user1
end
it "searches the user's username" do
results = UserSearch.search user4.username
results.size.should == 1
results.first.should == user4
end
it "searches the user's username case insensitive" do
results = UserSearch.search user4.username.upcase
results.size.should == 1
results.first.should == user4
end
it "searches the user's username substring" do
results = UserSearch.search "mr"
results.size.should == 6
results = UserSearch.search "mrb"
results.size.should == 3
end
it "searches the user's username substring upper case" do
results = UserSearch.search "MR"
results.size.should == 6
results = UserSearch.search "MRB"
results.size.should == 3
end
end
context "sort order respects users with posts on the topic" do
it "Mr. Blond is first when searching his topic" do
results = UserSearch.search "mrb", topic.id
results.first.should == user1
end
it "Mr. Blue is first when searching his topic" do
results = UserSearch.search "mrb", topic2.id
results.first.should == user2
end
it "Mr. Brown is first when searching his topic" do
results = UserSearch.search "mrb", topic3.id
results.first.should == user5
end
end
end