spec/mongoid/fulltext_spec.rb in mongoid_fulltext-0.5.7 vs spec/mongoid/fulltext_spec.rb in mongoid_fulltext-0.5.8

- old
+ new

@@ -27,20 +27,32 @@ let!(:flower_myth) { BasicArtwork.create(:title => 'Flower Myth') } let!(:flowers) { BasicArtwork.create(:title => 'Flowers') } let!(:lowered) { BasicArtwork.create(:title => 'Lowered') } let!(:cookies) { BasicArtwork.create(:title => 'Cookies') } let!(:empty) { BasicArtwork.create(:title => '') } - let!(:cesar) { BasicArtwork.create(:title => "C\u00e9sar Galicia") } - let!(:julio) { BasicArtwork.create(:title => "Julio Cesar Morales") } - let!(:csar) { BasicArtwork.create(:title => "Csar") } - + let!(:cesar) { BasicArtwork.create(:title => "C\u00e9sar Galicia") } + let!(:julio) { BasicArtwork.create(:title => "Julio Cesar Morales") } + let!(:csar) { BasicArtwork.create(:title => "Csar") } + let!(:percent) { BasicArtwork.create(:title => "Untitled (cal%desert)") } + + it "returns empty for empties" do + BasicArtwork.fulltext_search(nil, :max_results => 1).should == [] + BasicArtwork.fulltext_search("", :max_results => 1).should == [] + end + + it "finds percents" do + BasicArtwork.fulltext_search("cal%desert".force_encoding("ASCII-8BIT"), :max_results => 1).first.should == percent + BasicArtwork.fulltext_search("cal%desert".force_encoding("UTF-8"), :max_results => 1).first.should == percent + end + it "forgets accents" do BasicArtwork.fulltext_search('cesar', :max_results => 1).first.should == cesar BasicArtwork.fulltext_search('cesar g', :max_results => 1).first.should == cesar BasicArtwork.fulltext_search("C\u00e9sar", :max_results => 1).first.should == cesar - BasicArtwork.fulltext_search("C\303\251sar", :max_results => 1).first.should == cesar - BasicArtwork.fulltext_search("c%C3%A9sar".encode("ASCII-8BIT"), :max_results => 1).first.should == cesar + BasicArtwork.fulltext_search("C\303\251sar".force_encoding("UTF-8"), :max_results => 1).first.should == cesar + BasicArtwork.fulltext_search(CGI.unescape("c%C3%A9sar"), :max_results => 1).first.should == cesar + BasicArtwork.fulltext_search(CGI.unescape("c%C3%A9sar".encode("ASCII-8BIT")), :max_results => 1).first.should == cesar end it "returns exact matches" do BasicArtwork.fulltext_search('Flower Myth', :max_results => 1).first.should == flower_myth BasicArtwork.fulltext_search('Flowers', :max_results => 1).first.should == flowers @@ -368,13 +380,14 @@ index_collection = FilteredArtwork.collection.db.collection('mongoid_fulltext.artworks_and_artists') ngram_indexes = index_collection.index_information.find_all{ |name, definition| definition['key'].has_key?('ngram') } ngram_indexes.length.should == 1 keys = ngram_indexes.first[1]['key'].keys expected_keys = ['ngram','score', 'filter_values.is_fuzzy', 'filter_values.is_awesome', - 'filter_values.is_foobar', 'filter_values.is_artwork', 'filter_values.is_artist'].sort + 'filter_values.is_foobar', 'filter_values.is_artwork', 'filter_values.is_artist', 'filter_values.colors?'].sort keys.sort.should == expected_keys end + end context "with partitions applied to a model" do let!(:artist_2) { PartitionedArtist.create(:full_name => 'foobar', :exhibitions => [ "Art Basel 2011", "Armory NY" ]) } @@ -637,36 +650,22 @@ it "can re-create dropped indexes" do # there're no indexes by default as Mongoid.autocreate_indexes is set to false # but mongo will automatically attempt to index _id in the background Mongoid.master["mongoid_fulltext.index_basicartwork_0"].index_information.size.should <= 1 BasicArtwork.create_indexes - Mongoid.master["mongoid_fulltext.index_basicartwork_0"].index_information.should == - { - "_id_" => { - "name" => "_id_", - "ns" => "mongoid_fulltext_test.mongoid_fulltext.index_basicartwork_0", - "key" => { "_id" => 1 }, - "v" => 0 - }, - "fts_index" => { - "name" => "fts_index", - "ns" => "mongoid_fulltext_test.mongoid_fulltext.index_basicartwork_0", - "key" => { "ngram" => 1, "score" => -1 }, - "v" => 0 - }, - "document_id_1" => { - "name" => "document_id_1", - "ns" => "mongoid_fulltext_test.mongoid_fulltext.index_basicartwork_0", - "key" => { "document_id" => 1 }, - "v"=>0 - } - } + expected_indexes = ['_id_', 'fts_index', 'document_id_1'].sort + Mongoid.master["mongoid_fulltext.index_basicartwork_0"].index_information.keys.sort.should == expected_indexes end it "doesn't fail on models that don't have a fulltext index" do lambda { HiddenDragon.create_indexes }.should_not raise_error end + + it "doesn't blow up when the Mongoid.logger is set to false" do + Mongoid.logger = false + BasicArtwork.create_indexes + end end end @@ -675,9 +674,104 @@ it "should not rebuild index until explicitly invoked" do DelayedArtwork.fulltext_search("flowers").length.should == 0 DelayedArtwork.update_ngram_index DelayedArtwork.fulltext_search("flowers").length.should == 1 + end + end + + # For =~ operator documentation + # https://github.com/dchelimsky/rspec/blob/master/lib/spec/matchers/match_array.rb#L53 + + context "with artwork that returns an array of colors as a filter" do + let!(:title) {"title"} + let!(:nomatch) {"nomatch"} + let!(:red) {"red"} + let!(:green) {"green"} + let!(:blue) {"blue"} + let!(:yellow) {"yellow"} + let!(:brown) {"brown"} + + let!(:rgb_artwork) {FilteredArtwork.create(:title => "#{title} rgb", :colors => [red,green,blue])} + let!(:holiday_artwork) {FilteredArtwork.create(:title => "#{title} holiday", :colors => [red,green])} + let!(:aqua_artwork) {FilteredArtwork.create(:title => "#{title} aqua", :colors => [green,blue])} + + context "with a fulltext search passing red, green, and blue to the colors filter" do + it "should return the rgb artwork" do + FilteredArtwork.fulltext_search(title, :colors? => [red,green,blue]).should == [rgb_artwork] + end + end + + context "with a fulltext search passing blue and red to the colors filter" do + it "should return the rgb artwork" do + FilteredArtwork.fulltext_search(title, :colors? => [blue,red]).should == [rgb_artwork] + end + end + + context "with a fulltext search passing green to the colors filter" do + it "should return all artwork" do + FilteredArtwork.fulltext_search(title, :colors? => [green]).should =~ [rgb_artwork,holiday_artwork,aqua_artwork] + end + end + + context "with a fulltext search passing no colors to the filter" do + it "should return all artwork" do + FilteredArtwork.fulltext_search(title).should =~ [rgb_artwork,holiday_artwork,aqua_artwork] + end + end + + context "with a fulltext search passing green and yellow to the colors filter" do + it "should return no artwork" do + FilteredArtwork.fulltext_search(title, :colors? => [green,yellow]).should == [] + end + end + + context "with the query operator overridden to use $in instead of the default $all" do + context "with a fulltext search passing green and yellow to the colors filter" do + it "should return all of the artwork" do + FilteredArtwork.fulltext_search(title, :colors? => {:any => [green,yellow]}).should =~ [rgb_artwork,holiday_artwork,aqua_artwork] + end + end + + context "with a fulltext search passing brown and yellow to the colors filter" do + it "should return none of the artwork" do + FilteredArtwork.fulltext_search(title, :colors? => {:any => [brown,yellow]}).should == [] + end + end + + context "with a fulltext search passing blue to the colors filter" do + it "should return the rgb and aqua artwork" do + FilteredArtwork.fulltext_search(title, :colors? => {:any => [blue]}).should == [rgb_artwork,aqua_artwork] + end + end + + context "with a fulltext search term that won't match" do + it "should return none of the artwork" do + FilteredArtwork.fulltext_search(nomatch, :colors? => {:any => [green,yellow]}).should == [] + end + end + end + + context "with the query operator overridden to use $all" do + context "with a fulltext search passing red, green, and blue to the colors filter" do + it "should return the rgb artwork" do + FilteredArtwork.fulltext_search(title, :colors? => {:all => [red,green,blue]}).should == [rgb_artwork] + end + end + + context "with a fulltext search passing green to the colors filter" do + it "should return all artwork" do + FilteredArtwork.fulltext_search(title, :colors? => {:all => [green]}).should =~ [rgb_artwork,holiday_artwork,aqua_artwork] + end + end + end + + context "with an unknown query operator used to override the default $all" do + context "with a fulltext search passing red, green, and blue to the colors filter" do + it "should raise an error" do + -> {FilteredArtwork.fulltext_search(title, :colors? => {:unknown => [red,green,blue]})}.should raise_error(Mongoid::FullTextSearch::UnknownFilterQueryOperator) + end + end end end end end