Commit d052d8e5804136c4847a9ae88db423729e2e131a
1 parent
7e54e8ab
Exists in
master
and in
29 other branches
Add initial acts_as_faceted unit test
Showing
1 changed file
with
74 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,74 @@ | @@ -0,0 +1,74 @@ | ||
1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
2 | + | ||
3 | +class TestModel < ActiveRecord::Base | ||
4 | + def self.f_type_proc(klass) | ||
5 | + klass.constantize | ||
6 | + h = { | ||
7 | + 'UploadedFile' => "Uploaded File", | ||
8 | + 'TextArticle' => "Text", | ||
9 | + 'Folder' => "Folder", | ||
10 | + 'Event' => "Event", | ||
11 | + 'EnterpriseHomepage' => "Homepage", | ||
12 | + 'Gallery' => "Gallery", | ||
13 | + 'Blog' => "Blog", | ||
14 | + 'Forum' => "Forum" | ||
15 | + } | ||
16 | + h[klass] | ||
17 | + end | ||
18 | + def self.f_profile_type_proc(klass) | ||
19 | + h = { | ||
20 | + 'Enterprise' => "Enterprise", | ||
21 | + 'Community' => "Community", | ||
22 | + 'Person' => "Person", | ||
23 | + 'BscPlugin::Bsc' => "BSC" | ||
24 | + } | ||
25 | + h[klass] | ||
26 | + end | ||
27 | + acts_as_faceted :fields => { | ||
28 | + :f_type => {:label => 'Type', :proc => proc{|klass| f_type_proc(klass)}}, | ||
29 | + :f_published_at => {:type => :date, :label => 'Published date', :queries => {'[* TO NOW-1YEARS/DAY]' => "Older than one year", '[NOW-1YEARS TO NOW/DAY]' => "Last year"}}, | ||
30 | + :f_profile_type => {:label => 'Author', :proc => proc{|klass| f_profile_type_proc(klass)}}, | ||
31 | + :f_category => {:label => 'Categories'}}, | ||
32 | + :order => [:f_type, :f_published_at, :f_profile_type, :f_category] | ||
33 | +end | ||
34 | + | ||
35 | +class ActsAsFacetedTest < Test::Unit::TestCase | ||
36 | + def setup | ||
37 | + @facets = { | ||
38 | + "facet_fields"=> { | ||
39 | + "f_profile_type_facet"=>{"Person"=>29}, | ||
40 | + "f_type_facet"=>{"TextArticle"=>15, "Blog"=>3, "Folder"=>3, "Forum"=>1, "UploadedFile"=>6, "Gallery"=>1}, | ||
41 | + "f_category_facet"=>{}}, | ||
42 | + "facet_ranges"=>{}, "facet_dates"=>{}, | ||
43 | + "facet_queries"=>{"f_published_at_d:[* TO NOW-1YEARS/DAY]"=>10, "f_published_at_d:[NOW-1YEARS TO NOW/DAY]"=>19} | ||
44 | + } | ||
45 | + end | ||
46 | + | ||
47 | + should 'iterate over each result' do | ||
48 | + f = TestModel.facet_by_id(:f_type) | ||
49 | + r = [] | ||
50 | + TestModel.each_facet_result(f, @facets, {}) { |i| r.push i } | ||
51 | + assert_equal r, [["TextArticle", 'Text', 15], ["Blog", "Blog", 3], ["Folder", "Folder", 3], ["Forum", "Forum", 1], ["UploadedFile", "Uploaded File", 6], ["Gallery", "Gallery", 1]] | ||
52 | + | ||
53 | + f = TestModel.facet_by_id(:f_published_at) | ||
54 | + r = [] | ||
55 | + TestModel.each_facet_result(f, @facets, {}) { |i| r.push i } | ||
56 | + assert_equal r, [ ["[* TO NOW-1YEARS/DAY]", "Older than one year", 10], ["[NOW-1YEARS TO NOW/DAY]", "Last year", 19] ] | ||
57 | + end | ||
58 | + | ||
59 | + should 'query label of a facet' do | ||
60 | + l = TestModel.facet_by_id(:f_type) | ||
61 | + assert_equal l[:label], 'Type' | ||
62 | + l = TestModel.facet_by_id(:f_published_at) | ||
63 | + assert_equal l[:queries]['[* TO NOW-1YEARS/DAY]'], 'Older than one year' | ||
64 | + end | ||
65 | + | ||
66 | + should 'return browse options hash in acts_as_solr format' do | ||
67 | + o = TestModel.facets_find_options()[:facets] | ||
68 | + assert_equal o[:browse], [] | ||
69 | + | ||
70 | + o = TestModel.facets_find_options({'f_profile_type' => 'Person', 'f_published_at' => '[* TO NOW-1YEARS/DAY]'})[:facets] | ||
71 | + assert_equal o[:browse], ['f_profile_type:"Person"', 'f_published_at:[* TO NOW-1YEARS/DAY]'] | ||
72 | + end | ||
73 | + | ||
74 | +end |