require 'spec_helper' describe BrickLayer::Search do before(:each) do BrickLayer::DataSet.destroy_all end context "PARAMS to QUERY HASH" do it "should convert a params hash to a usable mongoid query hash" do query = BrickLayer::Search.params_to_query({:controller => "something", :action => "something", :title_in => "Title"}) query.expand_complex_criteria.should == {:title=>{"$in"=>["Title"]}} end it "should create query using the correct value by type of search" do describe "ARRAYS" do query = BrickLayer::Search.params_to_query({:title_all => "Title"}) query.expand_complex_criteria.should == {:title=>{"$all"=>["Title"]}} query = BrickLayer::Search.params_to_query({:title_in => "Title"}) query.expand_complex_criteria.should == {:title=>{"$in"=>["Title"]}} query = BrickLayer::Search.params_to_query({:title_nin => "Title"}) query.expand_complex_criteria.should == {:title=>{"$nin"=>["Title"]}} query = BrickLayer::Search.params_to_query({:title_nin => "['Another Name','My Name','Maybe']"}) query.expand_complex_criteria.should == {:title=>{"$nin"=>['Another Name','My Name','Maybe']}} end describe "INTEGERS" do query = BrickLayer::Search.params_to_query({:title_gt => "5"}) query.expand_complex_criteria.should == {:title=>{"$gt"=>5}} query = BrickLayer::Search.params_to_query({:title_gte => "5"}) query.expand_complex_criteria.should == {:title=>{"$gte"=>5}} query = BrickLayer::Search.params_to_query({:title_lt => "10"}) query.expand_complex_criteria.should == {:title=>{"$lt"=>10}} query = BrickLayer::Search.params_to_query({:title_lte => "12"}) query.expand_complex_criteria.should == {:title=>{"$lte"=>12}} query = BrickLayer::Search.params_to_query({:title_size => "2"}) query.expand_complex_criteria.should == {:title=>{"$size"=>2}} end describe "BOOLEANS" do query = BrickLayer::Search.params_to_query({:title_exists => "true"}) query.expand_complex_criteria.should == {:title=>{"$exists"=>true}} end describe "STRINGS" do query = BrickLayer::Search.params_to_query({:title_ne => "Title"}) query.expand_complex_criteria.should == {:title=>{"$ne"=>"Title"}} end end end context "searching ALL data sets" do before(:each) do @employee_1 = Employee.create(:title => "Some Title 1", :meta_description => "Some Description") @employee_2 = Employee.create(:title => "Some Title 2", :meta_description => "Some Description") @one_off_set = OneOffSet.create(:title => "Some Title 3", :meta_description => "Some Description") end it "should allow a STANDARD query" do results = BrickLayer::Search.perform_search({:meta_description_in => ['Some Description']}) results.count.should == 3 end it "should allow FULL TEXT search" do results = BrickLayer::Search.perform_search({:q => 'Title 2'}) results.first.should == @employee_2 end it "should allow QUERY CHAINING" do results = BrickLayer::Search.perform_search(:queries => { :meta_description_in => ['Some Description'], :title_in => "Some Title 3" }) results.first.should == @one_off_set end end context "searching SPECIFIC data sets" do before(:each) do @employee = Employee.create(:title => "Title", :meta_description => "Some description") @employee2 = Employee.create(:title => "My Employee 2", :meta_description => "Some Meta Description") @one_off_set = OneOffSet.create(:title => "Set Title", :meta_description => "Some description") end it "should allow a STANDARD query" do results = BrickLayer::Search.perform_search({:meta_description_in => 'Some Meta Description', :model => "employees"}) results.count.should == 1 end it "should allow FULL TEXT search" do results = BrickLayer::Search.perform_search({:q => 'Title', :model => "employee"}) results.count.should == 1 results.first.should == @employee end end end