spec/mongoid/fulltext_spec.rb in mongoid_fulltext-0.3.6 vs spec/mongoid/fulltext_spec.rb in mongoid_fulltext-0.4.0

- old
+ new

@@ -60,10 +60,27 @@ BasicArtwork.fulltext_search('coo').first.should == cookies BasicArtwork.fulltext_search('c!!!oo').first.should == cookies end end + + context "with default settings" do + + let!(:flower_myth) { Gallery::BasicArtwork.create(:title => 'Flower Myth') } + let!(:flowers) { Gallery::BasicArtwork.create(:title => 'Flowers') } + let!(:lowered) { Gallery::BasicArtwork.create(:title => 'Lowered') } + let!(:cookies) { Gallery::BasicArtwork.create(:title => 'Cookies') } + let!(:empty) { Gallery::BasicArtwork.create(:title => '') } + + it "returns exact matches for model within a module" do + Gallery::BasicArtwork.fulltext_search('Flower Myth', :max_results => 1).first.should == flower_myth + Gallery::BasicArtwork.fulltext_search('Flowers', :max_results => 1).first.should == flowers + Gallery::BasicArtwork.fulltext_search('Cookies', :max_results => 1).first.should == cookies + Gallery::BasicArtwork.fulltext_search('Lowered', :max_results => 1).first.should == lowered + end + + end context "with default settings" do let!(:yellow) { BasicArtwork.create(:title => 'Yellow') } let!(:yellow_leaves_2) { BasicArtwork.create(:title => 'Yellow Leaves 2') } @@ -308,21 +325,43 @@ FilteredArtist.fulltext_search('foobar', :is_artwork => false, :is_foobar => true).should == [foobar_artist] FilteredArtist.fulltext_search('foobar', :is_artwork => false, :is_foobar => false).should == [barfoo_artist] end end + + context "with different filters applied to multiple models" do + let!(:foo_artwork) { FilteredArtwork.create(:title => 'foo') } + let!(:bar_artist) { FilteredArtist.create(:full_name => 'bar') } + let!(:baz_other) { FilteredOther.create(:name => 'baz') } + + # These three models are all indexed by the same mongoid_fulltext index, but have different filters + # applied. The index created on the mongoid_fulltext collection should include the ngram and score + # fields as well as the union of all the filter fields to allow for efficient lookups. + + it "creates a proper index for searching efficiently" do + 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 + 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" ]) } let!(:artist_1) { PartitionedArtist.create(:full_name => 'foobar', :exhibitions => [ "Art Basel 2011", ]) } let!(:artist_0) { PartitionedArtist.create(:full_name => 'foobar', :exhibitions => [ ]) } it "allows partitioned searches" do - PartitionedArtist.fulltext_search('foobar').should == [ artist_2, artist_1, artist_0 ] + artists_by_exhibition_length = [ artist_0, artist_1, artist_2 ].sort_by{ |x| x.exhibitions.length } + PartitionedArtist.fulltext_search('foobar').sort_by{ |x| x.exhibitions.length }.should == artists_by_exhibition_length PartitionedArtist.fulltext_search('foobar', :exhibitions => [ "Armory NY" ]).should == [ artist_2 ] - PartitionedArtist.fulltext_search('foobar', :exhibitions => [ "Art Basel 2011" ]).should == [ artist_2, artist_1 ] + art_basel_only = PartitionedArtist.fulltext_search('foobar', :exhibitions => [ "Art Basel 2011" ]).sort_by{ |x| x.exhibitions.length } + art_basel_only.should == [ artist_1, artist_2 ].sort_by{ |x| x.exhibitions.length } PartitionedArtist.fulltext_search('foobar', :exhibitions => [ "Art Basel 2011", "Armory NY" ]).should == [ artist_2 ] end end @@ -340,9 +379,59 @@ first_result.is_a?(Array).should be_true first_result.size.should == 2 first_result[0].should == flowers first_result[1].is_a?(Float).should be_true end + end + + context "remove_from_ngram_index" do + let!(:flowers1) { BasicArtwork.create(:title => 'Flowers 1') } + let!(:flowers2) { BasicArtwork.create(:title => 'Flowers 1') } + + it "removes all records from the index" do + BasicArtwork.remove_from_ngram_index + BasicArtwork.fulltext_search('flower').length.should == 0 + end + + it "removes a single record from the index" do + flowers1.remove_from_ngram_index + BasicArtwork.fulltext_search('flower').length.should == 1 + end + end + + context "update_ngram_index" do + let!(:flowers1) { BasicArtwork.create(:title => 'Flowers 1') } + let!(:flowers2) { BasicArtwork.create(:title => 'Flowers 2') } + + context "from scratch" do + + before(:each) do + Mongoid.master["mongoid_fulltext.index_basicartwork_0"].remove + end + + it "updates index on a single record" do + flowers1.update_ngram_index + BasicArtwork.fulltext_search('flower').length.should == 1 + end + + it "updates index on all records" do + BasicArtwork.update_ngram_index + BasicArtwork.fulltext_search('flower').length.should == 2 + end + + end + + context "incremental" do + + it "removes an existing record" do + coll = Mongoid.master["mongoid_fulltext.index_basicartwork_0"] + Mongoid.master.stub(:collection).with("mongoid_fulltext.index_basicartwork_0").and_return { coll } + coll.should_receive(:remove).once.with({'document_id' => flowers1._id}) + flowers1.update_ngram_index + end + + end + end end end