2013-02-06 03:16:51 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'search'
|
|
|
|
|
|
|
|
describe Search do
|
|
|
|
|
|
|
|
def first_of_type(results, type)
|
|
|
|
return nil if results.blank?
|
|
|
|
results.each do |r|
|
|
|
|
return r[:results].first if r[:type] == type
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'post indexing observer' do
|
|
|
|
before do
|
2013-02-06 03:16:51 +08:00
|
|
|
@category = Fabricate(:category, name: 'america')
|
2013-02-07 09:09:31 +08:00
|
|
|
@topic = Fabricate(:topic, title: 'sam test topic', category: @category)
|
2013-02-06 03:16:51 +08:00
|
|
|
@post = Fabricate(:post, topic: @topic, raw: 'this <b>fun test</b> <img src="bla" title="my image">')
|
|
|
|
@indexed = Topic.exec_sql("select search_data from posts_search where id = #{@post.id}").first["search_data"]
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should include body in index" do
|
2013-02-06 03:16:51 +08:00
|
|
|
@indexed.should =~ /fun/
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should include title in index" do
|
2013-02-06 03:16:51 +08:00
|
|
|
@indexed.should =~ /sam/
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should include category in index" do
|
2013-02-06 03:16:51 +08:00
|
|
|
@indexed.should =~ /america/
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should pick up on title updates" do
|
2013-02-07 09:09:31 +08:00
|
|
|
@topic.title = "harpi is the new title"
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.save!
|
|
|
|
@indexed = Topic.exec_sql("select search_data from posts_search where id = #{@post.id}").first["search_data"]
|
|
|
|
|
|
|
|
@indexed.should =~ /harpi/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'user indexing observer' do
|
|
|
|
before do
|
2013-02-06 03:16:51 +08:00
|
|
|
@user = Fabricate(:user, username: 'fred', name: 'bob jones')
|
|
|
|
@indexed = User.exec_sql("select search_data from users_search where id = #{@user.id}").first["search_data"]
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should pick up on username" do
|
2013-02-06 03:16:51 +08:00
|
|
|
@indexed.should =~ /fred/
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should pick up on name" do
|
2013-02-06 03:16:51 +08:00
|
|
|
@indexed.should =~ /jone/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'category indexing observer' do
|
|
|
|
before do
|
2013-02-06 03:16:51 +08:00
|
|
|
@category = Fabricate(:category, name: 'america')
|
|
|
|
@indexed = Topic.exec_sql("select search_data from categories_search where id = #{@category.id}").first["search_data"]
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should pick up on name" do
|
2013-02-06 03:16:51 +08:00
|
|
|
@indexed.should =~ /america/
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns something blank on a nil search' do
|
|
|
|
ActiveRecord::Base.expects(:exec_sql).never
|
|
|
|
Search.query(nil).should be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'escapes non alphanumeric characters' do
|
|
|
|
ActiveRecord::Base.expects(:exec_sql).never
|
|
|
|
Search.query(':!$').should be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'works when given two terms with spaces' do
|
|
|
|
lambda { Search.query('evil trout') }.should_not raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'users' do
|
|
|
|
let!(:user) { Fabricate(:user) }
|
|
|
|
let(:result) { first_of_type(Search.query('bruce'), 'user') }
|
|
|
|
|
|
|
|
it 'returns a result' do
|
|
|
|
result.should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has the display name as the title' do
|
|
|
|
result['title'].should == user.username
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has the avatar_template is there so it can hand it to the client' do
|
|
|
|
result['avatar_template'].should_not be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a url for the record' do
|
|
|
|
result['url'].should == "/users/#{user.username_lower}"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'topics' do
|
|
|
|
let!(:topic) { Fabricate(:topic) }
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'searching the OP' do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
let!(:post) { Fabricate(:post, topic: topic, user: topic.user) }
|
2013-02-06 03:16:51 +08:00
|
|
|
let(:result) { first_of_type(Search.query('hello'), 'topic') }
|
|
|
|
|
|
|
|
it 'returns a result' do
|
|
|
|
result.should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has the topic title' do
|
|
|
|
result['title'].should == topic.title
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a url for the post' do
|
|
|
|
result['url'].should == topic.relative_url
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'categories' do
|
|
|
|
|
|
|
|
let!(:category) { Fabricate(:category) }
|
|
|
|
let(:result) { first_of_type(Search.query('amazing'), 'category') }
|
|
|
|
|
|
|
|
it 'returns a result' do
|
|
|
|
result.should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has the category name' do
|
|
|
|
result['title'].should == category.name
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a url for the topic' do
|
|
|
|
result['url'].should == "/category/#{category.slug}"
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
context 'type_filter' do
|
|
|
|
|
|
|
|
let!(:user) { Fabricate(:user, username: 'amazing', email: 'amazing@amazing.com') }
|
|
|
|
let!(:category) { Fabricate(:category, name: 'amazing category', user: user) }
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
context 'user filter' do
|
|
|
|
let(:results) { Search.query('amazing', 'user') }
|
|
|
|
|
|
|
|
it "returns a user result" do
|
|
|
|
results.detect {|r| r[:type] == 'user'}.should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns no category results" do
|
|
|
|
results.detect {|r| r[:type] == 'category'}.should be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'category filter' do
|
|
|
|
let(:results) { Search.query('amazing', 'category') }
|
|
|
|
|
|
|
|
it "returns a user result" do
|
|
|
|
results.detect {|r| r[:type] == 'user'}.should be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns no category results" do
|
|
|
|
results.detect {|r| r[:type] == 'category'}.should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|