Commit c893df1cdb1eec82af8a2ca91e29d2268baae2a8
1 parent
002f897f
Exists in
master
and in
29 other branches
Moving acts_as_faceted test to solr plugin
Showing
2 changed files
with
163 additions
and
162 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,163 @@ |
| 1 | +require File.dirname(__FILE__) + '/../test_helper' | |
| 2 | +require "#{File.dirname(__FILE__)}../../lib/acts_as_faceted" | |
| 3 | + | |
| 4 | + | |
| 5 | +class TestModel < ActiveRecord::Base | |
| 6 | + def self.f_type_proc(klass) | |
| 7 | + klass.constantize | |
| 8 | + h = { | |
| 9 | + 'UploadedFile' => "Uploaded File", | |
| 10 | + 'TextArticle' => "Text", | |
| 11 | + 'Folder' => "Folder", | |
| 12 | + 'Event' => "Event", | |
| 13 | + 'EnterpriseHomepage' => "Homepage", | |
| 14 | + 'Gallery' => "Gallery", | |
| 15 | + } | |
| 16 | + h[klass] | |
| 17 | + end | |
| 18 | + acts_as_faceted :fields => { | |
| 19 | + :f_type => {:label => 'Type', :proc => proc{|klass| f_type_proc(klass)}}, | |
| 20 | + :f_published_at => {:type => :date, :label => 'Published date', :queries => | |
| 21 | + {'[* TO NOW-1YEARS/DAY]' => "Older than one year", '[NOW-1YEARS TO NOW/DAY]' => "Last year"}}, | |
| 22 | + }, :order => [:f_type, :f_published_at] | |
| 23 | +end | |
| 24 | + | |
| 25 | +class ActsAsFacetedTest < ActiveSupport::TestCase | |
| 26 | + def setup | |
| 27 | + @facets = { | |
| 28 | + "facet_fields"=> { | |
| 29 | + "f_type_facet"=>{"TextArticle"=>15, "Folder"=>3, "UploadedFile"=>6, "Gallery"=>1}, | |
| 30 | + }, "facet_ranges"=>{}, "facet_dates"=>{}, | |
| 31 | + "facet_queries"=>{"f_published_at_d:[* TO NOW-1YEARS/DAY]"=>10, "f_published_at_d:[NOW-1YEARS TO NOW/DAY]"=>19} | |
| 32 | + } | |
| 33 | + #any facet selected | |
| 34 | + @facet_params = {} | |
| 35 | + @all_facets = @facets | |
| 36 | + end | |
| 37 | + | |
| 38 | + should 'get defined facets' do | |
| 39 | + assert TestModel.facets.has_key? :f_type | |
| 40 | + assert TestModel.facets.has_key? :f_published_at | |
| 41 | + end | |
| 42 | + | |
| 43 | + should 'get facets by id' do | |
| 44 | + facet = TestModel.facet_by_id :f_type | |
| 45 | + assert_equal :f_type, facet[:id] | |
| 46 | + assert_equal TestModel.facets[:f_type][:label], facet[:label] | |
| 47 | + assert_equal TestModel.facets[:f_type][:proc], facet[:proc] | |
| 48 | + end | |
| 49 | + | |
| 50 | + should 'convert facets to solr field names' do | |
| 51 | + solr_names = TestModel.solr_fields_names | |
| 52 | + assert solr_names.include?("f_type_facet") | |
| 53 | + assert solr_names.include?("f_published_at_d") | |
| 54 | + | |
| 55 | + solr_names = TestModel.to_solr_fields_names | |
| 56 | + | |
| 57 | + assert_equal solr_names[:f_type], 'f_type_facet' | |
| 58 | + assert_equal solr_names[:f_published_at], 'f_published_at_d' | |
| 59 | + end | |
| 60 | + | |
| 61 | + should 'return facets containers' do | |
| 62 | + containers = TestModel.facets_results_containers | |
| 63 | + | |
| 64 | + assert_equal containers.count, 3 | |
| 65 | + assert_equal containers[:fields], 'facet_fields' | |
| 66 | + assert_equal containers[:queries], 'facet_queries' | |
| 67 | + assert_equal containers[:ranges], 'facet_ranges' | |
| 68 | + end | |
| 69 | + | |
| 70 | + should 'show facets option for solr' do | |
| 71 | + assert TestModel.facets_option_for_solr.include?(:f_type) | |
| 72 | + assert !TestModel.facets_option_for_solr.include?(:f_published_at) | |
| 73 | + end | |
| 74 | + | |
| 75 | + should 'show facets fields for solr' do | |
| 76 | + TestModel.facets_fields_for_solr.each do |facet| | |
| 77 | + assert_equal facet[:f_type], :facet if facet[:f_type] | |
| 78 | + assert_equal facet[:f_published_at], :date if facet[:f_published_at] | |
| 79 | + end | |
| 80 | + end | |
| 81 | + | |
| 82 | + should 'iterate over each result' do | |
| 83 | + facets = TestModel.map_facets_for(Environment.default) | |
| 84 | + assert facets.count, 2 | |
| 85 | + | |
| 86 | + f = facets.select{ |f| f[:id] == 'f_type' }.first | |
| 87 | + r = TestModel.map_facet_results f, @facet_params, @facets, @all_facets, {} | |
| 88 | + assert_equivalent [["TextArticle", 'Text', 15], ["Folder", "Folder", 3], ["UploadedFile", "Uploaded File", 6], ["Gallery", "Gallery", 1]], r | |
| 89 | + | |
| 90 | + f = facets.select{ |f| f[:id] == 'f_published_at' }.first | |
| 91 | + r = TestModel.map_facet_results f, @facet_params, @facets, @all_facets, {} | |
| 92 | + assert_equivalent [["[* TO NOW-1YEARS/DAY]", "Older than one year", 10], ["[NOW-1YEARS TO NOW/DAY]", "Last year", 19]], r | |
| 93 | + end | |
| 94 | + | |
| 95 | + should 'return facet hash in map_facets_for' do | |
| 96 | + r = TestModel.map_facets_for(Environment.default) | |
| 97 | + assert r.count, 2 | |
| 98 | + | |
| 99 | + f_type = r.select{ |f| f[:id] == 'f_type' }.first | |
| 100 | + assert_equal f_type[:solr_field], :f_type | |
| 101 | + assert_equal f_type[:label], "Type" | |
| 102 | + | |
| 103 | + f_published = r.select{ |f| f[:id] == 'f_published_at' }.first | |
| 104 | + assert_equal :f_published_at, f_published[:solr_field] | |
| 105 | + assert_equal :date, f_published[:type] | |
| 106 | + assert_equal "Published date", f_published[:label] | |
| 107 | + hash = {"[NOW-1YEARS TO NOW/DAY]"=>"Last year", "[* TO NOW-1YEARS/DAY]"=>"Older than one year"} | |
| 108 | + assert_equal hash, f_published[:queries] | |
| 109 | + end | |
| 110 | + | |
| 111 | + should 'get label of a facet' do | |
| 112 | + f = TestModel.facet_by_id(:f_type) | |
| 113 | + assert_equal f[:label], 'Type' | |
| 114 | + end | |
| 115 | + | |
| 116 | + should "get facets' queries" do | |
| 117 | + f = TestModel.facet_by_id(:f_published_at) | |
| 118 | + assert_equal f[:queries]['[* TO NOW-1YEARS/DAY]'], 'Older than one year' | |
| 119 | + end | |
| 120 | + | |
| 121 | + should 'not map_facet_results without map_facets_for' do | |
| 122 | + assert_raise RuntimeError do | |
| 123 | + f = TestModel.facet_by_id(:f_published_at) | |
| 124 | + TestModel.map_facet_results f, @facet_params, @facets, @all_facets, {} | |
| 125 | + end | |
| 126 | + end | |
| 127 | + | |
| 128 | + should 'show correct ordering' do | |
| 129 | + assert_equal TestModel.facets_order, [:f_type, :f_published_at] | |
| 130 | + end | |
| 131 | + | |
| 132 | + should 'return facet options hash in acts_as_solr format' do | |
| 133 | + options = TestModel.facets_find_options()[:facets] | |
| 134 | + assert_equal [:f_type], options[:fields] | |
| 135 | + assert_equivalent ["f_published_at:[NOW-1YEARS TO NOW/DAY]", "f_published_at:[* TO NOW-1YEARS/DAY]"], options[:query] | |
| 136 | + end | |
| 137 | + | |
| 138 | + should 'return browse options hash in acts_as_solr format' do | |
| 139 | + options = TestModel.facets_find_options()[:facets] | |
| 140 | + assert_equal options[:browse], [] | |
| 141 | + | |
| 142 | + options = TestModel.facets_find_options({'f_published_at' => '[* TO NOW-1YEARS/DAY]'})[:facets] | |
| 143 | + assert_equal options[:browse], ['f_published_at:[* TO NOW-1YEARS/DAY]'] | |
| 144 | + end | |
| 145 | + | |
| 146 | + should 'sort facet results alphabetically' do | |
| 147 | + facets = TestModel.map_facets_for(Environment.default) | |
| 148 | + facet = facets.select{ |f| f[:id] == 'f_type' }.first | |
| 149 | + facet_data = TestModel.map_facet_results facet, @facet_params, @facets, @all_facets, {} | |
| 150 | + sorted = TestModel.facet_result_sort(facet, facet_data, :alphabetically) | |
| 151 | + assert_equal sorted, | |
| 152 | + [["Folder", "Folder", 3], ["Gallery", "Gallery", 1], ["TextArticle", 'Text', 15], ["UploadedFile", "Uploaded File", 6]] | |
| 153 | + end | |
| 154 | + | |
| 155 | + should 'sort facet results by count' do | |
| 156 | + facets = TestModel.map_facets_for(Environment.default) | |
| 157 | + facet = facets.select{ |f| f[:id] == 'f_type' }.first | |
| 158 | + facet_data = TestModel.map_facet_results facet, @facet_params, @facets, @all_facets, {} | |
| 159 | + sorted = TestModel.facet_result_sort(facet, facet_data, :count) | |
| 160 | + assert_equal sorted, | |
| 161 | + [["TextArticle", "Text", 15], ["UploadedFile", "Uploaded File", 6], ["Folder", "Folder", 3], ["Gallery", "Gallery", 1]] | |
| 162 | + end | |
| 163 | +end | ... | ... |
test/unit/acts_as_faceted_test.rb
| ... | ... | @@ -1,162 +0,0 @@ |
| 1 | -require File.dirname(__FILE__) + '/../test_helper' | |
| 2 | -require 'acts_as_faceted' | |
| 3 | - | |
| 4 | -class TestModel < ActiveRecord::Base | |
| 5 | - def self.f_type_proc(klass) | |
| 6 | - klass.constantize | |
| 7 | - h = { | |
| 8 | - 'UploadedFile' => "Uploaded File", | |
| 9 | - 'TextArticle' => "Text", | |
| 10 | - 'Folder' => "Folder", | |
| 11 | - 'Event' => "Event", | |
| 12 | - 'EnterpriseHomepage' => "Homepage", | |
| 13 | - 'Gallery' => "Gallery", | |
| 14 | - } | |
| 15 | - h[klass] | |
| 16 | - end | |
| 17 | - acts_as_faceted :fields => { | |
| 18 | - :f_type => {:label => 'Type', :proc => proc{|klass| f_type_proc(klass)}}, | |
| 19 | - :f_published_at => {:type => :date, :label => 'Published date', :queries => | |
| 20 | - {'[* TO NOW-1YEARS/DAY]' => "Older than one year", '[NOW-1YEARS TO NOW/DAY]' => "Last year"}}, | |
| 21 | - }, :order => [:f_type, :f_published_at] | |
| 22 | -end | |
| 23 | - | |
| 24 | -class ActsAsFacetedTest < ActiveSupport::TestCase | |
| 25 | - def setup | |
| 26 | - @facets = { | |
| 27 | - "facet_fields"=> { | |
| 28 | - "f_type_facet"=>{"TextArticle"=>15, "Folder"=>3, "UploadedFile"=>6, "Gallery"=>1}, | |
| 29 | - }, "facet_ranges"=>{}, "facet_dates"=>{}, | |
| 30 | - "facet_queries"=>{"f_published_at_d:[* TO NOW-1YEARS/DAY]"=>10, "f_published_at_d:[NOW-1YEARS TO NOW/DAY]"=>19} | |
| 31 | - } | |
| 32 | - #any facet selected | |
| 33 | - @facet_params = {} | |
| 34 | - @all_facets = @facets | |
| 35 | - end | |
| 36 | - | |
| 37 | - should 'get defined facets' do | |
| 38 | - assert TestModel.facets.has_key? :f_type | |
| 39 | - assert TestModel.facets.has_key? :f_published_at | |
| 40 | - end | |
| 41 | - | |
| 42 | - should 'get facets by id' do | |
| 43 | - facet = TestModel.facet_by_id :f_type | |
| 44 | - assert_equal :f_type, facet[:id] | |
| 45 | - assert_equal TestModel.facets[:f_type][:label], facet[:label] | |
| 46 | - assert_equal TestModel.facets[:f_type][:proc], facet[:proc] | |
| 47 | - end | |
| 48 | - | |
| 49 | - should 'convert facets to solr field names' do | |
| 50 | - solr_names = TestModel.solr_fields_names | |
| 51 | - assert solr_names.include?("f_type_facet") | |
| 52 | - assert solr_names.include?("f_published_at_d") | |
| 53 | - | |
| 54 | - solr_names = TestModel.to_solr_fields_names | |
| 55 | - | |
| 56 | - assert_equal solr_names[:f_type], 'f_type_facet' | |
| 57 | - assert_equal solr_names[:f_published_at], 'f_published_at_d' | |
| 58 | - end | |
| 59 | - | |
| 60 | - should 'return facets containers' do | |
| 61 | - containers = TestModel.facets_results_containers | |
| 62 | - | |
| 63 | - assert_equal containers.count, 3 | |
| 64 | - assert_equal containers[:fields], 'facet_fields' | |
| 65 | - assert_equal containers[:queries], 'facet_queries' | |
| 66 | - assert_equal containers[:ranges], 'facet_ranges' | |
| 67 | - end | |
| 68 | - | |
| 69 | - should 'show facets option for solr' do | |
| 70 | - assert TestModel.facets_option_for_solr.include?(:f_type) | |
| 71 | - assert !TestModel.facets_option_for_solr.include?(:f_published_at) | |
| 72 | - end | |
| 73 | - | |
| 74 | - should 'show facets fields for solr' do | |
| 75 | - TestModel.facets_fields_for_solr.each do |facet| | |
| 76 | - assert_equal facet[:f_type], :facet if facet[:f_type] | |
| 77 | - assert_equal facet[:f_published_at], :date if facet[:f_published_at] | |
| 78 | - end | |
| 79 | - end | |
| 80 | - | |
| 81 | - should 'iterate over each result' do | |
| 82 | - facets = TestModel.map_facets_for(Environment.default) | |
| 83 | - assert facets.count, 2 | |
| 84 | - | |
| 85 | - f = facets.select{ |f| f[:id] == 'f_type' }.first | |
| 86 | - r = TestModel.map_facet_results f, @facet_params, @facets, @all_facets, {} | |
| 87 | - assert_equivalent [["TextArticle", 'Text', 15], ["Folder", "Folder", 3], ["UploadedFile", "Uploaded File", 6], ["Gallery", "Gallery", 1]], r | |
| 88 | - | |
| 89 | - f = facets.select{ |f| f[:id] == 'f_published_at' }.first | |
| 90 | - r = TestModel.map_facet_results f, @facet_params, @facets, @all_facets, {} | |
| 91 | - assert_equivalent [["[* TO NOW-1YEARS/DAY]", "Older than one year", 10], ["[NOW-1YEARS TO NOW/DAY]", "Last year", 19]], r | |
| 92 | - end | |
| 93 | - | |
| 94 | - should 'return facet hash in map_facets_for' do | |
| 95 | - r = TestModel.map_facets_for(Environment.default) | |
| 96 | - assert r.count, 2 | |
| 97 | - | |
| 98 | - f_type = r.select{ |f| f[:id] == 'f_type' }.first | |
| 99 | - assert_equal f_type[:solr_field], :f_type | |
| 100 | - assert_equal f_type[:label], "Type" | |
| 101 | - | |
| 102 | - f_published = r.select{ |f| f[:id] == 'f_published_at' }.first | |
| 103 | - assert_equal :f_published_at, f_published[:solr_field] | |
| 104 | - assert_equal :date, f_published[:type] | |
| 105 | - assert_equal "Published date", f_published[:label] | |
| 106 | - hash = {"[NOW-1YEARS TO NOW/DAY]"=>"Last year", "[* TO NOW-1YEARS/DAY]"=>"Older than one year"} | |
| 107 | - assert_equal hash, f_published[:queries] | |
| 108 | - end | |
| 109 | - | |
| 110 | - should 'get label of a facet' do | |
| 111 | - f = TestModel.facet_by_id(:f_type) | |
| 112 | - assert_equal f[:label], 'Type' | |
| 113 | - end | |
| 114 | - | |
| 115 | - should "get facets' queries" do | |
| 116 | - f = TestModel.facet_by_id(:f_published_at) | |
| 117 | - assert_equal f[:queries]['[* TO NOW-1YEARS/DAY]'], 'Older than one year' | |
| 118 | - end | |
| 119 | - | |
| 120 | - should 'not map_facet_results without map_facets_for' do | |
| 121 | - assert_raise RuntimeError do | |
| 122 | - f = TestModel.facet_by_id(:f_published_at) | |
| 123 | - TestModel.map_facet_results f, @facet_params, @facets, @all_facets, {} | |
| 124 | - end | |
| 125 | - end | |
| 126 | - | |
| 127 | - should 'show correct ordering' do | |
| 128 | - assert_equal TestModel.facets_order, [:f_type, :f_published_at] | |
| 129 | - end | |
| 130 | - | |
| 131 | - should 'return facet options hash in acts_as_solr format' do | |
| 132 | - options = TestModel.facets_find_options()[:facets] | |
| 133 | - assert_equal [:f_type], options[:fields] | |
| 134 | - assert_equivalent ["f_published_at:[NOW-1YEARS TO NOW/DAY]", "f_published_at:[* TO NOW-1YEARS/DAY]"], options[:query] | |
| 135 | - end | |
| 136 | - | |
| 137 | - should 'return browse options hash in acts_as_solr format' do | |
| 138 | - options = TestModel.facets_find_options()[:facets] | |
| 139 | - assert_equal options[:browse], [] | |
| 140 | - | |
| 141 | - options = TestModel.facets_find_options({'f_published_at' => '[* TO NOW-1YEARS/DAY]'})[:facets] | |
| 142 | - assert_equal options[:browse], ['f_published_at:[* TO NOW-1YEARS/DAY]'] | |
| 143 | - end | |
| 144 | - | |
| 145 | - should 'sort facet results alphabetically' do | |
| 146 | - facets = TestModel.map_facets_for(Environment.default) | |
| 147 | - facet = facets.select{ |f| f[:id] == 'f_type' }.first | |
| 148 | - facet_data = TestModel.map_facet_results facet, @facet_params, @facets, @all_facets, {} | |
| 149 | - sorted = TestModel.facet_result_sort(facet, facet_data, :alphabetically) | |
| 150 | - assert_equal sorted, | |
| 151 | - [["Folder", "Folder", 3], ["Gallery", "Gallery", 1], ["TextArticle", 'Text', 15], ["UploadedFile", "Uploaded File", 6]] | |
| 152 | - end | |
| 153 | - | |
| 154 | - should 'sort facet results by count' do | |
| 155 | - facets = TestModel.map_facets_for(Environment.default) | |
| 156 | - facet = facets.select{ |f| f[:id] == 'f_type' }.first | |
| 157 | - facet_data = TestModel.map_facet_results facet, @facet_params, @facets, @all_facets, {} | |
| 158 | - sorted = TestModel.facet_result_sort(facet, facet_data, :count) | |
| 159 | - assert_equal sorted, | |
| 160 | - [["TextArticle", "Text", 15], ["UploadedFile", "Uploaded File", 6], ["Folder", "Folder", 3], ["Gallery", "Gallery", 1]] | |
| 161 | - end | |
| 162 | -end |