cirandas.net

ref: master

plugins/solr/test/unit/acts_as_faceted_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
require_relative '../test_helper'
require_relative '../../lib/solr_plugin/acts_as_faceted'

class TestModel < ApplicationRecord
  def self.f_type_proc(facet, id_count_arr)
    id_count_arr.map do |type, count|
      h = {
        'UploadedFile' => "Uploaded File",
        'TextArticle' => "Text",
        'Folder' => "Folder",
        'Event' => "Event",
        'EnterpriseHomepage' => "Homepage",
        'Gallery' => "Gallery",
      }
      [type, h[type], count]
    end
  end
  acts_as_faceted fields: {
      f_type: {label: 'Type', proc: method(:f_type_proc).to_proc},
      f_published_at: {type: :date, label: 'Published date', queries:
        {'[* TO NOW-1YEARS/DAY]' => "Older than one year", '[NOW-1YEARS TO NOW/DAY]' => "Last year"}},
    }, order: [:f_type, :f_published_at]
end

class ActsAsFacetedTest < ActiveSupport::TestCase
  def setup
    @facets = {
      "facet_fields"=> {
        "f_type_facet"=>{"TextArticle"=>15, "Folder"=>3, "UploadedFile"=>6, "Gallery"=>1},
      }, "facet_ranges"=>{}, "facet_dates"=>{},
      "facet_queries"=>{"f_published_at_d:[* TO NOW-1YEARS/DAY]"=>10, "f_published_at_d:[NOW-1YEARS TO NOW/DAY]"=>19}
    }
    #any facet selected
    @facet_params = {}
    @all_facets = @facets
  end

  should 'get defined facets' do
    assert TestModel.facets.has_key? :f_type
    assert TestModel.facets.has_key? :f_published_at
  end

  should 'get facets by id' do
    facet = TestModel.facet_by_id :f_type
    assert_equal :f_type, facet[:id]
    assert_equal TestModel.facets[:f_type][:label], facet[:label]
    assert_equal TestModel.facets[:f_type][:proc], facet[:proc]
  end

  should 'convert facets to solr field names' do
    solr_names = TestModel.facets_solr_fields_names
    assert solr_names.include?("f_type_facet")
    assert solr_names.include?("f_published_at_d")

    solr_names = TestModel.to_solr_facets_fields_names

    assert_equal solr_names[:f_type], 'f_type_facet'
    assert_equal solr_names[:f_published_at], 'f_published_at_d'
  end

  should 'return facets containers' do
    containers = TestModel.solr_facets_results_containers

    assert_equal containers.count, 3
    assert_equal containers[:fields], 'facet_fields'
    assert_equal containers[:queries], 'facet_queries'
    assert_equal containers[:ranges], 'facet_ranges'
  end

  should 'show facets option for solr' do
    assert TestModel.solr_facets_options.include?(:f_type)
    refute TestModel.solr_facets_options.include?(:f_published_at)
  end

  should 'show facets fields for solr' do
    TestModel.solr_facets_fields.each do |facet|
      assert_equal facet[:f_type], :facet if facet[:f_type]
      assert_equal facet[:f_published_at], :date if facet[:f_published_at]
    end
  end

  should 'iterate over each result' do
    facets = TestModel.map_facets_for(Environment.default)
    assert facets.count, 2

    f = facets.select{ |f| f[:id] == 'f_type' }.first
    r = TestModel.map_facet_results f, @facet_params, @facets, @all_facets, {}
    assert_equivalent [["TextArticle", 'Text', 15], ["Folder", "Folder", 3], ["UploadedFile", "Uploaded File", 6], ["Gallery", "Gallery", 1]], r

    f = facets.select{ |f| f[:id] == 'f_published_at' }.first
    r = TestModel.map_facet_results f, @facet_params, @facets, @all_facets, {}
    assert_equivalent [["[* TO NOW-1YEARS/DAY]", "Older than one year", 10], ["[NOW-1YEARS TO NOW/DAY]", "Last year", 19]], r
  end

  should 'return facet hash in map_facets_for' do
    r = TestModel.map_facets_for(Environment.default)
    assert r.count, 2

    f_type = r.select{ |f| f[:id] == 'f_type' }.first
    assert_equal f_type[:solr_field], :f_type
    assert_equal f_type[:label], "Type"

    f_published = r.select{ |f| f[:id] == 'f_published_at' }.first
    assert_equal :f_published_at, f_published[:solr_field]
    assert_equal :date, f_published[:type]
    assert_equal "Published date", f_published[:label]
    hash = {"[NOW-1YEARS TO NOW/DAY]"=>"Last year", "[* TO NOW-1YEARS/DAY]"=>"Older than one year"}
    assert_equal hash, f_published[:queries]
  end

  should 'get label of a facet' do
    f = TestModel.facet_by_id(:f_type)
    assert_equal f[:label], 'Type'
  end

  should "get facets' queries" do
    f = TestModel.facet_by_id(:f_published_at)
    assert_equal f[:queries]['[* TO NOW-1YEARS/DAY]'], 'Older than one year'
  end

  should 'not map_facet_results without map_facets_for' do
    assert_raise RuntimeError do
      f = TestModel.facet_by_id(:f_published_at)
      TestModel.map_facet_results f, @facet_params, @facets, @all_facets, {}
    end
  end

  should 'show correct ordering' do
    assert_equal TestModel.solr_facets_order, [:f_type, :f_published_at]
  end

  should 'return facet options hash in acts_as_solr format' do
    options = TestModel.solr_facets_find_options()[:facets]
    assert_equal [:f_type], options[:fields]
    assert_equivalent ["f_published_at:[NOW-1YEARS TO NOW/DAY]", "f_published_at:[* TO NOW-1YEARS/DAY]"], options[:query]
  end

  should 'return browse options hash in acts_as_solr format' do
    options = TestModel.solr_facets_find_options()[:facets]
    assert_equal options[:browse], []

    options = TestModel.solr_facets_find_options({'f_published_at' => '[* TO NOW-1YEARS/DAY]'})[:facets]
    assert_equal options[:browse], ['f_published_at:[* TO NOW-1YEARS/DAY]']
  end

  should 'sort facet results alphabetically' do
    facets = TestModel.map_facets_for(Environment.default)
    facet = facets.select{ |f| f[:id] == 'f_type' }.first
    facet_data = TestModel.map_facet_results facet, @facet_params, @facets, @all_facets, {}
    sorted = TestModel.facet_result_sort(facet, facet_data, :alphabetically)
    assert_equal sorted,
      [["Folder", "Folder", 3], ["Gallery", "Gallery", 1], ["TextArticle", 'Text', 15], ["UploadedFile", "Uploaded File", 6]]
  end

  should 'sort facet results by count' do
    facets = TestModel.map_facets_for(Environment.default)
    facet = facets.select{ |f| f[:id] == 'f_type' }.first
    facet_data = TestModel.map_facet_results facet, @facet_params, @facets, @all_facets, {}
    sorted = TestModel.facet_result_sort(facet, facet_data, :count)
    assert_equal sorted,
      [["TextArticle", "Text", 15], ["UploadedFile", "Uploaded File", 6], ["Folder", "Folder", 3], ["Gallery", "Gallery", 1]]
  end
end