From b1f54f79c73f175068d48f38c22663d64705fedf Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Tue, 11 Dec 2012 23:48:10 +0000 Subject: [PATCH] [pluginize-solr] Moving acts_as_[searchable,faceted] to the plugin --- app/models/article.rb | 68 -------------------------------------------------------------------- app/models/category.rb | 11 ----------- app/models/product.rb | 75 ++++----------------------------------------------------------------------- app/models/profile.rb | 72 +----------------------------------------------------------------------- config/initializers/dependencies.rb | 2 -- plugins/solr/lib/ext/article.rb | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/solr/lib/ext/category.rb | 11 +++++++++++ plugins/solr/lib/ext/product.rb | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/solr/lib/ext/profile.rb | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/solr/test/unit/category_test.rb | 24 +++++++++++++++++++++++- plugins/solr/test/unit/product_test.rb | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- plugins/solr/test/unit/profile_test.rb | 41 +++++++++++++++++++++++++++++++++++++++++ test/unit/acts_as_faceted_test.rb | 1 + test/unit/acts_as_searchable_test.rb | 2 +- test/unit/article_test.rb | 70 ---------------------------------------------------------------------- test/unit/category_test.rb | 22 ---------------------- test/unit/product_test.rb | 172 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- test/unit/profile_test.rb | 41 ----------------------------------------- 18 files changed, 487 insertions(+), 531 deletions(-) diff --git a/app/models/article.rb b/app/models/article.rb index 32f72da..aaaceca 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -647,80 +647,12 @@ class Article < ActiveRecord::Base Forum Event - def self.f_type_proc(klass) - klass.constantize.type_name - end - - def self.f_profile_type_proc(klass) - klass.constantize.type_name - end - - def f_type - #join common types - case self.class.name - when 'TinyMceArticle', 'TextileArticle' - TextArticle.name - else - self.class.name - end - end - - def f_profile_type - self.profile.class.name - end - - def f_published_at - self.published_at - end - - def f_category - self.categories.collect(&:name) - end - delegate :region, :region_id, :environment, :environment_id, :to => :profile, :allow_nil => true def name_sortable # give a different name for solr name end - def public - self.public? - end - - def category_filter - categories_including_virtual_ids - end - public - - acts_as_faceted :fields => { - :f_type => {:label => _('Type'), :proc => proc{|klass| f_type_proc(klass)}}, - :f_published_at => {:type => :date, :label => _('Published date'), :queries => {'[* TO NOW-1YEARS/DAY]' => _("Older than one year"), - '[NOW-1YEARS TO NOW/DAY]' => _("In the last year"), '[NOW-1MONTHS TO NOW/DAY]' => _("In the last month"), '[NOW-7DAYS TO NOW/DAY]' => _("In the last week"), '[NOW-1DAYS TO NOW/DAY]' => _("In the last day")}, - :queries_order => ['[NOW-1DAYS TO NOW/DAY]', '[NOW-7DAYS TO NOW/DAY]', '[NOW-1MONTHS TO NOW/DAY]', '[NOW-1YEARS TO NOW/DAY]', '[* TO NOW-1YEARS/DAY]']}, - :f_profile_type => {:label => _('Profile'), :proc => proc{|klass| f_profile_type_proc(klass)}}, - :f_category => {:label => _('Categories')}, - }, :category_query => proc { |c| "category_filter:\"#{c.id}\"" }, - :order => [:f_type, :f_published_at, :f_profile_type, :f_category] - - acts_as_searchable :fields => facets_fields_for_solr + [ - # searched fields - {:name => {:type => :text, :boost => 2.0}}, - {:slug => :text}, {:body => :text}, - {:abstract => :text}, {:filename => :text}, - # filtered fields - {:public => :boolean}, {:environment_id => :integer}, - {:profile_id => :integer}, :language, - {:category_filter => :integer}, - # ordered/query-boosted fields - {:name_sortable => :string}, :last_changed_by_id, :published_at, :is_image, - :updated_at, :created_at, - ], :include => [ - {:profile => {:fields => [:name, :identifier, :address, :nickname, :region_id, :lat, :lng]}}, - {:comments => {:fields => [:title, :body, :author_name, :author_email]}}, - {:categories => {:fields => [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}}, - ], :facets => facets_option_for_solr, - :boost => proc { |a| 10 if a.profile && a.profile.enabled }, - :if => proc{ |a| ! ['RssFeed'].include?(a.class.name) } handle_asynchronously :solr_save private diff --git a/app/models/category.rb b/app/models/category.rb index 8647cfd..a67b1fc 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -95,17 +95,6 @@ class Category < ActiveRecord::Base name end public - - acts_as_searchable :fields => [ - # searched fields - {:name => {:type => :text, :boost => 2.0}}, - {:path => :text}, {:slug => :text}, - {:abbreviation => :text}, {:acronym => :text}, - # filtered fields - :parent_id, - # ordered/query-boosted fields - {:name_sortable => :string}, - ] handle_asynchronously :solr_save end diff --git a/app/models/product.rb b/app/models/product.rb index 45b214a..2fc0cca 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -212,87 +212,20 @@ class Product < ActiveRecord::Base end private - def f_category - self.product_category.name - end - def f_region - self.enterprise.region.id if self.enterprise.region - end - def self.f_region_proc(id) - c = Region.find(id) - s = c.parent - if c and c.kind_of?(City) and s and s.kind_of?(State) and s.acronym - [c.name, ', ' + s.acronym] - else - c.name - end - end - def self.f_qualifier_proc(ids) - array = ids.split - qualifier = Qualifier.find_by_id array[0] - certifier = Certifier.find_by_id array[1] - certifier ? [qualifier.name, _(' cert. ') + certifier.name] : qualifier.name - end - def f_qualifier - product_qualifiers.map do |pq| - "#{pq.qualifier_id} #{pq.certifier_id}" - end - end - alias_method :name_sortable, :name delegate :enabled, :region, :region_id, :environment, :environment_id, :to => :enterprise + + alias_method :name_sortable, :name def name_sortable # give a different name for solr name end - def public - self.public? - end + def price_sortable (price.nil? or price.zero?) ? nil : price end - def category_filter - enterprise.categories_including_virtual_ids << product_category_id - end + public - acts_as_faceted :fields => { - :f_category => {:label => _('Related products')}, - :f_region => {:label => _('City'), :proc => proc { |id| f_region_proc(id) }}, - :f_qualifier => {:label => _('Qualifiers'), :proc => proc { |id| f_qualifier_proc(id) }}, - }, :category_query => proc { |c| "category_filter:#{c.id}" }, - :order => [:f_category, :f_region, :f_qualifier] - - Boosts = [ - [:image, 0.55, proc{ |p| p.image ? 1 : 0}], - [:qualifiers, 0.45, proc{ |p| p.product_qualifiers.count > 0 ? 1 : 0}], - [:open_price, 0.45, proc{ |p| p.price_described? ? 1 : 0}], - [:solidarity, 0.45, proc{ |p| p.percentage_from_solidarity_economy[0].to_f/100 }], - [:available, 0.35, proc{ |p| p.available ? 1 : 0}], - [:price, 0.35, proc{ |p| (!p.price.nil? and p.price > 0) ? 1 : 0}], - [:new_product, 0.35, proc{ |p| (p.updated_at.to_i - p.created_at.to_i) < 24*3600 ? 1 : 0}], - [:description, 0.3, proc{ |p| !p.description.blank? ? 1 : 0}], - [:enabled, 0.2, proc{ |p| p.enterprise.enabled ? 1 : 0}], - ] - - acts_as_searchable :fields => facets_fields_for_solr + [ - # searched fields - {:name => {:type => :text, :boost => 2.0}}, - {:description => :text}, {:category_full_name => :text}, - # filtered fields - {:public => :boolean}, {:environment_id => :integer}, - {:enabled => :boolean}, {:category_filter => :integer}, - # ordered/query-boosted fields - {:price_sortable => :decimal}, {:name_sortable => :string}, - {:lat => :float}, {:lng => :float}, - :updated_at, :created_at, - ], :include => [ - {:product_category => {:fields => [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}}, - {:region => {:fields => [:name, :path, :slug, :lat, :lng]}}, - {:enterprise => {:fields => [:name, :identifier, :address, :nickname, :lat, :lng]}}, - {:qualifiers => {:fields => [:name]}}, - {:certifiers => {:fields => [:name]}}, - ], :facets => facets_option_for_solr, - :boost => proc{ |p| boost = 1; Boosts.each{ |b| boost = boost * (1 - ((1 - b[2].call(p)) * b[1])) }; boost} handle_asynchronously :solr_save end diff --git a/app/models/profile.rb b/app/models/profile.rb index 309dd44..3fde3f6 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -887,82 +887,12 @@ private :generate_url, :url_options end private - def self.f_categories_label_proc(environment) - ids = environment.top_level_category_as_facet_ids - r = Category.find(ids) - map = {} - ids.map{ |id| map[id.to_s] = r.detect{|c| c.id == id}.name } - map - end - def self.f_categories_proc(facet, id) - id = id.to_i - return if id.zero? - c = Category.find(id) - c.name if c.top_ancestor.id == facet[:label_id].to_i or facet[:label_id] == 0 - end - def f_categories - category_ids - [region_id] - end - - def f_region - self.region_id - end - def self.f_region_proc(id) - c = Region.find(id) - s = c.parent - if c and c.kind_of?(City) and s and s.kind_of?(State) and s.acronym - [c.name, ', ' + s.acronym] - else - c.name - end - end - - def self.f_enabled_proc(enabled) - enabled = enabled == "true" ? true : false - enabled ? s_('facets|Enabled') : s_('facets|Not enabled') - end - def f_enabled - self.enabled - end def name_sortable # give a different name for solr name end - def public - self.public? - end - def category_filter - categories_including_virtual_ids - end - public - acts_as_faceted :fields => { - :f_enabled => {:label => _('Situation'), :type_if => proc { |klass| klass.kind_of?(Enterprise) }, - :proc => proc { |id| f_enabled_proc(id) }}, - :f_region => {:label => _('City'), :proc => proc { |id| f_region_proc(id) }}, - :f_categories => {:multi => true, :proc => proc {|facet, id| f_categories_proc(facet, id)}, - :label => proc { |env| f_categories_label_proc(env) }, :label_abbrev => proc{ |env| f_categories_label_abbrev_proc(env) }}, - }, :category_query => proc { |c| "category_filter:#{c.id}" }, - :order => [:f_region, :f_categories, :f_enabled] - - acts_as_searchable :fields => facets_fields_for_solr + [:extra_data_for_index, - # searched fields - {:name => {:type => :text, :boost => 2.0}}, - {:identifier => :text}, {:nickname => :text}, - # filtered fields - {:public => :boolean}, {:environment_id => :integer}, - {:category_filter => :integer}, - # ordered/query-boosted fields - {:name_sortable => :string}, {:user_id => :integer}, - :enabled, :active, :validated, :public_profile, - {:lat => :float}, {:lng => :float}, - :updated_at, :created_at, - ], - :include => [ - {:region => {:fields => [:name, :path, :slug, :lat, :lng]}}, - {:categories => {:fields => [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}}, - ], :facets => facets_option_for_solr, - :boost => proc{ |p| 10 if p.enabled } + public handle_asynchronously :solr_save diff --git a/config/initializers/dependencies.rb b/config/initializers/dependencies.rb index dc1f8ff..65fa81a 100644 --- a/config/initializers/dependencies.rb +++ b/config/initializers/dependencies.rb @@ -1,8 +1,6 @@ # locally-developed modules -require 'acts_as_faceted' require 'acts_as_filesystem' require 'acts_as_having_settings' -require 'acts_as_searchable' require 'acts_as_having_boxes' require 'acts_as_having_image' require 'acts_as_having_posts' diff --git a/plugins/solr/lib/ext/article.rb b/plugins/solr/lib/ext/article.rb index f6b1856..ec6a8c2 100644 --- a/plugins/solr/lib/ext/article.rb +++ b/plugins/solr/lib/ext/article.rb @@ -4,4 +4,74 @@ class Article def solr_plugin_comments_updated solr_save end + + acts_as_faceted :fields => { + :solr_plugin_f_type => {:label => _('Type'), :proc => proc{|klass| solr_plugin_f_type_proc(klass)}}, + :solr_plugin_f_published_at => {:type => :date, :label => _('Published date'), :queries => {'[* TO NOW-1YEARS/DAY]' => _("Older than one year"), + '[NOW-1YEARS TO NOW/DAY]' => _("In the last year"), '[NOW-1MONTHS TO NOW/DAY]' => _("In the last month"), '[NOW-7DAYS TO NOW/DAY]' => _("In the last week"), '[NOW-1DAYS TO NOW/DAY]' => _("In the last day")}, + :queries_order => ['[NOW-1DAYS TO NOW/DAY]', '[NOW-7DAYS TO NOW/DAY]', '[NOW-1MONTHS TO NOW/DAY]', '[NOW-1YEARS TO NOW/DAY]', '[* TO NOW-1YEARS/DAY]']}, + :solr_plugin_f_profile_type => {:label => _('Profile'), :proc => proc{|klass| solr_plugin_f_profile_type_proc(klass)}}, + :solr_plugin_f_category => {:label => _('Categories')}, + }, :category_query => proc { |c| "solr_plugin_category_filter:\"#{c.id}\"" }, + :order => [:solr_plugin_f_type, :solr_plugin_f_published_at, :solr_plugin_f_profile_type, :solr_plugin_f_category] + + acts_as_searchable :fields => facets_fields_for_solr + [ + # searched fields + {:name => {:type => :text, :boost => 2.0}}, + {:slug => :text}, {:body => :text}, + {:abstract => :text}, {:filename => :text}, + # filtered fields + {:solr_plugin_public => :boolean}, {:environment_id => :integer}, + {:profile_id => :integer}, :language, + {:solr_plugin_category_filter => :integer}, + # ordered/query-boosted fields + {:name_sortable => :string}, :last_changed_by_id, :published_at, :is_image, + :updated_at, :created_at, + ], :include => [ + {:profile => {:fields => [:name, :identifier, :address, :nickname, :region_id, :lat, :lng]}}, + {:comments => {:fields => [:title, :body, :author_name, :author_email]}}, + {:categories => {:fields => [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}}, + ], :facets => facets_option_for_solr, + :boost => proc { |a| 10 if a.profile && a.profile.enabled }, + :if => proc{ |a| ! ['RssFeed'].include?(a.class.name) } + + private + + def self.solr_plugin_f_type_proc(klass) + klass.constantize.type_name + end + + def self.solr_plugin_f_profile_type_proc(klass) + klass.constantize.type_name + end + + def solr_plugin_f_type + #join common types + case self.class.name + when 'TinyMceArticle', 'TextileArticle' + TextArticle.name + else + self.class.name + end + end + + def solr_plugin_f_profile_type + self.profile.class.name + end + + def solr_plugin_f_published_at + self.published_at + end + + def solr_plugin_f_category + self.categories.collect(&:name) + end + + def solr_plugin_public + self.public? + end + + def solr_plugin_category_filter + categories_including_virtual_ids + end end diff --git a/plugins/solr/lib/ext/category.rb b/plugins/solr/lib/ext/category.rb index d3d29c4..87c7df7 100644 --- a/plugins/solr/lib/ext/category.rb +++ b/plugins/solr/lib/ext/category.rb @@ -2,4 +2,15 @@ require_dependency 'category' class Category after_save_reindex [:articles, :profiles], :with => :delayed_job + + acts_as_searchable :fields => [ + # searched fields + {:name => {:type => :text, :boost => 2.0}}, + {:path => :text}, {:slug => :text}, + {:abbreviation => :text}, {:acronym => :text}, + # filtered fields + :parent_id, + # ordered/query-boosted fields + {:name_sortable => :string}, + ] end diff --git a/plugins/solr/lib/ext/product.rb b/plugins/solr/lib/ext/product.rb index a3d253a..dee4a8d 100644 --- a/plugins/solr/lib/ext/product.rb +++ b/plugins/solr/lib/ext/product.rb @@ -2,4 +2,84 @@ require_dependency 'product' class Product after_save_reindex [:enterprise], :with => :delayed_job + + acts_as_faceted :fields => { + :solr_plugin_f_category => {:label => _('Related products')}, + :solr_plugin_f_region => {:label => _('City'), :proc => proc { |id| solr_plugin_f_region_proc(id) }}, + :solr_plugin_f_qualifier => {:label => _('Qualifiers'), :proc => proc { |id| solr_plugin_f_qualifier_proc(id) }}, + }, :category_query => proc { |c| "solr_plugin_category_filter:#{c.id}" }, + :order => [:solr_plugin_f_category, :solr_plugin_f_region, :solr_plugin_f_qualifier] + + SolrPlugin::Boosts = [ + [:image, 0.55, proc{ |p| p.image ? 1 : 0}], + [:qualifiers, 0.45, proc{ |p| p.product_qualifiers.count > 0 ? 1 : 0}], + [:open_price, 0.45, proc{ |p| p.price_described? ? 1 : 0}], + [:solidarity, 0.45, proc{ |p| p.percentage_from_solidarity_economy[0].to_f/100 }], + [:available, 0.35, proc{ |p| p.available ? 1 : 0}], + [:price, 0.35, proc{ |p| (!p.price.nil? and p.price > 0) ? 1 : 0}], + [:new_product, 0.35, proc{ |p| (p.updated_at.to_i - p.created_at.to_i) < 24*3600 ? 1 : 0}], + [:description, 0.3, proc{ |p| !p.description.blank? ? 1 : 0}], + [:enabled, 0.2, proc{ |p| p.enterprise.enabled ? 1 : 0}], + ] + + acts_as_searchable :fields => facets_fields_for_solr + [ + # searched fields + {:name => {:type => :text, :boost => 2.0}}, + {:description => :text}, {:category_full_name => :text}, + # filtered fields + {:solr_plugin_public => :boolean}, {:environment_id => :integer}, + {:enabled => :boolean}, {:solr_plugin_category_filter => :integer}, + # ordered/query-boosted fields + {:price_sortable => :decimal}, {:name_sortable => :string}, + {:lat => :float}, {:lng => :float}, + :updated_at, :created_at, + ], :include => [ + {:product_category => {:fields => [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}}, + {:region => {:fields => [:name, :path, :slug, :lat, :lng]}}, + {:enterprise => {:fields => [:name, :identifier, :address, :nickname, :lat, :lng]}}, + {:qualifiers => {:fields => [:name]}}, + {:certifiers => {:fields => [:name]}}, + ], :facets => facets_option_for_solr, + :boost => proc{ |p| boost = 1; SolrPlugin::Boosts.each{ |b| boost = boost * (1 - ((1 - b[2].call(p)) * b[1])) }; boost} + + private + + def solr_plugin_f_category + self.product_category.name + end + + def solr_plugin_f_region + self.enterprise.region.id if self.enterprise.region + end + + def self.solr_plugin_f_region_proc(id) + c = Region.find(id) + s = c.parent + if c and c.kind_of?(City) and s and s.kind_of?(State) and s.acronym + [c.name, ', ' + s.acronym] + else + c.name + end + end + + def self.solr_plugin_f_qualifier_proc(ids) + array = ids.split + qualifier = Qualifier.find_by_id array[0] + certifier = Certifier.find_by_id array[1] + certifier ? [qualifier.name, _(' cert. ') + certifier.name] : qualifier.name + end + + def solr_plugin_f_qualifier + product_qualifiers.map do |pq| + "#{pq.qualifier_id} #{pq.certifier_id}" + end + end + + def solr_plugin_category_filter + enterprise.categories_including_virtual_ids << product_category_id + end + + def solr_plugin_public + self.public? + end end diff --git a/plugins/solr/lib/ext/profile.rb b/plugins/solr/lib/ext/profile.rb index 3d83b47..4a3f146 100644 --- a/plugins/solr/lib/ext/profile.rb +++ b/plugins/solr/lib/ext/profile.rb @@ -2,4 +2,84 @@ require_dependency 'profile' class Profile after_save_reindex [:articles], :with => :delayed_job + + acts_as_faceted :fields => { + :solr_plugin_f_enabled => {:label => _('Situation'), :type_if => proc { |klass| klass.kind_of?(Enterprise) }, + :proc => proc { |id| solr_plugin_f_enabled_proc(id) }}, + :solr_plugin_f_region => {:label => _('City'), :proc => proc { |id| solr_plugin_f_region_proc(id) }}, + :solr_plugin_f_categories => {:multi => true, :proc => proc {|facet, id| solr_plugin_f_categories_proc(facet, id)}, + :label => proc { |env| solr_plugin_f_categories_label_proc(env) }, :label_abbrev => proc{ |env| solr_plugin_f_categories_label_abbrev_proc(env) }}, + }, :category_query => proc { |c| "solr_plugin_category_filter:#{c.id}" }, + :order => [:solr_plugin_f_region, :solr_plugin_f_categories, :solr_plugin_f_enabled] + + acts_as_searchable :fields => facets_fields_for_solr + [:extra_data_for_index, + # searched fields + {:name => {:type => :text, :boost => 2.0}}, + {:identifier => :text}, {:nickname => :text}, + # filtered fields + {:solr_plugin_public => :boolean}, {:environment_id => :integer}, + {:solr_plugin_category_filter => :integer}, + # ordered/query-boosted fields + {:name_sortable => :string}, {:user_id => :integer}, + :enabled, :active, :validated, :public_profile, + {:lat => :float}, {:lng => :float}, + :updated_at, :created_at, + ], + :include => [ + {:region => {:fields => [:name, :path, :slug, :lat, :lng]}}, + {:categories => {:fields => [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}}, + ], :facets => facets_option_for_solr, + :boost => proc{ |p| 10 if p.enabled } + + private + + def self.solr_plugin_f_categories_label_proc(environment) + ids = environment.top_level_category_as_facet_ids + r = Category.find(ids) + map = {} + ids.map{ |id| map[id.to_s] = r.detect{|c| c.id == id}.name } + map + end + + def self.solr_plugin_f_categories_proc(facet, id) + id = id.to_i + return if id.zero? + c = Category.find(id) + c.name if c.top_ancestor.id == facet[:label_id].to_i or facet[:label_id] == 0 + end + + def solr_plugin_f_categories + category_ids - [region_id] + end + + def solr_plugin_f_region + self.region_id + end + + def self.solr_plugin_f_region_proc(id) + c = Region.find(id) + s = c.parent + if c and c.kind_of?(City) and s and s.kind_of?(State) and s.acronym + [c.name, ', ' + s.acronym] + else + c.name + end + end + + def self.solr_plugin_f_enabled_proc(enabled) + enabled = enabled == "true" ? true : false + enabled ? s_('facets|Enabled') : s_('facets|Not enabled') + end + + def solr_plugin_f_enabled + self.enabled + end + + def solr_plugin_public + self.public? + end + + def solr_plugin_category_filter + categories_including_virtual_ids + end end diff --git a/plugins/solr/test/unit/category_test.rb b/plugins/solr/test/unit/category_test.rb index 0d64c32..4be9a7b 100644 --- a/plugins/solr/test/unit/category_test.rb +++ b/plugins/solr/test/unit/category_test.rb @@ -9,7 +9,7 @@ class CategoryTest < ActiveSupport::TestCase attr_accessor :environment should 'reindex articles after saving' do - cat = Category.create!(:name => 'category 1', :environment_id => Environment.default.id) + cat = Category.create!(:name => 'category 1', :environment_id => environment.id) art = Article.create!(:name => 'something', :profile_id => fast_create(Person).id) art.add_category cat cat.reload @@ -18,4 +18,26 @@ class CategoryTest < ActiveSupport::TestCase Article.any_instance.expects(:to_solr_doc).returns(solr_doc) cat.save! end + + should 'act as searchable' do + TestSolr.enable + parent = fast_create(Category, :name => 'books') + c = Category.create!(:name => "science fiction", :acronym => "sf", :abbreviation => "sci-fi", + :environment_id => environment.id, :parent_id => parent.id) + + # fields + assert_includes Category.find_by_contents('fiction')[:results].docs, c + assert_includes Category.find_by_contents('sf')[:results].docs, c + assert_includes Category.find_by_contents('sci-fi')[:results].docs, c + # filters + assert_includes Category.find_by_contents('science', {}, { + :filter_queries => ["parent_id:#{parent.id}"]})[:results].docs, c + end + + should 'boost name matches' do + TestSolr.enable + c_abbr = Category.create!(:name => "something else", :abbreviation => "science", :environment_id => environment.id) + c_name = Category.create!(:name => "science fiction", :environment_id => environment.id) + assert_equal [c_name, c_abbr], Category.find_by_contents("science")[:results].docs + end end diff --git a/plugins/solr/test/unit/product_test.rb b/plugins/solr/test/unit/product_test.rb index 7230bb4..297353c 100644 --- a/plugins/solr/test/unit/product_test.rb +++ b/plugins/solr/test/unit/product_test.rb @@ -4,9 +4,11 @@ class ProductTest < ActiveSupport::TestCase def setup @environment = Environment.default @environment.enable_plugin(SolrPlugin) + @product_category = fast_create(ProductCategory, :name => 'Products') + @profile = fast_create(Enterprise) end - attr_accessor :environment + attr_accessor :environment, :product_category, :profile should 'reindex enterprise after saving' do ent = fast_create(Enterprise) @@ -15,5 +17,177 @@ class ProductTest < ActiveSupport::TestCase Product.expects(:solr_batch_add).with([ent]) prod.save! end + + should 'act as faceted' do + s = fast_create(State, :acronym => 'XZ') + c = fast_create(City, :name => 'Tabajara', :parent_id => s.id) + ent = fast_create(Enterprise, :region_id => c.id) + cat = fast_create(ProductCategory, :name => 'hardcore') + p = Product.create!(:name => 'black flag', :enterprise_id => ent.id, :product_category_id => cat.id) + pq = p.product_qualifiers.create!(:qualifier => fast_create(Qualifier, :name => 'qualifier'), + :certifier => fast_create(Certifier, :name => 'certifier')) + assert_equal 'Related products', Product.facet_by_id(:solr_plugin_f_category)[:label] + assert_equal ['Tabajara', ', XZ'], Product.facet_by_id(:solr_plugin_f_region)[:proc].call(p.send(:solr_plugin_f_region)) + assert_equal ['qualifier', ' cert. certifier'], Product.facet_by_id(:solr_plugin_f_qualifier)[:proc].call(p.send(:solr_plugin_f_qualifier).last) + assert_equal 'hardcore', p.send(:solr_plugin_f_category) + assert_equal "solr_plugin_category_filter:#{cat.id}", Product.facet_category_query.call(cat) + end + + should 'act as searchable' do + TestSolr.enable + s = fast_create(State, :acronym => 'XZ') + c = fast_create(City, :name => 'Tabajara', :parent_id => s.id) + ent = fast_create(Enterprise, :region_id => c.id, :name => "Black Sun") + category = fast_create(ProductCategory, :name => "homemade", :acronym => "hm", :abbreviation => "homey") + p = Product.create!(:name => 'bananas syrup', :description => 'surrounded by mosquitos', :enterprise_id => ent.id, + :product_category_id => category.id) + qual = Qualifier.create!(:name => 'qualificador', :environment_id => Environment.default.id) + cert = Certifier.create!(:name => 'certificador', :environment_id => Environment.default.id) + pq = p.product_qualifiers.create!(:qualifier => qual, :certifier => cert) + p.qualifiers.reload + p.certifiers.reload + p.save! + # fields + assert_includes Product.find_by_contents('bananas')[:results].docs, p + assert_includes Product.find_by_contents('mosquitos')[:results].docs, p + assert_includes Product.find_by_contents('homemade')[:results].docs, p + # filters + assert_includes Product.find_by_contents('bananas', {}, { :filter_queries => ["solr_plugin_public:true"]})[:results].docs, p + assert_not_includes Product.find_by_contents('bananas', {}, { :filter_queries => ["solr_plugin_public:false"]})[:results].docs, p + assert_includes Product.find_by_contents('bananas', {}, { :filter_queries => ["environment_id:\"#{Environment.default.id}\""]})[:results].docs, p + # includes + assert_includes Product.find_by_contents("homemade")[:results].docs, p + assert_includes Product.find_by_contents(category.slug)[:results].docs, p + assert_includes Product.find_by_contents("hm")[:results].docs, p + assert_includes Product.find_by_contents("homey")[:results].docs, p + assert_includes Product.find_by_contents("Tabajara")[:results].docs, p + assert_includes Product.find_by_contents("Black Sun")[:results].docs, p + assert_includes Product.find_by_contents("qualificador")[:results].docs, p + assert_includes Product.find_by_contents("certificador")[:results].docs, p + end + + should 'boost name matches' do + TestSolr.enable + ent = fast_create(Enterprise) + cat = fast_create(ProductCategory) + in_desc = Product.create!(:name => 'something', :enterprise_id => ent.id, :description => 'bananas in the description!', + :product_category_id => cat.id) + in_name = Product.create!(:name => 'bananas in the name!', :enterprise_id => ent.id, :product_category_id => cat.id) + assert_equal [in_name, in_desc], Product.find_by_contents('bananas')[:results].docs + end + + should 'boost search results that include an image' do + TestSolr.enable + product_without_image = Product.create!(:name => 'product without image', :product_category => product_category, + :enterprise_id => profile.id) + product_with_image = Product.create!(:name => 'product with image', :product_category => product_category, + :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}, + :enterprise_id => profile.id) + assert_equal [product_with_image, product_without_image], Product.find_by_contents('product image')[:results].docs + end + + should 'boost search results that include qualifier' do + TestSolr.enable + product_without_q = Product.create!(:name => 'product without qualifier', :product_category => product_category, + :enterprise_id => profile.id) + product_with_q = Product.create!(:name => 'product with qualifier', :product_category => product_category, + :enterprise_id => profile.id) + product_with_q.product_qualifiers.create(:qualifier => fast_create(Qualifier), :certifier => nil) + product_with_q.save! + + assert_equal [product_with_q, product_without_q], Product.find_by_contents('product qualifier')[:results].docs + end + + should 'boost search results with open price' do + TestSolr.enable + product = Product.create!(:name => 'product 1', :product_category => product_category, :enterprise_id => profile.id, :price => 100) + open_price = Product.new(:name => 'product 2', :product_category => product_category, :enterprise_id => profile.id, :price => 100) + open_price.inputs << Input.new(:product => open_price, :product_category_id => product_category.id, :amount_used => 10, :price_per_unit => 10) + open_price.save! + + assert_equal [open_price, product], Product.find_by_contents('product')[:results].docs + end + + should 'boost search results with solidarity inputs' do + TestSolr.enable + product = Product.create!(:name => 'product 1', :product_category => product_category, :enterprise_id => profile.id) + perc_50 = Product.create!(:name => 'product 2', :product_category => product_category, :enterprise_id => profile.id) + Input.create!(:product_id => perc_50.id, :product_category_id => product_category.id, + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) + Input.create!(:product_id => perc_50.id, :product_category_id => product_category.id, + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) + perc_50.save! + perc_75 = Product.create!(:name => 'product 3', :product_category => product_category, :enterprise_id => profile.id) + Input.create!(:product_id => perc_75.id, :product_category_id => product_category.id, + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) + Input.create!(:product_id => perc_75.id, :product_category_id => product_category.id, + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) + Input.create!(:product_id => perc_75.id, :product_category_id => product_category.id, + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) + Input.create!(:product_id => perc_75.id, :product_category_id => product_category.id, + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) + perc_75.save! + + assert_equal [perc_75, perc_50, product], Product.find_by_contents('product')[:results].docs + end + + should 'boost available search results' do + TestSolr.enable + product = Product.create!(:name => 'product 1', :product_category => product_category, :enterprise_id => profile.id) + product.available = false + product.save! + product2 = Product.create!(:name => 'product 2', :product_category => product_category, :enterprise_id => profile.id) + product2.available = true + product2.save! + + assert_equal [product2, product], Product.find_by_contents('product')[:results].docs + end + + should 'boost search results created updated recently' do + TestSolr.enable + product = Product.create!(:name => 'product 1', :product_category => product_category, :enterprise_id => profile.id) + product.update_attribute :created_at, Time.now - 10.day + product2 = Product.create!(:name => 'product 2', :product_category => product_category, :enterprise_id => profile.id) + + assert_equal [product2, product], Product.find_by_contents('product')[:results].docs + end + + should 'boost search results with description' do + TestSolr.enable + product = Product.create!(:name => 'product 1', :product_category => product_category, :enterprise_id => profile.id, + :description => '') + product2 = Product.create!(:name => 'product 2', :product_category => product_category, :enterprise_id => profile.id, + :description => 'a small description') + + assert_equal [product2, product], Product.find_by_contents('product')[:results].docs + end + + should 'boost if enterprise is enabled' do + TestSolr.enable + ent = Enterprise.create!(:name => 'ent', :identifier => 'ent', :enabled => false) + product = Product.create!(:name => 'product 1', :product_category => product_category, :enterprise_id => ent.id) + product2 = Product.create!(:name => 'product 2', :product_category => product_category, :enterprise_id => profile.id) + + assert_equal [product2, product], Product.find_by_contents('product')[:results].docs + end + + should 'combine different boost types' do + TestSolr.enable + product = Product.create!(:name => 'product', :product_category => product_category, :enterprise_id => profile.id) + image_only = Product.create!(:name => 'product with image', :product_category => product_category, + :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}, + :enterprise_id => profile.id) + qual_only = Product.create!(:name => 'product with qualifier', :product_category => product_category, + :enterprise_id => profile.id) + img_and_qual = Product.create!(:name => 'product with image and qualifier', :product_category => product_category, + :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}, + :enterprise_id => profile.id) + qual_only.product_qualifiers.create(:qualifier => fast_create(Qualifier), :certifier => nil) + img_and_qual.product_qualifiers.create(:qualifier => fast_create(Qualifier), :certifier => nil) + qual_only.save! + img_and_qual.save! + + assert_equal [img_and_qual, image_only, qual_only, product], Product.find_by_contents('product')[:results].docs + end end diff --git a/plugins/solr/test/unit/profile_test.rb b/plugins/solr/test/unit/profile_test.rb index 60ad1b3..fdcf027 100644 --- a/plugins/solr/test/unit/profile_test.rb +++ b/plugins/solr/test/unit/profile_test.rb @@ -14,4 +14,45 @@ class ProfileTest < ActiveSupport::TestCase Profile.expects(:solr_batch_add).with(includes(art)) profile.save! end + + should 'act as faceted' do + st = fast_create(State, :acronym => 'XZ') + city = fast_create(City, :name => 'Tabajara', :parent_id => st.id) + cat = fast_create(Category) + prof = fast_create(Person, :region_id => city.id) + prof.add_category(cat, true) + assert_equal ['Tabajara', ', XZ'], Profile.facet_by_id(:solr_plugin_f_region)[:proc].call(prof.send(:solr_plugin_f_region)) + assert_equal "solr_plugin_category_filter:#{cat.id}", Person.facet_category_query.call(cat) + end + + should 'act as searchable' do + TestSolr.enable + st = create(State, :name => 'California', :acronym => 'CA', :environment_id => Environment.default.id) + city = create(City, :name => 'Inglewood', :parent_id => st.id, :environment_id => Environment.default.id) + p = create(Person, :name => "Hiro", :address => 'U-Stor-It', :nickname => 'Protagonist', + :user_id => fast_create(User).id, :region_id => city.id) + cat = create(Category, :name => "Science Fiction", :acronym => "sf", :abbreviation => "sci-fi") + p.add_category cat + + # fields + assert_includes Profile.find_by_contents('Hiro')[:results].docs, p + assert_includes Profile.find_by_contents('Protagonist')[:results].docs, p + # filters + assert_includes Profile.find_by_contents('Hiro', {}, { :filter_queries => ["solr_plugin_public:true"]})[:results].docs, p + assert_not_includes Profile.find_by_contents('Hiro', {}, { :filter_queries => ["solr_plugin_public:false"]})[:results].docs, p + assert_includes Profile.find_by_contents('Hiro', {}, { :filter_queries => ["environment_id:\"#{Environment.default.id}\""]})[:results].docs, p + # includes + assert_includes Profile.find_by_contents("Inglewood")[:results].docs, p + assert_includes Profile.find_by_contents("California")[:results].docs, p + assert_includes Profile.find_by_contents("Science")[:results].docs, p + # not includes + assert_not_includes Profile.find_by_contents('Stor')[:results].docs, p + end + + should 'boost name matches' do + TestSolr.enable + in_addr = create(Person, :name => 'something', :address => 'bananas in the address!', :user_id => fast_create(User).id) + in_name = create(Person, :name => 'bananas in the name!', :user_id => fast_create(User).id) + assert_equal [in_name], Person.find_by_contents('bananas')[:results].docs + end end diff --git a/test/unit/acts_as_faceted_test.rb b/test/unit/acts_as_faceted_test.rb index 54c848b..391b71b 100644 --- a/test/unit/acts_as_faceted_test.rb +++ b/test/unit/acts_as_faceted_test.rb @@ -1,4 +1,5 @@ require File.dirname(__FILE__) + '/../test_helper' +require 'acts_as_faceted' class TestModel < ActiveRecord::Base def self.f_type_proc(klass) diff --git a/test/unit/acts_as_searchable_test.rb b/test/unit/acts_as_searchable_test.rb index 703be80..497a353 100644 --- a/test/unit/acts_as_searchable_test.rb +++ b/test/unit/acts_as_searchable_test.rb @@ -1,5 +1,5 @@ require File.dirname(__FILE__) + '/../test_helper' - +require 'acts_as_searchable' class ActsAsSearchableTest < ActiveSupport::TestCase diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 941e86a..dd764e8 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -1588,76 +1588,6 @@ class ArticleTest < ActiveSupport::TestCase article.environment_id end - should 'act as faceted' do - person = fast_create(Person) - cat = Category.create!(:name => 'hardcore', :environment_id => Environment.default.id) - a = Article.create!(:name => 'black flag review', :profile_id => person.id) - a.add_category(cat, true) - a.save! - assert_equal Article.type_name, Article.facet_by_id(:f_type)[:proc].call(a.send(:f_type)) - assert_equal Person.type_name, Article.facet_by_id(:f_profile_type)[:proc].call(a.send(:f_profile_type)) - assert_equal a.published_at, a.send(:f_published_at) - assert_equal ['hardcore'], a.send(:f_category) - assert_equal "category_filter:\"#{cat.id}\"", Article.facet_category_query.call(cat) - end - - should 'act as searchable' do - TestSolr.enable - person = fast_create(Person, :name => "Hiro", :address => 'U-Stor-It @ Inglewood, California', - :nickname => 'Protagonist') - person2 = fast_create(Person, :name => "Raven") - category = fast_create(Category, :name => "science fiction", :acronym => "sf", :abbreviation => "sci-fi") - a = Article.create!(:name => 'a searchable article about bananas', :profile_id => person.id, - :body => 'the body talks about mosquitos', :abstract => 'and the abstract is about beer', - :filename => 'not_a_virus.exe') - a.add_category(category) - c = a.comments.build(:title => 'snow crash', :author => person2, :body => 'wanna try some?') - c.save! - - # fields - assert_includes Article.find_by_contents('bananas')[:results].docs, a - assert_includes Article.find_by_contents('mosquitos')[:results].docs, a - assert_includes Article.find_by_contents('beer')[:results].docs, a - assert_includes Article.find_by_contents('not_a_virus.exe')[:results].docs, a - # filters - assert_includes Article.find_by_contents('bananas', {}, {:filter_queries => ["public:true"]})[:results].docs, a - assert_not_includes Article.find_by_contents('bananas', {}, {:filter_queries => ["public:false"]})[:results].docs, a - assert_includes Article.find_by_contents('bananas', {}, {:filter_queries => ["environment_id:\"#{Environment.default.id}\""]})[:results].docs, a - assert_includes Article.find_by_contents('bananas', {}, {:filter_queries => ["profile_id:\"#{person.id}\""]})[:results].docs, a - # includes - assert_includes Article.find_by_contents('Hiro')[:results].docs, a - assert_includes Article.find_by_contents("person-#{person.id}")[:results].docs, a - assert_includes Article.find_by_contents("California")[:results].docs, a - assert_includes Article.find_by_contents("Protagonist")[:results].docs, a -# FIXME: After merging with AI1826, searching on comments is not working -# assert_includes Article.find_by_contents("snow")[:results].docs, a -# assert_includes Article.find_by_contents("try some")[:results].docs, a -# assert_includes Article.find_by_contents("Raven")[:results].docs, a -# -# FIXME: After merging with AI1826, searching on categories is not working -# assert_includes Article.find_by_contents("science")[:results].docs, a -# assert_includes Article.find_by_contents(category.slug)[:results].docs, a -# assert_includes Article.find_by_contents("sf")[:results].docs, a -# assert_includes Article.find_by_contents("sci-fi")[:results].docs, a - end - - should 'boost name matches' do - TestSolr.enable - person = fast_create(Person) - in_body = Article.create!(:name => 'something', :profile_id => person.id, :body => 'bananas in the body!') - in_name = Article.create!(:name => 'bananas in the name!', :profile_id => person.id) - assert_equal [in_name, in_body], Article.find_by_contents('bananas')[:results].docs - end - - should 'boost if profile is enabled' do - TestSolr.enable - person2 = fast_create(Person, :enabled => false) - art_profile_disabled = Article.create!(:name => 'profile disabled', :profile_id => person2.id) - person1 = fast_create(Person, :enabled => true) - art_profile_enabled = Article.create!(:name => 'profile enabled', :profile_id => person1.id) - assert_equal [art_profile_enabled, art_profile_disabled], Article.find_by_contents('profile')[:results].docs - end - should 'remove all categorizations when destroyed' do art = Article.create!(:name => 'article 1', :profile_id => fast_create(Person).id) cat = Category.create!(:name => 'category 1', :environment_id => Environment.default.id) diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index a79c3b9..2982acb 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -513,28 +513,6 @@ class CategoryTest < ActiveSupport::TestCase assert p.categories.reload.empty? end - should 'act as searchable' do - TestSolr.enable - parent = fast_create(Category, :name => 'books') - c = Category.create!(:name => "science fiction", :acronym => "sf", :abbreviation => "sci-fi", - :environment_id => Environment.default.id, :parent_id => parent.id) - - # fields - assert_includes Category.find_by_contents('fiction')[:results].docs, c - assert_includes Category.find_by_contents('sf')[:results].docs, c - assert_includes Category.find_by_contents('sci-fi')[:results].docs, c - # filters - assert_includes Category.find_by_contents('science', {}, { - :filter_queries => ["parent_id:#{parent.id}"]})[:results].docs, c - end - - should 'boost name matches' do - TestSolr.enable - c_abbr = Category.create!(:name => "something else", :abbreviation => "science", :environment_id => Environment.default.id) - c_name = Category.create!(:name => "science fiction", :environment_id => Environment.default.id) - assert_equal [c_name, c_abbr], Category.find_by_contents("science")[:results].docs - end - should 'solr save' do c = @env.categories.build(:name => 'my category'); c.expects(:solr_save) diff --git a/test/unit/product_test.rb b/test/unit/product_test.rb index 93d5de5..f07c5cf 100644 --- a/test/unit/product_test.rb +++ b/test/unit/product_test.rb @@ -578,178 +578,6 @@ class ProductTest < ActiveSupport::TestCase product.environment_id end - should 'act as faceted' do - s = fast_create(State, :acronym => 'XZ') - c = fast_create(City, :name => 'Tabajara', :parent_id => s.id) - ent = fast_create(Enterprise, :region_id => c.id) - cat = fast_create(ProductCategory, :name => 'hardcore') - p = Product.create!(:name => 'black flag', :enterprise_id => ent.id, :product_category_id => cat.id) - pq = p.product_qualifiers.create!(:qualifier => fast_create(Qualifier, :name => 'qualifier'), - :certifier => fast_create(Certifier, :name => 'certifier')) - assert_equal 'Related products', Product.facet_by_id(:f_category)[:label] - assert_equal ['Tabajara', ', XZ'], Product.facet_by_id(:f_region)[:proc].call(p.send(:f_region)) - assert_equal ['qualifier', ' cert. certifier'], Product.facet_by_id(:f_qualifier)[:proc].call(p.send(:f_qualifier).last) - assert_equal 'hardcore', p.send(:f_category) - assert_equal "category_filter:#{cat.id}", Product.facet_category_query.call(cat) - end - - should 'act as searchable' do - TestSolr.enable - s = fast_create(State, :acronym => 'XZ') - c = fast_create(City, :name => 'Tabajara', :parent_id => s.id) - ent = fast_create(Enterprise, :region_id => c.id, :name => "Black Sun") - category = fast_create(ProductCategory, :name => "homemade", :acronym => "hm", :abbreviation => "homey") - p = Product.create!(:name => 'bananas syrup', :description => 'surrounded by mosquitos', :enterprise_id => ent.id, - :product_category_id => category.id) - qual = Qualifier.create!(:name => 'qualificador', :environment_id => Environment.default.id) - cert = Certifier.create!(:name => 'certificador', :environment_id => Environment.default.id) - pq = p.product_qualifiers.create!(:qualifier => qual, :certifier => cert) - p.qualifiers.reload - p.certifiers.reload - p.save! - # fields - assert_includes Product.find_by_contents('bananas')[:results].docs, p - assert_includes Product.find_by_contents('mosquitos')[:results].docs, p - assert_includes Product.find_by_contents('homemade')[:results].docs, p - # filters - assert_includes Product.find_by_contents('bananas', {}, { :filter_queries => ["public:true"]})[:results].docs, p - assert_not_includes Product.find_by_contents('bananas', {}, { :filter_queries => ["public:false"]})[:results].docs, p - assert_includes Product.find_by_contents('bananas', {}, { :filter_queries => ["environment_id:\"#{Environment.default.id}\""]})[:results].docs, p - # includes - assert_includes Product.find_by_contents("homemade")[:results].docs, p - assert_includes Product.find_by_contents(category.slug)[:results].docs, p - assert_includes Product.find_by_contents("hm")[:results].docs, p - assert_includes Product.find_by_contents("homey")[:results].docs, p - assert_includes Product.find_by_contents("Tabajara")[:results].docs, p - assert_includes Product.find_by_contents("Black Sun")[:results].docs, p - assert_includes Product.find_by_contents("qualificador")[:results].docs, p - assert_includes Product.find_by_contents("certificador")[:results].docs, p - end - - should 'boost name matches' do - TestSolr.enable - ent = fast_create(Enterprise) - cat = fast_create(ProductCategory) - in_desc = Product.create!(:name => 'something', :enterprise_id => ent.id, :description => 'bananas in the description!', - :product_category_id => cat.id) - in_name = Product.create!(:name => 'bananas in the name!', :enterprise_id => ent.id, :product_category_id => cat.id) - assert_equal [in_name, in_desc], Product.find_by_contents('bananas')[:results].docs - end - - should 'boost search results that include an image' do - TestSolr.enable - product_without_image = Product.create!(:name => 'product without image', :product_category => @product_category, - :enterprise_id => @profile.id) - product_with_image = Product.create!(:name => 'product with image', :product_category => @product_category, - :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}, - :enterprise_id => @profile.id) - assert_equal [product_with_image, product_without_image], Product.find_by_contents('product image')[:results].docs - end - - should 'boost search results that include qualifier' do - TestSolr.enable - product_without_q = Product.create!(:name => 'product without qualifier', :product_category => @product_category, - :enterprise_id => @profile.id) - product_with_q = Product.create!(:name => 'product with qualifier', :product_category => @product_category, - :enterprise_id => @profile.id) - product_with_q.product_qualifiers.create(:qualifier => fast_create(Qualifier), :certifier => nil) - product_with_q.save! - - assert_equal [product_with_q, product_without_q], Product.find_by_contents('product qualifier')[:results].docs - end - - should 'boost search results with open price' do - TestSolr.enable - product = Product.create!(:name => 'product 1', :product_category => @product_category, :enterprise_id => @profile.id, :price => 100) - open_price = Product.new(:name => 'product 2', :product_category => @product_category, :enterprise_id => @profile.id, :price => 100) - open_price.inputs << Input.new(:product => open_price, :product_category_id => @product_category.id, :amount_used => 10, :price_per_unit => 10) - open_price.save! - - assert_equal [open_price, product], Product.find_by_contents('product')[:results].docs - end - - should 'boost search results with solidarity inputs' do - TestSolr.enable - product = Product.create!(:name => 'product 1', :product_category => @product_category, :enterprise_id => @profile.id) - perc_50 = Product.create!(:name => 'product 2', :product_category => @product_category, :enterprise_id => @profile.id) - Input.create!(:product_id => perc_50.id, :product_category_id => @product_category.id, - :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) - Input.create!(:product_id => perc_50.id, :product_category_id => @product_category.id, - :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) - perc_50.save! - perc_75 = Product.create!(:name => 'product 3', :product_category => @product_category, :enterprise_id => @profile.id) - Input.create!(:product_id => perc_75.id, :product_category_id => @product_category.id, - :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) - Input.create!(:product_id => perc_75.id, :product_category_id => @product_category.id, - :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) - Input.create!(:product_id => perc_75.id, :product_category_id => @product_category.id, - :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) - Input.create!(:product_id => perc_75.id, :product_category_id => @product_category.id, - :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) - perc_75.save! - - assert_equal [perc_75, perc_50, product], Product.find_by_contents('product')[:results].docs - end - - should 'boost available search results' do - TestSolr.enable - product = Product.create!(:name => 'product 1', :product_category => @product_category, :enterprise_id => @profile.id) - product.available = false - product.save! - product2 = Product.create!(:name => 'product 2', :product_category => @product_category, :enterprise_id => @profile.id) - product2.available = true - product2.save! - - assert_equal [product2, product], Product.find_by_contents('product')[:results].docs - end - - should 'boost search results created updated recently' do - TestSolr.enable - product = Product.create!(:name => 'product 1', :product_category => @product_category, :enterprise_id => @profile.id) - product.update_attribute :created_at, Time.now - 10.day - product2 = Product.create!(:name => 'product 2', :product_category => @product_category, :enterprise_id => @profile.id) - - assert_equal [product2, product], Product.find_by_contents('product')[:results].docs - end - - should 'boost search results with description' do - TestSolr.enable - product = Product.create!(:name => 'product 1', :product_category => @product_category, :enterprise_id => @profile.id, - :description => '') - product2 = Product.create!(:name => 'product 2', :product_category => @product_category, :enterprise_id => @profile.id, - :description => 'a small description') - - assert_equal [product2, product], Product.find_by_contents('product')[:results].docs - end - - should 'boost if enterprise is enabled' do - TestSolr.enable - ent = Enterprise.create!(:name => 'ent', :identifier => 'ent', :enabled => false) - product = Product.create!(:name => 'product 1', :product_category => @product_category, :enterprise_id => ent.id) - product2 = Product.create!(:name => 'product 2', :product_category => @product_category, :enterprise_id => @profile.id) - - assert_equal [product2, product], Product.find_by_contents('product')[:results].docs - end - - should 'combine different boost types' do - TestSolr.enable - product = Product.create!(:name => 'product', :product_category => @product_category, :enterprise_id => @profile.id) - image_only = Product.create!(:name => 'product with image', :product_category => @product_category, - :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}, - :enterprise_id => @profile.id) - qual_only = Product.create!(:name => 'product with qualifier', :product_category => @product_category, - :enterprise_id => @profile.id) - img_and_qual = Product.create!(:name => 'product with image and qualifier', :product_category => @product_category, - :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}, - :enterprise_id => @profile.id) - qual_only.product_qualifiers.create(:qualifier => fast_create(Qualifier), :certifier => nil) - img_and_qual.product_qualifiers.create(:qualifier => fast_create(Qualifier), :certifier => nil) - qual_only.save! - img_and_qual.save! - - assert_equal [img_and_qual, image_only, qual_only, product], Product.find_by_contents('product')[:results].docs - end - should 'return more recent products' do Product.destroy_all diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 890e98d..cba49cc 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1868,47 +1868,6 @@ class ProfileTest < ActiveSupport::TestCase assert profile.errors.invalid?(:identifier) end - should 'act as faceted' do - st = fast_create(State, :acronym => 'XZ') - city = fast_create(City, :name => 'Tabajara', :parent_id => st.id) - cat = fast_create(Category) - prof = fast_create(Person, :region_id => city.id) - prof.add_category(cat, true) - assert_equal ['Tabajara', ', XZ'], Profile.facet_by_id(:f_region)[:proc].call(prof.send(:f_region)) - assert_equal "category_filter:#{cat.id}", Person.facet_category_query.call(cat) - end - - should 'act as searchable' do - TestSolr.enable - st = create(State, :name => 'California', :acronym => 'CA', :environment_id => Environment.default.id) - city = create(City, :name => 'Inglewood', :parent_id => st.id, :environment_id => Environment.default.id) - p = create(Person, :name => "Hiro", :address => 'U-Stor-It', :nickname => 'Protagonist', - :user_id => fast_create(User).id, :region_id => city.id) - cat = create(Category, :name => "Science Fiction", :acronym => "sf", :abbreviation => "sci-fi") - p.add_category cat - - # fields - assert_includes Profile.find_by_contents('Hiro')[:results].docs, p - assert_includes Profile.find_by_contents('Protagonist')[:results].docs, p - # filters - assert_includes Profile.find_by_contents('Hiro', {}, { :filter_queries => ["public:true"]})[:results].docs, p - assert_not_includes Profile.find_by_contents('Hiro', {}, { :filter_queries => ["public:false"]})[:results].docs, p - assert_includes Profile.find_by_contents('Hiro', {}, { :filter_queries => ["environment_id:\"#{Environment.default.id}\""]})[:results].docs, p - # includes - assert_includes Profile.find_by_contents("Inglewood")[:results].docs, p - assert_includes Profile.find_by_contents("California")[:results].docs, p - assert_includes Profile.find_by_contents("Science")[:results].docs, p - # not includes - assert_not_includes Profile.find_by_contents('Stor')[:results].docs, p - end - - should 'boost name matches' do - TestSolr.enable - in_addr = create(Person, :name => 'something', :address => 'bananas in the address!', :user_id => fast_create(User).id) - in_name = create(Person, :name => 'bananas in the name!', :user_id => fast_create(User).id) - assert_equal [in_name], Person.find_by_contents('bananas')[:results].docs - end - should 'respond to redirection_after_login' do assert_respond_to Profile.new, :redirection_after_login end -- libgit2 0.21.2