test/mongoid-haystack_test.rb in mongoid-haystack-1.1.0 vs test/mongoid-haystack_test.rb in mongoid-haystack-1.2.0

- old
+ new

@@ -14,10 +14,21 @@ assert{ Mongoid::Haystack.search('cat').map(&:model) == [b] } end ## # + testing 'that results are returned as chainable Mongoid::Criteria' do + k = new_klass + + 3.times{ k.create! :content => 'cats' } + + results = assert{ Mongoid::Haystack.search('cat') } + assert{ results.is_a?(Mongoid::Criteria) } + end + +## +# testing 'that word occurance affects the sort' do a = A.create!(:content => 'dog') b = A.create!(:content => 'dog dog') c = A.create!(:content => 'dog dog dog') @@ -65,11 +76,11 @@ assert{ Mongoid::Haystack.index(A) } assert{ Mongoid::Haystack::Token.count == 2 } assert{ Mongoid::Haystack::Token.all.map(&:value).sort == %w( cat dog ) } - assert{ Mongoid::Haystack::Count[:tokens].value == 3 } + assert{ Mongoid::Haystack::Token.total == 3 } end testing 'that removing a model from the index decrements counts appropriately' do a = A.create!(:content => 'dog') b = A.create!(:content => 'cat') @@ -79,31 +90,31 @@ assert{ Mongoid::Haystack.search('cat').first } assert{ Mongoid::Haystack::Token.where(:value => 'cat').first.count == 2 } assert{ Mongoid::Haystack::Token.where(:value => 'dog').first.count == 2 } - assert{ Mongoid::Haystack::Count[:tokens].value == 4 } + assert{ Mongoid::Haystack::Token.total == 4 } assert{ Mongoid::Haystack::Token.all.map(&:value).sort == %w( cat dog ) } assert{ Mongoid::Haystack.unindex(c) } assert{ Mongoid::Haystack::Token.all.map(&:value).sort == %w( cat dog ) } - assert{ Mongoid::Haystack::Count[:tokens].value == 2 } + assert{ Mongoid::Haystack::Token.total == 2 } assert{ Mongoid::Haystack::Token.where(:value => 'cat').first.count == 1 } assert{ Mongoid::Haystack::Token.where(:value => 'dog').first.count == 1 } - assert{ Mongoid::Haystack::Count[:tokens].value == 2 } + assert{ Mongoid::Haystack::Token.total == 2 } assert{ Mongoid::Haystack::Token.all.map(&:value).sort == %w( cat dog ) } assert{ Mongoid::Haystack.unindex(b) } assert{ Mongoid::Haystack::Token.all.map(&:value).sort == %w( cat dog ) } - assert{ Mongoid::Haystack::Count[:tokens].value == 1 } + assert{ Mongoid::Haystack::Token.total == 1 } assert{ Mongoid::Haystack::Token.where(:value => 'cat').first.count == 0 } assert{ Mongoid::Haystack::Token.where(:value => 'dog').first.count == 1 } - assert{ Mongoid::Haystack::Count[:tokens].value == 1 } + assert{ Mongoid::Haystack::Token.total == 1 } assert{ Mongoid::Haystack::Token.all.map(&:value).sort == %w( cat dog ) } assert{ Mongoid::Haystack.unindex(a) } assert{ Mongoid::Haystack::Token.all.map(&:value).sort == %w( cat dog ) } - assert{ Mongoid::Haystack::Count[:tokens].value == 0 } + assert{ Mongoid::Haystack::Token.total == 0 } assert{ Mongoid::Haystack::Token.where(:value => 'cat').first.count == 0 } assert{ Mongoid::Haystack::Token.where(:value => 'dog').first.count == 0 } end ## @@ -233,24 +244,123 @@ assert{ Mongoid::Haystack.search('meow').count == 2 } assert{ Mongoid::Haystack.search('bark').count == 1 } assert{ Mongoid::Haystack.search('dog').first.model == b } end +## +# + testing 'that re-indexing a class is idempotent' do + k = new_klass do + field(:title) + field(:body) + + def to_haystack + { :keywords => title, :fulltext => body } + end + end + + n = 10 + + n.times do + k.create!(:title => 'the cats and dogs', :body => 'now now is is the the time time for for all all good good men women') + end + + n.times do + k.create!(:title => 'a b c abc xyz abc xyz b', :body => 'pdq pdq pdq xyz teh ngr am') + end + + assert{ Mongoid::Haystack.search('cat').count == n } + assert{ Mongoid::Haystack.search('pdq').count == n } + + ca = Mongoid::Haystack::Token.all.inject({}){|hash, token| hash.update token.id => token.value} + + assert{ k.search_index_all! } + + cb = Mongoid::Haystack::Token.all.inject({}){|hash, token| hash.update token.id => token.value} + + assert{ ca.size == Mongoid::Haystack::Token.count } + assert{ cb.size == Mongoid::Haystack::Token.count } + assert{ ca == cb } + end + +## +# + testing 'that not just any model can be indexed' do + o = new_klass.create! + assert{ begin; Mongoid::Haystack::Index.add(o); rescue Object => e; e.is_a?(ArgumentError); end } + end + +## +# + testing 'that results can be expanded efficiently if need be' do + k = new_klass + 3.times{ k.create! :content => 'cats' } + + results = assert{ Mongoid::Haystack.search('cat') } + assert{ Mongoid::Haystack.models_for(results).map{|model| model.class} == [k, k, k] } + end + +## +# + testing 'basic pagination' do + k = new_klass + 11.times{|i| k.create! :content => "cats #{ i }" } + + assert{ k.search('cat').paginate(:page => 1, :size => 2).to_a.size == 2 } + assert{ k.search('cat').paginate(:page => 2, :size => 5).to_a.size == 5 } + + accum = [] + + n = 6 + size = 2 + (1..n).each do |page| + list = assert{ k.search('cat').paginate(:page => page, :size => size) } + accum.push(*list) + assert{ list.num_pages == n } + assert{ list.total_pages == n } + assert{ list.current_page == page } + end + + a = accum.map{|i| i.model}.sort_by{|m| m.content} + b = k.all.sort_by{|m| m.content} + + assert{ a == b } + end + +## +# + testing 'that pagination preserves the #model terminator' do + k = new_klass + 11.times{|i| k.create! :content => "cats #{ i }" } + + list = assert{ k.search('cat').paginate(:page => 1, :size => 2) } + assert{ list.is_a?(Mongoid::Criteria) } + + models = assert{ list.models } + assert{ models.is_a?(Array) } + end + protected def new_klass(&block) - Object.send(:remove_const, :K) if Object.send(:const_defined?, :K) + if Object.send(:const_defined?, :K) + Object.const_get(:K).destroy_all + Object.send(:remove_const, :K) + end k = Class.new(A) do self.default_collection_name = :ks def self.name() 'K' end - include ::Mongoid::Haystack::Search - class_eval(&block) if block end Object.const_set(:K, k) + k.class_eval do + include ::Mongoid::Haystack::Search + class_eval(&block) if block + end + k end H = Mongoid::Haystack T = Mongoid::Haystack::Token @@ -258,6 +368,8 @@ setup do [A, B, C].map{|m| m.destroy_all} Mongoid::Haystack.destroy_all end + + at_exit{ K.destroy_all if defined?(K) } end